ApiAction.class.php 23 KB

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