SmartWxApiAction.class.php 131 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690
  1. <?php
  2. class SmartWxApiAction extends Action {
  3. public function getAudioListByImei( ){
  4. header('Access-Control-Allow-Origin: *');
  5. //json_fail('登录失败');
  6. $userid= I('get.userid');
  7. $imei = I('get.imei');
  8. $limit = 10;
  9. $voiceCond = [];
  10. $voiceCond['card_no'] = $imei;
  11. $beforeid = I('get.beforeid');
  12. $afterid = I('get.afterid');
  13. if($beforeid){
  14. $voiceCond['id'] = ['lt', $beforeid];
  15. }
  16. if($afterid){
  17. $voiceCond['id'] = ['gt', $afterid];
  18. }
  19. $audioList = M('kq_voice')->where($voiceCond)->limit($limit)->order('id desc')->select();
  20. if(!$audioList){
  21. json_fail('没有语音了');
  22. }
  23. $audioList = array_reverse($audioList);
  24. foreach($audioList as &$v){
  25. $v['created_at'] = date('Y-m-d H:i:s', $v['created_at']);
  26. }
  27. json_success('获取成功',$audioList);
  28. }
  29. public function getChatDevices( ){
  30. header('Access-Control-Allow-Origin: *');
  31. //json_fail('登录失败');
  32. $userid= I('get.userid');
  33. $hasDevices = M('devices')->where(['user_id' => $userid])->getField('imei, device_name');
  34. if(!$hasDevices){
  35. json_fail('名下查询不到设备');
  36. }
  37. $imeis = array_keys($hasDevices);
  38. $cond = [
  39. 'card_no' => ['in', $imeis],
  40. 'type' => 1
  41. ];
  42. $chatList = M('kq_voice')->field('card_no as imei, MAX(`created_at`) as lastTime, sum(case when hasread = 0 then 1 else 0 end) AS unreadCount')->where($cond)->group('card_no')->select();
  43. if(!$chatList){
  44. $chatList = [];
  45. foreach($hasDevices as $k => $v){
  46. $avatar = M('devices')->where(['imei' => $k])->getField('avatar');
  47. $arr = [
  48. 'imei' => $k,
  49. 'lastTime' => '暂无互动哦!',
  50. 'unreadCount' => 0,
  51. 'device_name' => $v,
  52. 'avatar' => $avatar ? $avatar : ''
  53. ];
  54. array_push($chatList, $arr);
  55. }
  56. }else{
  57. foreach($chatList as &$v){
  58. $avatar = M('devices')->where(['imei' => $v['imei']])->getField('avatar');
  59. $v['awayTime'] = $this->getHumenTime( time() - (int)$v['lastTime'] );
  60. $v['dateTime'] = date('Y-m-d H:i:s', $v['lastTime']);
  61. $v['device_name'] = $hasDevices[$v['imei']];
  62. $v['avatar'] = $avatar ? $avatar : '';
  63. }
  64. }
  65. json_success('消息获取成功', $chatList);
  66. }
  67. private function getHumenTime( $timeInterval ){
  68. if ($timeInterval < 60){
  69. $formatime = $timeInterval.'秒';
  70. }elseif($timeInterval < 60 * 60){
  71. $min = floor($timeInterval/60);
  72. $formatime = $min.'分钟';
  73. }elseif($timeInterval < 60 * 60 * 24){
  74. $h = floor($timeInterval/(60*60));
  75. $formatime = $h.'小时';
  76. }elseif($timeInterval < 60 * 60 * 24 * 30){
  77. $d = floor($timeInterval/(60*60*24));
  78. $formatime = $d.'天';
  79. }elseif($timeInterval < 60 * 60 * 24 * 30 * 12){
  80. $m = floor($timeInterval/(60*60*24*30));
  81. $formatime = $m.'月';
  82. }else{
  83. $y = floor($timeInterval/(60*60*24*30*12));
  84. $formatime = $y.'年';
  85. }
  86. return $formatime;
  87. }
  88. public function saveParentUploadAudio( ){
  89. header('Access-Control-Allow-Origin: *');
  90. $imei = $_POST['imei'];
  91. $file = $_FILES['upfile'];
  92. $userid = $_POST['userid'];
  93. if(!$imei){
  94. json_fail('获取不到设备标识,请返回上一页重进');
  95. }
  96. if(!$userid){
  97. json_fail('获取不到Y用户标识');
  98. }
  99. $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
  100. if($ext != 'wav'){
  101. json_fail('不是音频文件');
  102. }
  103. //文件保存目录
  104. $folder = realpath(__ROOT__).'/static/assets/wav/'.date('Ymd').'/';
  105. if (!is_dir($folder)){
  106. mkdir($folder,0777,true);
  107. }
  108. $uploadfile = $folder . basename($file['name']);
  109. if (move_uploaded_file($file['tmp_name'], $uploadfile)) {
  110. //1.获取语音时长
  111. $time = $this->getVoiceTime($uploadfile);
  112. //2.语音大小
  113. $size = $file['size'];
  114. //3.语音url
  115. $url = 'http://'.$_SERVER['HTTP_HOST'].'/static/assets/wav/'.date('Ymd').'/'.basename($file['name']);
  116. //4.设备code
  117. $data = array(
  118. 'card_no'=>$imei,
  119. 'file'=>$url,
  120. 'size'=>$size,
  121. 'time'=>$time,
  122. 'user_id' => $userid,
  123. 'type' => 2,
  124. 'result'=> 0,
  125. 'created_at'=>time()
  126. );
  127. //保存数据
  128. $id=M('kq_voice')->createAdd($data);
  129. if($id){
  130. sendVoiceIdToCtwing($imei,$id);
  131. json_success('保存成功');
  132. }else{
  133. json_fail('保存失败');
  134. }
  135. } else {
  136. json_fail('发送文件失败');
  137. }
  138. json_success('success');
  139. }
  140. private function getVoiceTime( $file, $base ){
  141. $fp = fopen($file, 'r');
  142. if (fread($fp, 4) != 'RIFF') {
  143. return '';
  144. }
  145. fseek($fp, 20);
  146. $rawheader = fread($fp, 16);
  147. $header = unpack('vtype/vchannels/Vsamplerate/Vbytespersec/valignment/vbits', $rawheader);
  148. $pos = ftell($fp);
  149. while (fread($fp, 4) != 'data' && !feof($fp)) {
  150. $pos++;
  151. fseek($fp, $pos);
  152. }
  153. $rawheader = fread($fp, 4);
  154. $data = unpack('Vdatasize', $rawheader);
  155. $sec = $data[datasize] / $header[bytespersec];
  156. if ($base == 60) {
  157. $seconds = intval($sec % 60);
  158. $minutes = intval(($sec - $sec * 60) / 60);
  159. return $minutes . ':' . $seconds;
  160. } else {
  161. return round($sec);
  162. }
  163. }
  164. public function updateAudioStatus( ){
  165. header('Access-Control-Allow-Origin: *');
  166. $id = I('get.vid');
  167. if(!$id){
  168. json_fail('获取不到语音消息标识');
  169. }
  170. $res = M('kq_voice')->where(['id' => $id])->save(['hasread' => 1]);
  171. if($res === false){
  172. json_fail('已读状态修改失败');
  173. }
  174. json_success('已读状态修改成功');
  175. }
  176. public function login_test( ){
  177. header('Access-Control-Allow-Origin: *');
  178. //json_fail('登录失败');
  179. $openid = I('get.openid');
  180. /*
  181. if(!$openid){
  182. json_fail('用户未授权');
  183. }
  184. */
  185. $data = json_decode( file_get_contents("php://input") ,true);
  186. $cond = array(
  187. 'username'=>$data['mobile'],
  188. );
  189. $user_info = M('users')->where($cond)->find();
  190. if(!$user_info){
  191. json_fail('用户不存在');
  192. }
  193. //if($user_info['password'] != password_hash($data['password'],PASSWORD_DEFAULT) ){
  194. // json_fail('密码不正确1');
  195. //}
  196. if(!password_verify($data['password'], $user_info['password'])){
  197. json_fail('密码不正确');
  198. }
  199. if($user_info['wx_open_id'] != $openid){
  200. $update = array(
  201. 'wx_open_id'=>$openid
  202. );
  203. $result = M('users')->createSave($cond,$update);
  204. if(!$result){
  205. json_fail('登陆失败');
  206. }
  207. }
  208. //如果当前openid与其他账号openid相同,清空其他表中openid
  209. $where = array(
  210. 'wx_open_id' => $openid,
  211. 'id' => ['NEQ', $user_info['id']],
  212. );
  213. $result = M('users')->createSave($where,['wx_open_id'=>'']);
  214. if(!$result){
  215. json_fail('登陆失败');
  216. }
  217. /*
  218. $oid_info = M('users')->where($where)->find();
  219. if($oid_info['id'] != $user_info['id']){
  220. $result = M('users')->createSave($where,['wx_open_id'=>'']);
  221. if(!$result){
  222. json_fail('登陆失败');
  223. }
  224. }*/
  225. json_success('登录成功', $user_info['id']);
  226. }
  227. public function register( ){
  228. header('Access-Control-Allow-Origin: *');
  229. // 通过手机号和短信验证码注册
  230. $data = json_decode( file_get_contents("php://input") ,true);
  231. // 用户类型:个人(personal)、团体(group)
  232. $userType = $data['userType'];
  233. // 设备类型:卡牌(card)、徽章(badge)
  234. $deviceType = $data['deviceType'];
  235. // 团体卡牌用户(group_card_user) 不可注册
  236. // 个人用户(personal),团体徽章用户(group_badge_user) 可注册
  237. if ($userType == 'personal') { // 个人用户
  238. $identify = 'personal';
  239. } elseif ($userType == 'group' && $deviceType == 'badge') { // 团体徽章用户
  240. $identify = 'group_badge_user';
  241. } elseif ($userType == 'group' && $deviceType == 'card') { // 团体卡牌用户
  242. // $identify = 'group_card_user';
  243. json_fail('团体卡牌用户无需注册,请联系发卡人询问账号!');
  244. } else {
  245. json_fail('未知用户类型');
  246. }
  247. if(!$data['phone']){
  248. json_fail('手机号码不能为空');
  249. }
  250. if(!$data['smsCode']){
  251. json_fail('短信验证码不能为空');
  252. }
  253. if(! $data['password']){
  254. json_fail('密码不能为空');
  255. }
  256. if(! $data['confirmPassword']){
  257. json_fail('请输入确认密码');
  258. }
  259. if ($data['password'] != $data['confirmPassword']) {
  260. json_fail('两次密码不一致');
  261. }
  262. /*
  263. $cond = array(
  264. 'username'=>$data['phone']
  265. );
  266. $user_info = M('users')->where($cond)->join('user_has_roles ON users.id = user_has_roles.uid')->find();
  267. if($user_info){
  268. //存在用户则判断
  269. $role_identify=M('roles')->where(array('id'=>$user_info['role_id']))->getField('identify');
  270. if($role_identify==$identify){
  271. json_fail('该类型用户已经存在,请前往登陆');
  272. }
  273. }*/
  274. // 获取角色id
  275. $roleId = M('roles')->where(['identify' => $identify])->getField('id');
  276. if (!$roleId) {
  277. json_fail('未知角色类型');
  278. }
  279. // 判断角色、号码是否已注册
  280. $userinfo = M('users')->alias('a')->where(['username'=>$data['phone']])->join("INNER JOIN user_has_roles b ON a.id = b.uid AND b.role_id = {$roleId}")->find();
  281. if ($userinfo) {
  282. json_fail('该号码已注册');
  283. }
  284. // 验证码有效性
  285. $res = $this->isValidSmsCode($data['phone'], $data['smsCode']);
  286. if (!$res['success']) {
  287. json_fail($res['message']);
  288. }
  289. //if($user_info['password'] != password_hash($data['password'],PASSWORD_DEFAULT) ){
  290. $savePwd = password_hash($data['password'],PASSWORD_DEFAULT);
  291. $saveData = [
  292. 'username' => $data['phone'],
  293. 'realname' => '用户'.$data['phone'],
  294. 'password' => $savePwd,
  295. 'phone' => $data['phone'],
  296. 'created_at' => time(),
  297. ];
  298. // 个人用户给定默认部门
  299. if ($identify == 'personal') {
  300. $where = [
  301. 'type' => 'basic_config',
  302. 'field' => 'department_id',
  303. ];
  304. $department_id = M('sys_config')->where($where)->getField('fieldValue');
  305. if ($department_id) {
  306. $saveData['department_id'] = $department_id;
  307. }
  308. }
  309. M()->startTrans();
  310. $userid = M('users')->add($saveData);
  311. if(!$userid){
  312. M()->rollback();
  313. json_fail('注册失败');
  314. }
  315. $hasRoleData = [
  316. 'uid' => $userid,
  317. 'role_id' => $roleId
  318. ];
  319. $userHasRoleRes = M('user_has_roles')->add($hasRoleData);
  320. if(!$userHasRoleRes){
  321. M()->rollback();
  322. json_fail('注册失败');
  323. }
  324. M()->commit();
  325. json_success('注册成功',$userid);
  326. }
  327. public function login( ){
  328. header('Access-Control-Allow-Origin: *');
  329. //json_fail('登录失败');
  330. $openid = I('get.openid');
  331. if(!$openid){
  332. json_fail('未授权,请关闭当前页面重新进入');
  333. }
  334. $data = json_decode( file_get_contents("php://input") ,true);
  335. // 用户类型:个人(personal)、团体(group)
  336. $userType = $data['userType'];
  337. // 设备类型:卡牌(card)、徽章(badge)
  338. $deviceType = $data['deviceType'];
  339. if ($userType == 'personal') { // 个人用户
  340. $identify = 'personal';
  341. } elseif ($userType == 'group' && $deviceType == 'badge') { // 团体徽章用户
  342. $identify = 'group_badge_user';
  343. } elseif ($userType == 'group' && $deviceType == 'card') { // 团体卡牌用户
  344. $identify = 'group_card_user';
  345. } else {
  346. json_fail('未知用户类型');
  347. }
  348. $role_id = M('roles')->where(array('identify'=>$identify))->getField('id');
  349. if (!$role_id) {
  350. json_fail('未知用户类型2');
  351. }
  352. $cond = array(
  353. 'username' => $data['username'],
  354. );
  355. $user_info = M('users')->alias('u')->where($cond)->join("INNER JOIN user_has_roles AS r ON u.id = r.uid AND r.role_id = {$role_id}")->find();
  356. if(!$user_info){
  357. $identifyArrs=['personal','group_badge_user','group_card_user'];
  358. $other_user=[];
  359. foreach($identifyArrs as $item){
  360. if($item == $identify){
  361. continue;
  362. }
  363. $other_role = M('roles')->where(array('identify'=>$item))->find();
  364. if (!$other_role) {
  365. continue;
  366. }
  367. $other_user_info = M('users')->alias('u')->where($cond)->join("INNER JOIN user_has_roles AS r ON u.id = r.uid AND r.role_id = {$other_role['id']}")->count();
  368. if($other_user_info){
  369. $other_user[]=$other_role['role_name'];
  370. }else{
  371. continue;
  372. }
  373. }
  374. $other_count=count($other_user);
  375. if($other_count==2){
  376. json_fail('账号类型错误');
  377. }elseif($other_count==1){
  378. json_fail('账号类型错误,请选择'.$other_user[0]);
  379. }else{
  380. json_fail('账号不存在');
  381. }
  382. json_fail('账号错误');
  383. }
  384. if(!password_verify($data['password'], $user_info['password'])){
  385. json_fail('密码错误');
  386. }
  387. $where = array(
  388. 'wx_open_id' => $openid,
  389. 'id' => ['NEQ', $user_info['uid']],
  390. );
  391. //$result = M('users')->createSave($where,['wx_open_id'=>'']);
  392. $result = M('users')->where($where)->save(['wx_open_id'=>'']);
  393. if($result===false){
  394. json_fail('登陆失败');
  395. }
  396. if($user_info['wx_open_id'] != $openid){
  397. $update = array(
  398. 'wx_open_id' => $openid,
  399. 'last_login_ip' => $_SERVER['REMOTE_ADDR'],
  400. 'last_login_time' => time(),
  401. );
  402. $result = M('users')->createSave(array('id'=>$user_info['uid']),$update);
  403. if(!$result){
  404. json_fail('登陆失败');
  405. }
  406. }
  407. //如果当前openid与其他账号openid相同,清空其他表中openid
  408. //查询用户未同步设备
  409. $imei_list=M('devices')->where(array('user_id'=>$user_info['uid']))->getField('imei',true);
  410. if($imei_list){
  411. foreach($imei_list as $val){
  412. $result=$this->addToVoiceCenter($val);
  413. if(!$result['success']){
  414. create_log($val.'同步失败 message: '.$result['message'], 'AddVoiceCenter');
  415. json_fail('登陆失败,网络异常');
  416. }
  417. }
  418. }
  419. $res = array(
  420. 'id'=>$user_info['uid'],
  421. 'realname'=>$user_info['realname'],
  422. 'avatar' => $user_info['avatar'],
  423. 'phone'=>$user_info['phone'],
  424. );
  425. json_success('登录成功', $res);
  426. }
  427. public function getLastPositionMulti( ){
  428. header('Access-Control-Allow-Origin: *');
  429. $userid = I('get.userid');
  430. if(!$userid){
  431. json_fail('未检测到用户标识');
  432. }
  433. $devices = M('devices')->where(['user_id' => $userid])->select();
  434. if(!$devices){
  435. json_fail('名下查询不到任何设备');
  436. }
  437. $resp = [];
  438. foreach($devices as &$locationInfo){
  439. //new start
  440. $isThirdParty = M('third_party_devices')->where(['device_imei' => $locationInfo['imei']])->count();
  441. if($isThirdParty){
  442. $thindLoc = $this->getThirdLocation($locationInfo['imei']);
  443. if($thindLoc){
  444. $locationInfo['lng'] = $thindLoc['lon'];
  445. $locationInfo['lat'] = $thindLoc['lat'];
  446. $locationInfo['time'] = $thindLoc['receiveAt'];
  447. }
  448. }
  449. //new end
  450. //wifi_online_time
  451. //wifi_longitude
  452. //wifi_latitude
  453. //addr
  454. $locationInfo['isAlarm'] = $locationInfo['alarm_status'];
  455. $isWifi = $locationInfo['wifi_online_time'] > $locationInfo['online_time'] || intval($locationInfo['latitude']) < 1 ;
  456. $locationInfo['time'] = $isWifi ? $locationInfo['wifi_online_time'] : $locationInfo['online_time'];
  457. $locationInfo['lat'] = $isWifi ? $locationInfo['wifi_latitude'] : $locationInfo['latitude'];
  458. $locationInfo['lng'] = $isWifi ? $locationInfo['wifi_longitude'] : $locationInfo['longitude'];
  459. if(!$isThirdParty){
  460. $locationInfo['address'] = $locationInfo['address'];
  461. }
  462. //
  463. if(!$locationInfo['time']){
  464. $locationInfo['awayTime'] = '从未在线';
  465. }else{
  466. $timeInterval = time() - (int)$locationInfo['time'];
  467. $locationInfo['awayTime'] = $this->getHumenTime($timeInterval);
  468. }
  469. $lngLatAlter = new \Jms\Algo\Geometry();
  470. if(!$locationInfo['lat'] || !$locationInfo['lng']){
  471. continue;
  472. }
  473. if($isThirdParty){
  474. $latLng = $lngLatAlter->wgsTOgcj($locationInfo['lat'], $locationInfo['lng']);
  475. $locationInfo['lat'] = $latLng['lat'];
  476. $locationInfo['lng'] = $latLng['lng'];
  477. }else{
  478. $latLng = $lngLatAlter->convertBd09ToGcj02($locationInfo['lat'], $locationInfo['lng']);
  479. $locationInfo['lat'] = $latLng['lat'];
  480. $locationInfo['lng'] = $latLng['lng'];
  481. }
  482. $locationInfo['date_time'] = date('Y-m-d H:i:s', $locationInfo['time']);
  483. array_push($resp, $locationInfo);
  484. }
  485. if(!$resp){
  486. json_fail('暂无任何设备上报过数据哦!');
  487. }
  488. json_success('查询成功', $resp);
  489. }
  490. private function checkLoginState( $openid, $userid ){
  491. $wx_open_id=M('users')->where(array('id'=>$userid))->getField('wx_open_id');
  492. if($wx_open_id === 'test_openid'){
  493. return array('status'=>true,'message'=>'');
  494. }
  495. if($wx_open_id!=$openid){
  496. //已被他人登录
  497. return array('status'=>false,'message'=>'账号可能在其他设备登录,请重新登录');
  498. }
  499. return array('status'=>true,'message'=>'');
  500. }
  501. public function getMyDevices( ){
  502. header('Access-Control-Allow-Origin: *');
  503. $openid = I('get.openid');
  504. $userid = I('get.userid');
  505. //检测登录状态
  506. $res=$this->checkLoginState($openid,$userid);
  507. if(!$res['status']){
  508. json_fail($res['message']);
  509. }
  510. $list=M('devices')->where(array('user_id'=>$userid))->select();
  511. $type_id=M('sys_dict_type')->where(['code'=>'DeviceType'])->getField('id');
  512. $rela_type_id=M('sys_dict_type')->where(['code'=>'Relationships'])->getField('id');
  513. foreach($list as &$val){
  514. $val['badge_user_info']=M("badgeuser")->where(['id'=>$val['badge_user_id']])->find();
  515. $val['device_type']=M('sys_dict_data')->where(['type_id'=>$type_id,'code'=>$val['device_type']])->getField('value');
  516. $val['department_name']=M('departments')->where(['id'=>$val['department_id']])->getField('department_name');
  517. if(!$val['device_name'] || $val['device_name']==null){
  518. $val['device_name']='设备'.substr($val['imei'],-4);
  519. }
  520. $cond = ['device_id'=>$val['id']];
  521. $res = M('kq_urgent')->where($cond)->select();
  522. if($res){
  523. foreach($res as &$item){
  524. $item['relationship']=M('sys_dict_data')->where(['type_id'=>$rela_type_id,'code'=>$item['relationship']])->getField('value');
  525. }
  526. $val['urgent_list']=$res;
  527. }
  528. }
  529. if(!$list){
  530. json_fail('暂无绑定设备');
  531. }
  532. json_success('查询成功',$list);
  533. }
  534. public function getNews( ){
  535. header('Access-Control-Allow-Origin: *');
  536. $openid = I('get.openid');
  537. $userid = I('get.userid');
  538. if(!$userid || $userid == 'undefined' || $userid == 'null'){
  539. json_fail('获取不到你的用户标识,请重新登陆');
  540. }
  541. //检测登录状态
  542. $res=$this->checkLoginState($openid,$userid);
  543. if(!$res['status']){
  544. json_fail($res['message']);
  545. }
  546. $where = ['id' => $userid];
  547. $userinfo = M('users')->where($where)->field('department_id')->find();
  548. if (empty($userinfo)) {
  549. json_fail('未获取到用户信息');
  550. }
  551. $level_str = M('departments')->where(array('id'=>$userinfo['department_id']))->getField('level');
  552. if($level_str){
  553. $levelArr = explode('-', $level_str);
  554. }else{
  555. $levelArr =[];
  556. }
  557. array_push($levelArr,$userinfo['department_id']);
  558. $page = I('get.page') ? I('get.page') : 1;
  559. $pageSize = 10;
  560. $newsModel = M('news');
  561. $start = ($page - 1) * $pageSize;
  562. $cond = [
  563. 'status' => 1,
  564. 'department_id' => array('in',$levelArr)
  565. ];
  566. $list = $newsModel->where($cond)->limit( $start, $pageSize)->order('created_at desc')->select();
  567. //$list = $newsModel->limit( $start, 10)->select();
  568. $total = $newsModel->where($cond)->count() ?: 0;
  569. foreach($list as &$v){
  570. $v['updated_at_datetime'] = date('Y-m-d H:i:s',$v['updated_at']);
  571. $v['creator'] = M('users')->where(['id' => $v['creator_id'] ])->getField('username') ? : 'admin';
  572. }
  573. if (!$list) {
  574. $list = [];
  575. }
  576. $resp = [
  577. 'list' => $list,
  578. 'total' => $total,
  579. 'pageSize' => $pageSize
  580. ];
  581. json_success('获取成功', $resp);
  582. }
  583. public function getUrgentList( ){
  584. header('Access-Control-Allow-Origin: *');
  585. $userid = I('get.userid');
  586. if(!$userid || $userid == 'undefined' || $userid == 'null'){
  587. json_fail('获取不到你的用户标识,请重新登陆');
  588. }
  589. $openid = I('get.openid');
  590. $device_id = I('get.device_id');
  591. //检测登录状态
  592. $res=$this->checkLoginState($openid,$userid);
  593. if(!$res['status']){
  594. json_fail($res['message']);
  595. }
  596. if($device_id){
  597. $cond=array('id'=>$device_id);
  598. }else{
  599. $cond=array('user_id'=>$userid);
  600. }
  601. $device_list=M('devices')->where($cond)->field('id,imei,rfid,device_name,device_type')->select();
  602. if(!$device_list){
  603. json_fail('暂无绑定设备');
  604. }
  605. $type_id=M('sys_dict_type')->where(['code'=>'Relationships'])->getField('id');
  606. foreach($device_list as &$val){
  607. if ($val['device_name'] === '' || $val['device_name'] === null) {
  608. $name = substr($val['imei'], -4);
  609. $val['device_name'] = '设备'. $name;
  610. }
  611. $cond = ['device_id'=>$val['id']];
  612. $res = M('kq_urgent')->where($cond)->select();
  613. if($res){
  614. foreach($res as &$item){
  615. $item['relationship']=M('sys_dict_data')->where(['type_id'=>$type_id,'code'=>$item['relationship']])->getField('value');
  616. }
  617. $val['urgent_list']=$res;
  618. }
  619. }
  620. json_success('获取成功', $device_list);
  621. }
  622. public function getAlarmList( ){
  623. header('Access-Control-Allow-Origin: *');
  624. $userid = I('get.userid');
  625. if(!$userid || $userid == 'undefined' || $userid == 'null'){
  626. json_fail('获取不到你的用户标识,请重新登陆');
  627. }
  628. $openid = I('get.openid');
  629. //检测登录状态
  630. $res=$this->checkLoginState($openid,$userid);
  631. if(!$res['status']){
  632. json_fail($res['message']);
  633. }
  634. $imeiArr=M('devices')->where(['user_id' => $userid])->getField('imei',true);
  635. $page = I('get.page') ? I('get.page') : 1;
  636. $pageSize = 10;
  637. $start = ($page - 1) * $pageSize;
  638. $cond = [];
  639. $alarmType = I('get.alarmType');
  640. $imei = I('get.imei');
  641. if(!$imei){
  642. $cond['device_number'] = array('in',$imeiArr);
  643. }else{
  644. $cond['device_number'] = $imei;
  645. }
  646. if($alarmType){
  647. $cond['alarm_type'] = $alarmType;
  648. }
  649. $list = M('alarm_report')->where($cond)->limit( $start, $pageSize)->order('created_at desc')->select();
  650. $type_id=M('sys_dict_type')->where(array('code'=>'AlarmType'))->getField('id');
  651. foreach($list as &$val){
  652. $val['alarm_type_str']=M('sys_dict_data')->where(array('type_id'=>$type_id,'code'=>$val['alarm_type']))->getField('value');
  653. $val['created_at_str'] = date('Y-m-d H:i:s', $val['created_at']);
  654. }
  655. $total = M('alarm_report')->where($cond)->count() ?: 0;
  656. $resp = [
  657. 'list' => $list ?:[],
  658. 'total' => $total,
  659. 'pageSize' => $pageSize
  660. ];
  661. json_success('获取成功', $resp);
  662. }
  663. public function getSignaturEtc( ){
  664. header('Access-Control-Allow-Origin: *');
  665. $url = I('get.url');
  666. if(!$url){
  667. json_fail('no url');
  668. }
  669. $url = urldecode($url);
  670. $accessToken = $this->getWeixinAccessToken();
  671. if (!$accessToken) {
  672. json_fail('获取 access token 失败');
  673. }
  674. $apiTiket = $this->getJsapiTikcet($accessToken);
  675. if(is_array($apiTiket)){
  676. json_fail($apiTiket['errcode'].':'.$apiTiket['errmsg']);
  677. }
  678. $noncestr = substr(md5(time()), 0, 16);
  679. $timestamp = time();
  680. //jsapi_ticket=sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg&noncestr=Wm3WZYTPz0wzccnW&timestamp=1414587457&url=http://mp.weixin.qq.com?params=value
  681. $str = 'jsapi_ticket='.$apiTiket.'&noncestr='.$noncestr.'&timestamp='.$timestamp.'&url='.$url;
  682. $signature = sha1($str);
  683. $data = [
  684. 'appId' => C('WECHAT_APPID'),
  685. 'timeStamp' => $timestamp,
  686. 'nonceStr' => $noncestr,
  687. 'signature' => $signature,
  688. 'url' => $url,
  689. 'jsapi_ticket' =>S('wx_jsapi_ticket'),
  690. 'accessToken' => $accessToken,
  691. ];
  692. json_success('get success', $data);
  693. }
  694. private function getJsapiTikcet( $accessToken ){
  695. $jsapiTicket = S('wx_jsapi_ticket');
  696. if(!$jsapiTicket){
  697. $ch = curl_init();
  698. curl_setopt($ch, CURLOPT_URL, 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$accessToken .'&type=jsapi');
  699. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  700. $output = json_decode(curl_exec($ch), true);
  701. curl_close($ch);
  702. if($output['errcode'] != 0){
  703. return $output;
  704. }
  705. S('wx_jsapi_ticket', $output['ticket'], '7100');
  706. return $output['ticket'];
  707. }else{
  708. return $jsapiTicket;
  709. }
  710. }
  711. public function getActivity( ){
  712. header('Access-Control-Allow-Origin: *');
  713. $userid = I('get.userid');
  714. if(!$userid || $userid == 'undefined' || $userid == 'null'){
  715. json_fail('获取不到你的用户标识,请重新登陆');
  716. }
  717. $openid = I('get.openid');
  718. //检测登录状态
  719. $res=$this->checkLoginState($openid,$userid);
  720. if(!$res['status']){
  721. json_fail($res['message']);
  722. }
  723. // 查出学生所在部门
  724. $where = ['id' => $userid];
  725. $userinfo = M('users')->where($where)->field('department_id')->find();
  726. if (empty($userinfo)) {
  727. json_fail('未获取到用户信息');
  728. }
  729. $page = I('get.page') ? I('get.page') : 1;
  730. $pageSize = 10;
  731. $actModel = M('activity');
  732. $start = ($page - 1) * $pageSize;
  733. $cond = [
  734. 'status' => 1,
  735. 'department_id' => $userinfo['department_id']
  736. ];
  737. $list = $actModel->where($cond)->limit( $start, $pageSize)->select();
  738. $total = $actModel->where($cond)->count() ?: 0;
  739. foreach($list as &$v){
  740. $v['updated_at_datetime'] = date('Y-m-d H:i:s',$v['updated_at']);
  741. $v['start_time'] = date('Y-m-d H:i:s',$v['start_time']);
  742. $v['end_time'] = date('Y-m-d H:i:s',$v['end_time']);
  743. $v['creator'] = M('users')->where(['id' => $v['creator_id'] ])->getField('username') ? : 'admin';
  744. $applyCount = M('activity_has_users')->where(['activity_id' => $v['id']])->count();
  745. $v['apply_count'] = $applyCount ? $applyCount : 0;
  746. }
  747. if (!$list) {
  748. $list = [];
  749. }
  750. $resp = [
  751. 'list' => $list,
  752. 'total' => $total,
  753. 'pageSize' => $pageSize
  754. ];
  755. json_success('获取成功', $resp);
  756. }
  757. public function getGpsRoutes( ){
  758. header('Access-Control-Allow-Origin: *');
  759. $imei = I('get.imei');
  760. $startTime = I('get.startTime');
  761. $endTime = I('get.endTime');
  762. if(!$imei){
  763. json_fail('获取不到设备号');
  764. }
  765. if(!$startTime){
  766. json_fail('无起始时间');
  767. }
  768. if(!$endTime){
  769. json_fail('无结束时间');
  770. }
  771. //end_time
  772. //start_time
  773. //student_id
  774. //queyRouteListByStudentId
  775. $routeInfo = [];
  776. $lngLatAlter = new \Jms\Algo\Geometry();
  777. //new start
  778. $isThirdParty = M('third_party_devices')->where(['device_imei' => $imei])->count();
  779. if($isThirdParty){
  780. $imei = M('users')->where(['id' => $studentId])->getField('imei');
  781. $option = [
  782. 'startTime' => strtotime($startTime),
  783. 'endTime' => strtotime($endTime),
  784. 'pageSize' => 500
  785. ];
  786. $thirdRoutes = $this->getThirdRoutes($imei, $option);
  787. $routes = $thirdRoutes['content'];
  788. if(!$routes){
  789. json_success('success', $routeInfo);
  790. }
  791. foreach($routes as $item){
  792. $latLng = $latLng = $lngLatAlter->wgsTOgcj($item['lat'], $item['lon']);
  793. $arr =[
  794. 'OnlineTime' => $item['receiveAt'],
  795. 'lat' => $latLng['lat'],
  796. 'lng' => $latLng['lng']
  797. ];
  798. array_push($routeInfo, $arr);
  799. }
  800. }else{
  801. $routeInfo = queyGpsRouteListByimei($imei, $startTime, $endTime);
  802. //$routeInfo = analyzeRouteInfoGaodeBatch($routeInfo);
  803. $routeInfo = analyzeRouteInfoWayzBatch($routeInfo);
  804. $arr = [];
  805. foreach($routeInfo as &$v){
  806. $v['lat'] = $v['Latitude'];
  807. $v['lng'] = $v['Longitude'];
  808. if($v['lat']){
  809. array_push($arr, $v);
  810. }
  811. }
  812. $routeInfo =bmapFilterOutliers($arr);
  813. }
  814. json_success('success', $routeInfo);
  815. }
  816. private function analyzeRouteInfo( $list ){
  817. if(!$list){
  818. return array();
  819. }
  820. $arr= [];
  821. foreach($list as $k=> $v){
  822. if(isset($v['WifiMacs']) && $v['WifiMacs'] !=''){
  823. $wifiRes =$this->requestWifiLBS($v);
  824. if($wifiRes['success']){
  825. if($wifiRes['data']['lon'] == '' || is_null($wifiRes['data']['lat'])){
  826. continue;
  827. }
  828. $list[$k]['Latitude'] = $wifiRes['data']['lat'];
  829. $list[$k]['Longitude'] = $wifiRes['data']['lon'];
  830. $list[$k]['SignalType'] = 'WiFi';
  831. $list[$k]['WifiAddress'] = $wifiRes['data']['address'];
  832. array_push($arr, $list[$k]);
  833. }else{
  834. // array_splice($list,$k,1);
  835. continue;
  836. }
  837. }else{
  838. $list[$k]['SignalType'] = 'GPS';
  839. array_push($arr, $list[$k]);
  840. }
  841. }
  842. return $arr;
  843. }
  844. private function requestWifiLBS( $routeInfo ){
  845. if(!$routeInfo){
  846. return array(
  847. 'success' => false,
  848. 'msg' => '空数据'
  849. );
  850. }
  851. if(!isset( $routeInfo['WifiMacs']) ){
  852. return array(
  853. 'success' => false,
  854. 'msg' => '无wifi信息'
  855. );
  856. }
  857. $WifiMacs = $routeInfo['WifiMacs'];
  858. $userKey = '';
  859. if(!$userKey){
  860. $userKey = 'f107b0b3a513e6e37c6fb0424bed6633';
  861. }
  862. //$url = 'http://api.cellocation.com:81/loc/?wl=' . $WifiMacs .'&output=json&coord=gcj02';
  863. $url = 'http://apilocate.amap.com/position?accesstype=1&imei=' . $routeInfo['DeviceNumber'] . '&macs=' . $WifiMacs . '&key=' . $userKey;
  864. $curl = curl_init($url);
  865. curl_setopt($curl, CURLOPT_TIMEOUT, 3);
  866. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  867. $response = curl_exec($curl);
  868. if (curl_errno($curl)) {
  869. $errmsg = curl_error($curl);
  870. curl_close($curl);
  871. return array(
  872. 'success' => false,
  873. 'msg' => $errmsg
  874. );
  875. }
  876. curl_close($curl);
  877. $response = json_decode($response, true);
  878. if($response['info'] != 'OK' || count($response['result']) <2 ){
  879. return array(
  880. 'success' => false,
  881. 'msg' => $response['info']
  882. );
  883. }
  884. $result = $response['result'];
  885. $location = $response['result']['location'];
  886. $location = explode(',', $location);
  887. $response = array(
  888. 'lon' => $location[0],
  889. 'lat' => $location[1],
  890. 'address' => $result['desc']
  891. );
  892. return array(
  893. 'success' => true,
  894. 'data' => $response
  895. );
  896. }
  897. private function registerDevice2third( $deviceImei ){
  898. $tokenInfo = $this->getThirdTokenInfo();
  899. if(!$tokenInfo){
  900. return false;
  901. }
  902. $postData = [
  903. "deviceImei" => $deviceImei,
  904. "phone" => "",
  905. "userId" => $tokenInfo['holder']
  906. ];
  907. var_dump($postData);
  908. $url = "http://rinlink-a18.beijing-cn-k8s-test.rinlink.com/device";
  909. $headers[] = "Content-Type: application/json";
  910. $headers[] = "Authorization: ". $tokenInfo['token'];
  911. $ch = curl_init();
  912. curl_setopt($ch, CURLOPT_URL, $url);
  913. curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
  914. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  915. curl_setopt($ch, CURLOPT_POST, 1);
  916. curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($postData) );
  917. $output = curl_exec($ch);
  918. curl_close($ch);
  919. if($output === false){
  920. $this->push_log('thirdapi_register_device_curl_error',curl_error($curl));
  921. return false;
  922. }
  923. $deviceRes = json_decode($output, ture) ;
  924. if($deviceRes['error'] || $deviceRes['code']){
  925. $this->push_log('thirdapi_register_device_response_error',$deviceRes);
  926. return false;
  927. }
  928. $this->push_log('thirdapi_register_device_response_success',$deviceRes);
  929. return true;
  930. }
  931. private function getThirdRoutes( $imei, $option ){
  932. /*
  933. {
  934. "page": 0,
  935. "size": 6000,
  936. "total": 8,
  937. "content": [{
  938. "collectDt": "2021-07-07 10:33:39",
  939. "receiveAt": "2021-07-07 10:33:39",
  940. "lat": "40.02577751048506",
  941. "lon": "116.35762812669113",
  942. "battery": 91,
  943. "positionType": 2,
  944. "imei": "71041607133",
  945. "isOnline": null
  946. }]
  947. }
  948. */
  949. if (!$imei) {
  950. $this->push_log('thirdapi_devicelocation_curl_error', 'imei 为空');
  951. return false;
  952. }
  953. $tokenInfo = $this->getThirdTokenInfo();
  954. if(!$tokenInfo){
  955. return false;
  956. }
  957. $pageNo = $option['pageNo'] ? $option['pageNo'] : 0;
  958. $pageSize = $option['pageSize'] ?$option['pageSize']: 6000;
  959. $startTime = isset($option['startTime']) ? $option['startTime'] * 1000 : '';
  960. $endTime = isset($option['endTime']) ? $option['endTime'] * 1000 : '';
  961. $positionTypes = $option['positionTypes'] ? $option['positionTypes']: '';
  962. $headers[] = "Content-Type: application/json";
  963. $headers[] = "Authorization: ". $tokenInfo['token'];
  964. $url = 'http://rinlink-a18.beijing-cn-k8s-test.rinlink.com/devicedata/location/filter?imei='.$imei.'&pageNo='.$pageNo.'&pageSize='.$pageSize.'&startTime='.$startTime.'&endTime='.$endTime.'&positionTypes='.$positionTypes;
  965. //var_dump($url);
  966. $response = $this->http_get($url, $headers);
  967. if (!$response) {
  968. $this->push_log('thirdapi_devicelocation_curl_error', $response);
  969. return false;
  970. }
  971. return json_decode($response, true);
  972. }
  973. private function getThirdLocation( $imei ){
  974. if (!$imei) {
  975. $this->push_log('third_newlocation_curl_error', 'imei 为空');
  976. return false;
  977. }
  978. $tokenInfo = $this->getThirdTokenInfo();
  979. if(!$tokenInfo){
  980. return false;
  981. }
  982. $headers[] = "Content-Type: application/json";
  983. $headers[] = "Authorization: ". $tokenInfo['token'];
  984. $url = 'http://rinlink-a18.beijing-cn-k8s-test.rinlink.com/devicedata/location/'.$imei;
  985. $response = $this->http_get($url, $headers);
  986. if (!$response) {
  987. $this->push_log('third_lastLocation_error', $response);
  988. return false;
  989. }
  990. return json_decode($response, true);
  991. }
  992. private function http_get( $url, $headers ){
  993. $oCurl = curl_init();
  994. if(!$headers){
  995. $headers = [];
  996. }
  997. if(stripos($url,"https://")!==FALSE){
  998. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  999. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
  1000. }
  1001. curl_setopt($oCurl, CURLOPT_URL, $url);
  1002. curl_setopt($oCurl, CURLOPT_HTTPHEADER, $headers );
  1003. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
  1004. $sContent = curl_exec($oCurl);
  1005. $aStatus = curl_getinfo($oCurl);
  1006. if(intval($aStatus["http_code"])==200){
  1007. curl_close($oCurl);
  1008. return $sContent;
  1009. }else{
  1010. $this->push_log('thirdapi_token_curl_error',curl_error($oCurl));
  1011. curl_close($oCurl);
  1012. return false;
  1013. }
  1014. }
  1015. public function sendSmsCode( ){
  1016. header('Access-Control-Allow-Origin: *');
  1017. $postData = json_decode(file_get_contents('php://input'), true) ?:[];
  1018. if (!$postData['phone']) {
  1019. json_fail('手机号码不能为空');
  1020. }
  1021. if (!preg_match('/^1[3456789]\d{9}$/', $postData['phone'])) {
  1022. json_fail('手机号码格式不正确');
  1023. }
  1024. // 是否重置密码
  1025. $isReset = $postData['isReset'];
  1026. if ($postData['userType'] == 'personal') { // 个人用户
  1027. $identify = 'personal';
  1028. } elseif ($postData['userType'] == 'group' && $postData['deviceType'] == 'badge') { // 团体徽章用户
  1029. $identify = 'group_badge_user';
  1030. } elseif ($postData['userType'] == 'group' && $postData['deviceType'] == 'card') { // 团体卡牌用户
  1031. $identify = 'group_card_user';
  1032. } else {
  1033. json_fail('暂不支持的用户类型');
  1034. }
  1035. // 注册时检测
  1036. if (!$isReset && $identify == 'group_card_user') {
  1037. json_fail('暂不支持的注册用户类型');
  1038. }
  1039. // 获取角色id
  1040. $roleId = M('roles')->where(['identify' => $identify])->getField('id');
  1041. // 判断角色、号码是否已注册
  1042. $userinfo = M('users')->alias('a')->where(['username'=>$postData['phone']])->join("INNER JOIN user_has_roles b ON a.id = b.uid AND b.role_id = {$roleId}")->find();
  1043. // 重置密码时
  1044. if ($isReset && empty($userinfo)) {
  1045. json_fail('该号码未注册');
  1046. } elseif (!$isReset && $userinfo) {
  1047. json_fail('该号码已注册');
  1048. }
  1049. $sms_code_mode = M('sms_verification_code'); // 短信验证码模型
  1050. // 防盗刷IP地址检测
  1051. $todayTime = strtotime(date('Y-m-d'));
  1052. $where = ['access_ip' => $_SERVER['REMOTE_ADDR'], 'created_at' => ['GT', $todayTime]];
  1053. $count = $sms_code_mode->where($where)->count();
  1054. if ($count >= 10) {
  1055. json_fail('已达到当日获取次数上限');
  1056. }
  1057. // 获取短信配置
  1058. $rlyunId = M('sms_config')->where(['name' => 'rlyun'])->getField('id');
  1059. $smsConfig = M('sms_config')->where(['pid' => $rlyunId])->getField('key,value');
  1060. if (empty($smsConfig)) {
  1061. json_fail('获取短信配置失败');
  1062. }
  1063. // 获取短信验证码模板信息
  1064. $where = ['operator' => 'rlyun', 'code' => 'verification_code'];
  1065. $tmpInfo = M('sms_template')->where($where)->find();
  1066. if (empty($tmpInfo)) {
  1067. json_fail('获取短信验证码模板信息失败');
  1068. }
  1069. // 生成验证码
  1070. $code = rand(0, 9999);
  1071. $code = str_pad($code, 4, '0',STR_PAD_LEFT);
  1072. // 保存验证码
  1073. $time = time();
  1074. $saveData = [
  1075. 'mobile' => $postData['phone'],
  1076. 'code' => $code,
  1077. 'access_ip' => $_SERVER['REMOTE_ADDR'],
  1078. 'created_at' => $time,
  1079. 'updated_at' => $time,
  1080. ];
  1081. M()->startTrans(); // 开启事务
  1082. $res = $sms_code_mode->createAdd($saveData);
  1083. if ($res === false) {
  1084. M()->rollback();
  1085. json_fail('发送失败,请稍后重试');
  1086. }
  1087. //M()->commit();
  1088. //json_success('测试,不发短信,看数据库');
  1089. // 发送短信验证码
  1090. $content = str_replace('{1}', $code, $tmpInfo['content']);
  1091. $content = str_replace('{2}', '5分钟', $content);
  1092. $contentData = [
  1093. 'tplno' => $tmpInfo['identify'],
  1094. 'tpldata' => [ $code, '5分钟' ],
  1095. //'tpldata' => $content, // error test
  1096. 'info' => $content,
  1097. ];
  1098. //$res = (new \Jms\Sms\RlyuntxV100)->send($postData['phone'], $contentData, $smsConfig);
  1099. $res = send_sms_with_config($postData['phone'], $contentData, $smsConfig);
  1100. if (!$res['success']) {
  1101. M()->rollback();
  1102. $res = M('sms_send_log')->add($res['data']);
  1103. json_fail($res['message'] ?: '发送失败');
  1104. }
  1105. M()->commit();
  1106. json_success($res['message']);
  1107. }
  1108. public function applyCourse( ){
  1109. header('Access-Control-Allow-Origin: *');
  1110. $openid = I('get.openid');
  1111. $userid = I('get.userid');
  1112. if(!$userid || $userid == 'undefined' || $userid == 'null'){
  1113. json_fail('获取不到你的用户标识,请重新登陆');
  1114. }
  1115. //检测登录状态
  1116. $res = $this->checkLoginState($openid,$userid);
  1117. if(!$res['status']){
  1118. json_fail($res['message']);
  1119. }
  1120. $post_data = json_decode(file_get_contents('php://input'), true) ?:[];
  1121. if (!$post_data['course_id']) {
  1122. json_fail('未获取到报名课程');
  1123. }
  1124. $courses_model = M('courses');
  1125. $apply_user_model = M('apply_users');
  1126. // 检测课程是否发布
  1127. $where = ['id' => $post_data['course_id'],'publish'=> 1];
  1128. $publish = $courses_model->where($where)->getField('publish');
  1129. if (!$publish) {
  1130. json_fail('课程不存在或未发布');
  1131. }
  1132. // 获取用户信息
  1133. $where = ['id' => $userid];
  1134. $user_info = M('users')->where($where)->find();
  1135. if (empty($user_info)) {
  1136. json_fail('未获取到报名用户信息');
  1137. }
  1138. // 检测是否已报名
  1139. $where = ['user_id' => $userid, 'course_id' => $post_data['course_id']];
  1140. if ($apply_user_model->where($where)->count()) {
  1141. json_fail('已报名,请勿重复操作');
  1142. }
  1143. $time = time();
  1144. $save_data = [
  1145. 'course_id' => $post_data['course_id'],
  1146. 'user_id'=> $userid,
  1147. 'apply_time'=> $time,
  1148. 'department_id' => $user_info['department_id'],
  1149. 'creator_id' => $userid,
  1150. 'created_at' => $time,
  1151. ];
  1152. $res = $apply_user_model->createAdd($save_data);
  1153. if (!$res) {
  1154. json_fail('报名失败,请稍后重试');
  1155. }
  1156. json_success('报名成功');
  1157. }
  1158. public function getCourseDetail( ){
  1159. header('Access-Control-Allow-Origin: *');
  1160. $openid = I('get.openid');
  1161. $userid = I('get.userid');
  1162. if(!$userid || $userid == 'undefined' || $userid == 'null'){
  1163. json_fail('获取不到你的用户标识,请重新登陆');
  1164. }
  1165. //检测登录状态
  1166. $res = $this->checkLoginState($openid,$userid);
  1167. if(!$res['status']){
  1168. json_fail($res['message']);
  1169. }
  1170. $course_id = I('get.id');
  1171. if (!$course_id) {
  1172. json_fail('未获取到课程信息');
  1173. }
  1174. // 查出课程信息
  1175. $where = ['id' => $course_id];
  1176. $course_info = M('courses')->where($where)->find() ? : [];
  1177. if ($course_info) {
  1178. // 查出课程视频
  1179. $where = ['course_id' => $course_id,'publish' => 1];
  1180. $video_list = M('videos')->where($where)->field('id,name,path,cover_image,introduction,duration,lector_id')->select() ? : [];
  1181. $users_model = M('users');
  1182. foreach ($video_list as &$video) {
  1183. $video['lector'] = '';
  1184. if ($video['lector_id']) {
  1185. $where = ['id' => $video['lector_id']];
  1186. $video['lector'] = $users_model->where($where)->getField('realname') ? : '';
  1187. }
  1188. unset($video['lector_id']);
  1189. }
  1190. $course_info['video_list'] = $video_list;
  1191. // 是否报名
  1192. $course_info['apply_status'] = 0;
  1193. $where = ['course_id' => $course_id, 'user_id' => $userid];
  1194. if (M('apply_users')->where($where)->find()) {
  1195. $course_info['apply_status'] = 1;
  1196. }
  1197. }
  1198. json_success('获取成功', $course_info);
  1199. }
  1200. public function getCourseVideos( ){
  1201. header('Access-Control-Allow-Origin: *');
  1202. $userid = I('get.userid');
  1203. if(!$userid || $userid == 'undefined' || $userid == 'null'){
  1204. json_fail('获取不到你的用户标识,请重新登陆');
  1205. }
  1206. $id = I('get.id');
  1207. if (!$id || $id == 'undefined' || $id == 'null') {
  1208. json_fail('未获取到课程信息');
  1209. }
  1210. // 查出学生所在学校
  1211. $where = ['course_id' => $id,'publish' => 1];
  1212. $video_list = M('videos')->where($where)->field('name,path,cover_image,introduction,duration')->select() ? : [];
  1213. json_success('获取成功', $video_list);
  1214. }
  1215. public function getUserCourses( ){
  1216. header('Access-Control-Allow-Origin: *');
  1217. $openid = I('get.openid');
  1218. $userid = I('get.userid');
  1219. if(!$userid || $userid == 'undefined' || $userid == 'null'){
  1220. json_fail('获取不到你的用户标识,请重新登陆');
  1221. }
  1222. //检测登录状态
  1223. $res = $this->checkLoginState($openid,$userid);
  1224. if(!$res['status']){
  1225. json_fail($res['message']);
  1226. }
  1227. /*
  1228. $applyState = I('get.applyState');
  1229. $deviceId = I('get.deviceId');
  1230. if (!$deviceId) {
  1231. json_fail('请选择一个设备用户');
  1232. }*/
  1233. // 查出用户信息
  1234. $where = ['id' => $userid];
  1235. $user_info = M('users')->where($where)->find();
  1236. if (empty($user_info)) {
  1237. json_fail('获取用户信息失败');
  1238. }
  1239. $total = 0;
  1240. $limit = I('get.limit') ? : 10;
  1241. $page = I('get.page') ? : 1;
  1242. $courses_model = M('courses');
  1243. $apply_students_model = M('apply_users');
  1244. // 查出该设备所在部门下的课程
  1245. $where = [
  1246. 'publish' => 1,
  1247. '_string' => "FIND_IN_SET({$user_info['department_id']}, departments)",
  1248. ];
  1249. // 课程数量
  1250. $total = $courses_model->where($where)->count();
  1251. // 课程
  1252. $course_list = $courses_model->where($where)->order('created_at desc')->page($page,$limit)->select() ?: [];
  1253. foreach ($course_list as &$course) {
  1254. // 转换创建时间
  1255. $course['created_at'] = date('Y-m-d H:i:s', $course['created_at']);
  1256. // 报名人数
  1257. $where = ['course_id' => $course['id']];
  1258. $course['apply_count'] = $apply_students_model->where($where)->count() ? : 0;
  1259. }
  1260. $data = [
  1261. 'page' => $page,
  1262. 'limit' => $limit,
  1263. 'total' => $total,
  1264. 'list' => $course_list,
  1265. ];
  1266. json_success('获取成功', $data);
  1267. }
  1268. public function getVideoDetail( ){
  1269. header('Access-Control-Allow-Origin: *');
  1270. $openid = I('get.openid');
  1271. $userid = I('get.userid');
  1272. if(!$userid || $userid == 'undefined' || $userid == 'null'){
  1273. json_fail('获取不到你的用户标识,请重新登陆');
  1274. }
  1275. //检测登录状态
  1276. $res = $this->checkLoginState($openid,$userid);
  1277. if(!$res['status']){
  1278. json_fail($res['message']);
  1279. }
  1280. $id = I('get.id');
  1281. if (!$id) {
  1282. json_fail('未获取到视频信息');
  1283. }
  1284. $where = ['id' => $id,'publish' => 1];
  1285. $video_info = M('videos')->where($where)->field('id,name,path,cover_image,introduction,duration,lector_id')->find() ? : [];
  1286. $video_info['lector'] = '';
  1287. if ($video_info['lector_id']) {
  1288. $where = ['id' => $video_info['lector_id']];
  1289. $video_info['lector'] = M('users')->where($where)->getField('realname') ? : '';
  1290. }
  1291. unset($video_info['lector_id']);
  1292. json_success('获取成功', $video_info);
  1293. }
  1294. public function saveVideoPlayingData( ){
  1295. header('Access-Control-Allow-Origin: *');
  1296. $openid = I('get.openid');
  1297. $userid = I('get.userid');
  1298. if(!$userid || $userid == 'undefined' || $userid == 'null'){
  1299. json_fail('获取不到你的用户标识,请重新登陆');
  1300. }
  1301. //检测登录状态
  1302. $res = $this->checkLoginState($openid,$userid);
  1303. if(!$res['status']){
  1304. json_fail($res['message']);
  1305. }
  1306. $post_data = file_get_contents("php://input");
  1307. $post_data = json_decode($post_data, true) ?: [];
  1308. if (!isset($post_data['video_id'])) {
  1309. json_fail('未获取到视频id');
  1310. }
  1311. $post_data['first_play_time'] = $post_data['first_play_time'] ? substr($post_data['first_play_time'], 0 , 10) : time();
  1312. // 查出用户所在部门
  1313. $where = ['id' => $userid];
  1314. $department_id = M('users')->where($where)->getField('department_id');
  1315. $post_data['department_id'] = $department_id;
  1316. // 添加数据
  1317. $res = M('video_play')->createAdd($post_data);
  1318. if (!$res) {
  1319. json_fail('保存失败');
  1320. }
  1321. json_success('保存成功');
  1322. }
  1323. public function getAlarmTypeOptions( ){
  1324. header('Access-Control-Allow-Origin: *');
  1325. $typeid = M('sys_dict_type')->where(['code' => 'AlarmType'])->getField('id');
  1326. $options = M('sys_dict_data')->field('id, value as text, code as value')->where(['type_id' => $typeid])->select();
  1327. if(!$options){
  1328. json_fail('获取告警选项失败');
  1329. }
  1330. json_success('获取选项成功', $options);
  1331. //AlarmType
  1332. }
  1333. private function isValidSmsCode( $mobile, $code ){
  1334. if (!$mobile) {
  1335. return ['success' => false, 'message' => '手机号码不存在'];
  1336. }
  1337. if (!$code) {
  1338. return ['success' => false, 'message' => '验证码不存在'];
  1339. }
  1340. // 根据手机号码和验证码查询最后一次验证码
  1341. $sms_verification_code_model = M('sms_verification_code');
  1342. $cond = [ 'mobile' => $mobile, 'code' => $code ];
  1343. $info = $sms_verification_code_model->where($cond)->order('created_at', 'desc')->find();
  1344. // 不存在
  1345. if (empty($info)) {
  1346. return ['success' => false, 'message' => '验证失败,请确保手机号码和验证码输入无误'];
  1347. }
  1348. // 超过时间(暂定5分钟)
  1349. $valid_time = 60 * 5;
  1350. if (time() - $info['created_at'] > $valid_time) {
  1351. return ['success' => false, 'message' => '验证码已失效'];
  1352. }
  1353. // 超过使用次数(暂定3次)
  1354. if ($info['use_times'] >= 3) {
  1355. return ['success' => false, 'message' => '验证码已失效'];
  1356. }
  1357. // 可以使用,使用次数+1
  1358. $cond = ['id' => $info['id']];
  1359. $res = $sms_verification_code_model->where($cond)->setInc('use_times');
  1360. if ($res === false) {
  1361. return ['success' => false, 'message' => '操作失败,请重试'];
  1362. }
  1363. return ['success' => true, 'message' => '验证码有效'];
  1364. }
  1365. public function userBindDevice( ){
  1366. header('Access-Control-Allow-Origin: *');
  1367. $openid = I('get.openid');
  1368. $userid = I('get.userid');
  1369. //检测登录状态
  1370. $res=$this->checkLoginState($openid,$userid);
  1371. if(!$res['status']){
  1372. json_fail($res['message']);
  1373. }
  1374. // 通过手机号和短信验证码注册
  1375. $data = json_decode( file_get_contents("php://input") ,true);
  1376. // 用户类型:个人(personal)、团体(group)
  1377. $userType = $data['userType'];
  1378. // 设备类型:卡牌(card)、徽章(badge)
  1379. $deviceType = $data['deviceType'];
  1380. // 团体卡牌用户(group_card_user) 不可注册
  1381. // 个人用户(personal),团体徽章用户(group_badge_user) 可注册
  1382. if ($userType == 'personal') { // 个人用户
  1383. $identify = 'personal';
  1384. } elseif ($userType == 'group' && $deviceType == 'badge') { // 团体徽章用户
  1385. $identify = 'group_badge_user';
  1386. } elseif ($userType == 'group' && $deviceType == 'card') { // 团体卡牌用户
  1387. $identify = 'group_card_user';
  1388. } else {
  1389. json_fail('未知用户类型');
  1390. }
  1391. if ($identify == 'group_card_user') {
  1392. json_fail('无绑定设备权限');
  1393. }
  1394. //$data['imei'],$data['user_no'],$data['username']
  1395. if(!$data['imei'] ){
  1396. json_fail('缺少IMEI参数');
  1397. }
  1398. //先查询设备信息 看是否被绑定
  1399. $where = array('imei'=>$data['imei']);
  1400. $device_info=M('devices')->where($where)->find();
  1401. if(!$device_info){
  1402. json_fail('设备不存在,请确认输入无误');
  1403. }
  1404. if($device_info['user_id']){
  1405. json_fail('设备已被绑定');
  1406. }
  1407. if ($identify == 'group_badge_user' && $device_info['device_type'] != 1 ) {
  1408. json_fail('无法绑定该类型的设备');
  1409. }
  1410. $userType = $data['userType'];
  1411. $device_name = '';
  1412. if ($data['device_name']) {
  1413. $device_name = $data['device_name'];
  1414. } elseif ($data['username']) {
  1415. $device_name = $data['username'];
  1416. } else {
  1417. $device_name = '用户'. substr($data['imei'], -4);
  1418. }
  1419. $save_data=array(
  1420. 'device_name' => $device_name,
  1421. 'user_id'=>$userid,
  1422. 'use_state'=>1,
  1423. );
  1424. M()->startTrans();
  1425. if ($identify == 'group_badge_user'){
  1426. //团体用户 绑定设备 并绑定用户信息
  1427. if( !$data['user_no'] || !$data['username'] ){
  1428. json_fail('用户姓名和编号不能为空');
  1429. }
  1430. // 第一次绑定:将 badgeuser 中的 department_id 赋给 users 的 department_id,之后将只能绑定该部门下的设备
  1431. // badge_cond
  1432. $badge_cond = ['user_no'=>$data['user_no'], 'username'=>$data['username']];
  1433. $userinfo = M('users')->where(['id'=>$userid])->find();
  1434. if ($userinfo['department_id']) {
  1435. $badge_cond['department_id'] = $userinfo['department_id'];
  1436. }
  1437. $badge_info = M('badgeuser')->where($badge_cond)->find();
  1438. if(!$badge_info){
  1439. json_fail('绑定用户不存在');
  1440. }
  1441. if ($badge_info['is_used']) {
  1442. json_fail('该用户已绑定');
  1443. }
  1444. // 检测设备与用户是否在同一部门
  1445. if ($device_info['department_id'] != $badge_info['department_id']) {
  1446. json_fail('用户与设备所属部门不一致');
  1447. }
  1448. $update_data=array('is_used'=>1);
  1449. //$badgeRes=M('badgeuser')->createSave(array('id'=>$badge_info['uid']),$update_data);
  1450. // 修改徽章用户状态
  1451. $badgeRes = M('badgeuser')->where(array('id'=>$badge_info['id']))->setField($update_data);
  1452. if(!$badgeRes){
  1453. M()->rollback();
  1454. json_fail('绑定用户失败');
  1455. }
  1456. $save_data['badge_user_id']=$badge_info['id'];
  1457. // 设置登录用户部门
  1458. if (!$userinfo['department_id']) {
  1459. $res = M('users')->where(['id'=>$userid])->setField(['department_id'=>$badge_info['department_id']]);
  1460. if($res === false){
  1461. M()->rollback();
  1462. json_fail('设置用户部门失败');
  1463. }
  1464. }
  1465. }
  1466. $devRes=M('devices')->createSave(array('id'=>$device_info['id']),$save_data);
  1467. if(!$devRes){
  1468. M()->rollback();
  1469. json_fail('绑定设备失败');
  1470. }
  1471. //未同步设备需要先同步
  1472. if($device_info['sync_status']!=1){
  1473. $result=$this->addToVoiceCenter($device_info['imei']);
  1474. if(!$result['success']){
  1475. create_log($device_info['imei'].'绑定时同步失败 message: '.$result['messaget'], 'AddVoiceCenter');
  1476. M()->rollback();
  1477. json_fail('绑定设备失败');
  1478. }
  1479. }
  1480. M()->commit();
  1481. json_success('绑定成功',$userid);
  1482. }
  1483. public function getUserFences( ){
  1484. //user_has_students
  1485. header('Access-Control-Allow-Origin: *');
  1486. $userid =intval(I('get.userid'));
  1487. if(!$userid){
  1488. json_fail('获取不到你的用户标识,请重新登陆');
  1489. }
  1490. $field = 'id, name, fence_shape as shape, creator_id as userid, is_check_in as inFenceAlarm, is_check_out as outFenceAlarm, fence_info as fenceInfo, sent_interval';
  1491. $res = M('fences')->field($field)->where(['creator_id' => $userid, 'fence_type' => 1])->select();
  1492. if(!$res){
  1493. json_fail('查询不到用户围栏');
  1494. }
  1495. foreach($res as &$v){
  1496. $v['inFenceAlarm'] = (bool)($v['inFenceAlarm']);
  1497. $v['outFenceAlarm'] = (bool)($v['outFenceAlarm']);
  1498. $v['fenceInfo'] = json_decode($v['fenceInfo'], true);
  1499. $convPoint = (new \Jms\Algo\Geometry())->convertBd09ToGcj02($v['fenceInfo']['center']['lat'], $v['fenceInfo']['center']['lng']);
  1500. $v['fenceInfo']['center']['lat'] = $convPoint['lat'];
  1501. $v['fenceInfo']['center']['lng'] = $convPoint['lng'];
  1502. }
  1503. json_success('查询成功', $res);
  1504. }
  1505. public function changeUserFenceAlarmStatus( ){
  1506. //user_has_students
  1507. header('Access-Control-Allow-Origin: *');
  1508. $post_data = file_get_contents('php://input');
  1509. $post_data = json_decode($post_data, true);
  1510. $fenceId = $post_data['id'];
  1511. if(!$fenceId){
  1512. json_fail('获取不到围栏标识,请刷新下');
  1513. }
  1514. $alarmType = $post_data['alarmType'];
  1515. $value = $post_data['value'];
  1516. if(is_null($value) ){
  1517. json_fail('获取不到告警标识值');
  1518. }
  1519. $saveData = [];
  1520. if($alarmType == 'in'){
  1521. $saveData['is_check_in'] = $value;
  1522. }else{
  1523. $saveData['is_check_out'] = $value;
  1524. }
  1525. $res = M('fences')->where(['id' => $fenceId])->save($saveData);
  1526. if(!res){
  1527. json_fail('修改告警状态失败');
  1528. }
  1529. json_success('修改告警状态成功');
  1530. }
  1531. public function updateDeviceInfo( ){
  1532. header('Access-Control-Allow-Origin: *');
  1533. $openid = I('get.openid');
  1534. $userid = I('get.userid');
  1535. //检测登录状态
  1536. $res=$this->checkLoginState($openid,$userid);
  1537. if(!$res['status']){
  1538. json_fail($res['message']);
  1539. }
  1540. $data = json_decode( file_get_contents("php://input") ,true);
  1541. $device_id=$data['device_id'];
  1542. if(!$device_id){
  1543. json_fail('缺少设备ID');
  1544. }
  1545. $device_name=$data['device_name'];
  1546. if(!$device_name){
  1547. json_fail('缺少修改信息');
  1548. }
  1549. $cond=array('id'=>$device_id);
  1550. $res=M('devices')->createSave($cond,array('device_name'=>$device_name));
  1551. if(!$res){
  1552. json_fail('修改失败');
  1553. }
  1554. json_success('修改成功',$list);
  1555. }
  1556. public function getSystemFences( ){
  1557. //user_has_students
  1558. header('Access-Control-Allow-Origin: *');
  1559. $userid =intval(I('get.userid'));
  1560. if(!$userid){
  1561. json_fail('获取不到你的用户标识,请重新登陆');
  1562. }
  1563. $userBelongDep = M('users')->where(['id' => $userid ])->getField('department_id');
  1564. if(!$userBelongDep){
  1565. json_fail('查询不到用户归属组织');
  1566. }
  1567. $departIds = M('departments')->where(['id' => $userBelongDep ])->getField('level');
  1568. if($departIds){
  1569. $departIds = explode('-', $departIds);
  1570. }else{
  1571. $departIds =[];
  1572. }
  1573. array_push($departIds, $userBelongDep);
  1574. $typeid = M('sys_dict_type')->where(['code' =>'FenceType'])->getField('id');
  1575. $fenceTypeAssoc = M('sys_dict_data')->where(['type_id' => $typeid])->getField('code, value');
  1576. $fenceCond = [
  1577. 'fence_type' => ['in', [0, 2]],
  1578. 'departments' => ['in', $departIds]
  1579. ];
  1580. $field = 'fences.id, name, fence_shape as shape,fences.creator_id as userid, is_check_in as inFenceAlarm,fence_type, is_check_out as outFenceAlarm, fence_info as fenceInfo, departments.department_name';
  1581. $res = M('fences')->field($field)
  1582. ->join('departments on fences.departments = departments.id')
  1583. ->where($fenceCond)
  1584. ->select();
  1585. if(!$res){
  1586. json_fail('查询不到系统围栏');
  1587. }
  1588. foreach($res as &$v){
  1589. $v['inFenceAlarm'] = (bool)($v['inFenceAlarm']);
  1590. $v['outFenceAlarm'] = (bool)($v['inFenceAlarm']);
  1591. $v['fence_type_text'] = $fenceTypeAssoc[$v['fence_type']];
  1592. }
  1593. json_success('查询成功', $res);
  1594. }
  1595. public function userUnbindDevice( ){
  1596. header('Access-Control-Allow-Origin: *');
  1597. $openid = I('get.openid');
  1598. $userid = I('get.userid');
  1599. //检测登录状态
  1600. $res=$this->checkLoginState($openid,$userid);
  1601. if(!$res['status']){
  1602. json_fail($res['message']);
  1603. }
  1604. // 通过手机号和短信验证码注册
  1605. $data = json_decode( file_get_contents("php://input") ,true);
  1606. //$data['imei'],$data['user_no'],$data['username']
  1607. //先查询设备信息 看是否被绑定
  1608. if(!$data['device_id']){
  1609. json_fail('缺少设备ID参数');
  1610. }
  1611. $device_info=M('devices')->where(array('id'=>$data['device_id']))->find();
  1612. if(!$device_info){
  1613. json_fail('设备不存在,请确认输入无误');
  1614. }
  1615. $save_data=array(
  1616. 'user_id'=>'',
  1617. 'badge_user_id'=>'',
  1618. 'use_state'=>0,
  1619. );
  1620. M()->startTrans();
  1621. if($device_info['badge_user_id']){
  1622. $badRes=M('badgeuser')->createSave(array('id'=>$device_info['badge_user_id']),array('is_used'=>0));
  1623. if(!$badRes){
  1624. json_fail('解绑失败');
  1625. }
  1626. }
  1627. $devRes=M('devices')->createSave(array('id'=>$device_info['id']),$save_data);
  1628. if(!$devRes){
  1629. M()->rollback();
  1630. json_fail('解绑失败');
  1631. }
  1632. // 如果没有设备了,置空 department_id
  1633. if (!M('devices')->where(['user_id'=>$userid])->count()) {
  1634. $res = M('users')->where(['id'=>$userid])->setField(['department_id'=>'']);
  1635. if($res === false){
  1636. M()->rollback();
  1637. json_fail('解绑失败2');
  1638. }
  1639. }
  1640. M()->commit();
  1641. json_success('解绑成功',$userid);
  1642. }
  1643. public function addUserFence( ){
  1644. //user_has_students
  1645. header('Access-Control-Allow-Origin: *');
  1646. $data = json_decode( file_get_contents("php://input") ,true);
  1647. $userid =intval($data['userid']);
  1648. if(!$userid){
  1649. json_fail('获取不到你的用户标识,请重新登陆');
  1650. }
  1651. $fenceName = $data['fenceName'];
  1652. if(!$fenceName){
  1653. json_fail('获取不到围栏名');
  1654. }
  1655. $fenceInfo = $data['fenceInfo'];
  1656. if(!$fenceInfo){
  1657. json_fail('获取不到围栏信息');
  1658. }
  1659. $convPoint = (new \Jms\Algo\Geometry())->convertGcj02ToBd09($fenceInfo['center']['lat'], $fenceInfo['center']['lng']);
  1660. $fenceInfo['center']['lat'] = $convPoint['lat'];
  1661. $fenceInfo['center']['lng'] = $convPoint['lng'];
  1662. $saveData = [
  1663. 'creator_id' => $userid,
  1664. 'name' => $fenceName,
  1665. 'push_users' => json_encode([$userid]),
  1666. 'fence_shape' => 'circle',
  1667. 'fence_type' => 1,
  1668. 'fence_info' => json_encode($fenceInfo),
  1669. 'created_at' => time(),
  1670. 'created_at' => time()
  1671. ];
  1672. $res = M('fences')->add($saveData);
  1673. if(!$res){
  1674. json_fail('保存入库失败');
  1675. }
  1676. json_success('保存入库成功');
  1677. }
  1678. private function getWeixinAccessToken( ){
  1679. $token = (new \Jiaruan\WxTmp())->getAccessToken();
  1680. return $token;
  1681. /*
  1682. $appid= C('WECHAT_APPID');
  1683. $appSecret = C('WECHAT_APPSECRET');
  1684. $accessToken = S('wx_pub_accessToken');
  1685. if(!$accessToken){
  1686. $ch = curl_init();
  1687. curl_setopt($ch, CURLOPT_URL, 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$appSecret);
  1688. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  1689. $output = json_decode(curl_exec($ch), true);
  1690. curl_close($ch);
  1691. if($output['errcode']){
  1692. return $output;
  1693. }
  1694. S('wx_pub_accessToken', $output['access_token'], '7100');
  1695. return $output['access_token'];
  1696. }else{
  1697. return $accessToken;
  1698. }
  1699. */
  1700. }
  1701. public function getAlarmRecords( ){
  1702. header('Access-Control-Allow-Origin: *');
  1703. $post_data = json_decode(file_get_contents('php://input'), true);
  1704. $openid = $post_data['openid'];
  1705. $userid = $post_data['userid'];
  1706. //检测登录状态
  1707. $res = $this->checkLoginState($openid,$userid);
  1708. if(!$res['status']){
  1709. json_fail($res['message']);
  1710. }
  1711. // 获取登录账号部门下所有告警状态的学生数据
  1712. $user_model = M('users');
  1713. $where = ['id' => $userid];
  1714. $userinfo = $user_model->field('id,department_id')->where($where)->find();
  1715. // 获取权限范围内的用户id
  1716. $creator_ids = $this->getRightCreatorIds($userinfo);
  1717. if ($creator_ids === false) {
  1718. json_fail('获取数据失败');
  1719. }
  1720. $limit = isset($post_data['limit']) ? $post_data['limit'] : 10;
  1721. $page = isset($post_data['page']) ? $post_data['page'] : 1;
  1722. // 告警记录条件
  1723. // 初始化条件
  1724. $cond = [];
  1725. $reason = I('get.reason');
  1726. $state = I('get.state');
  1727. $result = I('get.result');
  1728. if ($reason) {
  1729. if ($reason == 'fence') {
  1730. $cond['alarm_reason'] = ['IN', ['fence_in', 'fence_out']];
  1731. } else {
  1732. $cond['alarm_reason'] = $reason;
  1733. }
  1734. } else {
  1735. // 默认只显示 SOS 和围栏告警记录
  1736. $cond['alarm_reason'] = ['IN', ['fence_in', 'fence_out', 'press']];
  1737. }
  1738. if ($state) {
  1739. $cond['state'] = $state;
  1740. }
  1741. if ($result !== '' && is_numeric($result)) {
  1742. $cond['result'] = $result;
  1743. }
  1744. if (is_array($creator_ids)) {
  1745. $cond['creator_id'] = ['IN', $creator_ids];
  1746. }
  1747. // 查出总数量
  1748. $list = [];
  1749. $total = M('alarm_records')->where($cond)->count();
  1750. if ($total) {
  1751. if ($state == 'start' || $state == 'end') {
  1752. $order = "{$state}_time desc";
  1753. } else {
  1754. $order = 'id desc';
  1755. }
  1756. $fields = 'id,device_number,alarm_reason,handler_id,start_time,end_time,state,comment,result';
  1757. $list = M('alarm_records')->field($fields)->where($cond)->limit($limit)->page($page)->order($order)->select() ?: [];
  1758. if (!empty($list)) {
  1759. // alarm_reason,handler_id,result
  1760. // 获取告警类型
  1761. $typeid = M('sys_dict_type')->where(['code' => 'AlarmType'])->getField('id');
  1762. $reason_arr = M('sys_dict_data')->where(['type_id' => $typeid])->getField('code, value');
  1763. // 处理结果
  1764. $result_arr = [
  1765. 0 => '待处理',
  1766. 1 => '已处理',
  1767. 2 => '误报',
  1768. ];
  1769. // 中文转换
  1770. foreach ($list as &$alarm) {
  1771. $alarm['start_time'] = $alarm['start_time'] ? date('Y-m-d H:i:s', $alarm['start_time']) : '';
  1772. $alarm['end_time'] = $alarm['end_time'] ? date('Y-m-d H:i:s', $alarm['end_time']) : '';
  1773. if (is_array($reason_arr)) {
  1774. $alarm['alarm_reason'] = $reason_arr[$alarm['alarm_reason']];
  1775. }
  1776. if (is_numeric($alarm['result'])) {
  1777. $alarm['result'] = $result_arr[$alarm['result']];
  1778. }
  1779. }
  1780. }
  1781. }
  1782. // 返回
  1783. $data = [
  1784. 'total' => $total,
  1785. 'limit' => $limit,
  1786. 'page' => $page,
  1787. 'list' => $list,
  1788. ];
  1789. json_success('获取成功', $data);
  1790. }
  1791. public function getAlarmDetail( ){
  1792. header('Access-Control-Allow-Origin: *');
  1793. $openid = I('get.openid');
  1794. $userid = I('get.userid');
  1795. if(!$userid || $userid == 'undefined' || $userid == 'null'){
  1796. json_fail('获取不到你的用户标识,请重新登陆');
  1797. }
  1798. //检测登录状态
  1799. $res = $this->checkLoginState($openid,$userid);
  1800. if(!$res['status']){
  1801. json_fail($res['message']);
  1802. }
  1803. $alarm_id = I('get.id');
  1804. if (!$alarm_id) {
  1805. json_fail('获取告警信息失败1');
  1806. }
  1807. // 查出告警数据
  1808. $where = ['id' => $alarm_id];
  1809. $alarm_info = M('alarm_records')->where($where)->find();
  1810. if (empty($alarm_info)) {
  1811. json_fail('获取告警信息失败2');
  1812. }
  1813. $report_id = I('get.rid');
  1814. if (!$report_id) {
  1815. json_fail('获取告警信息失败3');
  1816. }
  1817. $where = ['id' => $report_id];
  1818. $alarm_report = M('alarm_report')->where($where)->find();
  1819. if (empty($alarm_report)) {
  1820. json_fail('获取告警信息失败4');
  1821. }
  1822. $alarm_info['address']=$alarm_report['address'];
  1823. // 查出设备信息
  1824. $where = ['imei' => $alarm_info['device_number']];
  1825. $device_info = M('devices')->where($where)->find();
  1826. if (empty($alarm_info)) {
  1827. json_fail('获取告警设备信息失败');
  1828. }
  1829. if ($device_info['online_time'] > $device_info['wifi_online_time']) {
  1830. $device_info['last_online_time'] = date('Y-m-d H:i:s', $device_info['online_time']);
  1831. $device_info['last_location'] = $device_info['longitude'] .','. $device_info['latitude'];
  1832. $res = bmap_geocoding($device_info['latitude'], $device_info['longitude']);
  1833. if($res['success']){
  1834. $device_info['address'] = $res['address'];
  1835. }else{
  1836. $device_info['address']='';
  1837. }
  1838. } elseif ($device_info['online_time'] < $device_info['wifi_online_time']) {
  1839. $device_info['last_online_time'] = date('Y-m-d H:i:s', $device_info['wifi_online_time']);
  1840. $device_info['last_location'] = $device_info['wifi_longitude'] .','. $device_info['wifi_latitude'];
  1841. } else {
  1842. $device_info['last_online_time'] = '0000-00-00 00:00:00';
  1843. $device_info['last_location'] = '';
  1844. $device_info['address']='';
  1845. }
  1846. // 查出设备用户
  1847. if ($device_info['badge_user_id']) {
  1848. $where = ['id' => $device_info['badge_user_id']];
  1849. $user_info = M('badgeuser')->where($where)->find() ?: [];
  1850. } else {
  1851. $where = ['id' => $device_info['user_id']];
  1852. $user_info = M('users')->where($where)->find() ?: [];
  1853. }
  1854. //if (empty($user_info)) {
  1855. //json_fail('获取告警用户信息失败');
  1856. //}
  1857. // 获取紧急联系人
  1858. $where = ['device_id' => $device_info['id']];
  1859. $urgent_list = M('kq_urgent')->where($where)->select() ? : [];
  1860. if (!empty($urgent_list)) {
  1861. $type_id = M('sys_dict_type')->where(['code' => 'Relationships'])->getField('id');
  1862. $ships = M('sys_dict_data')->where(['type_id' => $type_id])->getField('code,value');
  1863. foreach ($urgent_list as $key => &$urgent) {
  1864. $urgent['relationship_text'] = $ships[$urgent['relationship']];
  1865. }
  1866. }
  1867. // 部门名称
  1868. $alarm_info['department_name'] = M('departments')->where(['id' => $user_info['department_id']])->getField('department_name');
  1869. $alarm_info['user_info'] = $user_info;
  1870. $alarm_info['urgent_list'] = $urgent_list;
  1871. $alarm_info['device_info'] = $device_info;
  1872. $alarm_info['device_info']['department_name'] = $alarm_info['department_name'];
  1873. json_success('获取成功', $alarm_info);
  1874. }
  1875. public function handleAlarm( ){
  1876. header('Access-Control-Allow-Origin: *');
  1877. $post_data = json_decode(file_get_contents('php://input'), true);
  1878. $openid = I('get.openid');
  1879. $userid = I('get.userid');
  1880. if(!$userid || $userid == 'undefined' || $userid == 'null'){
  1881. json_fail('获取不到你的用户标识,请重新登陆');
  1882. }
  1883. //检测登录状态
  1884. $res = $this->checkLoginState($openid,$userid);
  1885. if(!$res['status']){
  1886. json_fail($res['message']);
  1887. }
  1888. // result 1-出警,2-误报
  1889. if (!$post_data['result']) {
  1890. json_fail('处理结果不能为空');
  1891. }
  1892. if (!$post_data['comment']) {
  1893. json_fail('处理原因不能为空');
  1894. }
  1895. if (!$post_data['id']) {
  1896. json_fail('未获取到告警信息1');
  1897. }
  1898. $time = time();
  1899. M()->startTrans();
  1900. // 修改前,检测结果是否一致,如果一致不允许重复操作
  1901. $records_model = M('alarm_records');
  1902. $where = ['id' => $post_data['id']];
  1903. $record_info = $records_model->where($where)->field('device_number,result')->find();
  1904. if (empty($record_info)) {
  1905. json_fail('未获取到告警信息2');
  1906. }
  1907. //if ($record_info['result'] > 0) {
  1908. if ($record_info['result'] == $post_data['result']) {
  1909. M()->rollback();
  1910. json_fail('已处理,请勿重复操作');
  1911. }
  1912. $where = ['id' => $post_data['id']];
  1913. $save_data = [
  1914. 'comment' => $post_data['comment'],
  1915. 'result' => $post_data['result'],
  1916. 'handler_id' => $userid,
  1917. 'state' => 'end',
  1918. 'end_time' => $time,
  1919. 'updated_at' => $time,
  1920. ];
  1921. $res = $records_model->createSave($where, $save_data);
  1922. if ($res === false) {
  1923. M()->rollback();
  1924. json_fail('操作失败');
  1925. }
  1926. // 查出设备告警状态
  1927. $where = ['imei' => $record_info['device_number']];
  1928. $device_info = M('devices')->where($where)->field('id,alarm_state')->find();
  1929. if ($device_info['alarm_state'] > 0) {
  1930. // 修改设备状态
  1931. $where = ['id' => $device_info['id']];
  1932. $res = M('devices')->where($where)->setField('alarm_state', 0);
  1933. if (!$res) {
  1934. M()->rollback();
  1935. json_fail('修改设备状态失败');
  1936. }
  1937. }
  1938. M()->commit();
  1939. json_success('操作成功');
  1940. }
  1941. public function editYysProfile( ){
  1942. header('Access-Control-Allow-Origin: *');
  1943. $openid = I('get.openid');
  1944. $userid = I('get.userid');
  1945. //检测登录状态
  1946. $res = $this->checkLoginState($openid,$userid);
  1947. if(!$res['status']){
  1948. json_fail($res['message']);
  1949. }
  1950. $data = json_decode( file_get_contents("php://input") ,true);
  1951. $data = array_map(function($item){
  1952. return trim($item);
  1953. }, $data);
  1954. if (!$data['nickName']) {
  1955. json_fail('昵称不能为空');
  1956. }
  1957. if (!$data['phone']) {
  1958. json_fail('手机号码不能为空');
  1959. }
  1960. if (!preg_match('/^[-_a-zA-Z0-9]{6,16}?$/', $data['phone'])) {
  1961. json_fail('手机号码格式不正确');
  1962. }
  1963. // 修改密码
  1964. if ($data['oldPwd']) {
  1965. if (!$data['newPwd']) {
  1966. json_fail('新密码不能为空');
  1967. }
  1968. if (!$data['confirmNewPwd']) {
  1969. json_fail('确认密码不能为空');
  1970. }
  1971. if ($data['newPwd'] != $data['confirmNewPwd']) {
  1972. json_fail('两次新密码不一致');
  1973. }
  1974. }
  1975. $userCond = array( 'id' => $userid);
  1976. $user_info = M('users')->where($userCond)->find();
  1977. if(!$user_info){
  1978. json_fail('获取用户信息失败');
  1979. }
  1980. if ($user_info['wx_open_id'] != $openid) {
  1981. json_fail('账号可能被其他用户登录,请重新登录');
  1982. }
  1983. if ($data['oldPwd']) {
  1984. if(!password_verify($data['oldPwd'], $user_info['password'])){
  1985. json_fail('原密码不正确');
  1986. }
  1987. }
  1988. $saveData = [
  1989. 'realname' => $data['nickName'],
  1990. 'phone' => $data['phone'],
  1991. ];
  1992. // 有原密码才能修改
  1993. if ($data['oldPwd']) {
  1994. $saveData['password'] = password_hash($data['newPwd'], PASSWORD_DEFAULT);
  1995. }
  1996. $result = M('users')->createSave($userCond, $saveData);
  1997. if(!$result){
  1998. json_fail('修改失败');
  1999. }
  2000. $respData = [
  2001. 'id' => $user_info['id'],
  2002. 'realname' => $data['nickName'],
  2003. 'avatar' => $user_info['avatar'],
  2004. 'phone' => $data['phone'],
  2005. ];
  2006. json_success('修改成功', $respData);
  2007. }
  2008. public function changeAuthorize( ){
  2009. //user_has_students
  2010. header('Access-Control-Allow-Origin: *');
  2011. $imei = I('get.imei');
  2012. if(!$imei){
  2013. json_fail('获取不到设备号');
  2014. }
  2015. $authorize = intval(I('get.authorize') );
  2016. if($authorize !== 0 && $authorize !== 1){
  2017. json_fail('授权状态值不合法');
  2018. }
  2019. $saveData = [
  2020. 'authorize' => $authorize,
  2021. 'updated_at' => time()
  2022. ];
  2023. $res = M('devices')->where(['imei' => $imei])->save($saveData);
  2024. if(!res){
  2025. json_fail('权限修改失败');
  2026. }
  2027. json_success('权限修改成功');
  2028. }
  2029. public function getDeviceInfoSingle( ){
  2030. //user_has_students
  2031. header('Access-Control-Allow-Origin: *');
  2032. $imei = I('get.imei');
  2033. if(!$imei){
  2034. json_fail('获取不到设备号');
  2035. }
  2036. $res = M('devices')->where(['imei' => $imei])->find();
  2037. if(!res){
  2038. json_fail('获取设备信息失败');
  2039. }
  2040. json_success('获取设备信息成功', $res);
  2041. }
  2042. public function getDevices( ){
  2043. // 获取登录用户信息
  2044. $userinfo = $this->getYysLoginUserinfo();
  2045. // 获取权限范围内的用户id
  2046. $creator_ids = $this->getRightCreatorIds($userinfo);
  2047. if ($creator_ids === false) {
  2048. json_fail('获取失败');
  2049. }
  2050. // 获取 POST 数据
  2051. $post_data = json_decode(file_get_contents('php://input'), true);
  2052. $limit = isset($post_data['limit']) ? $post_data['limit'] : 10;
  2053. $page = isset($post_data['page']) ? $post_data['page'] : 1;
  2054. $imei = $post_data['imei'];
  2055. $list = [];
  2056. $cond = [];
  2057. $devices_model = M('devices');
  2058. // 查出总数量
  2059. if (is_array($creator_ids)) {
  2060. $cond['creator_id'] = ['IN', $creator_ids];
  2061. }
  2062. if ($imei) {
  2063. $cond['imei'] = ['LIKE', "%{$imei}%"];
  2064. }
  2065. $total = $devices_model->where($cond)->count();
  2066. if ($total) {
  2067. $list = $devices_model->where($cond)->limit($limit)->page($page)->select() ?: [];
  2068. }
  2069. // 返回
  2070. $data = [
  2071. 'total' => $total,
  2072. 'limit' => $limit,
  2073. 'page' => $page,
  2074. 'list' => $list,
  2075. ];
  2076. json_success('获取成功', $data);
  2077. }
  2078. private function getYysLoginUserinfo( ){
  2079. header('Access-Control-Allow-Origin: *');
  2080. $openid = I('get.openid');
  2081. if(!$openid){
  2082. json_fail('未获取到微信授权信息,请重新点击菜单进入并授权');
  2083. }
  2084. // 微信运营角色
  2085. $where = ['identify' => 'wxyy'];
  2086. $role_id = M('roles')->where($where)->getField('id');
  2087. if (!$role_id) {
  2088. json_fail('未开放微信运营角色');
  2089. }
  2090. // 获取登录账号信息
  2091. $userid = I('get.userid');
  2092. $cond = [
  2093. //'u.wx_open_id' => $openid,
  2094. 'u.id' => $userid,
  2095. ];
  2096. //$userinfo = M('users')->field('id,department_id')->where($where)->find();
  2097. $userinfo = M('users')->alias('u')->field('u.*')->where($cond)->join("INNER JOIN user_has_roles AS r ON u.id = r.uid AND r.role_id = {$role_id}")->find();
  2098. if (!$userinfo) {
  2099. json_fail('获取用户信息失败,请重新登录');
  2100. }
  2101. if ($userinfo['wx_open_id'] != $openid) {
  2102. json_fail('账号可能在其他设备登录,请重新登录');
  2103. }
  2104. return $userinfo;
  2105. }
  2106. public function getDeviceDetail( ){
  2107. header('Access-Control-Allow-Origin: *');
  2108. $userid = I('get.userid');
  2109. if(!$userid || $userid == 'undefined' || $userid == 'null'){
  2110. json_fail('未获取到用户标识,请重新登陆');
  2111. }
  2112. $id = I('get.id');
  2113. if (!$id) {
  2114. json_fail('获取设备信息失败');
  2115. }
  2116. $openid = I('get.openid');
  2117. //检测登录状态
  2118. $res = $this->checkLoginState($openid, $userid);
  2119. if(!$res['status']){
  2120. json_fail($res['message']);
  2121. }
  2122. // 检测是否在查询权限范围
  2123. // 查出设备信息
  2124. $where = ['id' => $id];
  2125. $device_info = M('devices')->where($where)->find() ? : [];
  2126. if (empty($device_info)) {
  2127. json_fail('未获取到设备信息');
  2128. }
  2129. if ($device_info['online_time'] >= $device_info['wifi_online_time']) {
  2130. $device_info['last_online_time'] = date('Y-m-d H:i:s', $device_info['online_time']);
  2131. $convPoint = (new \Jms\Algo\Geometry())->convertBd09ToGcj02($device_info['latitude'],$device_info['longitude']);
  2132. $device_info['last_location'] = $convPoint['lng'] .','. $convPoint['lat'];
  2133. } elseif ($device_info['online_time'] < $device_info['wifi_online_time']) {
  2134. $device_info['last_online_time'] = date('Y-m-d H:i:s', $device_info['wifi_online_time']);
  2135. $convPoint = (new \Jms\Algo\Geometry())->convertBd09ToGcj02($device_info['wifi_latitude'],$device_info['wifi_longitude']);
  2136. $device_info['last_location'] = $convPoint['lng'] .','. $convPoint['lat'];
  2137. } else {
  2138. $device_info['last_online_time'] = '0000-00-00 00:00:00';
  2139. $device_info['last_location'] = '';
  2140. }
  2141. // 查出设备用户
  2142. if ($device_info['badge_user_id']) {
  2143. $where = ['id' => $device_info['badge_user_id']];
  2144. $user_info = M('badgeuser')->where($where)->find() ?: [];
  2145. } else {
  2146. $where = ['id' => $device_info['user_id']];
  2147. $user_info = M('users')->where($where)->find() ?: [];
  2148. }
  2149. //if (empty($user_info)) {
  2150. //json_fail('获取告警用户信息失败');
  2151. //}
  2152. // 获取紧急联系人
  2153. $where = ['device_id' => $device_info['id']];
  2154. $urgent_list = M('kq_urgent')->where($where)->select() ? : [];
  2155. if (!empty($urgent_list)) {
  2156. $type_id = M('sys_dict_type')->where(['code' => 'Relationships'])->getField('id');
  2157. $ships = M('sys_dict_data')->where(['type_id' => $type_id])->getField('code,value');
  2158. foreach ($urgent_list as $key => &$urgent) {
  2159. $urgent['relationship_text'] = $ships[$urgent['relationship']];
  2160. }
  2161. }
  2162. // 部门名称
  2163. $device_info['department_name'] = M('departments')->where(['id' => $user_info['department_id']])->getField('department_name');
  2164. $device_info['user_info'] = $user_info;
  2165. $device_info['urgent_list'] = $urgent_list;
  2166. json_success('获取成功', $device_info);
  2167. }
  2168. public function getWaitHandleAlarmCounts( ){
  2169. // 获取登录用户信息
  2170. $userinfo = $this->getYysLoginUserinfo();
  2171. // 获取权限范围内的用户id
  2172. $creator_ids = $this->getRightCreatorIds($userinfo);
  2173. if ($creator_ids === false) {
  2174. json_fail('获取失败');
  2175. }
  2176. $cond = [
  2177. 'state' => 'start',
  2178. ];
  2179. // 查出总数量
  2180. if (is_array($creator_ids)) {
  2181. $cond['creator_id'] = ['IN', $creator_ids];
  2182. }
  2183. $fields = 'alarm_reason, count(alarm_reason) as count';
  2184. $list = M('alarm_records')->where($cond)->group('alarm_reason')->getField($fields) ?:[];
  2185. json_success('获取成功', $list);
  2186. }
  2187. public function getDevicePositionList( ){
  2188. // 获取登录用户信息
  2189. $userinfo = $this->getYysLoginUserinfo();
  2190. // 获取权限范围内的用户id
  2191. $creator_ids = $this->getRightCreatorIds($userinfo);
  2192. if ($creator_ids === false) {
  2193. json_fail('获取失败');
  2194. }
  2195. $cond = [
  2196. 'use_state' => 1,
  2197. ];
  2198. // 查出总数量
  2199. if (is_array($creator_ids)) {
  2200. $cond['creator_id'] = ['IN', $creator_ids];
  2201. }
  2202. $fields = 'imei,device_name,device_type,battery_level,loc_mode,loc_ploy,online_time,longitude,latitude,wifi_online_time,wifi_longitude,wifi_latitude,alarm_state';
  2203. $list = M('devices')->field($fields)->where($cond)->select() ?: [];
  2204. $resp = [];
  2205. foreach($list as &$locationInfo){
  2206. //new end
  2207. //wifi_online_time
  2208. //wifi_longitude
  2209. //wifi_latitude
  2210. //addr
  2211. $locationInfo['isAlarm'] = $locationInfo['alarm_state'];
  2212. $isWifi = $locationInfo['wifi_online_time'] > $locationInfo['online_time'] || intval($locationInfo['latitude']) < 1 ;
  2213. $locationInfo['time'] = $isWifi ? $locationInfo['wifi_online_time'] : $locationInfo['online_time'];
  2214. $locationInfo['lat'] = $isWifi ? $locationInfo['wifi_latitude'] : $locationInfo['latitude'];
  2215. $locationInfo['lng'] = $isWifi ? $locationInfo['wifi_longitude'] : $locationInfo['longitude'];
  2216. if(!$locationInfo['time']){
  2217. $locationInfo['awayTime'] = '从未在线';
  2218. }else{
  2219. $timeInterval = time() - (int)$locationInfo['time'];
  2220. $locationInfo['awayTime'] = $this->getHumenTime($timeInterval);
  2221. }
  2222. $lngLatAlter = new \Jms\Algo\Geometry();
  2223. if(!$locationInfo['lat'] || !$locationInfo['lng']){
  2224. continue;
  2225. }
  2226. $latLng = $lngLatAlter->convertBd09ToGcj02($locationInfo['lat'], $locationInfo['lng']);
  2227. $locationInfo['lat'] = $latLng['lat'];
  2228. $locationInfo['lng'] = $latLng['lng'];
  2229. $locationInfo['date_time'] = date('Y-m-d H:i:s', $locationInfo['time']);
  2230. array_push($resp, $locationInfo);
  2231. }
  2232. json_success('获取成功', $resp);
  2233. }
  2234. public function getAllFences( ){
  2235. //user_has_students
  2236. header('Access-Control-Allow-Origin: *');
  2237. $userid =intval(I('get.userid'));
  2238. if(!$userid){
  2239. json_fail('获取不到你的用户标识,请重新登陆');
  2240. }
  2241. $field = 'id, name, fence_shape as shape, creator_id as userid, is_check_in as inAlarm, is_check_out as outAlarm, fence_info as info';
  2242. $cond = [
  2243. 'fence_type' => 0,
  2244. 'creator_id' => $userid,
  2245. '_logic' => 'OR'
  2246. ];
  2247. $res = M('fences')->field($field)->where($cond)->select();
  2248. if(!$res){
  2249. json_fail('查询不到可见的围栏');
  2250. }
  2251. foreach($res as &$v){
  2252. $v['inAlarm'] = (bool)($v['inAlarm']);
  2253. $v['outAlarm'] = (bool)($v['outAlarm']);
  2254. $v['info'] = json_decode($v['info'], true);
  2255. }
  2256. json_success('查询成功', $res);
  2257. }
  2258. public function editUserFence( ){
  2259. //user_has_students
  2260. header('Access-Control-Allow-Origin: *');
  2261. $data = json_decode( file_get_contents("php://input") ,true);
  2262. $userid =intval($data['userid']);
  2263. if(!$userid){
  2264. json_fail('获取不到你的用户标识,请重新登陆');
  2265. }
  2266. $fenceId = intval($data['fenceId']);
  2267. $fenceName = $data['fenceName'];
  2268. if(!$fenceName){
  2269. json_fail('获取不到围栏名');
  2270. }
  2271. $fenceInfo = $data['fenceInfo'];
  2272. if(!$fenceInfo){
  2273. json_fail('获取不到围栏信息');
  2274. }
  2275. $convPoint = (new \Jms\Algo\Geometry())->convertGcj02ToBd09($fenceInfo['center']['lat'], $fenceInfo['center']['lng']);
  2276. $fenceInfo['center']['lat'] = $convPoint['lat'];
  2277. $fenceInfo['center']['lng'] = $convPoint['lng'];
  2278. $saveData = [
  2279. 'name' => $fenceName,
  2280. 'fence_shape' => 'circle',
  2281. 'fence_info' => json_encode($fenceInfo),
  2282. 'updated_at' => time()
  2283. ];
  2284. $res = M('fences')->where(['id' => $fenceId])->save($saveData);
  2285. if(!$res){
  2286. json_fail('保存入库失败');
  2287. }
  2288. json_success('保存入库成功');
  2289. }
  2290. public function getRelationshipList( ){
  2291. header('Access-Control-Allow-Origin: *');
  2292. // 查出 Relationships id
  2293. $where = array('code' => 'Relationships');
  2294. $type_id = M('sys_dict_type')->where($where)->getField('id');
  2295. if (empty($type_id)) {
  2296. json_fail('获取失败');
  2297. }
  2298. $where = array('type_id' => $type_id);
  2299. $list = M('sys_dict_data')->field('code as id, value as text')->where($where)->select();
  2300. if (empty($list)) {
  2301. json_fail('获取失败');
  2302. }
  2303. json_success('获取成功', $list);
  2304. }
  2305. public function addEditUrgent( ){
  2306. //user_has_students
  2307. header('Access-Control-Allow-Origin: *');
  2308. $post_data = file_get_contents('php://input');
  2309. $post_data = json_decode($post_data, true);
  2310. $userid = $post_data['userid'];
  2311. $name = $post_data['name'];
  2312. $phone = $post_data['phone'];
  2313. $relationship = $post_data['relationship'];
  2314. $device_id = $post_data['device_id'];
  2315. $id = $post_data['id'];
  2316. if(!$userid){
  2317. json_fail('获取不到你的用户标识,请重新登陆');
  2318. }
  2319. if(!$device_id){
  2320. json_fail('获取设备信息失败');
  2321. }
  2322. if(!$name || !$phone || !$relationship){
  2323. json_fail('紧急联系人姓名、电话和社会关系不能为空');
  2324. }
  2325. if ($id) {
  2326. $saveData = [
  2327. 'name' => $name,
  2328. 'phone' => $phone,
  2329. 'relationship' => $relationship,
  2330. 'device_id' => $device_id,
  2331. ];
  2332. $res = M('kq_urgent')->createSave(['id'=>$id], $saveData);
  2333. if($res === false){
  2334. json_fail('修改失败');
  2335. }
  2336. json_success('修改成功');
  2337. }
  2338. $saveData = [
  2339. 'name' => $name,
  2340. 'phone' => $phone,
  2341. 'relationship' => $relationship,
  2342. 'creator_id' => $userid,
  2343. 'created_at' => time(),
  2344. 'device_id' => $device_id,
  2345. ];
  2346. $res = M('kq_urgent')->createAdd($saveData);
  2347. if($res === false){
  2348. json_fail('添加失败');
  2349. }
  2350. json_success('添加成功');
  2351. }
  2352. public function getUrgentById( ){
  2353. header('Access-Control-Allow-Origin: *');
  2354. $userid = I('get.userid');
  2355. if(!$userid){
  2356. json_fail('获取不到你的用户标识,请重新登陆');
  2357. }
  2358. $id = I('get.id');
  2359. if(!$id){
  2360. json_fail('获取信息失败');
  2361. }
  2362. $where = array('id'=>$id);
  2363. $res = M('kq_urgent')->where($where)->find();
  2364. if($res === false){
  2365. json_fail('获取失败');
  2366. }
  2367. json_success('获取成功', $res);
  2368. }
  2369. public function deleteUrgent( ){
  2370. header('Access-Control-Allow-Origin: *');
  2371. $userid = I('get.userid');
  2372. if(!$userid){
  2373. json_fail('获取不到你的用户标识,请重新登陆');
  2374. }
  2375. $id = I('get.id');
  2376. if(!$id){
  2377. json_fail('获取信息失败');
  2378. }
  2379. $device_id = I('get.device_id');
  2380. $where = array('id'=>$id, 'device_id'=>$device_id);
  2381. $res = M('kq_urgent')->where($where)->delete();
  2382. if($res === false){
  2383. json_fail('删除失败');
  2384. }
  2385. json_success('删除成功', $res);
  2386. }
  2387. public function editProfile( ){
  2388. header('Access-Control-Allow-Origin: *');
  2389. $openid = I('get.openid');
  2390. $userid = I('get.userid');
  2391. //检测登录状态
  2392. $res = $this->checkLoginState($openid,$userid);
  2393. if(!$res['status']){
  2394. json_fail($res['message']);
  2395. }
  2396. $data = json_decode( file_get_contents("php://input") ,true);
  2397. $data = array_map(function($item){
  2398. return trim($item);
  2399. }, $data);
  2400. if (!$data['nickName']) {
  2401. json_fail('昵称不能为空');
  2402. }
  2403. if (!$data['phone']) {
  2404. json_fail('手机号码不能为空');
  2405. } else {
  2406. if (!preg_match('/^[-_a-zA-Z0-9]{6,16}?$/', $data['phone'])) {
  2407. json_fail('手机号码格式不正确');
  2408. }
  2409. }
  2410. // 修改密码
  2411. if ($data['oldPwd']) {
  2412. if (!$data['newPwd']) {
  2413. json_fail('新密码不能为空');
  2414. }
  2415. if (!$data['confirmNewPwd']) {
  2416. json_fail('确认密码不能为空');
  2417. }
  2418. if ($data['newPwd'] != $data['confirmNewPwd']) {
  2419. json_fail('两次新密码不一致');
  2420. }
  2421. }
  2422. $userCond = array( 'id' => $userid);
  2423. $user_info = M('users')->where($userCond)->find();
  2424. if(!$user_info){
  2425. json_fail('获取用户信息失败');
  2426. }
  2427. if ($user_info['wx_open_id'] != $openid) {
  2428. json_fail('账号可能被其他用户登录,请重新登录');
  2429. }
  2430. if ($data['oldPwd']) {
  2431. if(!password_verify($data['oldPwd'], $user_info['password'])){
  2432. json_fail('原密码不正确');
  2433. }
  2434. }
  2435. $saveData = [
  2436. 'realname' => $data['nickName'],
  2437. 'phone' => $data['phone'],
  2438. ];
  2439. // 有原密码才能修改
  2440. if ($data['oldPwd']) {
  2441. $saveData['password'] = password_hash($data['newPwd'], PASSWORD_DEFAULT);
  2442. }
  2443. $result = M('users')->createSave($userCond, $saveData);
  2444. if(!$result){
  2445. json_fail('修改失败');
  2446. }
  2447. $respData = [
  2448. 'id' => $user_info['id'],
  2449. 'realname' => $data['nickName'],
  2450. 'avatar' => $user_info['avatar'],
  2451. 'phone' => $data['phone'],
  2452. ];
  2453. json_success('修改成功', $respData);
  2454. }
  2455. public function getNewsDetail( ){
  2456. header('Access-Control-Allow-Origin: *');
  2457. $userid = I('get.userid');
  2458. if(!$userid || $userid == 'undefined' || $userid == 'null'){
  2459. json_fail('获取不到你的用户标识,请重新登陆');
  2460. }
  2461. $openid = I('get.openid');
  2462. $id = I('get.id');
  2463. if (!$id) {
  2464. json_fail('缺少参数:id');
  2465. }
  2466. //检测登录状态
  2467. $res=$this->checkLoginState($openid,$userid);
  2468. if(!$res['status']){
  2469. json_fail($res['message']);
  2470. }
  2471. $cond = ['id' => $id];
  2472. $info = M('news')->where($cond)->find();
  2473. if (empty($info)) {
  2474. json_fail('获取信息失败');
  2475. }
  2476. json_success('获取成功', $info);
  2477. }
  2478. public function getActivityDetail( ){
  2479. header('Access-Control-Allow-Origin: *');
  2480. $openid = I('get.openid');
  2481. $userid = I('get.userid');
  2482. if(!$userid || $userid == 'undefined' || $userid == 'null'){
  2483. json_fail('获取不到你的用户标识,请重新登陆');
  2484. }
  2485. //检测登录状态
  2486. $res=$this->checkLoginState($openid,$userid);
  2487. if(!$res['status']){
  2488. json_fail($res['message']);
  2489. }
  2490. $id = I('get.id');
  2491. if (!$id) {
  2492. json_fail('缺少参数:id');
  2493. }
  2494. // 获取详情
  2495. $info = M('activity')->where(['id' => $id])->find();
  2496. if (empty($info)) {
  2497. json_fail('未获取到活动信息');
  2498. }
  2499. $info['updated_at_datetime'] = date('Y-m-d H:i:s',$info['updated_at']);
  2500. $info['start_time'] = date('Y-m-d H:i:s',$info['start_time']);
  2501. $info['end_time'] = date('Y-m-d H:i:s',$info['end_time']);
  2502. $info['creator'] = M('users')->where(['id' => $info['creator_id'] ])->getField('username') ? : 'admin';
  2503. // 获取报名用户数量
  2504. $applyCount = M('activity_has_users')->where(['activity_id' => $id])->count();
  2505. $info['apply_count'] = $applyCount ? $applyCount : 0;
  2506. // 该用户报名状态
  2507. $where = ['activity_id' => $id, 'user_id' => $userid];
  2508. $info['apply_status'] = M('activity_has_users')->where($where)->find() ? 1 : 0;
  2509. json_success('获取成功', $info);
  2510. }
  2511. public function applyActivity( ){
  2512. header('Access-Control-Allow-Origin: *');
  2513. $openid = I('get.openid');
  2514. $userid = I('get.userid');
  2515. if(!$userid || $userid == 'undefined' || $userid == 'null'){
  2516. json_fail('获取不到你的用户标识,请重新登陆');
  2517. }
  2518. //检测登录状态
  2519. $res = $this->checkLoginState($openid,$userid);
  2520. if(!$res['status']){
  2521. json_fail($res['message']);
  2522. }
  2523. $post_data = json_decode(file_get_contents('php://input'), true);
  2524. if (!$post_data['id']) {
  2525. json_fail('未获取到报名活动');
  2526. }
  2527. $courses_model = M('activity');
  2528. $apply_users_model = M('activity_has_users');
  2529. // 检测课程是否发布
  2530. $where = ['id' => $post_data['id']];
  2531. $publish = $courses_model->where($where)->getField('status');
  2532. if (!$publish) {
  2533. json_fail('活动不存在或未发布');
  2534. }
  2535. // 获取设备信息
  2536. $where = ['id' => $userid];
  2537. $user_info = M('users')->where($where)->find();
  2538. if (empty($user_info)) {
  2539. json_fail('获取报名用户信息失败');
  2540. }
  2541. // 检测是否已报名
  2542. // $apply_user_id = $device_info['badge_user_id'] ? : $device_info['user_id'];
  2543. $apply_user_id = $userid;
  2544. $where = ['user_id' => $apply_user_id, 'activity_id' => $post_data['id']];
  2545. if ($apply_users_model->where($where)->count()) {
  2546. json_fail('已报名,请勿重复操作');
  2547. }
  2548. $time = time();
  2549. $save_data = [
  2550. 'activity_id' => $post_data['id'],
  2551. 'user_id'=> $apply_user_id,
  2552. 'apply_time'=> $time,
  2553. 'department_id' => $user_info['department_id'],
  2554. 'creator_id' => $userid,
  2555. 'created_at' => $time,
  2556. ];
  2557. $res = $apply_users_model->createAdd($save_data);
  2558. if (!$res) {
  2559. json_fail('报名失败,请稍后重试');
  2560. }
  2561. json_success('报名成功');
  2562. }
  2563. public function getLastPositionSingle( ){
  2564. header('Access-Control-Allow-Origin: *');
  2565. $imei = I('get.imei');
  2566. if(!$imei){
  2567. json_fail('服务端未检测到设备编号');
  2568. }
  2569. $locationInfo = M('devices')->where(['imei' => $imei])->find();
  2570. if(!$locationInfo){
  2571. json_fail('查询不到该设备当前信息哦!');
  2572. }
  2573. //new start
  2574. $isThirdParty = M('third_party_devices')->where(['device_imei' => $locationInfo['imei']])->count();
  2575. if($isThirdParty){
  2576. $thindLoc = $this->getThirdLocation($locationInfo['imei']);
  2577. if($thindLoc){
  2578. $locationInfo['lng'] = $thindLoc['lon'];
  2579. $locationInfo['lat'] = $thindLoc['lat'];
  2580. $locationInfo['time'] = $thindLoc['receiveAt'];
  2581. }
  2582. }
  2583. //new end
  2584. //wifi_online_time
  2585. //wifi_longitude
  2586. //wifi_latitude
  2587. //addr
  2588. $locationInfo['isAlarm'] = $locationInfo['alarm_status'];
  2589. $isWifi = $locationInfo['wifi_online_time'] > $locationInfo['online_time'] || intval($locationInfo['latitude']) < 1 ;;
  2590. $locationInfo['time'] = $isWifi ? $locationInfo['wifi_online_time'] : $locationInfo['online_time'];
  2591. $locationInfo['lat'] = $isWifi ? $locationInfo['wifi_latitude'] : $locationInfo['latitude'];
  2592. $locationInfo['lng'] = $isWifi ? $locationInfo['wifi_longitude'] : $locationInfo['longitude'];
  2593. if(!$isThirdParty){
  2594. $locationInfo['address'] = $locationInfo['address'];
  2595. }
  2596. //
  2597. if(!$locationInfo['time']){
  2598. $locationInfo['awayTime'] = '从未在线';
  2599. }else{
  2600. $timeInterval = time() - (int)$locationInfo['time'];
  2601. $locationInfo['awayTime'] = $this->getHumenTime($timeInterval);
  2602. }
  2603. $lngLatAlter = new \Jms\Algo\Geometry();
  2604. if($isThirdParty){
  2605. $latLng = $lngLatAlter->wgsTOgcj($locationInfo['lat'], $locationInfo['lng']);
  2606. $locationInfo['lat'] = $latLng['lat'];
  2607. $locationInfo['lng'] = $latLng['lng'];
  2608. }else{
  2609. $latLng = $lngLatAlter->convertBd09ToGcj02($locationInfo['lat'], $locationInfo['lng']);
  2610. $locationInfo['lat'] = $latLng['lat'];
  2611. $locationInfo['lng'] = $latLng['lng'];
  2612. }
  2613. $locationInfo['date_time'] = date('Y-m-d H:i:s', $locationInfo['time']);
  2614. json_success('查询成功', $locationInfo);
  2615. }
  2616. public function yysLogin( ){
  2617. header('Access-Control-Allow-Origin: *');
  2618. //json_fail('登录失败');
  2619. $openid = I('get.openid');
  2620. if(!$openid){
  2621. json_fail('未授权,请关闭当前页面重新进入');
  2622. }
  2623. $data = json_decode( file_get_contents("php://input") ,true);
  2624. // 微信运营角色
  2625. $where = ['identify' => 'wxyy'];
  2626. $role_id = M('roles')->where($where)->getField('id');
  2627. if (!$role_id) {
  2628. json_fail('未开放微信运营角色');
  2629. }
  2630. $cond = array(
  2631. 'username' => $data['username'],
  2632. );
  2633. $user_info = M('users')->alias('u')->where($cond)->join("INNER JOIN user_has_roles AS r ON u.id = r.uid AND r.role_id = {$role_id}")->find();
  2634. if(!$user_info){
  2635. json_fail('无登录权限或账号、密码错误');
  2636. }
  2637. if(!password_verify($data['password'], $user_info['password'])){
  2638. json_fail('账号或密码错误');
  2639. }
  2640. if($user_info['wx_open_id'] != $openid){
  2641. $update = array(
  2642. 'wx_open_id' => $openid,
  2643. 'last_login_ip' => $_SERVER['REMOTE_ADDR'],
  2644. 'last_login_time' => time(),
  2645. );
  2646. $result = M('users')->createSave(array('id'=>$user_info['uid']),$update);
  2647. if(!$result){
  2648. json_fail('登陆失败');
  2649. }
  2650. }
  2651. //如果当前openid与其他账号openid相同,清空其他表中openid
  2652. /*
  2653. $where = array(
  2654. 'wx_open_id' => $openid,
  2655. 'id' => ['NEQ', $user_info['id']],
  2656. );
  2657. $result = M('users')->createSave($where,['wx_open_id'=>'']);
  2658. if(!$result){
  2659. json_fail('登陆失败');
  2660. }
  2661. */
  2662. $res = array(
  2663. 'id'=>$user_info['uid'],
  2664. 'realname'=>$user_info['realname'],
  2665. 'avatar' => $user_info['avatar'],
  2666. 'phone'=>$user_info['phone'],
  2667. );
  2668. json_success('登录成功', $res);
  2669. }
  2670. public function getSlider( ){
  2671. header('Access-Control-Allow-Origin: *');
  2672. //json_fail('登录失败');
  2673. $openid = I('get.openid');
  2674. if(!$openid){
  2675. json_fail('用户未授权');
  2676. }
  2677. $cond = array(
  2678. 'is_enable'=>1
  2679. );
  2680. $list = M('banner')->where($cond)->select();
  2681. if(!$list){
  2682. $list = [
  2683. ['banner_url' => 'https://rlfd.oss-cn-hangzhou.aliyuncs.com/wxt_school/no_banner.jpg']
  2684. ];
  2685. }
  2686. json_success('获取成功',$list);
  2687. }
  2688. public function getWaitHandleAlarmData( ){
  2689. header('Access-Control-Allow-Origin: *');
  2690. $post_data = json_decode(file_get_contents('php://input'), true);
  2691. $openid = $post_data['openid'];
  2692. $userid = $post_data['userid'];
  2693. //检测登录状态
  2694. $res = $this->checkLoginState($openid,$userid);
  2695. if(!$res['status']){
  2696. json_fail($res['message']);
  2697. }
  2698. // 获取登录账号部门下所有告警状态的学生数据
  2699. $user_model = M('users');
  2700. $where = ['id' => $userid];
  2701. $userinfo = $user_model->field('id,department_id')->where($where)->find();
  2702. // 获取权限范围内的用户id
  2703. $creator_ids = $this->getRightCreatorIds($userinfo);
  2704. if ($creator_ids === false) {
  2705. json_fail('获取数据失败');
  2706. }
  2707. $limit = isset($post_data['limit']) ? $post_data['limit'] : 10;
  2708. $page = isset($post_data['page']) ? $post_data['page'] : 1;
  2709. // 查出告警学生id
  2710. $cond = [
  2711. 'alarm_status' => 1,
  2712. ];
  2713. if (is_array($creator_ids)) {
  2714. $cond['creator_id'] = ['IN', $creator_ids];
  2715. }
  2716. // 查出总数量
  2717. $student_list = [];
  2718. $total = $user_model->where($cond)->count();
  2719. if ($total) {
  2720. $fields = 'id,avatar,realname,sex,age,active_rfid,student_no,online_time,last_station_mac,longitude,latitude';
  2721. $student_list = $user_model->field($fields)->where($cond)->limit($post_data['limit'])->page($post_data['page'])->select() ?: [];
  2722. }
  2723. // 返回
  2724. $data = [
  2725. 'total' => $total,
  2726. 'limit' => $limit,
  2727. 'page' => $page,
  2728. 'list' => $student_list,
  2729. ];
  2730. json_success('获取成功', $data);
  2731. }
  2732. private function getRightCreatorIds( $userinfo ){
  2733. $uid = $userinfo['id'];
  2734. $dept_id = $userinfo['department_id'];
  2735. // 非系统角色
  2736. $where = ['identify' => ['IN', 'personal,group_badge_user,group_card_user']];
  2737. $devides_user_ids = M('roles')->where($where)->getField('id', true);
  2738. // 查出拥有系统角色
  2739. $where = [
  2740. 'uid' => $uid,
  2741. 'role_id' => ['NOT IN', $devides_user_ids],
  2742. ];
  2743. $role_ids = M('user_has_roles')->where($where)->getField('role_id', true);
  2744. if (empty($role_ids)) {
  2745. return false;
  2746. }
  2747. $user_ids = [];
  2748. $is_all = false; // 全部数据
  2749. // 查出角色拥有权限
  2750. $where = ['id' => ['IN', $role_ids]];
  2751. $range_list = M('roles')->distinct(true)->where($where)->getField('data_range', true);
  2752. if (empty($range_list)) {
  2753. return false;
  2754. }
  2755. // 根据角色权限查出
  2756. $depart_model = M('departments');
  2757. foreach($range_list as $range) {
  2758. switch ($range) {
  2759. case '1': // 全部数据
  2760. $is_all = true;
  2761. break;
  2762. case '2': // 自定义数据
  2763. case '3': // 本人数据
  2764. $user_ids[] = $uid;
  2765. break;
  2766. case '4': // 部门数据
  2767. $user_ids = array_merge($user_ids, $this->getUserIdsByDepartmentId([$dept_id]));
  2768. break;
  2769. case '5': // 部门及以下数据
  2770. // 查一下下级部门
  2771. // REGEXP '(^76$)|(^76-)'
  2772. //$departmentIds = $depart_model->where(['parent_id' => $dept_id])->getField('id', true);
  2773. $cond = ['level' => ['EXP',"REGEXP '(^{$dept_id}$)|(^{$dept_id}-)'"]];
  2774. $departmentIds = $depart_model->where($cond)->getField('id', true) ?: [];
  2775. $departmentIds[] = $dept_id;
  2776. $user_ids = array_merge([$uid], $this->getUserIdsByDepartmentId($departmentIds));
  2777. break;
  2778. default:
  2779. break;
  2780. }
  2781. // 如果有全部数据 直接跳出
  2782. if ($is_all) {
  2783. break;
  2784. }
  2785. }
  2786. if ($is_all) {
  2787. return true;
  2788. }
  2789. return array_unique(array_filter($user_ids));
  2790. }
  2791. private function getUserIdsByDepartmentId( $id ){
  2792. // 取出非设备用户 (roles identify not in 'personal,group_badge_user,group_card_user')
  2793. $where = ['identify' => ['IN', 'personal,group_badge_user,group_card_user']];
  2794. $devide_role_ids = M('roles')->where($where)->getField('id', true);
  2795. $devide_role_ids = join(',', $devide_role_ids);
  2796. $cond = [
  2797. 'u.department_id' => ['IN', $id],
  2798. ];
  2799. $ids = M('users')->alias('u')
  2800. ->field('u.id')
  2801. ->where($cond)
  2802. ->join("INNER JOIN user_has_roles r ON u.id=r.uid AND r.role_id NOT IN ({$devide_role_ids})")
  2803. ->select() ? : [];
  2804. return array_unique(array_column($ids, 'id'));
  2805. }
  2806. public function getConfirmAlarmData( ){
  2807. header('Access-Control-Allow-Origin: *');
  2808. $post_data = json_decode(file_get_contents('php://input'), true);
  2809. $openid = $post_data['openid'];
  2810. if(!$openid){
  2811. json_fail('未获取到微信授权信息,请重新点击菜单进入并授权');
  2812. }
  2813. $limit = isset($post_data['limit']) ? $post_data['limit'] : 10;
  2814. $page = isset($post_data['page']) ? $post_data['page'] : 1;
  2815. // 获取登录账号部门下所有告警状态的学生数据
  2816. $where = ['wx_open_id' => $openid];
  2817. $userinfo = M('users')->field('id,department_id')->where($where)->find();
  2818. // 获取权限范围内的用户id
  2819. $creator_ids = $this->getRightCreatorIds($userinfo);
  2820. if ($creator_ids === false) {
  2821. json_fail('获取失败');
  2822. }
  2823. // 查出告警学生id
  2824. $cond = [
  2825. 'result' => 1,
  2826. ];
  2827. if (is_array($creator_ids)) {
  2828. $cond['creator_id'] = ['IN', $creator_ids];
  2829. }
  2830. // 查出总数量
  2831. $record_list = [];
  2832. $alarm_handle_records_model = M('alarm_handle_records');
  2833. $total = $alarm_handle_records_model->where($cond)->count();
  2834. if ($total) {
  2835. $record_list = $alarm_handle_records_model->where($cond)->order('created_at desc')->limit($post_data['limit'])->page($post_data['page'])->select() ?: [];
  2836. }
  2837. // 返回
  2838. $data = [
  2839. 'total' => $total,
  2840. 'limit' => $limit,
  2841. 'page' => $page,
  2842. 'list' => $record_list,
  2843. ];
  2844. json_success('获取成功', $data);
  2845. }
  2846. public function getCancelAlarmData( ){
  2847. header('Access-Control-Allow-Origin: *');
  2848. $post_data = json_decode(file_get_contents('php://input'), true);
  2849. $openid = $post_data['openid'];
  2850. if(!$openid){
  2851. json_fail('未获取到微信授权信息,请重新点击菜单进入并授权');
  2852. }
  2853. $limit = isset($post_data['limit']) ? $post_data['limit'] : 10;
  2854. $page = isset($post_data['page']) ? $post_data['page'] : 1;
  2855. // 获取登录账号部门下所有告警状态的学生数据
  2856. $where = ['wx_open_id' => $openid];
  2857. $userinfo = M('users')->field('id,department_id')->where($where)->find();
  2858. // 获取权限范围内的用户id
  2859. $creator_ids = $this->getRightCreatorIds($userinfo);
  2860. if ($creator_ids === false) {
  2861. json_fail('获取失败');
  2862. }
  2863. // 查出告警学生id
  2864. $cond = [
  2865. 'result' => 2,
  2866. ];
  2867. if (is_array($creator_ids)) {
  2868. $cond['creator_id'] = ['IN', $creator_ids];
  2869. }
  2870. // 查出总数量
  2871. $record_list = [];
  2872. $alarm_handle_records_model = M('alarm_handle_records');
  2873. $total = $alarm_handle_records_model->where($cond)->count();
  2874. if ($total) {
  2875. $record_list = $alarm_handle_records_model->where($cond)->order('created_at desc')->limit($post_data['limit'])->page($post_data['page'])->select() ?: [];
  2876. }
  2877. // 返回
  2878. $data = [
  2879. 'total' => $total,
  2880. 'limit' => $limit,
  2881. 'page' => $page,
  2882. 'list' => $record_list,
  2883. ];
  2884. json_success('获取成功', $data);
  2885. }
  2886. public function getFenceAlarmData( ){
  2887. header('Access-Control-Allow-Origin: *');
  2888. $post_data = json_decode(file_get_contents('php://input'), true);
  2889. $openid = $post_data['openid'];
  2890. if(!$openid){
  2891. json_fail('未获取到微信授权信息,请重新点击菜单进入并授权');
  2892. }
  2893. $limit = isset($post_data['limit']) ? $post_data['limit'] : 10;
  2894. $page = isset($post_data['page']) ? $post_data['page'] : 1;
  2895. // 获取登录账号部门下所有告警状态的学生数据
  2896. $where = ['wx_open_id' => $openid];
  2897. $userinfo = M('users')->field('id,department_id')->where($where)->find();
  2898. // 获取权限范围内的用户id
  2899. $creator_ids = $this->getRightCreatorIds($userinfo);
  2900. if ($creator_ids === false) {
  2901. json_fail('获取失败');
  2902. }
  2903. // 查出围栏
  2904. $cond = [];
  2905. if (!$post_data['type']) {
  2906. $cond['alarm_type'] = ['IN',['fence_in','fence_out']];
  2907. } elseif ($post_data['type'] == 'in') {
  2908. $cond['alarm_type'] = 'fence_in';
  2909. } elseif ($post_data['type'] == 'out') {
  2910. $cond['alarm_type'] = 'fence_out';
  2911. } else {
  2912. json_fail('未知的围栏类型');
  2913. }
  2914. if (is_array($creator_ids)) {
  2915. $cond['creator_id'] = ['IN', $creator_ids];
  2916. }
  2917. // 查出总数量
  2918. $list = [];
  2919. $alarm_report = M('alarm_report');
  2920. $total = $alarm_report->where($cond)->count();
  2921. if ($total) {
  2922. $list = $alarm_report->where($cond)->order('created_at desc')->limit($post_data['limit'])->page($post_data['page'])->select() ?: [];
  2923. }
  2924. // 返回
  2925. $data = [
  2926. 'total' => $total,
  2927. 'limit' => $limit,
  2928. 'page' => $page,
  2929. 'list' => $list,
  2930. ];
  2931. json_success('获取成功', $data);
  2932. }
  2933. public function getYysFences( ){
  2934. // 获取登录用户信息
  2935. $userinfo = $this->getYysLoginUserinfo();
  2936. // 获取权限范围内的用户id
  2937. $creator_ids = $this->getRightCreatorIds($userinfo);
  2938. if ($creator_ids === false) {
  2939. json_fail('获取失败');
  2940. }
  2941. $fence_type = I('get.type');
  2942. if (!$fence_type && $fence_type !== 0 && $fence_type !== '0') {
  2943. json_fail('未获取到围栏类型');
  2944. }
  2945. $cond = [
  2946. 'fence_type' => $fence_type,
  2947. ];
  2948. // 查出总数量
  2949. if (is_array($creator_ids)) {
  2950. $cond['creator_id'] = ['IN', $creator_ids];
  2951. }
  2952. $field = 'id, name, fence_shape as shape, creator_id as userid, is_check_in as inAlarm, is_check_out as outAlarm, fence_info as fenceInfo';
  2953. $list = M('fences')->field($field)->where($cond)->select();
  2954. if(!$list){
  2955. json_fail('暂无围栏');
  2956. }
  2957. $geometry = new \Jms\Algo\Geometry();
  2958. foreach($list as &$v){
  2959. $v['inAlarm'] = (bool)($v['inAlarm']);
  2960. $v['outAlarm'] = (bool)($v['outAlarm']);
  2961. // 坐标转换:GCJ-02 -> BD-09
  2962. $v['fenceInfo'] = json_decode($v['fenceInfo'], true) ?:[];
  2963. $convPoint = $geometry->convertBd09ToGcj02($v['fenceInfo']['center']['lat'], $v['fenceInfo']['center']['lng']);
  2964. $v['fenceInfo']['center']['lat'] = $convPoint['lat'];
  2965. $v['fenceInfo']['center']['lng'] = $convPoint['lng'];
  2966. }
  2967. json_success('获取成功', $list);
  2968. }
  2969. public function recorder_wxfunc( ){
  2970. header('Access-Control-Allow-Origin: *');
  2971. $action = I('post.action');
  2972. if($action === 'sign'){
  2973. $url=I('post.url');
  2974. $res = $this->getRecSignature($url);
  2975. echo json_encode($res);
  2976. }
  2977. if($action === 'wxdown'){
  2978. $mediaId = I('post.mediaID');
  2979. $res = $this->getWxRecByMid($mediaId);
  2980. // echo json_encode(['c' => 0]);
  2981. echo json_encode($res);
  2982. }
  2983. }
  2984. private function getRecSignature( $url ){
  2985. header('Access-Control-Allow-Origin: *');
  2986. $url = urldecode($url);
  2987. $accessToken = $this->getWeixinAccessToken();
  2988. if (!$accessToken) {
  2989. return [
  2990. 'c' => 99,
  2991. 'm' => '获取微信accesstoken失败'
  2992. ];
  2993. }
  2994. $apiTiket = $this->getJsapiTikcet($accessToken);
  2995. if(is_array($apiTiket)){
  2996. json_fail($apiTiket['errcode'].':'.$apiTiket['errmsg']);
  2997. return [
  2998. 'c' => $apiTiket['errcode'],
  2999. 'm' => $apiTiket['errmsg']
  3000. ];
  3001. }
  3002. $noncestr = substr(md5(time()), 0, 16);
  3003. $timestamp = time();
  3004. //jsapi_ticket=sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg&noncestr=Wm3WZYTPz0wzccnW&timestamp=1414587457&url=http://mp.weixin.qq.com?params=value
  3005. $str = 'jsapi_ticket='.$apiTiket.'&noncestr='.$noncestr.'&timestamp='.$timestamp.'&url='.$url;
  3006. $signature = sha1($str);
  3007. return [
  3008. 'c' => 0,
  3009. 'm' => '',
  3010. 'v' => [
  3011. 'appid' => C('WECHAT_APPID'),
  3012. 'timestamp' => $timestamp,
  3013. 'noncestr' => $noncestr,
  3014. 'signature' => $signature,
  3015. ]
  3016. ];
  3017. }
  3018. public function addEditFence( ){
  3019. // 获取登录用户信息
  3020. $userinfo = $this->getYysLoginUserinfo();
  3021. $id = I('get.id');
  3022. $data = json_decode( file_get_contents("php://input") ,true) ?: [];
  3023. // 围栏类型
  3024. $fenceTypeArr = [
  3025. 'inout' => 0, // 机构安全围栏
  3026. 'person_inout' => 1, // 个人安全围栏
  3027. 'sos' => 2, // SOS围栏
  3028. ];
  3029. if ($data['name']) {
  3030. $data['name'] = trim($data['name']);
  3031. }
  3032. if ($data['fence_info']) {
  3033. // 坐标转换:GCJ-02 -> BD-09
  3034. $convPoint = (new \Jms\Algo\Geometry())->convertGcj02ToBd09($data['fence_info']['center']['lat'], $data['fence_info']['center']['lng']);
  3035. $data['fence_info']['center']['lat'] = $convPoint['lat'];
  3036. $data['fence_info']['center']['lng'] = $convPoint['lng'];
  3037. $data['fence_info'] = json_encode($data['fence_info']);
  3038. }
  3039. if ($data['in_fence_time_area'] && is_array($data['in_fence_time_area'])) {
  3040. $data['in_fence_time_area'] = json_encode($data['in_fence_time_area']);
  3041. }
  3042. if ($data['out_fence_time_area'] && is_array($data['out_fence_time_area'])) {
  3043. $data['out_fence_time_area'] = json_encode($data['out_fence_time_area']);
  3044. }
  3045. if ($data['push_users'] && is_array($data['push_users'])) {
  3046. foreach($data['push_users'] as &$v){
  3047. $v = intval($v);
  3048. }
  3049. $data['push_users'] = json_encode($data['push_users']);
  3050. }
  3051. // 新增
  3052. if (!$id) {
  3053. if($data['name'] === ''){
  3054. json_fail('围栏名称不能为空');
  3055. }
  3056. if(!$data['fence_info']){
  3057. json_fail('未获取到围栏信息');
  3058. }
  3059. if (!isset($fenceTypeArr[$data['fence_type']])) {
  3060. json_fail('未知的围栏类型');
  3061. }
  3062. $data['fence_shape'] = $data['fence_shape'] ?: 'circle';
  3063. $data['fence_type'] = $fenceTypeArr[$data['fence_type']];
  3064. $data['creator_id'] = $userinfo['id'];
  3065. $data['departments'] = $userinfo['department_id'];
  3066. $data['created_at'] = time();
  3067. $result = M('fences')->createAdd($data);
  3068. if (!$result) {
  3069. json_fail('添加失败');
  3070. }
  3071. json_success('添加成功', $result);
  3072. }
  3073. // 修改
  3074. // 获取权限范围内的用户id
  3075. $creatorIds = $this->getRightCreatorIds($userinfo);
  3076. $info = M('fences')->where(['id'=>$id])->find();
  3077. if (empty($info)) {
  3078. json_fail('获取围栏信息失败');
  3079. }
  3080. if (!in_array($info['creator_id'], $creatorIds)) {
  3081. //json_fail('无修改权限');
  3082. }
  3083. unset($data['fence_type']);
  3084. $cond = ['id'=>$id];
  3085. $result = M('fences')->createSave($cond, $data);
  3086. if(!$result){
  3087. json_fail('保存失败');
  3088. }
  3089. json_success('保存成功', $id);
  3090. }
  3091. public function deleteYysFences( ){
  3092. // 获取登录用户信息
  3093. $userinfo = $this->getYysLoginUserinfo();
  3094. // 获取权限范围内的用户id
  3095. $creator_ids = $this->getRightCreatorIds($userinfo);
  3096. if ($creator_ids === false) {
  3097. json_fail('获取失败');
  3098. }
  3099. $id = I('get.id');
  3100. if (!$id) {
  3101. json_fail('参数不合法');
  3102. }
  3103. $fence_info = M('fences')->where(['id'=>$id])->find();
  3104. if (!$fence_info) {
  3105. json_fail('围栏不存在');
  3106. }
  3107. if ($creator_ids !== true && !in_array($fence_info['creator_id'], $creator_ids)) {
  3108. json_fail('无权限删除该围栏');
  3109. }
  3110. $res = M('fences')->where(['id'=>$id])->delete();
  3111. if (!$res) {
  3112. json_fail('删除失败');
  3113. }
  3114. json_success('删除成功');
  3115. }
  3116. public function getFenceInfo( ){
  3117. // 获取登录用户信息
  3118. $userinfo = $this->getYysLoginUserinfo();
  3119. $id = I('get.id');
  3120. if (!$id) {
  3121. json_fail('缺少参数');
  3122. }
  3123. $info = M('fences')->where(['id'=>$id])->find();
  3124. if(!$info){
  3125. json_fail('获取失败');
  3126. }
  3127. // GCJ-02 -> BD-09
  3128. $info['fence_info'] = json_decode($info['fence_info'], true) ?: [];
  3129. $convPoint = (new \Jms\Algo\Geometry())->convertBd09ToGcj02($info['fence_info']['center']['lat'], $info['fence_info']['center']['lng']);
  3130. $info['fence_info']['center']['lat'] = $convPoint['lat'];
  3131. $info['fence_info']['center']['lng'] = $convPoint['lng'];
  3132. $info['in_fence_time_area'] = json_decode($info['in_fence_time_area'], true) ?: [];
  3133. $info['out_fence_time_area'] = json_decode($info['out_fence_time_area'], true) ?: [];
  3134. $info['push_users'] = json_decode($info['push_users'], true) ?: [];
  3135. json_success('获取成功', $info);
  3136. }
  3137. private function getWxRecByMid( $mediaId ){
  3138. $accesstoken = $this->getWeixinAccessToken();
  3139. $url = 'https://api.weixin.qq.com/cgi-bin/media/get/jssdk?access_token='.$accesstoken.'&media_id='.$mediaId;
  3140. $ch = curl_init();
  3141. curl_setopt($ch, CURLOPT_URL, $url);
  3142. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  3143. $output = curl_exec($ch);
  3144. //var_dump($decodeRes);
  3145. //var_dump($output);
  3146. //$output = json_decode(curl_exec($ch), true);
  3147. curl_close($ch);
  3148. $decodeRes = json_decode($output, true);
  3149. if(is_array($decodeRes) && $decodeRes['errcode']){
  3150. return [
  3151. 'c' => $decodeRes['errcode'],
  3152. 'm' => $decodeRes['errmsg'],
  3153. ];
  3154. }
  3155. $transformResPath = $this->wxSpeex2wav($output, $mediaId);
  3156. $fileRes = file_get_contents($transformResPath);
  3157. if($fileRes === false){
  3158. return [
  3159. 'c' => 91,
  3160. 'm' => '音频转码失败',
  3161. ];
  3162. }
  3163. $duration = $this->getVoiceTime($transformResPath);
  3164. unlink($transformResPath);
  3165. return [
  3166. 'c' => 0,
  3167. 'm' => '',
  3168. 'v' => [
  3169. 'mime'=> 'audio/wav',
  3170. 'data' => base64_encode($fileRes),
  3171. 'duration' => $duration
  3172. ]
  3173. ];
  3174. }
  3175. private function wxSpeex2wav( $audioContent, $mediaId ){
  3176. //$speex2wav = " speex2wav ".$file['storePath']."/a.speex ".$file['storePath']."/b.mp3";
  3177. $dirPath = realpath(__ROOT__).'/static/assets/wav/'.date('Ymd').'/transform_temp/';
  3178. if (!is_dir($dirPath)){
  3179. mkdir($dirPath,0777,true);
  3180. }
  3181. $speexPath = $dirPath . $mediaId . '.speex';
  3182. $fp = fopen($speexPath, 'a');
  3183. fwrite($fp, $audioContent);
  3184. fclose($fp);
  3185. $tempWavPath = $dirPath . $mediaId . "_temp.wav";
  3186. $cmd1 = " speex2wav ".$speexPath .' '. $tempWavPath;
  3187. //$time = $this->getVoiceTime($uploadfile);
  3188. exec($cmd1);
  3189. $wavPath = $dirPath . $mediaId . ".wav";
  3190. $cmd2 = " ffmpeg -i ".$tempWavPath. " -b:a 128k -ar 8000 -ac 1 ". $wavPath;
  3191. exec($cmd2);
  3192. unlink($tempWavPath);
  3193. unlink($speexPath);
  3194. return $wavPath;
  3195. }
  3196. public function getFenceAlarmIntervals( ){
  3197. header('Access-Control-Allow-Origin: *');
  3198. $userid = I('get.userid');
  3199. $openid = I('get.openid');
  3200. if(!$userid || $userid == 'undefined' || $userid == 'null'){
  3201. json_fail('未获取到用户标识,请重新登陆');
  3202. }
  3203. //检测登录状态
  3204. $res = $this->checkLoginState($openid, $userid);
  3205. if(!$res['status']){
  3206. json_fail($res['message']);
  3207. }
  3208. try {
  3209. $typeid = M('sys_dict_type')->where(['code'=>'FenceSentInterval'])->getField('id');
  3210. $list = M('sys_dict_data')->field('value as text,code as value')->where(['type_id'=>$typeid])->select() ? : [];
  3211. } catch (\Exception $e) {
  3212. json_fail($e->getMessage());
  3213. }
  3214. json_success('获取成功', $list);
  3215. }
  3216. public function getRightUsers( ){
  3217. // 获取登录用户信息
  3218. $userinfo = $this->getYysLoginUserinfo();
  3219. // 获取权限范围内的用户id
  3220. $creator_ids = $this->getRightCreatorIds($userinfo);
  3221. if ($creator_ids === false) {
  3222. json_fail('获取失败');
  3223. }
  3224. $cond = [
  3225. 'username' => ['NOT IN', 'admin,manage']
  3226. ];
  3227. if (is_array($creator_ids)) {
  3228. $cond['id'] = ['IN', $creator_ids];
  3229. }
  3230. if ($creator_ids === true) {
  3231. $where = ['identify' => ['IN', 'personal,group_badge_user,group_card_user']];
  3232. $devide_user_ids = M('roles')->where($where)->getField('id', true);
  3233. $device_ids = join(',', $devide_user_ids);
  3234. $list = M('users')->alias('u')->field('u.id,u.realname,u.phone')
  3235. ->where($cond)
  3236. ->distinct(true)
  3237. ->join("INNER JOIN user_has_roles r ON u.id=r.uid AND r.role_id NOT IN ({$device_ids})")
  3238. ->select() ?:[];
  3239. } else {
  3240. $list = M('users')->field('id,realname,phone')->distinct(true)->where($cond)->select();
  3241. }
  3242. json_success('获取成功', $list);
  3243. }
  3244. public function getUserAlarmRecords( ){
  3245. header('Access-Control-Allow-Origin: *');
  3246. $post_data = json_decode(file_get_contents('php://input'), true);
  3247. $openid = $post_data['openid'];
  3248. $userid = $post_data['userid'];
  3249. //检测登录状态
  3250. $res = $this->checkLoginState($openid,$userid);
  3251. if(!$res['status']){
  3252. json_fail($res['message']);
  3253. }
  3254. $limit = isset($post_data['limit']) ? $post_data['limit'] : 10;
  3255. $page = isset($post_data['page']) ? $post_data['page'] : 1;
  3256. // 告警记录条件
  3257. $where = ['user_id' => $userid];
  3258. $device_numbers = M('devices')->where($where)->getField('imei', true);
  3259. if (empty($device_numbers)) {
  3260. json_fail('暂无设备,请先绑定设备');
  3261. }
  3262. // 初始化条件
  3263. $cond = [
  3264. 'device_number' => ['IN', $device_numbers],
  3265. ];
  3266. $reason = I('get.reason');
  3267. $state = I('get.state');
  3268. $result = I('get.result');
  3269. if ($reason) {
  3270. if ($reason == 'fence') {
  3271. $cond['alarm_reason'] = ['IN', ['fence_in', 'fence_out']];
  3272. } else {
  3273. $cond['alarm_reason'] = $reason;
  3274. }
  3275. } else {
  3276. // 默认只显示 SOS
  3277. $cond['alarm_reason'] = 'press';
  3278. }
  3279. if ($state) {
  3280. $cond['state'] = $state;
  3281. }
  3282. if ($result !== '' && is_numeric($result)) {
  3283. $cond['result'] = $result;
  3284. }
  3285. // 查出总数量
  3286. $list = [];
  3287. $total = M('alarm_records')->where($cond)->count();
  3288. if ($total) {
  3289. if ($state == 'start' || $state == 'end') {
  3290. $order = "{$state}_time desc";
  3291. } else {
  3292. $order = 'id desc';
  3293. }
  3294. $fields = 'id,device_number,alarm_reason,handler_id,start_time,end_time,state,comment,result';
  3295. $list = M('alarm_records')->field($fields)->where($cond)->limit($limit)->page($page)->order($order)->select() ?: [];
  3296. if (!empty($list)) {
  3297. // alarm_reason,handler_id,result
  3298. // 获取告警类型
  3299. $typeid = M('sys_dict_type')->where(['code' => 'AlarmType'])->getField('id');
  3300. $reason_arr = M('sys_dict_data')->where(['type_id' => $typeid])->getField('code, value');
  3301. // 处理结果
  3302. $result_arr = [
  3303. 0 => '待处理',
  3304. 1 => '已处理',
  3305. 2 => '误报',
  3306. ];
  3307. // 中文转换
  3308. foreach ($list as &$alarm) {
  3309. $alarm['start_time'] = $alarm['start_time'] ? date('Y-m-d H:i:s', $alarm['start_time']) : '';
  3310. $alarm['end_time'] = $alarm['end_time'] ? date('Y-m-d H:i:s', $alarm['end_time']) : '';
  3311. if (is_array($reason_arr)) {
  3312. $alarm['alarm_reason'] = $reason_arr[$alarm['alarm_reason']];
  3313. }
  3314. if (is_numeric($alarm['result'])) {
  3315. $alarm['result'] = $result_arr[$alarm['result']];
  3316. }
  3317. }
  3318. }
  3319. }
  3320. // 返回
  3321. $data = [
  3322. 'total' => $total,
  3323. 'limit' => $limit,
  3324. 'page' => $page,
  3325. 'list' => $list,
  3326. ];
  3327. json_success('获取成功', $data);
  3328. }
  3329. public function getUserAlarmDetail( ){
  3330. header('Access-Control-Allow-Origin: *');
  3331. $openid = I('get.openid');
  3332. $userid = I('get.userid');
  3333. if(!$userid || $userid == 'undefined' || $userid == 'null'){
  3334. json_fail('获取不到你的用户标识,请重新登陆');
  3335. }
  3336. //检测登录状态
  3337. $res = $this->checkLoginState($openid,$userid);
  3338. if(!$res['status']){
  3339. json_fail($res['message']);
  3340. }
  3341. $alarm_id = I('get.id');
  3342. if (!$alarm_id) {
  3343. json_fail('获取告警信息失败1');
  3344. }
  3345. // 查出告警数据
  3346. $where = ['id' => $alarm_id];
  3347. $alarm_info = M('alarm_records')->where($where)->find();
  3348. if (empty($alarm_info)) {
  3349. json_fail('获取告警信息失败2');
  3350. }
  3351. $report_id = I('get.rid');
  3352. if (!$report_id) {
  3353. json_fail('获取告警信息失败3');
  3354. }
  3355. $where = ['id' => $report_id];
  3356. $alarm_report = M('alarm_report')->where($where)->find();
  3357. if (empty($alarm_report)) {
  3358. json_fail('获取告警信息失败4');
  3359. }
  3360. $alarm_info['address']=$alarm_report['address'];
  3361. // 查出设备信息
  3362. $where = ['imei' => $alarm_info['device_number']];
  3363. $device_info = M('devices')->where($where)->find();
  3364. if (empty($alarm_info)) {
  3365. json_fail('获取告警设备信息失败');
  3366. }
  3367. if ($device_info['online_time'] > $device_info['wifi_online_time']) {
  3368. $device_info['last_online_time'] = date('Y-m-d H:i:s', $device_info['online_time']);
  3369. $device_info['last_location'] = $device_info['longitude'] .','. $device_info['latitude'];
  3370. $res = bmap_geocoding($device_info['latitude'], $device_info['longitude']);
  3371. if($res['success']){
  3372. $device_info['address'] = $res['address'];
  3373. }else{
  3374. $device_info['address']='';
  3375. }
  3376. } elseif ($device_info['online_time'] < $device_info['wifi_online_time']) {
  3377. $device_info['last_online_time'] = date('Y-m-d H:i:s', $device_info['wifi_online_time']);
  3378. $device_info['last_location'] = $device_info['wifi_longitude'] .','. $device_info['wifi_latitude'];
  3379. } else {
  3380. $device_info['last_online_time'] = '0000-00-00 00:00:00';
  3381. $device_info['last_location'] = '';
  3382. $device_info['address']='';
  3383. }
  3384. // 查出设备用户
  3385. if ($device_info['badge_user_id']) {
  3386. $where = ['id' => $device_info['badge_user_id']];
  3387. $user_info = M('badgeuser')->where($where)->find() ?: [];
  3388. } else {
  3389. $where = ['id' => $device_info['user_id']];
  3390. $user_info = M('users')->where($where)->find() ?: [];
  3391. }
  3392. //if (empty($user_info)) {
  3393. //json_fail('获取告警用户信息失败');
  3394. //}
  3395. // 获取紧急联系人
  3396. $where = ['device_id' => $device_info['id']];
  3397. $urgent_list = M('kq_urgent')->where($where)->select() ? : [];
  3398. if (!empty($urgent_list)) {
  3399. $type_id = M('sys_dict_type')->where(['code' => 'Relationships'])->getField('id');
  3400. $ships = M('sys_dict_data')->where(['type_id' => $type_id])->getField('code,value');
  3401. foreach ($urgent_list as $key => &$urgent) {
  3402. $urgent['relationship_text'] = $ships[$urgent['relationship']];
  3403. }
  3404. }
  3405. // 部门名称
  3406. $alarm_info['department_name'] = M('departments')->where(['id' => $user_info['department_id']])->getField('department_name');
  3407. $alarm_info['user_info'] = $user_info;
  3408. $alarm_info['urgent_list'] = $urgent_list;
  3409. $alarm_info['device_info'] = $device_info;
  3410. $alarm_info['device_info']['department_name'] = $alarm_info['department_name'];
  3411. json_success('获取成功', $alarm_info);
  3412. }
  3413. public function setFenceAlarmInterval( ){
  3414. //user_has_students
  3415. header('Access-Control-Allow-Origin: *');
  3416. $fenceId = I('get.fenceId');
  3417. $interval = intval(I('get.interval') );
  3418. if(!$fenceId){
  3419. json_fail('获取不到围栏标识,请刷新下');
  3420. }
  3421. if(!$interval){
  3422. json_fail('获取不到时间间隔,请重设重试');
  3423. }
  3424. if($interval < 1 || $interval > 720){
  3425. json_fail('推送间隔目前只支持720分钟以内');
  3426. }
  3427. $saveData = [
  3428. 'sent_interval' => $interval
  3429. ];
  3430. $saveData['sent_interval'] = $interval * 60;
  3431. $res = M('fences')->where(['id' => $fenceId])->save($saveData);
  3432. if($res === false){
  3433. json_fail('修改推送间隔失败');
  3434. }
  3435. json_success('修改推送间隔成功');
  3436. }
  3437. public function resetPwd( ){
  3438. header('Access-Control-Allow-Origin: *');
  3439. // 通过手机号和短信验证码注册
  3440. $data = json_decode( file_get_contents("php://input") ,true);
  3441. // 用户类型:个人(personal)、团体(group)
  3442. $userType = $data['userType'];
  3443. // 设备类型:卡牌(card)、徽章(badge)
  3444. $deviceType = $data['deviceType'];
  3445. // 团体卡牌用户(group_card_user) 不可注册
  3446. // 个人用户(personal),团体徽章用户(group_badge_user) 可注册
  3447. if ($userType == 'personal') { // 个人用户
  3448. $identify = 'personal';
  3449. } elseif ($userType == 'group' && $deviceType == 'badge') { // 团体徽章用户
  3450. $identify = 'group_badge_user';
  3451. } elseif ($userType == 'group' && $deviceType == 'card') { // 团体卡牌用户
  3452. $identify = 'group_card_user';
  3453. } else {
  3454. json_fail('未知用户类型');
  3455. }
  3456. if(!$data['phone']){
  3457. json_fail('手机号码不能为空');
  3458. }
  3459. if(!$data['smsCode']){
  3460. json_fail('短信验证码不能为空');
  3461. }
  3462. if(! $data['password']){
  3463. json_fail('密码不能为空');
  3464. }
  3465. if(! $data['confirmPassword']){
  3466. json_fail('确认密码不能为空');
  3467. }
  3468. if ($data['password'] != $data['confirmPassword']) {
  3469. json_fail('两次密码不一致');
  3470. }
  3471. // 获取角色id
  3472. $roleId = M('roles')->where(['identify' => $identify])->getField('id');
  3473. if (!$roleId) {
  3474. json_fail('未知角色类型');
  3475. }
  3476. // 判断角色、号码是否已注册
  3477. $userinfo = M('users')->alias('a')->field('a.id')->where(['username'=>$data['phone']])->join("INNER JOIN user_has_roles b ON a.id = b.uid AND b.role_id = {$roleId}")->find();
  3478. if (empty($userinfo)) {
  3479. json_fail('该号码未注册');
  3480. }
  3481. // 验证码有效性
  3482. $res = $this->isValidSmsCode($data['phone'], $data['smsCode']);
  3483. if (!$res['success']) {
  3484. json_fail($res['message']);
  3485. }
  3486. // 重置密码
  3487. $savePwd = password_hash($data['password'], PASSWORD_DEFAULT);
  3488. $cond = ['id' => $userinfo['id']];
  3489. $res = M('users')->where($cond)->setField('password', $savePwd);
  3490. if($res === false){
  3491. json_fail('重置失败');
  3492. }
  3493. json_success('重置成功');
  3494. }
  3495. public function checkSingleLogin( ){
  3496. header('Access-Control-Allow-Origin: *');
  3497. $userid = I('get.userid');
  3498. $openid = I('get.openid');
  3499. $where = [
  3500. 'id' => $userid,
  3501. 'wx_open_id'=> $openid
  3502. ];
  3503. $flag = M('users')->where($where)->find();
  3504. if($flag){
  3505. json_success('goon');
  3506. }
  3507. json_fail('relogin');
  3508. }
  3509. public function verify_vehicle_code( ){
  3510. header('Access-Control-Allow-Origin: *');
  3511. $postData = json_decode( file_get_contents('php://input'), true );
  3512. $licenseCode = $postData['licenseCode'];
  3513. $plateCode = $postData['plateCode'];
  3514. $rfidCode = $postData['rfidCode'];
  3515. if(!$licenseCode){
  3516. json_fail('服务端未收到证件码');
  3517. }
  3518. if(!$plateCode){
  3519. json_fail('服务端未收到车牌码');
  3520. }
  3521. if($licenseCode !== $plateCode){
  3522. json_fail('服务端判断车牌码与证件码不一致');
  3523. }
  3524. if(!$rfidCode){
  3525. json_fail('服务端未收到标签码');
  3526. }
  3527. $addTime = time();
  3528. //java -classpath "/home/wwwroot/api.wxt.renlianiot.com/.data/java_qr" TendencyEncrypt [code]
  3529. $prefix = 'java -classpath "/home/wwwroot/api.wxt.renlianiot.com/java_qr" TendencyEncrypt ';
  3530. $cmd = $prefix . $licenseCode;
  3531. $decodeRes = exec($cmd);
  3532. if(strlen($decodeRes) !== 7 ){
  3533. $errContent = '['. date('Y-m-d H:i:s') . ']' . ' decode qrcode failed, QRCONTENT:'. $licenseCode . '| RFID:'.$rfidCode.PHP_EOL;
  3534. debug_log('license_decode_error', $errContent);
  3535. json_fail('二维码内容解码失败,请检查');
  3536. }
  3537. $saveData = [
  3538. 'license_code' => $licenseCode,
  3539. 'rfid_code' => $rfidCode,
  3540. 'created_at' => $addTime,
  3541. 'decode_result' => $decodeRes
  3542. ];
  3543. $res = M('vehice_code_verify')->add($saveData);
  3544. if($res === false){
  3545. json_fail('添加失败,数据库异常或重复添加');
  3546. }
  3547. $count = M('vehice_code_verify')->count();
  3548. json_success('success', $count);
  3549. }
  3550. public function getSeriCount( ){
  3551. header('Access-Control-Allow-Origin: *');
  3552. $count = M('vehice_code_verify')->count();
  3553. if($count ===false){
  3554. json_fail('服务端异常');
  3555. }
  3556. json_success('success', $count);
  3557. }
  3558. public function decodeContent( ){
  3559. header('Access-Control-Allow-Origin: *');
  3560. $list = M('vehice_code_verify')->select();
  3561. $prefix = 'java -classpath "/home/wwwroot/api.wxt.renlianiot.com/java_qr" TendencyEncrypt ';
  3562. foreach($list as $v){
  3563. $cmd = $prefix . $v['license_code'];
  3564. $decodeRes = exec($cmd);
  3565. if(strlen($decodeRes) !== 7 ){
  3566. $errContent = '['. date('Y-m-d H:i:s') . ']' . ' decode qrcode failed, QRCONTENT:'. $v['license_code'] . '| RFID:'.$v['rfid_code'].PHP_EOL;
  3567. debug_log('license_decode_error', $errContent);
  3568. continue;
  3569. }
  3570. $res = M('vehice_code_verify')->where(['id' => $v['id']])->save(['decode_result' => $decodeRes]);
  3571. }
  3572. }
  3573. public function exportSql_km( ){
  3574. $start = intval(I('get.start'));
  3575. $end = intval(I('get.end'));
  3576. if(!$start || !$end){
  3577. json_fail('缺少参数,起止参数都需要');
  3578. }
  3579. $where= [
  3580. 'id' => [ ['EGT', $start], ['ELT', $end]],
  3581. ];
  3582. $list = M('vehice_code_verify')->where($where)->select();
  3583. $prefix = "INSERT INTO TB_ELECTRICCARS_CODE (PLATENUMBER,THEFTNO,THEFTNO2,ICNO,ORI_THEFTNO,ORI_THEFTNO2)VALUES('";
  3584. $fileName = SOLUTION_LOG_PATH .'km_sql_export/sql_after_transform'.time().'.sql';
  3585. $folder=dirname($fileName);
  3586. if (!is_dir($folder)){
  3587. mkdir($folder,0777,true);
  3588. }
  3589. $param4 = 'null';
  3590. foreach($list as $v){
  3591. $param1 = $v['decode_result']; //param1
  3592. $hex = dechex($v['rfid_code']);
  3593. $hexRes = str_pad($hex, 8,'0' , STR_PAD_LEFT);
  3594. $param5 = '8023'.$v['rfid_code'];
  3595. $param6 = '8021'.$v['rfid_code'];
  3596. $param2 = hexdec('8023'.$hexRes);
  3597. $param3 = hexdec('8021'.$hexRes);
  3598. $sqlStr = $prefix . $param1 . "'," . $param2 . ',' . $param3 . ',' . $param4 . ',' . $param5 . ',' . $param6 . ');' . PHP_EOL;
  3599. file_put_contents($fileName,$sqlStr ,FILE_APPEND);
  3600. }
  3601. echo 'export file path: '. $fileName;
  3602. }
  3603. public function decodeQrSecret( ){
  3604. header('Access-Control-Allow-Origin: *');
  3605. $postData = json_decode( file_get_contents('php://input'), true);
  3606. $code = $postData['qrCode'];
  3607. $type = $postData['type'];
  3608. $prefix = 'java -classpath "/home/wwwroot/api.wxt.renlianiot.com/java_qr" TendencyEncrypt ';
  3609. $cmd = $prefix . $code;
  3610. $decodeRes = exec($cmd);
  3611. if(strlen($decodeRes) !== 7 ){
  3612. $errContent = '['. date('Y-m-d H:i:s') . ']' . ' decode 【'.$type.' 】qrcode failed, QRCONTENT:'. $code. 'JAVA_RESP:'.$decodeRes .PHP_EOL;
  3613. debug_log('license_decode_error', $errContent);
  3614. json_fail('解码失败');
  3615. }
  3616. json_success('decode success',$decodeRes);
  3617. }
  3618. private function getMaxDbm( $avFile, $start ){
  3619. /*
  3620. $string = file_get_contents($avFile);
  3621. $bytes = array();
  3622. for($i = 0; $i < strlen($string); $i++){
  3623. $bytes[] = ord($string[$i]);
  3624. }
  3625. $counter = strlen($string);
  3626. */
  3627. $counter = 4;
  3628. $bytes = [1,2,3,4];
  3629. $absolute = 0;
  3630. $maximum = 0;
  3631. for ($i = $start; $i < $counter; $i++) {
  3632. // $absolute = abs(getShort(new byte[] {vector[i], vector[i + 1]}, 0));
  3633. $absolute = abs( $this->bytesToShort([$bytes[$i], $bytes[$i+1]], 0) ) ;
  3634. if ($absolute > $maximum) {
  3635. $maximum = $absolute;
  3636. }
  3637. $i++;
  3638. }
  3639. if ($maximum > 32767) {
  3640. $maximum = 32767;
  3641. }
  3642. return (int)$maximum;
  3643. }
  3644. public function test_cus_function( ){
  3645. $avFile = '/data/wwwroot/wxt.rltest.cn/1.0.0/.data/rec-869678040083761-1638237389747.wav';
  3646. $res = $this->getMaxDbm($avFile, 44);
  3647. var_dump($res);exit;
  3648. }
  3649. private function bytesToShort( $bytes, $position ){
  3650. $val = 0;
  3651. $val = $bytes[$position + 1] & 0xFF;
  3652. $val = $val << 8;
  3653. $val |= $bytes[$position] & 0xFF;
  3654. return $val;
  3655. }
  3656. public function getDeviceAvatar( ){
  3657. header('Access-Control-Allow-Origin: *');
  3658. $imei = I('get.imei');
  3659. if(!$imei){
  3660. json_fail('获取不到设备标识');
  3661. }
  3662. $avatar = M('devices')->where(['imei' => $imei])->getField('avatar');
  3663. if(!$avatar){
  3664. json_fail('获取头像失败');
  3665. }
  3666. json_success('获取头像成功',$avatar);
  3667. }
  3668. public function getUserAvatar( ){
  3669. header('Access-Control-Allow-Origin: *');
  3670. $uid = I('get.uid');
  3671. if(!$uid){
  3672. json_fail('获取不到用户标识');
  3673. }
  3674. $avatar = M('users')->where(['id' => $uid])->getField('avatar');
  3675. if(!$avatar){
  3676. json_fail('获取头像失败');
  3677. }
  3678. json_success('获取头像成功',$avatar);
  3679. }
  3680. public function saveDeviceAvatar( ){
  3681. header('Access-Control-Allow-Origin: *');
  3682. $postData = json_decode( file_get_contents('php://input'), true);
  3683. if(!$postData['imei']){
  3684. json_fail('获取不到设备标识');
  3685. }
  3686. if(!$postData['avatar64content']){
  3687. json_fail('获取不到头像信息');
  3688. }
  3689. $folder = realpath(__ROOT__).'/static/assets/avatar/'.date('Ymd').'/';
  3690. if (!is_dir($folder)){
  3691. $mres= mkdir($folder,0777,true);
  3692. if($mres === false){
  3693. json_fail('服务端创建目录权限不足');
  3694. }
  3695. }
  3696. $fileName = 'avatar-device-imei'.$postData['imei'] .'-'. time();
  3697. $saveFile = $this->base64_image_content($postData['avatar64content'], $folder, $fileName);
  3698. if(!$saveFile){
  3699. json_fail('服务端保存头像失败');
  3700. }
  3701. $res = M('devices')->where(['imei' => $postData['imei']])->save(['avatar' => $saveFile]);
  3702. if($res === false){
  3703. json_fail('保存头像地址失败');
  3704. }
  3705. json_success('保存成功');
  3706. }
  3707. public function saveUserAvatar( ){
  3708. header('Access-Control-Allow-Origin: *');
  3709. $postData = json_decode( file_get_contents('php://input'), true);
  3710. if(!$postData['uid']){
  3711. json_fail('获取不到用户标识');
  3712. }
  3713. if(!$postData['avatar64content']){
  3714. json_fail('获取不到头像信息');
  3715. }
  3716. $folder = realpath(__ROOT__).'/static/assets/avatar/'.date('Ymd').'/';
  3717. if (!is_dir($folder)){
  3718. $mres= mkdir($folder,0777,true);
  3719. if($mres === false){
  3720. json_fail('服务端创建目录权限不足');
  3721. }
  3722. }
  3723. $fileName = 'avatar-user-uid'.$postData['uid'] .'-'. time();
  3724. $saveFile = $this->base64_image_content($postData['avatar64content'], $folder, $fileName);
  3725. if(!$saveFile){
  3726. json_fail('服务端保存头像失败');
  3727. }
  3728. $res = M('users')->where(['id' => $postData['uid']])->save(['avatar' => $saveFile]);
  3729. if($res === false){
  3730. json_fail('保存头像地址失败');
  3731. }
  3732. json_success('保存成功');
  3733. }
  3734. private function base64_image_content( $base64_image_content, $path, $name ){
  3735. //匹配出图片的格式
  3736. if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
  3737. $type = $result[2];
  3738. $new_file = $path.$name.".{$type}";
  3739. $file = 'http://'.$_SERVER['HTTP_HOST'].'/static/assets/avatar/'.date('Ymd').'/'.$name.".{$type}";
  3740. if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
  3741. return $file;
  3742. }else{
  3743. return false;
  3744. }
  3745. }else{
  3746. return false;
  3747. }
  3748. }
  3749. public function setLocModel( ){
  3750. /*
  3751. 请求url上带上 openid userid
  3752. post请求 参数: imei id(设备id) post_mode 定位模式 0-常规模式 1-追踪模式 2-定时模式
  3753. 定时模式:需要设置 定位采样间隔(stopped_rtc_invl) 定位上报间隔(stopped_rpt_invl)
  3754. */
  3755. header('Access-Control-Allow-Origin: *');
  3756. $openid = I('get.openid');
  3757. $userid = I('get.userid');
  3758. //检测登录状态
  3759. $res=$this->checkLoginState($openid,$userid);
  3760. if(!$res['status']){
  3761. json_fail($res['message']);
  3762. }
  3763. $data = json_decode( file_get_contents("php://input") ,true);
  3764. $imei=$data['imei'];
  3765. if(!$imei){
  3766. json_fail('缺少设备imei号');
  3767. }
  3768. $config['pos_mode']=$data['pos_mode'];
  3769. if(!isset($config['pos_mode']) || $config['pos_mode'] === ''){
  3770. json_fail('缺少定位模式');
  3771. }
  3772. $dev_id=$data['id'];
  3773. if(!$dev_id){
  3774. json_fail('缺少设备id');
  3775. }
  3776. $redis = Redis('c61_gps_device_configs_'.$imei,"queue");
  3777. //开启事务
  3778. M()->startTrans();
  3779. $config['msg_id'] = 0xAAAA0010;
  3780. //判断是不是定时模式
  3781. if ($config['pos_mode'] == 2) {
  3782. if(!$data['stopped_rtc_invl']){
  3783. json_fail('请设置定位采样间隔');
  3784. }
  3785. if(!$data['stopped_rpt_invl']){
  3786. json_fail('请设置定位上报间隔');
  3787. }
  3788. $config_arr = array(
  3789. 'stopped_rtc_invl' => intval($data['stopped_rtc_invl']),
  3790. 'stopped_rpt_invl' => intval($data['stopped_rpt_invl']),
  3791. 'msg_id' => 0xAAAA0007
  3792. );
  3793. //下发日志
  3794. $log_data = array(
  3795. 'send_contents' => json_encode($config_arr),
  3796. 'type'=>'sensor',
  3797. 'imei'=>$imei,
  3798. 'created_at' => time(),
  3799. 'creator_id' => $userid,
  3800. 'device_id' => $dev_id
  3801. );
  3802. $lg_id = M('send_config_log')->createAdd($log_data);
  3803. if (!$lg_id) {
  3804. json_fail('下发日志添加失败');
  3805. M()->rollback();
  3806. }
  3807. $config_arr['id'] = $lg_id;
  3808. $redis->push(json_encode($config_arr));
  3809. }else{//非定时模式 采样时间设为3000 上报间隔3600
  3810. $config_arr = array(
  3811. 'stopped_rtc_invl' => 3000,
  3812. 'stopped_rpt_invl' => 3600,
  3813. 'msg_id' => 0xAAAA0007
  3814. );
  3815. //下发日志
  3816. $log_data = array(
  3817. 'send_contents' => json_encode($config_arr),
  3818. 'type'=>'sensor',
  3819. 'imei'=>$imei,
  3820. 'created_at' => time(),
  3821. 'creator_id' => $userid,
  3822. 'device_id' => $dev_id
  3823. );
  3824. $lg_id = M('send_config_log')->createAdd($log_data);
  3825. if (!$lg_id) {
  3826. json_fail('下发日志添加失败');
  3827. M()->rollback();
  3828. }
  3829. $config_arr['id'] = $lg_id;
  3830. $redis->push(json_encode($config_arr));
  3831. }
  3832. //下发日志
  3833. $log_data = array(
  3834. 'send_contents' => json_encode($config),
  3835. 'type'=>'pos_mode',
  3836. 'imei'=>$imei,
  3837. 'created_at' => time(),
  3838. 'creator_id' => $userid,
  3839. 'device_id' => $dev_id
  3840. );
  3841. $config['id'] = M('send_config_log')->createAdd($log_data);
  3842. if (!$config['id']) {
  3843. json_fail('下发日志添加失败');
  3844. M()->rollback();
  3845. }
  3846. $redis->push(json_encode($config));
  3847. M()->commit();
  3848. json_success('设置成功');
  3849. }
  3850. public function addToVoiceCenter( $imei ){
  3851. $code=C('VOICE_CENTER_CODE');
  3852. if(!$code){
  3853. return array('success'=>false,'message'=>'VOICE_CENTER_CODE not set');
  3854. }
  3855. $url =C('VOICE_CENTER_URL')?C('VOICE_CENTER_URL'):'http://47.114.185.186:8103/?s=voicecenter/deviceAddToCenter';
  3856. $post_data=array(
  3857. 'imei'=>$imei,
  3858. 'code'=>$code,
  3859. );
  3860. $send_res = curl_http_post(http_build_query($post_data),$url,false);
  3861. $result=json_decode($send_res,true);
  3862. if($result['success']){
  3863. //修改设备同步状态
  3864. $res=M('devices')->createSave(array('imei'=>$imei),['sync_status' => 1]);
  3865. return array('success'=>true,'message'=>'同步成功');
  3866. }else{
  3867. return array('success'=>false,'message'=>'同步失败');
  3868. }
  3869. }
  3870. public function delUserFence( ){
  3871. header('Access-Control-Allow-Origin: *');
  3872. $userid = I('get.userid');
  3873. if(!$userid){
  3874. json_fail('获取不到用户标识,尝试重登');
  3875. }
  3876. $fenceId =I('get.fenceId');
  3877. if(!$fenceId){
  3878. json_fail('获取不到围栏标识,尝试刷新');
  3879. }
  3880. $res = M('fences')->where(['id' => $fenceId, 'creator_id' => $userid, 'fence_type' => 1] )->delete();
  3881. if(!$res){
  3882. json_fail('删除失败');
  3883. }
  3884. json_success('删除成功');
  3885. }
  3886. public function sendReverveNotice( ){
  3887. header('Access-Control-Allow-Origin: *');
  3888. $voice_template_id=C('WX_VOICE_TEMPLATE_ID');
  3889. if(!$voice_template_id){
  3890. debug_log('voice_notice','WX_VOICE_TEMPLATE_ID not exist');
  3891. return false;
  3892. }
  3893. $data=$_POST;
  3894. $this->wxMsg = new \Jiaruan\WxTmp();
  3895. $access_token =$this->wxMsg->getAccessToken();
  3896. $msgArray= array(
  3897. "touser"=>$data['openid'],
  3898. "msgtype"=>"text",
  3899. "text"=>array(
  3900. "content"=>urlencode("测试啥是")
  3901. )
  3902. );
  3903. $json=urldecode(json_encode($msgArray));
  3904. //var_dump($json);exit;
  3905. #群发 文本消息
  3906. $ch = curl_init();
  3907. curl_setopt($ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token);
  3908. curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
  3909. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  3910. $res = json_decode(curl_exec($ch),true);
  3911. curl_close($ch);
  3912. json_success($res);
  3913. }
  3914. }