123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- <?php
- // 应用公共文件
- use think\facade\Env;
- use think\facade\Db;
- use Aliyun\OTS\OTSClient;
- use app\Request;
- use catcher\base\CatchRequest;
- use catcher\CatchResponse;
- use catcher\generate\build\types\Arr;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- /**
- * 增量解析含wifi信号Gps
- */
- 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'];
- // $otsClient = new OTSClient (array (
- // 'EndPoint' => 'https://rlfd.cn-hangzhou.ots.aliyuncs.com',
- // 'AccessKeyID' => 'LTAI4FecTAyMPdhAdkUwhAGA',
- // 'AccessKeySecret' => 'Ih0knSSfcje3OUi1YrdjeXQZYIfmTK',
- // 'InstanceName' => 'rlfd',
- // ));
- $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;
- // var_dump($otsClient);
- ini_set('memory_limit', '512M');
- $startPK = array(
- array('DeviceNumber', $device_number),
- array('Timestamp', $start_time)
- // array('Timestamp', 1620885120)
- );
- $endPK = array(
- array('DeviceNumber', $device_number),
- array('Timestamp', $end_time)
- // array('Timestamp', 1620885441)
- );
- 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', // 方向可以为 FORWARD 或者 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) {
- // $str=($limit-4000)/2000;
- $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'];
- // 如果 next_start_primary_key 不为空并且 limit > 0 则循环继续
- }
- 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);
- }
- /**
- * debug日志
- */
- 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);
- }
- }
- /**
- * api日志
- */
- 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);
- }
- /**
- *postt请求
- */
- 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_USERAGENT,$_SERVER["HTTP_USER_AGENT"]);
- 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;
- }
- /**
- * api日志
- */
- function updateDeviceStatus($imei)
- {
- $res = Db::table('devices')->where('imei', $imei)->update(['sync_status' => 1]);
- //var_dump(Db::table('devices')->getLastSql());
- }
- //短信管理
- function send_sms_with_config($mobile, $content, $sms_config, $info)
- {
- $config = array();
- /*url示例
- $url="https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/SMS/TemplateSMS?sig=$sig";
- */
- //沙盒环境(应用开发调试):sandboxapp.cloopen.com
- //生产环境(应用上线使用):app.cloopen.com
- $serverIP = 'app.cloopen.com';
- //sdk版本号
- $softVersion = '2013-12-26';
- //请求端口,一般为8883
- $serverPort = '8883';
- //主账号,对应开官网发者主账号下的 ACCOUNT SID
- $accountSid = $sms_config['accountSid'];
- //$accountSid="8aaf0708754a3ef201755a063f070592";
- $config['accountSid'] = $accountSid;
- //密钥
- $config['accountToken'] = $sms_config['accountToken'];
- //$config['accountToken'] = "324fb91d40cf472580dc8dc4d9a3e0d5";
- //应用Id,在官网应用列表中点击应用,对应应用详情中的APP ID
- $config['appId'] = $sms_config['appId'];
- //$config['appId'] = "8aaf070875774c6d0175910e26fc0bf9";
- //时间戳
- $batch = date("YmdHis");
- //验证信息编码
- $config['authen'] = base64_encode($accountSid . ":" . $batch);
- //加密REST API验证参数
- $sig = strtoupper(md5($accountSid . $config['accountToken'] . $batch));
- //拼接url
- $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'];
- //$result = $this->curlPost( $url , $postArr, $header);
- //初始化curl
- $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);
- //var_dump($result);
- $data = array(
- 'recipient' => $mobile,
- 'content' => $content['info'],
- // 'type'=>'sms',
- 'created_at' => time(),
- 'creator_id' => 0,
- // 'sent_time'=>time(),
- );
- 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;
- }
- /**
- * 返回JSON成功信息
- * message: 消息文本
- * data: 额外数据
- * @time 2022年06月09日 14:22
- */
- function json_success($message, $data = '', $imei = '')
- {
- echo json_encode(array('success' => true, 'message' => $message, 'data' => $data, 'imei' => $imei), JSON_UNESCAPED_UNICODE);
- exit;
- }
- /**
- * 失败JSON成功信息
- * message: 消息文本
- * data: 额外数据
- * @time 2022年06月09日 14:22
- */
- function json_fail($message, $data = '', $imei = '')
- {
- echo json_encode(array('success' => false, 'message' => $message, 'data' => $data, 'imei' => $imei), JSON_UNESCAPED_UNICODE);
- exit;
- }
- /**
- * @Descripttion: 获取当前毫秒级时间戳
- * @name: likang
- * @return {*}
- */
- function msectime()
- {
- list($msec, $sec) = explode(' ', microtime());
- $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
- return $msectime;
- }
- /**
- * @Descripttion: 优化json字符串
- * @name: likang
- * @return {*}
- */
- function optimize_json($tempResult)
- {
- return mb_convert_encoding($tempResult, 'UTF-8', 'UTF-8');;
- }
|