123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- <?php
- use think\facade\Env;
- use think\facade\Db;
- use Aliyun\OTS\OTSClient;
- function queryTableStoreGpsRoute($options)
- {
- if (!isset($options['device_number']) && $options['device_number'] != '') {
- return array('success' => false, 'message' => '缺少设备号');
- }
- $device_number = $options['device_number'];
- if (!isset($options['start_time']) && $options['start_time'] > 0) {
- return array('success' => false, 'message' => '缺少起始时间');
- }
- $start_time = $options['start_time'];
- if (!isset($options['end_time']) && $options['end_time'] > 0) {
- $end_time = time();
- } else {
- $end_time = $options['end_time'];
- }
- if (!isset($options['table_name']) && $options['table_name'] > 0) {
- return array('success' => false, 'message' => '缺少表名参数');
- }
- $table_name = $options['table_name'];
-
-
-
-
-
-
- $table_config = Db::table('sys_config')->where('type', 'table_store')->column('fieldValue', 'field');
- if (!isset($table_config['EndPoint']) || !isset($table_config['AccessKeyID']) || !isset($table_config['AccessKeySecret']) || !isset($table_config['InstanceName'])) {
- return array('success' => false, 'message' => '未配置表格存储');
- }
- $otsClient = new OTSClient(array(
- 'EndPoint' => $table_config['EndPoint'],
- 'AccessKeyID' => $table_config['AccessKeyID'],
- 'AccessKeySecret' => $table_config['AccessKeySecret'],
- 'InstanceName' => $table_config['InstanceName'],
- ));
- $otsClient->getClientConfig()->debugLogHandler = null;
- $otsClient->getClientConfig()->errorLogHandler = null;
-
- ini_set('memory_limit', '512M');
- $startPK = array(
- array('DeviceNumber', $device_number),
- array('Timestamp', $start_time)
-
- );
- $endPK = array(
- array('DeviceNumber', $device_number),
- array('Timestamp', $end_time)
-
- );
- if (isset($options['limit']) && $options['limit'] > 0) {
- $limit = $options['limit'];
- } else {
- $limit = 5500;
- }
- $rows = [];
- while (!empty($startPK) && $limit > 0) {
- $request = array(
- 'table_name' => $table_name,
- 'max_versions' => 1,
- 'direction' => 'BACKWARD',
- 'inclusive_start_primary_key' => $endPK,
- 'exclusive_end_primary_key' => $startPK,
- 'limit' => $limit
- );
- $response = [];
- try {
- $response = $otsClient->getRange($request);
- } catch (\Exception $e) {
- var_dump($e->getMessage());
- }
- if (!isset($response['rows'])) {
- return CatchResponse::success([]);
- }
- foreach ($response['rows'] as $rowData) {
-
- $item = [];
- foreach ($rowData['primary_key'] as $keyItem) {
- $item[$keyItem[0]] = $keyItem[1];
- }
- foreach ($rowData['attribute_columns'] as $colItem) {
- $item[$colItem[0]] = $colItem[1];
- }
- $item['PassTime'] = date('Y-m-d H:i:s', $item['Timestamp']);
- $rows[] = $item;
- $limit--;
-
- }
- $startPK = $response['next_start_primary_key'];
-
- }
- if (!$rows) {
- return array('success' => true, 'message' => '查询成功', 'data' => []);
- }
- $arr = [];
- foreach ($rows as $k => $v) {
- if (!isset($v['Latitude']) || !isset($v['Longitude']) || $v['Latitude'] < 0.065 || $v['Longitude'] < 0.065) {
- continue;
- }
- array_push($arr, $rows[$k]);
- }
- return array('success' => true, 'message' => '查询成功', 'data' => $arr);
- }
- function debug_log($filename, $data)
- {
- if (Env::get('APP_DEBUG')) {
- $file = runtime_path() . '/log/' . date("Y-m-d", time()) . "/" . $filename . ".log";
- $folder = dirname($file);
- if (!is_dir($folder)) {
- mkdir($folder, 0777, true);
- }
- if (is_array($data)) {
- $data = json_encode($data);
- }
- file_put_contents($file, '[' . date('Y-m-d H:i:s') . ']' . $data . PHP_EOL, FILE_APPEND);
- }
- }
- function api_log($filename, $data)
- {
- $file = runtime_path() . '/log/' . date("Ymd", time()) . "/" . $filename . ".log";
- $folder = dirname($file);
- if (!is_dir($folder)) {
- mkdir($folder, 0777, true);
- }
- if (is_array($data)) {
- $data = json_encode($data);
- }
- file_put_contents($file, '[' . date('Y-m-d H:i:s') . ']' . $data . PHP_EOL, FILE_APPEND);
- }
- function curl_http_post($data, $url, $ssl)
- {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- if ($ssl) {
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
- curl_setopt($curl, CURLOPT_SSLVERSION, 3);
- }
-
- curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- curl_setopt($curl, CURLOPT_TIMEOUT, 30);
- curl_setopt($curl, CURLOPT_HEADER, 0);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $tmpInfo = curl_exec($curl);
- if (curl_errno($curl)) {
- dump(curl_error($curl));
- return false;
- }
- curl_close($curl);
- return $tmpInfo;
- }
- function updateDeviceStatus($imei)
- {
- $res = Db::table('devices')->where('imei', $imei)->update(['sync_status' => 1]);
-
- }
- function send_sms_with_config($mobile, $content, $sms_config, $info)
- {
- $config = array();
-
-
-
- $serverIP = 'app.cloopen.com';
-
- $softVersion = '2013-12-26';
-
- $serverPort = '8883';
-
- $accountSid = $sms_config['accountSid'];
-
- $config['accountSid'] = $accountSid;
-
- $config['accountToken'] = $sms_config['accountToken'];
-
-
- $config['appId'] = $sms_config['appId'];
-
-
- $batch = date("YmdHis");
-
- $config['authen'] = base64_encode($accountSid . ":" . $batch);
-
- $sig = strtoupper(md5($accountSid . $config['accountToken'] . $batch));
-
- $config['api_send_url'] = "https://$serverIP:$serverPort/$softVersion/Accounts/$accountSid/SMS/TemplateSMS?sig=" . $sig;
- $postArr = array(
- 'to' => $mobile,
- 'templateId' => $content['tplno'],
- 'appId' => $config['appId'],
- 'datas' => $content['tpldata'],
- );
- $postArr = json_encode($postArr);
- $authen = $config['authen'];
- $header = array("Accept:application/json", "Content-Type:application/json;charset=utf-8", "Authorization:$authen");
- $url = $config['api_send_url'];
-
-
- $ch = curl_init();
-
- $res = curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $postArr);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- $result = curl_exec($ch);
- curl_close($ch);
- $result = json_decode($result, true);
-
- $data = array(
- 'recipient' => $mobile,
- 'content' => $content['info'],
-
- 'created_at' => time(),
- 'creator_id' => 0,
-
- );
- if ($result['statusCode'] === '000000') {
- $data['sent_result'] = '发送成功';
- $res = Db::name('sms_send_log')->insert($data);
- return array('success' => true, 'message' => '发送成功');
- } else {
- $data['sent_result'] = json_encode($result, JSON_UNESCAPED_UNICODE);
- $res = Db::name('sms_send_log')->insert($data);
- return array('success' => false, 'errorCode' => $result['statusCode'], 'message' => $result['statusMsg'], 'data' => $data);
- }
- }
- function get_sms_config($type)
- {
- $key = "wxt_sms_send_config_" . $type;
- $sms_cache_config = S($key);
- $sms_config = array();
- if (!$sms_cache_config) {
- $config_id = Db::name('sms_config')->where(array('name' => $type, 'pid' => '0'))->getField('id');
- if (!$config_id) {
- return array('result' => 'fail', 'message' => '未配置' . $type);
- }
- $config_list = Db::name('sms_config')->where(array('pid' => $config_id))->select();
- if (!$config_list) {
- return array('result' => 'fail', 'message' => '未配置参数' . $type);
- }
- foreach ($config_list as $val) {
- $sms_config[$val['key']] = $val['value'];
- }
- S($key, json_encode($sms_config), 60);
- } else {
- $sms_config = json_decode($sms_cache_config, true);
- }
- return $sms_config;
- }
- function json_success($message, $data = '', $imei = '')
- {
- echo json_encode(array('success' => true, 'message' => $message, 'data' => $data, 'imei' => $imei), JSON_UNESCAPED_UNICODE);
- exit;
- }
- function json_fail($message, $data = '', $imei = '')
- {
- echo json_encode(array('success' => false, 'message' => $message, 'data' => $data, 'imei' => $imei), JSON_UNESCAPED_UNICODE);
- exit;
- }
- function msectime()
- {
- list($msec, $sec) = explode(' ', microtime());
- $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
- return $msectime;
- }
|