ApiAction.class.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. <?php
  2. class ApiAction extends Action {
  3. public function login( ){
  4. $data = I('post.data');
  5. //$data = json_decode(htmlspecialchars_decode(I('post.data')),true);
  6. if(!$data){
  7. $this->api_fail(C('FAIL'),'没有用户信息');
  8. }
  9. $cond = array(
  10. 'LicensePlate|GpsDeviceNumber|DeviceNumber'=>$data['username'],
  11. 'Password'=>$data['password']
  12. );
  13. $userinfo = M('jms_vehicle')->where($cond)->find();
  14. if(!$userinfo){
  15. $this->api_fail(C('FAIL'),'用户名或者密码不正确');
  16. }
  17. $token = md5($data['username'].I('post.loginMark').$data['password'].date('Y-m-d H:i:s'));
  18. $result = \Jiaruan\RedisCache::setSessionRules(I('post.loginMark'),$token);
  19. if($result === false){
  20. $this->api_fail(C('FAIL'),'添加redis失败');
  21. }
  22. $where = array("ID" => $userinfo['ID']);
  23. $data = array(
  24. "LoginMark" => I('post.loginMark'),
  25. "Token" => $token
  26. );
  27. $res = M('jms_vehicle')->createSave($where,$data);
  28. if(!$res){
  29. $this->api_fail(C('FAIL'),'token信息保存失败');
  30. }
  31. $baseinfo = array(
  32. "userId" => $userinfo['ID'],
  33. "enCode" => "System",
  34. "account" => $userinfo['FullName'],
  35. "password" => $userinfo['Password'],
  36. "secretkey" => '',
  37. "realName" => $userinfo['FullName'],
  38. "nickName" => $userinfo['FullName'],
  39. "headIcon" => ".jpg",
  40. "gender" => $userinfo['Sex'],
  41. "mobile" => $userinfo['UserPhone'],
  42. "telephone" => $userinfo['Telephone'],
  43. "email" => $userinfo['Email'],
  44. "oICQ" => $userinfo['QQ'],
  45. "weChat" => $userinfo['WxName'],
  46. "companyId" => "",
  47. "companyIds" => array(),
  48. "departmentId" => "",
  49. "departmentIds" => array(),
  50. "openId" => $userinfo['WxOpenId'],
  51. "roleIds" => $userinfo['RoleId'],
  52. "postIds" => "",
  53. "isSystem" => true,
  54. "appId" => "renlian_1.0.0_App",
  55. "logTime" => date('Y-m-d H:i:s'),
  56. "iPAddress" => $_SERVER['REMOTE_ADDR'],
  57. "browser" => "Safari 11.0",
  58. "loginMark" => I('post.loginMark'),
  59. "token" => $token,
  60. "imUrl" => '',
  61. "imOpen" => '',
  62. "wfProcessId" => '',
  63. "photoUrl" => $userinfo['Avatar'],
  64. "plate" => $userinfo['LicensePlate'],
  65. 'deviceNumber' => $userinfo['DeviceNumber'],
  66. 'gpsNumber' => $userinfo['GpsDeviceNumber'],
  67. 'cityid' => $userinfo['CityId']
  68. );
  69. $data = array(
  70. "baseinfo" => $baseinfo,
  71. "post" => array(),
  72. "role" => array()
  73. );
  74. $this->api_success('登录成功',$data);
  75. }
  76. public function modifypassword( ){
  77. $this->token_verify();
  78. $post_data = I('post.data');
  79. if( empty($post_data) ){
  80. $this->api_fail(C('FAIL'),'post_data不能为空!');
  81. }
  82. $userid = $post_data['userid'];
  83. $newpassword = $post_data['newpassword'];
  84. $oldpassword = $post_data['oldpassword'];
  85. if(!M('jms_vehicle')->where(array('ID'=>$userid,'Password'=>$oldpassword))->find()){
  86. $this->api_fail(C('FAIL'),'旧密码不正确');
  87. }
  88. $where = array(
  89. 'ID' => $userid,
  90. 'Password' => $oldpassword
  91. );
  92. $data = array(
  93. 'Password' => $newpassword
  94. );
  95. $res = M('jms_vehicle')->createSave($where,$data);
  96. if(!$res){
  97. $this->api_fail(C('FAIL'),'修改密码失败');
  98. }
  99. $this->api_success('修改成功');
  100. }
  101. public function get_baojing_info( ){
  102. $this->token_verify();
  103. $plate = I('get.plate');
  104. $msg = M('jms_baojing_message')->where(array('Type'=>C('STOLEN_ALARM'),'LicensePlate'=>$plate))->select();
  105. if(!$msg){
  106. $this->api_fail(C('FAIL'),'报警信息不存在');
  107. }
  108. $this->api_success('成功',$msg);
  109. }
  110. public function get_map_index_data( ){
  111. $this->token_verify();
  112. $deviceNumber = $_GET['data']['deviceNumber'];
  113. $licensePlate = $_GET['data']['licensePlate'];
  114. if(!$deviceNumber){
  115. $this->api_fail(C('FAIL'),'faild, no deviceNumber !');
  116. }
  117. //获取车辆最新位置mysql
  118. /*
  119. $result = M('jms_vehicle')->field('GpsLongitude, GpsLatitude, Address, FenceShapeInfo, LockStatus, FenceAlarmEnable, Battery, GpsOnlineTime, Speed, CityId')->where(array('DeviceNumber' => $deviceNumber))->find();
  120. */
  121. //redis获取
  122. $vehicle = Redis('fdrc_vehicle','hash');
  123. //var_dump($vehicle);
  124. $vehicleInfo = $vehicle->get($deviceNumber);
  125. $vehicleInfo = json_decode($vehicleInfo, true);
  126. $rlfd_vehicle_fence = Redis('rlfd_vehicle_fence','hash');
  127. $fence = $rlfd_vehicle_fence->get($licensePlate);
  128. //$fence = json_decode($fence, true);
  129. $lastLocReis = Redis('rfld_gps_last_location','hash');
  130. $lastLoction = $lastLocReis->get(strtoupper($gpsNumber));
  131. $lastLoction = $lastLoction = json_decode($lastLocation, true);
  132. /*
  133. if(!$result){
  134. $this->api_fail(C('FAIL'),'cant find this vehicle !');
  135. }
  136. */
  137. $onlineTimeStamp = strtotime($lastLoction['GpsOnlineTime']);
  138. if(!$onlineTimeStamp){
  139. $onlineTimeStamp = 0;
  140. }
  141. /*
  142. if(!$result['GpsLongitude'] || !$result['GpsLatitude']){
  143. $where = array('ID' => $result['CityId']);
  144. $cityLngLat = M('jms_city')->field('CenterLng, CenterLat')->where($where)->find();
  145. $result['GpsLongitude'] = $cityLngLat['CenterLng'];
  146. $result['GpsLatitude'] = $cityLngLat['CenterLat'];
  147. }
  148. */
  149. $mapData = array(
  150. 'vehicleLocation' => array('longitude' => $lastLoction['GpsLongitude'], 'latitude' => $lastLoction['GpsLatitude']),
  151. // 'vehicleAddress' => $result['Address'],
  152. 'lockStatus' => $vehicleInfo['LockStatus'],
  153. 'fenceShapeInfo' => $fence,
  154. 'battery' => $lastLoction['Battery'],
  155. // 'fenceAlarmStatus' => $result['FenceAlarmEnable'],
  156. 'onlineTime' => $onlineTimeStamp,
  157. 'speed' => $result['Speed']
  158. );
  159. $this->api_success('success', $mapData);
  160. }
  161. public function get_insure_info( ){
  162. $this->token_verify();
  163. $cityid = I('get.cityid');
  164. $insure_info = M('jms_insurance')->where(array('CityId'=>$cityid))->select();
  165. if(!$insure_info){
  166. $this->api_fail(C('FAIL'),'保险信息不存在');
  167. }
  168. $this->api_success('成功',$insure_info);
  169. }
  170. public function get_user_platenumber( ){
  171. $this->token_verify();
  172. $userid = I('get.userid');
  173. if(!$userid){
  174. $this->api_fail(C('FAIL'),'用户id不存在');
  175. }
  176. $plate_info = M('jms_vehicle')->where(array('ID'=>$userid))->field('ID,LicensePlate')->select();
  177. if(!$plate_info){
  178. $this->api_fail(C('FAIL'),'该车主没有使用的车牌');
  179. }
  180. $this->api_success('成功',$plate_info);
  181. }
  182. public function submit_add_baojing( ){
  183. $this->token_verify();
  184. $post_data = I('post.data');
  185. if( empty($post_data) ){
  186. $this->api_fail(C('FAIL'),'post_data不能为空!');
  187. }
  188. $Keyword = strtoupper($post_data['LicensePlate']);
  189. if ( empty($Keyword)) {
  190. $this->api_fail(C('FAIL'),'报警车辆不能为空');
  191. }
  192. $userid = $post_data['userid'];
  193. if ( empty($userid)) {
  194. $this->api_fail(C('FAIL'),'用户id不能为空');
  195. }
  196. $stolenaddress = $post_data['StolenAddress'];
  197. if ( empty($stolenaddress)) {
  198. $this->api_fail(C('FAIL'),'事发地址不能为空');
  199. }
  200. //获取登录用户城市id
  201. /*$cond = array('ID'=>$userid);
  202. $cityid = M('uc_user')->where($cond)->getField('CityId');
  203. if(!$cityid){
  204. $this->api_fail('failerror','获取登录城市失败!');
  205. }*/
  206. $vehicle_info = M('jms_vehicle')->where(array( 'LicensePlate|IdCard|UserPhone' => $Keyword ))->field('ID,UserId,CityId,IdCard,UserPhone,LicensePlate')->find();
  207. if(!$vehicle_info){
  208. $this->api_fail(C('FAIL'),'报警车辆不存在!');
  209. }
  210. /*if( $vehicle_info['CityId'] != $cityid ){
  211. $this->api_fail('failerror','该车辆不在管辖区域,无法添加报警!');
  212. }
  213. if(!$vehicle_info['UserId']){
  214. $this->api_fail('failerror','此车辆还未开户,不能添加报警信息!');
  215. }*/
  216. //$vehicle_id = $vehicle_info['ID'];
  217. /*$chezhu_id = $vehicle_info['UserId'];
  218. //$chezhu_info = fd_get_userinfo($chezhu_id);
  219. //$chezhu_info = get_chezhu_info($chezhu_id);*/
  220. //$this->api_check_vehicle_isrepeat2($vehicle_id);
  221. if (I('post.StolenDate')) {
  222. $StolenDate = $post_data['StolenDate'];
  223. $StolenDate = date('Y-m-d',strtotime($StolenDate));
  224. // 被盗日期不能超过当前时间
  225. $now_time = date('Y-m-d');
  226. if(strtotime($StolenDate) > strtotime($now_time)){
  227. $this->api_fail(C('FAIL'),'被盗日期不能超过当前时间!');
  228. }
  229. } else {
  230. $StolenDate = date('Y-m-d');
  231. }
  232. if(M('jms_baojing')->where(array('LicensePlate' => $vehicle_info['LicensePlate'],'StolenState'=>0))->find()){
  233. $this->api_fail(C('FAIL'),'报警信息已经存在');
  234. }
  235. $data = array(
  236. 'WoContent' => $post_data['WoContent'],
  237. 'VehicleId' => $userid,
  238. 'LicensePlate' => $vehicle_info['LicensePlate'],
  239. 'IdCard' => $vehicle_info['IdCard'],
  240. 'UserId' => $userid,
  241. 'UserPhone' => I('post.UserPhone'),
  242. 'CityId' => $vehicle_info['CityId'],
  243. 'SubmitUserId' => $userid,
  244. 'SubmitTime' => date('Y-m-d H:i:s'),
  245. 'StolenDate' => $StolenDate,
  246. 'StolenCityId' => $vehicle_info['CityId'], //新增:被盗城市id
  247. );
  248. if (!M('jms_baojing')->createAdd($data)) {
  249. $this->api_fail(C('FAIL'),'添加报警信息失败');
  250. }
  251. $this->api_success('添加报警信息成功');
  252. }
  253. public function update_jg_registrationid( ){
  254. $this->token_verify();
  255. $post_data = I('post.data');
  256. //$post_data = json_decode(htmlspecialchars_decode(I('post.data')),true);
  257. if( empty($post_data) ){
  258. $this->api_fail(C('FAIL'),'post_data不能为空!');
  259. }
  260. $regid = $post_data['regid'];//极光推设备唯一标识id
  261. if( empty($regid) ){
  262. $this->api_fail(C('FAIL'),'极光推注册id不能为空!');
  263. }
  264. if(!$post_data['uid']){
  265. $this->api_fail(C('FAIL'),'uid不能为空!');
  266. }
  267. $cond = array(
  268. 'ID' => $post_data['uid']
  269. );
  270. $result = M('jms_vehicle')->where($cond)->setField('JgClientRegistrationId',$regid);
  271. if( false === $result ){
  272. $this->api_fail(C('FAIL'),'更新极光推注册信息失败! error:'.M('jms_vehicle')->getDbError());
  273. }
  274. $this->api_success('更新成功');
  275. }
  276. public function save_fence_info( ){
  277. $this->token_verify();
  278. $fence_info =I('post.data');
  279. $fenceData = htmlspecialchars_decode($fence_info['data']);
  280. $plate = $fence_info['plate'];
  281. if( !$fence_info || !$plate){
  282. json_fail('Missing param !');
  283. }
  284. var_dump($fence_info );
  285. var_dump($fenceData );exit;
  286. $data = array(
  287. 'fenceStatus' => $fence_info['fenceAlarmEnable'] == 'true' ? true : false,
  288. 'fenceInfo' => $fenceData
  289. );
  290. //围栏信息改为存入redis表 rlfd_vehicle_fence( key车牌号,value围栏信息)
  291. $rlfd_vehicle_fence = Redis('rlfd_vehicle_fence','hash');
  292. $key = $plate;
  293. $val = json_encode($data, JSON_UNESCAPED_UNICODE);
  294. $hash = array($key=>$val);
  295. $result = $rlfd_vehicle_fence->add($hash);
  296. if( $result === false ){
  297. json_fail('保存失败');
  298. }
  299. json_success('保存成功');
  300. }
  301. public function get_gps_route( ){
  302. $this->token_verify();
  303. $postParm = I('post.data');
  304. $search_date = $postParm['date'];
  305. $gps_number = $postParm['gpsNumber'];
  306. if( !$gps_number || !$search_date){
  307. json_fail('Missing param !');
  308. }
  309. $timestamp = strtotime($search_date);
  310. if( !$timestamp ){
  311. json_fail('Date format error !');
  312. }
  313. $start_date = strtotime( date('Y-m-d 00:00:00',$timestamp) );
  314. $end_date = strtotime( date('Y-m-d 23:59:59',$timestamp) );
  315. $cond = array(
  316. 'DeviceId' => $gps_number,
  317. 'DeviceTime' => array( 'between', array($start_date,$end_date) ),
  318. );
  319. $fields = 'Longitude as lng,Latitude as lat,Speed as speed, DeviceTime as deviceTime';
  320. $dateStr = str_replace("-","",$search_date);
  321. $route_list = M('gps_location_'.$dateStr, '', C('DB_DSN_GPS'))->field($fields)->where($cond)->order('DeviceTime asc')->select();
  322. //var_dump(M('gps_location_'.$dateStr, '', C('DB_DSN_GPS'))->getLastSql());
  323. if( !$route_list ){
  324. json_fail('无轨迹 !');
  325. }
  326. foreach($route_list as &$v){
  327. $v['deviceTime'] = date('Y-m-d H:i:s', $v['deviceTime']);
  328. }
  329. $route_data = json_encode($route_list);
  330. //$testData = json_encode($testData);
  331. json_success('success',$route_data);
  332. //json_success('success',$testData);
  333. }
  334. public function change_lock_status( ){
  335. $this->token_verify();
  336. $lock_state = I('post.data')['lockStatus'];
  337. $device_number = I('post.data')['deviceNumber'];
  338. if( !$device_number || $lock_state === ''){
  339. json_fail('Missing param !');
  340. }
  341. //检查锁定状态值
  342. if( !is_numeric($lock_state) && ($lock_state != 0 && $lock_state != 1) ){
  343. json_fail('unknown state !');
  344. }
  345. $cond = array('DeviceNumber' => $device_number);
  346. $result = M('jms_vehicle')->where($cond)->setField('LockStatus',$lock_state);
  347. if( $result === false ){
  348. json_fail('设置失败');
  349. }
  350. json_success('设置成功');
  351. }
  352. public function api_success( $msg, $data ){
  353. $array = array(
  354. 'success'=>true,
  355. 'message'=>$msg,
  356. 'data' => $data,
  357. 'code'=>200
  358. );
  359. echo json_encode($array,JSON_UNESCAPED_UNICODE);
  360. exit;
  361. }
  362. public function api_fail( $code, $msg, $data ){
  363. $array = array(
  364. 'success'=>false,
  365. 'message'=>$msg,
  366. 'data' => $data,
  367. 'code'=>$code
  368. );
  369. echo json_encode($array,JSON_UNESCAPED_UNICODE);
  370. exit;
  371. }
  372. public function get_gonggao_info( ){
  373. $this->token_verify();
  374. $plate = I('get.plate');
  375. $msg = M('jms_baojing_message')->where(array('Type'=>C('BROADCASTING'),'LicensePlate'=>$plate))->select();
  376. if(!$msg){
  377. $this->api_fail(C('FAIL'),'公告信息不存在');
  378. }
  379. $this->api_success('成功',$msg);
  380. }
  381. public function token_verify( ){
  382. $token = I('token');//用户登录token
  383. $login_mark = I('loginMark');
  384. if(!$token){
  385. $this->api_fail(C('FAIL'),'token不存在 !');
  386. }
  387. if(!$login_mark){
  388. $this->api_fail(C('FAIL'),'login_mark不存在 !');
  389. }
  390. //获取token
  391. $redis = Redis('czapp_client_login_session','hash');
  392. $key = $login_mark;
  393. $val = json_decode($redis->get($key),true);
  394. if(!$val){
  395. $this->api_fail(C('FAIL'),'无效token1 !');
  396. }
  397. //验证token是否一致
  398. if( $token != $val['token'] ){
  399. $this->api_fail(C('FAIL'),'无效token2 !');
  400. }
  401. //验证token是否过期
  402. if(!$val['login_time']){
  403. $this->api_fail(C('FAIL'),'没有登录时间,token校验失败 !');
  404. }
  405. $login_time = strtotime($val['login_time']);
  406. $end_time = $login_time + $val['expire']*60;
  407. if(time()>$end_time){
  408. $this->api_fail(C('TOKEN_OVERTIME'),'token失效,请重新登录 !');
  409. }
  410. }
  411. public function get_gonggao_detail( ){
  412. header('Access-Control-Allow-Origin:*');
  413. $this->token_verify();
  414. $plate = I('get.plate');
  415. $msg = M('jms_baojing_message')->where(array('Type'=>C('BROADCASTING'),'LicensePlate'=>$plate))->find();
  416. if(!$msg){
  417. $this->api_fail(C('FAIL'),'公告信息不存在');
  418. }
  419. $this->api_success('成功',$msg);
  420. }
  421. public function get_baojing_detail( ){
  422. $this->token_verify();
  423. $plate = I('get.plate');
  424. $addtime = I('get.addtime');
  425. $msg = M('jms_baojing_message')->where(array('Type'=>C('STOLEN_ALARM'),'LicensePlate'=>$plate,'AddTime'=>$addtime))->find();
  426. if(!$msg){
  427. $this->api_fail(C('FAIL'),'报警信息不存在');
  428. }
  429. $this->api_success('成功',$msg);
  430. }
  431. public function register( ){
  432. $where = array('LicensePlate'=>I('post.LicensePlate'));
  433. $userinfo = M('jms_vehicle')->where($where)->find();
  434. if(!$userinfo){
  435. $this->api_fail(C('FAIL'),'用户车牌还未登记');
  436. }
  437. if($userinfo['FullName'] != I('post.FullName')){
  438. $this->api_fail(C('FAIL'),'姓名和备案时填写不一致');
  439. }
  440. if($userinfo['IdCard'] != I('post.IdCard')){
  441. $this->api_fail(C('FAIL'),'身份证和备案时填写不一致');
  442. }
  443. $data = array(
  444. 'Password' => I('post.Password'),
  445. );
  446. $result = M('jms_vehicle')->createSave($where,$data);
  447. if(!$result){
  448. $this->api_fail(C('FAIL'),'注册失败');
  449. }
  450. $this->api_success('注册成功');
  451. }
  452. public function get_fence_info( ){
  453. $this->token_verify();
  454. $plate = I('get.data');
  455. if( !$plate){
  456. json_fail('Missing param !');
  457. }
  458. $rlfd_vehicle_fence = Redis('rlfd_vehicle_fence','hash');
  459. $fence = $rlfd_vehicle_fence->get($plate);
  460. if( !$fence ){
  461. json_fail('无围栏 !');
  462. }
  463. //$fence = json_encode($fence);
  464. json_success('success',$fence);
  465. }
  466. public function get_gps_route_table( ){
  467. $this->token_verify();
  468. $postParm = I('post.data');
  469. $search_date = $postParm['date'];
  470. $gps_number = $postParm['gpsNumber'];
  471. if( !$gps_number || !$search_date){
  472. json_fail('请检查GPS标签或者日期 !');
  473. }
  474. //$list = $this->getBTGpsRoute($search_date,'1');//test
  475. $list = $this->getBTGpsRoute($search_date, $gps_number);
  476. $lngLatAlter = new \Jms\Algo\Geometry();
  477. $respData = array();
  478. foreach($list as $v){
  479. $alterRes = $lngLatAlter->convertBd09ToGcj02($v['Latitude'], $v['Longitude']);
  480. $nv = array(
  481. 'lat' => $alterRes['lat'],
  482. 'lng' => $alterRes['lng'],
  483. 'speed' => $v['Speed'],
  484. 'deviceTime' => $v['OnlineTime']
  485. );
  486. array_push($respData, $nv);
  487. }
  488. /*
  489. foreach(&$respData as &$v){
  490. $v['deviceTime'] = date('Y-m-d H:i:s', $v['deviceTime']);
  491. }
  492. */
  493. if(!$respData){
  494. json_success('无轨迹');
  495. }
  496. $route_data = json_encode($respData);
  497. //$testData = json_encode($testData);
  498. json_success('success',$route_data);
  499. //json_success('success',$testData);
  500. }
  501. private function getBTGpsRoute( $date, $gpsNumber ){
  502. if(!$gpsNumber){
  503. return false;
  504. }
  505. if(!$date){
  506. $date = date('Y-m-d');
  507. }
  508. //获取所有轨迹信号
  509. $option = array();
  510. $option['pagesize'] = 1000;
  511. $option['asc'];
  512. $option['fields'] = array('Longitude','Latitude','Speed','DeviceTime');
  513. $routesig = new \Rlfd\Route\RouteSignal();
  514. $list = $routesig->queryEbicyleDailyGpsSignals($gpsNumber, $date,$option);
  515. //var_dump($list);
  516. //var_dump(array('route'=>$list,'vehicle'=>$vehicle_info));
  517. return $list;
  518. }
  519. public function get_driving_data( ){
  520. $gpsNumber = $_GET['data']['gpsNumber'];
  521. //var_dump($_GET);
  522. $days = C('DRIVING_STATISTICS_DAYS');
  523. if(!$gpsNumber){
  524. json_fail('未获取到gps设备号');
  525. }
  526. if(!$days){
  527. $days = 5;
  528. }
  529. $drivingData = array();
  530. for($i = $days-1; $i > 0; --$i){
  531. $preStr2time = strtotime('-'.$i.'days');
  532. $date = date('Y-m-d', $preStr2time);
  533. /*
  534. $where = array(
  535. 'GpsDeviceNumber' => $gpsNumber,
  536. 'Date' => $date
  537. );
  538. $sqlData = M('gps_driving_data')->where($where)->find();//当天前的数据查询sql数据库
  539. if($sqlData){
  540. $dayData = array(
  541. 'date' => $date,
  542. 'averageSpeed' => $sqlData['AverageSpeed'],
  543. 'totalTime' => $sqlData['TotalTime'],
  544. 'totalDist' => $sqlData['TotalDistance']
  545. );
  546. array_push($drivingData, $dayData);
  547. continue;
  548. }
  549. */
  550. $cache = S($gpsNumber.'_cache'.$date);
  551. if($cache){
  552. array_push($drivingData, $cache);
  553. continue;
  554. }else{
  555. $dayData = $this->searchDrivingData($gpsNumber, $date);
  556. array_push($drivingData, $dayData);
  557. S($gpsNumber.'_cache'.$date, $dayData, 3600*24*7);
  558. }
  559. }
  560. //当日
  561. $todayDate = date('Y-m-d');
  562. $todayCache = S($gpsNumber.'_cache'.$todayDate);
  563. if(!$todayCache){
  564. $todayData = $this->searchDrivingData($gpsNumber, $todayDate);
  565. array_push($drivingData, $todayData);
  566. $todayCache = S($gpsNumber.'_cache'.$todayData, $todayData, 3600*24);
  567. }else{
  568. $newOnlineTime = M('jms_vehicle')->where(array('GpsDeviceNumber' => $gpsNumber))->getField('GpsOnlineTime');
  569. $nTime = strtotime($newOnlineTime);
  570. $oTime = strtotime($lastOnlineTime);
  571. if($nTime == $oTime){
  572. array_push($drivingData, $todayCache);
  573. }else{
  574. $todayData = $this->searchDrivingData($gpsNumber, $date);
  575. array_push($drivingData, $todayData);
  576. $todayCache = S($gpsNumber.'_cache'.$todayDate, $todayData, 3600*24);
  577. }
  578. }
  579. //dump($drivingData);
  580. if(!$drivingData){
  581. json_fail('未查询到近'.$days.'日行驶数据');
  582. }
  583. json_success('查询成功', $drivingData);
  584. }
  585. private function searchDrivingData( $gpsNumber, $date ){
  586. if(!$gpsNumber){
  587. return false;
  588. }
  589. $start = microtime(true);
  590. $result = $this->getBTGpsRoute( $date,$gpsNumber);
  591. $end = microtime(true);
  592. $totalTime = 0;
  593. $totalDist = 0;
  594. $lastOnlineTime =end($result)['OnlineTime'];
  595. for($i = 0; $i < count($result)-1; ++$i){
  596. $lat1 = $result[$i]['lat'];
  597. $lng1 = $result[$i]['lng'];
  598. $lat2 = $result[$i+1]['lat'];
  599. $lng2 = $result[$i+1]['lng'];
  600. $dist = $this->get_2points_distance($lat1,$lng1,$lat2,$lng2 );
  601. $totalDist += $dist;
  602. $time = strtotime($result[$i+1]['OnlineTime']) - strtotime($result[$i]['OnlineTime']);
  603. $totalTime += ($time/3600);
  604. }
  605. return array(
  606. 'totalTime' => $totalTime,
  607. 'totalDist' => $totalDist,
  608. // 'averageSpeed' => $totalDist/$totalTime,
  609. 'lastOnlineTime' => $lastOnlineTime,
  610. 'caculateTime' => $end - $start
  611. );
  612. }
  613. private function get_2points_distance( $lat1, $lng1, $lat2, $lng2, $radius = 6378.137 ){
  614. $rad = floatval(M_PI / 180.0);
  615. $lat1 = floatval($lat1) * $rad;
  616. $lng1 = floatval($lng1) * $rad;
  617. $lat2 = floatval($lat2) * $rad;
  618. $lng2 = floatval($lng2) * $rad;
  619. $theta = $lng2 - $lng1;
  620. $dist = acos(sin($lat1) * sin($lat2) +
  621. cos($lat1) * cos($lat2) * cos($theta)
  622. );
  623. if ($dist < 0 ) {
  624. $dist += M_PI;
  625. }
  626. return $dist = $dist * $radius;
  627. }
  628. public function test_function( ){
  629. $lat1 = '31.253411';
  630. $lng1 = '121.518998';
  631. $lat2 = '31.277117';
  632. $lng2 = '120.744587';
  633. $distance = $this->get_2points_distance($lat1, $lng1, $lat2, $lng2);
  634. var_dump($distance);
  635. }
  636. public function get_vehicle_ElectronicPlateUrl( ){
  637. $this->token_verify();
  638. $userid = I('userid');
  639. $result = M('jms_vehicle')->where(array('ID'=>$userid))->field('FrontElectronicPlateUrl,BackElectronicPlateUrl')->cache('key',86400)->select();
  640. $electronicPlateUrl = S('key');
  641. $this->api_success('成功',$electronicPlateUrl[0]);
  642. }
  643. public function getCityInfo( ){
  644. $this->token_verify();
  645. $cityid = I('get.cityid');
  646. if(!$cityid){
  647. $this->api_fail(C('FAIL'),'开户城市不存在');
  648. }
  649. $parentcity = M('jms_city')->where(array('ID'=>$cityid))->find();
  650. $child_1 = M('jms_city')->where(array('ParentId'=>$parentcity['ParentId']))->select();
  651. $this->api_success('成功',$child_1);
  652. }
  653. public function get_cityinfo_by_cityid( ){
  654. $cityid = I('cityid');
  655. $ids = M('jms_city')->field('ParentId,ProvinceId')->where(array('ID'=>$cityid))->find();
  656. $result = $ids['ProvinceId'].','.($ids['ParentId']-100).','.($cityid-1000);
  657. $this->api_success('成功',$result);
  658. }
  659. }