common.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. // 应用公共文件
  3. use think\facade\Env;
  4. use think\facade\Db;
  5. use Aliyun\OTS\OTSClient;
  6. /**
  7. * 增量解析含wifi信号Gps
  8. */
  9. function queryTableStoreGpsRoute($options)
  10. {
  11. if (!isset($options['device_number']) && $options['device_number'] != '') {
  12. return array('success' => false, 'message' => '缺少设备号');
  13. }
  14. $device_number = $options['device_number'];
  15. if (!isset($options['start_time']) && $options['start_time'] > 0) {
  16. return array('success' => false, 'message' => '缺少起始时间');
  17. }
  18. $start_time = $options['start_time'];
  19. if (!isset($options['end_time']) && $options['end_time'] > 0) {
  20. $end_time = time();
  21. } else {
  22. $end_time = $options['end_time'];
  23. }
  24. if (!isset($options['table_name']) && $options['table_name'] > 0) {
  25. return array('success' => false, 'message' => '缺少表名参数');
  26. }
  27. $table_name = $options['table_name'];
  28. // $otsClient = new OTSClient (array (
  29. // 'EndPoint' => 'https://rlfd.cn-hangzhou.ots.aliyuncs.com',
  30. // 'AccessKeyID' => 'LTAI4FecTAyMPdhAdkUwhAGA',
  31. // 'AccessKeySecret' => 'Ih0knSSfcje3OUi1YrdjeXQZYIfmTK',
  32. // 'InstanceName' => 'rlfd',
  33. // ));
  34. $table_config = Db::table('sys_config')->where('type', 'table_store')->column('fieldValue', 'field');
  35. if (!isset($table_config['EndPoint']) || !isset($table_config['AccessKeyID']) || !isset($table_config['AccessKeySecret']) || !isset($table_config['InstanceName'])) {
  36. return array('success' => false, 'message' => '未配置表格存储');
  37. }
  38. $otsClient = new OTSClient(array(
  39. 'EndPoint' => $table_config['EndPoint'],
  40. 'AccessKeyID' => $table_config['AccessKeyID'],
  41. 'AccessKeySecret' => $table_config['AccessKeySecret'],
  42. 'InstanceName' => $table_config['InstanceName'],
  43. ));
  44. $otsClient->getClientConfig()->debugLogHandler = null;
  45. $otsClient->getClientConfig()->errorLogHandler = null;
  46. // var_dump($otsClient);
  47. ini_set('memory_limit', '512M');
  48. $startPK = array(
  49. array('DeviceNumber', $device_number),
  50. array('Timestamp', $start_time)
  51. // array('Timestamp', 1620885120)
  52. );
  53. $endPK = array(
  54. array('DeviceNumber', $device_number),
  55. array('Timestamp', $end_time)
  56. // array('Timestamp', 1620885441)
  57. );
  58. if (isset($options['limit']) && $options['limit'] > 0) {
  59. $limit = $options['limit'];
  60. } else {
  61. $limit = 5500;
  62. }
  63. $rows = [];
  64. while (!empty($startPK) && $limit > 0) {
  65. $request = array(
  66. 'table_name' => $table_name,
  67. 'max_versions' => 1,
  68. 'direction' => 'BACKWARD', // 方向可以为 FORWARD 或者 BACKWARD
  69. 'inclusive_start_primary_key' => $endPK, // 开始主键
  70. 'exclusive_end_primary_key' => $startPK, // 结束主键
  71. 'limit' => $limit
  72. );
  73. $response = [];
  74. try {
  75. $response = $otsClient->getRange($request);
  76. } catch (\Exception $e) {
  77. var_dump($e->getMessage());
  78. }
  79. if (!isset($response['rows'])) {
  80. return CatchResponse::success([]);
  81. }
  82. foreach ($response['rows'] as $rowData) {
  83. // $str=($limit-4000)/2000;
  84. $item = [];
  85. foreach ($rowData['primary_key'] as $keyItem) {
  86. $item[$keyItem[0]] = $keyItem[1];
  87. }
  88. foreach ($rowData['attribute_columns'] as $colItem) {
  89. $item[$colItem[0]] = $colItem[1];
  90. }
  91. $item['PassTime'] = date('Y-m-d H:i:s', $item['Timestamp']);
  92. $rows[] = $item;
  93. $limit--;
  94. // 处理每一行数据
  95. }
  96. $startPK = $response['next_start_primary_key'];
  97. // 如果 next_start_primary_key 不为空并且 limit > 0 则循环继续
  98. }
  99. if (!$rows) {
  100. return array('success' => true, 'message' => '查询成功', 'data' => []);
  101. }
  102. $arr = [];
  103. foreach ($rows as $k => $v) {
  104. if (!isset($v['Latitude']) || !isset($v['Longitude']) || $v['Latitude'] < 0.065 || $v['Longitude'] < 0.065) {
  105. continue;
  106. }
  107. array_push($arr, $rows[$k]);
  108. }
  109. return array('success' => true, 'message' => '查询成功', 'data' => $arr);
  110. }
  111. /**
  112. * debug日志
  113. */
  114. function debug_log($filename, $data)
  115. {
  116. if (Env::get('APP_DEBUG')) {
  117. $file = runtime_path() . '/log/' . date("Y-m-d", time()) . "/" . $filename . ".log";
  118. $folder = dirname($file);
  119. if (!is_dir($folder)) {
  120. mkdir($folder, 0777, true);
  121. }
  122. if (is_array($data)) {
  123. $data = json_encode($data);
  124. }
  125. file_put_contents($file, '[' . date('Y-m-d H:i:s') . ']' . $data . PHP_EOL, FILE_APPEND);
  126. }
  127. }
  128. /**
  129. * api日志
  130. */
  131. function api_log($filename, $data)
  132. {
  133. $file = runtime_path() . '/log/' . date("Ymd", time()) . "/" . $filename . ".log";
  134. $folder = dirname($file);
  135. if (!is_dir($folder)) {
  136. mkdir($folder, 0777, true);
  137. }
  138. if (is_array($data)) {
  139. $data = json_encode($data);
  140. }
  141. file_put_contents($file, '[' . date('Y-m-d H:i:s') . ']' . $data . PHP_EOL, FILE_APPEND);
  142. }
  143. /**
  144.  *postt请求
  145. */
  146. function curl_http_post($data, $url, $ssl)
  147. {
  148. $curl = curl_init();
  149. curl_setopt($curl, CURLOPT_URL, $url);
  150. if ($ssl) {
  151. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
  152. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
  153. curl_setopt($curl, CURLOPT_SSLVERSION, 3);
  154. }
  155. // curl_setopt($curl,CURLOPT_USERAGENT,$_SERVER["HTTP_USER_AGENT"]);
  156. curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
  157. curl_setopt($curl, CURLOPT_POST, 1);
  158. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  159. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  160. curl_setopt($curl, CURLOPT_HEADER, 0);
  161. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  162. $tmpInfo = curl_exec($curl);
  163. if (curl_errno($curl)) {
  164. dump(curl_error($curl));
  165. return false;
  166. }
  167. curl_close($curl);
  168. return $tmpInfo;
  169. }
  170. /**
  171. * api日志
  172. */
  173. function updateDeviceStatus($imei)
  174. {
  175. $res = Db::table('devices')->where('imei', $imei)->update(['sync_status' => 1]);
  176. //var_dump(Db::table('devices')->getLastSql());
  177. }
  178. //短信管理
  179. function send_sms_with_config($mobile, $content, $sms_config, $info)
  180. {
  181. $config = array();
  182. /*url示例
  183. $url="https://$this->ServerIP:$this->ServerPort/$this->SoftVersion/Accounts/$this->AccountSid/SMS/TemplateSMS?sig=$sig";
  184. */
  185. //沙盒环境(应用开发调试):sandboxapp.cloopen.com
  186. //生产环境(应用上线使用):app.cloopen.com
  187. $serverIP = 'app.cloopen.com';
  188. //sdk版本号
  189. $softVersion = '2013-12-26';
  190. //请求端口,一般为8883
  191. $serverPort = '8883';
  192. //主账号,对应开官网发者主账号下的 ACCOUNT SID
  193. $accountSid = $sms_config['accountSid'];
  194. //$accountSid="8aaf0708754a3ef201755a063f070592";
  195. $config['accountSid'] = $accountSid;
  196. //密钥
  197. $config['accountToken'] = $sms_config['accountToken'];
  198. //$config['accountToken'] = "324fb91d40cf472580dc8dc4d9a3e0d5";
  199. //应用Id,在官网应用列表中点击应用,对应应用详情中的APP ID
  200. $config['appId'] = $sms_config['appId'];
  201. //$config['appId'] = "8aaf070875774c6d0175910e26fc0bf9";
  202. //时间戳
  203. $batch = date("YmdHis");
  204. //验证信息编码
  205. $config['authen'] = base64_encode($accountSid . ":" . $batch);
  206. //加密REST API验证参数
  207. $sig = strtoupper(md5($accountSid . $config['accountToken'] . $batch));
  208. //拼接url
  209. $config['api_send_url'] = "https://$serverIP:$serverPort/$softVersion/Accounts/$accountSid/SMS/TemplateSMS?sig=" . $sig;
  210. $postArr = array(
  211. 'to' => $mobile,
  212. 'templateId' => $content['tplno'],
  213. 'appId' => $config['appId'],
  214. 'datas' => $content['tpldata'],
  215. );
  216. $postArr = json_encode($postArr);
  217. $authen = $config['authen'];
  218. $header = array("Accept:application/json", "Content-Type:application/json;charset=utf-8", "Authorization:$authen");
  219. $url = $config['api_send_url'];
  220. //$result = $this->curlPost( $url , $postArr, $header);
  221. //初始化curl
  222. $ch = curl_init();
  223. //参数设置
  224. $res = curl_setopt($ch, CURLOPT_URL, $url);
  225. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  226. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  227. curl_setopt($ch, CURLOPT_HEADER, 0);
  228. curl_setopt($ch, CURLOPT_POST, 1);
  229. curl_setopt($ch, CURLOPT_POSTFIELDS, $postArr);
  230. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  231. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  232. $result = curl_exec($ch);
  233. curl_close($ch);
  234. $result = json_decode($result, true);
  235. //var_dump($result);
  236. $data = array(
  237. 'recipient' => $mobile,
  238. 'content' => $content['info'],
  239. // 'type'=>'sms',
  240. 'created_at' => time(),
  241. 'creator_id' => 0,
  242. // 'sent_time'=>time(),
  243. );
  244. if ($result['statusCode'] === '000000') {
  245. $data['sent_result'] = '发送成功';
  246. $res = Db::name('sms_send_log')->insert($data);
  247. return array('success' => true, 'message' => '发送成功');
  248. } else {
  249. $data['sent_result'] = json_encode($result, JSON_UNESCAPED_UNICODE);
  250. $res = Db::name('sms_send_log')->insert($data);
  251. return array('success' => false, 'errorCode' => $result['statusCode'], 'message' => $result['statusMsg'], 'data' => $data);
  252. }
  253. }
  254. function get_sms_config($type)
  255. {
  256. $key = "wxt_sms_send_config_" . $type;
  257. $sms_cache_config = S($key);
  258. $sms_config = array();
  259. if (!$sms_cache_config) {
  260. $config_id = Db::name('sms_config')->where(array('name' => $type, 'pid' => '0'))->getField('id');
  261. if (!$config_id) {
  262. return array('result' => 'fail', 'message' => '未配置' . $type);
  263. }
  264. $config_list = Db::name('sms_config')->where(array('pid' => $config_id))->select();
  265. if (!$config_list) {
  266. return array('result' => 'fail', 'message' => '未配置参数' . $type);
  267. }
  268. foreach ($config_list as $val) {
  269. $sms_config[$val['key']] = $val['value'];
  270. }
  271. S($key, json_encode($sms_config), 60);
  272. } else {
  273. $sms_config = json_decode($sms_cache_config, true);
  274. }
  275. return $sms_config;
  276. }
  277. /**
  278. * 返回JSON成功信息
  279. * message: 消息文本
  280. * data: 额外数据
  281. * @time 2022年06月09日 14:22
  282. */
  283. function json_success($message, $data = '', $imei = '')
  284. {
  285. echo json_encode(array('success' => true, 'message' => $message, 'data' => $data, 'imei' => $imei), JSON_UNESCAPED_UNICODE);
  286. exit;
  287. }
  288. /**
  289. * 失败JSON成功信息
  290. * message: 消息文本
  291. * data: 额外数据
  292. * @time 2022年06月09日 14:22
  293. */
  294. function json_fail($message, $data = '', $imei = '')
  295. {
  296. echo json_encode(array('success' => false, 'message' => $message, 'data' => $data, 'imei' => $imei), JSON_UNESCAPED_UNICODE);
  297. exit;
  298. }
  299. /**
  300. * @Descripttion: 获取当前毫秒级时间戳
  301. * @name: likang
  302. * @return {*}
  303. */
  304. function msectime()
  305. {
  306. list($msec, $sec) = explode(' ', microtime());
  307. $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
  308. return $msectime;
  309. }
  310. /**
  311. * @Descripttion: 优化json字符串
  312. * @name: likang
  313. * @return {*}
  314. */
  315. function optimize_json($tempResult)
  316. {
  317. return preg_replace('/[\x00-\x1F\x80-\x9F]/u', '', trim($tempResult));
  318. }