common.php 11 KB

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