ApiAction.class.php 27 KB

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