ApiAction.class.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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. $bjtype = $post_data['bjtypeid'];
  255. if ( empty($bjtype)) {
  256. $this->api_fail(C('FAIL'),'请选择报警类型');
  257. }
  258. $stolenaddress = $post_data['StolenAddress'];
  259. if ( empty($stolenaddress)) {
  260. $this->api_fail(C('FAIL'),'事发地址不能为空');
  261. }
  262. $WoContent = trim($post_data['WoContent']);
  263. if (empty($WoContent)) {
  264. $this->api_fail(C('FAIL'),'报警原因不能为空');
  265. }
  266. //获取登录用户城市id
  267. /*$cond = array('ID'=>$userid);
  268. $cityid = M('uc_user')->where($cond)->getField('CityId');
  269. if(!$cityid){
  270. $this->api_fail('failerror','获取登录城市失败!');
  271. }*/
  272. $vehicle_info = M('jms_vehicle')->where(array( 'LicensePlate|IdCard|UserPhone' => $Keyword ))->field('ID,UserId,CityId,IdCard,UserPhone,LicensePlate')->find();
  273. if(!$vehicle_info){
  274. $this->api_fail(C('FAIL'),'报警车辆不存在!');
  275. }
  276. /*if( $vehicle_info['CityId'] != $cityid ){
  277. $this->api_fail('failerror','该车辆不在管辖区域,无法添加报警!');
  278. }
  279. if(!$vehicle_info['UserId']){
  280. $this->api_fail('failerror','此车辆还未开户,不能添加报警信息!');
  281. }*/
  282. //$vehicle_id = $vehicle_info['ID'];
  283. /*$chezhu_id = $vehicle_info['UserId'];
  284. //$chezhu_info = fd_get_userinfo($chezhu_id);
  285. //$chezhu_info = get_chezhu_info($chezhu_id);*/
  286. //$this->api_check_vehicle_isrepeat2($vehicle_id);
  287. if ($post_data['StolenDate'] != '请选择') {
  288. $StolenDate = $post_data['StolenDate'];
  289. $StolenDate = date('Y-m-d',strtotime($StolenDate));
  290. // 被盗日期不能超过当前时间
  291. $now_time = date('Y-m-d');
  292. if(strtotime($StolenDate) > strtotime($now_time)){
  293. $this->api_fail(C('FAIL'),'被盗日期不能超过当前时间!');
  294. }
  295. } else {
  296. $StolenDate = date('Y-m-d');
  297. }
  298. if(M('jms_baojing')->where(array('LicensePlate' => $vehicle_info['LicensePlate'],'StolenState'=>0))->find()){
  299. $this->api_fail(C('FAIL'),'报警信息已经存在');
  300. }
  301. $StolenCityId = explode(',',$stolenCityId);
  302. $StolenCityId = $StolenCityId[2]+1000;
  303. $bjtype = explode(',',$bjtype);
  304. if($bjtype[2]=='-'){
  305. $alarmnumber = $bjtype[1];
  306. }else{
  307. $alarmnumber = $bjtype[2];
  308. }
  309. $data = array(
  310. 'WoContent' => $WoContent,
  311. 'VehicleId' => $userid,
  312. 'LicensePlate' => $vehicle_info['LicensePlate'],
  313. 'IdCard' => $vehicle_info['IdCard'],
  314. 'UserId' => $userid,
  315. 'UserPhone' =>$mobile,
  316. 'CityId' => $vehicle_info['CityId'],
  317. 'SubmitUserId' => $userid,
  318. 'SubmitTime' => date('Y-m-d H:i:s'),
  319. 'StolenDate' => $StolenDate,
  320. 'StolenCityId' => $StolenCityId, //新增:被盗城市id
  321. 'StolenAddress' => $stolenaddress,
  322. 'AlarmNumber' => $alarmnumber
  323. );
  324. if (!M('jms_baojing')->createAdd($data)) {
  325. $this->api_fail(C('FAIL'),'添加报警信息失败');
  326. }
  327. $this->api_success('添加报警信息成功');
  328. }
  329. public function update_jg_registrationid( ){
  330. $this->token_verify();
  331. $post_data = I('post.data');
  332. //$post_data = json_decode(htmlspecialchars_decode(I('post.data')),true);
  333. if( empty($post_data) ){
  334. $this->api_fail(C('FAIL'),'post_data不能为空!');
  335. }
  336. $regid = $post_data['regid'];//极光推设备唯一标识id
  337. if( empty($regid) ){
  338. $this->api_fail(C('FAIL'),'极光推注册id不能为空!');
  339. }
  340. if(!$post_data['uid']){
  341. $this->api_fail(C('FAIL'),'uid不能为空!');
  342. }
  343. $cond = array(
  344. 'ID' => $post_data['uid']
  345. );
  346. $result = M('jms_vehicle')->where($cond)->setField('JgClientRegistrationId',$regid);
  347. if( false === $result ){
  348. $this->api_fail(C('FAIL'),'更新极光推注册信息失败! error:'.M('jms_vehicle')->getDbError());
  349. }
  350. $this->api_success('更新成功');
  351. }
  352. public function save_fence_info( ){
  353. $this->token_verify();
  354. $fence_info =I('post.data');
  355. $fenceData = htmlspecialchars_decode($fence_info['data']);
  356. $plate = $fence_info['plate'];
  357. if( !$fence_info || !$plate){
  358. json_fail('Missing param !');
  359. }
  360. $fence_data = json_decode($fenceData, true);
  361. // 保存百度地图
  362. $fence_data['data']['center'] = \Jms\Algo\Geometry::convertGcj02ToBd09($fence_data['data']['center']['lat'], $fence_data['data']['center']['lng']);
  363. $data = array(
  364. 'fenceStatus' => $fence_info['fenceAlarmEnable'] == 'true' ? true : false,
  365. 'fenceInfo' => $fence_data
  366. );
  367. //围栏信息改为存入redis表 rlfd_vehicle_fence( key车牌号,value围栏信息)
  368. $rlfd_vehicle_fence = Redis('rlfd_vehicle_fence','hash');
  369. $key = $plate;
  370. $val = json_encode($data, JSON_UNESCAPED_UNICODE);
  371. $hash = array($key=>$val);
  372. $result = $rlfd_vehicle_fence->add($hash);
  373. if( $result === false ){
  374. json_fail('保存失败');
  375. }
  376. json_success('保存成功');
  377. }
  378. public function get_gps_route( ){
  379. $this->token_verify();
  380. $postParm = I('post.data');
  381. $search_date = $postParm['date'];
  382. $gps_number = $postParm['gpsNumber'];
  383. if( !$gps_number || !$search_date){
  384. json_fail('Missing param !');
  385. }
  386. $timestamp = strtotime($search_date);
  387. if( !$timestamp ){
  388. json_fail('Date format error !');
  389. }
  390. $start_date = strtotime( date('Y-m-d 00:00:00',$timestamp) );
  391. $end_date = strtotime( date('Y-m-d 23:59:59',$timestamp) );
  392. $cond = array(
  393. 'DeviceId' => $gps_number,
  394. 'DeviceTime' => array( 'between', array($start_date,$end_date) ),
  395. );
  396. $fields = 'Longitude as lng,Latitude as lat,Speed as speed, DeviceTime as deviceTime';
  397. $dateStr = str_replace("-","",$search_date);
  398. $route_list = M('gps_location_'.$dateStr, '', C('DB_DSN_GPS'))->field($fields)->where($cond)->order('DeviceTime asc')->select();
  399. //var_dump(M('gps_location_'.$dateStr, '', C('DB_DSN_GPS'))->getLastSql());
  400. if( !$route_list ){
  401. json_fail('无轨迹 !');
  402. }
  403. foreach($route_list as &$v){
  404. $v['deviceTime'] = date('Y-m-d H:i:s', $v['deviceTime']);
  405. }
  406. $route_data = json_encode($route_list);
  407. //$testData = json_encode($testData);
  408. json_success('success',$route_data);
  409. //json_success('success',$testData);
  410. }
  411. public function change_lock_status( ){
  412. $this->token_verify();
  413. $lock_state = I('post.data')['lockStatus'];
  414. if($lock_state === ''){
  415. json_fail('无法获取锁车状态');
  416. }
  417. $device_number = I('post.data')['deviceNumber'];
  418. $gpsNumber = I('post.data')['gpsNumber'];
  419. $plate = I('post.data')['plate'];
  420. if( !$device_number && !$gpsNumber){
  421. json_fail('无法获取rfid或者gps设备号,无法锁车 !');
  422. }
  423. if(!$device_number){
  424. $cond = array('GpsDeviceNumber' => $gpsNumber);
  425. }else{
  426. $cond = array('DeviceNumber' => $device_number);
  427. }
  428. if(!$plate){
  429. json_fail('无法获取车牌号');
  430. }
  431. //检查锁定状态值
  432. if( !is_numeric($lock_state) && ($lock_state != 0 && $lock_state != 1) ){
  433. json_fail('未知的锁车状态值 !');
  434. }
  435. $result = M('jms_vehicle')->where($cond)->setField('LockStatus',$lock_state);
  436. if( $result === false ){
  437. json_fail('设置失败');
  438. }
  439. //同步redis
  440. $vehicle = Redis('czapp_lock_status','hash');
  441. $hash = array($plate => $lock_state);
  442. $lockStatus = $vehicle->add($hash);
  443. if(!$lockStatus){
  444. json_fail('同步锁车状态失败');
  445. }
  446. if($lock_state == 1){
  447. $lastLoction = Redis('rfld_gps_last_location', 'hash');
  448. $lockInfo = $lastLoction->get($gpsNumber);
  449. $lockInfoRedis = Redis('czapp_lockinfo', 'hash');
  450. $lockInfoRes = $lockInfoRedis->add(array($gpsNumber => $lockInfo));
  451. if(!$lockInfoRes){
  452. json_fail('同步锁车信息失败');
  453. }
  454. }
  455. json_success('设置成功');
  456. }
  457. public function api_success( $msg, $data ){
  458. $array = array(
  459. 'success'=>true,
  460. 'message'=>$msg,
  461. 'data' => $data,
  462. 'code'=>200
  463. );
  464. echo json_encode($array,JSON_UNESCAPED_UNICODE);
  465. exit;
  466. }
  467. public function api_fail( $code, $msg, $data ){
  468. $array = array(
  469. 'success'=>false,
  470. 'message'=>$msg,
  471. 'data' => $data,
  472. 'code'=>$code
  473. );
  474. echo json_encode($array,JSON_UNESCAPED_UNICODE);
  475. exit;
  476. }
  477. public function get_gonggao_info( ){
  478. $this->token_verify();
  479. $plate = I('get.plate');
  480. if(!$plate){
  481. json_fail('无法获取车牌');
  482. }
  483. $cityid = I('get.cityid');
  484. if(!$cityid){
  485. $cityid = C('MAP_DEFAULT_CITY_ID');
  486. }
  487. $pageSize = I('get.pagesize');
  488. if(!$pageSize){
  489. $pageSize = 10;
  490. }
  491. $page = I('get.page');
  492. if(!$page){
  493. $page = 0;
  494. }
  495. $start = $pageSize * $page;
  496. $msg = M('jms_baojing_message')->where(array('Type'=>C('BROADCASTING'), 'CityId' => $cityid))->limit($start, $pageSize)->order('AddTime desc')->select();
  497. if(!$msg){
  498. $this->api_fail(C('FAIL'),'公告信息不存在');
  499. }
  500. $this->api_success('成功',$msg);
  501. }
  502. public function token_verify( ){
  503. $token = I('token');//用户登录token
  504. $login_mark = I('loginMark');
  505. if(!$token){
  506. $this->api_fail(C('FAIL'),'token不存在 !');
  507. }
  508. if(!$login_mark){
  509. $this->api_fail(C('FAIL'),'login_mark不存在 !');
  510. }
  511. //获取token
  512. $redis = Redis('czapp_client_login_session','hash');
  513. $key = $login_mark;
  514. $val = json_decode($redis->get($key),true);
  515. if(!$val){
  516. $this->api_fail(C('FAIL'),'无效token1 !');
  517. }
  518. //验证token是否一致
  519. if( $token != $val['token'] ){
  520. $this->api_fail(C('FAIL'),'无效token2 !');
  521. }
  522. //验证token是否过期
  523. if(!$val['login_time']){
  524. $this->api_fail(C('FAIL'),'没有登录时间,token校验失败 !');
  525. }
  526. $login_time = strtotime($val['login_time']);
  527. $end_time = $login_time + $val['expire']*60;
  528. if(time()>$end_time){
  529. $this->api_fail(C('TOKEN_OVERTIME'),'token失效,请重新登录 !');
  530. }
  531. }
  532. public function get_gonggao_detail( ){
  533. header('Access-Control-Allow-Origin:*');
  534. $this->token_verify();
  535. $plate = I('get.plate');
  536. $msg = M('jms_baojing_message')->where(array('Type'=>C('BROADCASTING'),'LicensePlate'=>$plate))->find();
  537. if(!$msg){
  538. $this->api_fail(C('FAIL'),'公告信息不存在');
  539. }
  540. $this->api_success('成功',$msg);
  541. }
  542. public function get_baojing_detail( ){
  543. $this->token_verify();
  544. $plate = I('get.plate');
  545. $id = I('get.id');
  546. $msg = M('jms_baojing_message')->where(array('ID' => $id, 'LicensePlate'=>$plate))->find();
  547. if(!$msg){
  548. $this->api_fail(C('FAIL'),'报警信息不存在');
  549. }
  550. $this->api_success('成功',$msg);
  551. }
  552. public function register( ){
  553. $where = array('LicensePlate'=>I('post.LicensePlate'));
  554. $userinfo = M('jms_vehicle')->where($where)->find();
  555. if(!$userinfo){
  556. $this->api_fail(C('FAIL'),'用户车牌还未登记');
  557. }
  558. if($userinfo['FullName'] != I('post.FullName')){
  559. $this->api_fail(C('FAIL'),'姓名和备案时填写不一致');
  560. }
  561. if($userinfo['IdCard'] != I('post.IdCard')){
  562. $this->api_fail(C('FAIL'),'身份证和备案时填写不一致');
  563. }
  564. $data = array(
  565. 'Password' => I('post.Password'),
  566. );
  567. $result = M('jms_vehicle')->createSave($where,$data);
  568. if(!$result){
  569. $this->api_fail(C('FAIL'),'注册失败');
  570. }
  571. $this->api_success('注册成功');
  572. }
  573. public function get_fence_info( ){
  574. $this->token_verify();
  575. $plate = I('get.data');
  576. if( !$plate){
  577. json_fail('Missing param !');
  578. }
  579. $rlfd_vehicle_fence = Redis('rlfd_vehicle_fence','hash');
  580. $fence = $rlfd_vehicle_fence->get($plate);
  581. if( !$fence ){
  582. json_fail('无围栏 !');
  583. }
  584. $fence_info = json_decode($fence, true);
  585. // 转高德地图坐标
  586. $fence_info['fenceInfo']['data']['center'] = \Jms\Algo\Geometry::convertBd09ToGcj02($fence_info['fenceInfo']['data']['center']['lat'], $fence_info['fenceInfo']['data']['center']['lng']);
  587. $fence = json_encode($fence_info);
  588. json_success('success',$fence);
  589. }
  590. public function get_gps_route_table( ){
  591. $this->token_verify();
  592. $postParm = I('post.data');
  593. $search_date = $postParm['date'];
  594. $gps_number = $postParm['gpsNumber'];
  595. if( !$gps_number || !$search_date){
  596. json_fail('请检查GPS标签或者日期 !');
  597. }
  598. //$list = $this->getBTGpsRoute($search_date,'1');//test
  599. $list = $this->getBTGpsRoute($search_date, $gps_number);
  600. $lngLatAlter = new \Jms\Algo\Geometry();
  601. $respData = array();
  602. foreach($list as $v){
  603. $alterRes = $lngLatAlter->convertBd09ToGcj02($v['Latitude'], $v['Longitude']);
  604. $nv = array(
  605. 'lat' => $alterRes['lat'],
  606. 'lng' => $alterRes['lng'],
  607. 'speed' => $v['Speed'],
  608. 'deviceTime' => $v['OnlineTime']
  609. );
  610. array_push($respData, $nv);
  611. }
  612. /*
  613. foreach(&$respData as &$v){
  614. $v['deviceTime'] = date('Y-m-d H:i:s', $v['deviceTime']);
  615. }
  616. */
  617. if(!$respData){
  618. json_fail('无轨迹');
  619. }
  620. $route_data = json_encode($respData);
  621. //$testData = json_encode($testData);
  622. json_success('success',$route_data);
  623. //json_success('success',$testData);
  624. }
  625. private function getBTGpsRoute( $date, $gpsNumber ){
  626. if(!$gpsNumber){
  627. return false;
  628. }
  629. if(!$date){
  630. $date = date('Y-m-d');
  631. }
  632. //获取所有轨迹信号
  633. $option = array();
  634. $option['pagesize'] = 1000;
  635. $option['asc'];
  636. $option['fields'] = array('Longitude','Latitude','Speed','DeviceTime');
  637. $routesig = new \Rlfd\Route\RouteSignal();
  638. $list = $routesig->queryEbicyleDailyGpsSignals($gpsNumber, $date,$option);
  639. //var_dump($list);
  640. //var_dump(array('route'=>$list,'vehicle'=>$vehicle_info));
  641. return $list;
  642. }
  643. public function get_driving_data( ){
  644. $gpsNumber = $_GET['data']['gpsNumber'];
  645. //var_dump($_GET);
  646. $days = C('DRIVING_STATISTICS_DAYS');
  647. if(!$gpsNumber){
  648. json_fail('未获取到gps设备号');
  649. }
  650. if(!$days){
  651. $days = 5;
  652. }
  653. $drivingData = array();
  654. for($i = $days-1; $i > 0; --$i){
  655. $preStr2time = strtotime('-'.$i.'days');
  656. $date = date('Y-m-d', $preStr2time);
  657. /*
  658. $where = array(
  659. 'GpsDeviceNumber' => $gpsNumber,
  660. 'Date' => $date
  661. );
  662. $sqlData = M('gps_driving_data')->where($where)->find();//当天前的数据查询sql数据库
  663. if($sqlData){
  664. $dayData = array(
  665. 'date' => $date,
  666. 'averageSpeed' => $sqlData['AverageSpeed'],
  667. 'totalTime' => $sqlData['TotalTime'],
  668. 'totalDist' => $sqlData['TotalDistance']
  669. );
  670. array_push($drivingData, $dayData);
  671. continue;
  672. }
  673. */
  674. $cache = S($gpsNumber.'_cache'.$date);
  675. if($cache){
  676. array_push($drivingData, $cache);
  677. continue;
  678. }else{
  679. $dayData = $this->searchDrivingData($gpsNumber, $date);
  680. array_push($drivingData, $dayData);
  681. S($gpsNumber.'_cache'.$date, $dayData, 3600*24*7);
  682. }
  683. }
  684. //当日
  685. $todayDate = date('Y-m-d');
  686. $todayCache = S($gpsNumber.'_cache'.$todayDate);
  687. if(!$todayCache){
  688. $todayData = $this->searchDrivingData($gpsNumber, $todayDate);
  689. array_push($drivingData, $todayData);
  690. $todayCache = S($gpsNumber.'_cache'.$todayData, $todayData, 3600*24);
  691. }else{
  692. $newOnlineTime = M('jms_vehicle')->where(array('GpsDeviceNumber' => $gpsNumber))->getField('GpsOnlineTime');
  693. $nTime = strtotime($newOnlineTime);
  694. $oTime = strtotime($lastOnlineTime);
  695. if($nTime == $oTime){
  696. array_push($drivingData, $todayCache);
  697. }else{
  698. $todayData = $this->searchDrivingData($gpsNumber, $date);
  699. array_push($drivingData, $todayData);
  700. $todayCache = S($gpsNumber.'_cache'.$todayDate, $todayData, 3600*24);
  701. }
  702. }
  703. //dump($drivingData);
  704. if(!$drivingData){
  705. json_fail('未查询到近'.$days.'日行驶数据');
  706. }
  707. json_success('查询成功', $drivingData);
  708. }
  709. private function searchDrivingData( $gpsNumber, $date ){
  710. if(!$gpsNumber){
  711. return false;
  712. }
  713. $start = microtime(true);
  714. $result = $this->getBTGpsRoute( $date,$gpsNumber);
  715. $end = microtime(true);
  716. $totalTime = 0;
  717. $totalDist = 0;
  718. $lastOnlineTime =end($result)['OnlineTime'];
  719. for($i = 0; $i < count($result)-1; ++$i){
  720. $lat1 = $result[$i]['lat'];
  721. $lng1 = $result[$i]['lng'];
  722. $lat2 = $result[$i+1]['lat'];
  723. $lng2 = $result[$i+1]['lng'];
  724. $dist = $this->get_2points_distance($lat1,$lng1,$lat2,$lng2 );
  725. $totalDist += $dist;
  726. $time = strtotime($result[$i+1]['OnlineTime']) - strtotime($result[$i]['OnlineTime']);
  727. $totalTime += ($time/3600);
  728. }
  729. return array(
  730. 'totalTime' => $totalTime,
  731. 'totalDist' => $totalDist,
  732. // 'averageSpeed' => $totalDist/$totalTime,
  733. 'lastOnlineTime' => $lastOnlineTime,
  734. 'caculateTime' => $end - $start
  735. );
  736. }
  737. private function get_2points_distance( $lat1, $lng1, $lat2, $lng2, $radius = 6378.137 ){
  738. $rad = floatval(M_PI / 180.0);
  739. $lat1 = floatval($lat1) * $rad;
  740. $lng1 = floatval($lng1) * $rad;
  741. $lat2 = floatval($lat2) * $rad;
  742. $lng2 = floatval($lng2) * $rad;
  743. $lat1 = sprintf("%.8f", $lat1);
  744. $lng1 = sprintf("%.8f", $lng1);
  745. $lat2 = sprintf("%.8f", $lat2);
  746. $lng2 = sprintf("%.8f", $lng2);
  747. $theta = $lng2 - $lng1;
  748. $dist = acos(sin($lat1) * sin($lat2) +
  749. cos($lat1) * cos($lat2) * cos($theta)
  750. );
  751. if ($dist < 0 ) {
  752. $dist += M_PI;
  753. }
  754. $dist = $dist * $radius;
  755. return sprintf('%.8f', $dist);
  756. }
  757. public function test_function( ){
  758. //距离测试
  759. /*
  760. $lat1 = '31.253411';
  761. $lng1 = '121.518998';
  762. $lat2 = '31.277117';
  763. $lng2 = '120.744587';
  764. $distance = $this->get_2points_distance($lat1, $lng1, $lat2, $lng2);
  765. var_dump($distance);
  766. */
  767. //离线时间测试
  768. $interval = 343088;
  769. $time = $this->getFormatTime($interval);
  770. echo $time;
  771. }
  772. public function get_vehicle_ElectronicPlateUrl( ){
  773. $this->token_verify();
  774. $userid = I('userid');
  775. $electronicPlateUrl = S('electronicPlateUrl_'.$userid);
  776. if(!empty($electronicPlateUrl['FrontElectronicPlateUrl']) && !empty($electronicPlateUrl['BackElectronicPlateUrl'] )){
  777. $this->api_success('成功',$electronicPlateUrl);
  778. }
  779. else{
  780. $vehicle_info = M('jms_vehicle')->where(array('ID'=>$userid))->field('FrontElectronicPlateUrl,BackElectronicPlateUrl')->find();
  781. S('electronicPlateUrl_'.$userid,$vehicle_info,86400);
  782. $this->api_success('成功',$vehicle_info);
  783. }
  784. }
  785. public function getCityInfo( ){
  786. $this->token_verify();
  787. $cityid = I('get.cityid');
  788. if(!$cityid){
  789. $this->api_fail(C('FAIL'),'开户城市不存在');
  790. }
  791. $parentcity = M('jms_city')->where(array('ID'=>$cityid))->find();
  792. $child_1 = M('jms_city')->where(array('ParentId'=>$parentcity['ParentId']))->select();
  793. $this->api_success('成功',$child_1);
  794. }
  795. public function get_cityinfo_by_cityid( ){
  796. $cityid = I('cityid');
  797. $ids = M('jms_city')->field('ParentId,ProvinceId')->where(array('ID'=>$cityid))->find();
  798. $result = $ids['ProvinceId'].','.($ids['ParentId']-100).','.($cityid-1000);
  799. $this->api_success('成功',$result);
  800. }
  801. private function getFormatTime( $timeInterval ){
  802. if ($timeInterval < 60){
  803. $formatime = $timeInterval.'秒';
  804. }elseif($timeInterval < 60 * 60){
  805. $min = floor($timeInterval/60);
  806. $formatime = $min.'分钟';
  807. }elseif($timeInterval < 60 * 60 * 24){
  808. $h = floor($timeInterval/(60*60));
  809. $formatime = $h.'小时';
  810. }else{
  811. $d = floor($timeInterval/(60*60*24));
  812. $formatime = $d.'天';
  813. }
  814. return $formatime;
  815. }
  816. public function get_insurance_detail( ){
  817. $this->token_verify();
  818. $data = I('post.');
  819. $plate = $data['plate'];
  820. $vehicleId = M('jms_vehicle')->where(array('LicensePlate'=>$plate))->field('ID')->find();
  821. $insureInfo = M('jms_insure')->where(array('VehicleId'=>$vehicleId['ID']))->field('InsuranceId,StartDate,EndDate')->find();
  822. if($insureInfo){
  823. $insuranceInfo = M('jms_insurance')->where(array('ID'=>$insureInfo['InsuranceId']))->field('InsuranceName')->find();
  824. $insuranceDetailInfo = M('jms_insurance_detail')->where(array('VehicleId'=>$vehicleId['ID']))->select();
  825. $result['insuranceDetailInfo'] = $insuranceDetailInfo;
  826. $result['insuranceName'] = $insuranceInfo['InsuranceName'];
  827. $result['insureInfo'] = $insureInfo;
  828. $this->api_success('成功',$result);
  829. }
  830. else{
  831. $this->api_success('无保险',null);
  832. }
  833. }
  834. }