common.php 11 KB

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