ApiAction.class.php 23 KB

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