<?php namespace app\admin\controller; use app\watch\controller\Api; use think\App; use think\Db; class Location extends Auth { protected $lx = 'http://manager.aiqiangua.com:8080/organS2/themes/default/images/loc2.png'; protected $zx = 'http://manager.aiqiangua.com:8080/organS2/themes/default/images/loc.png'; public function __construct(App $app) { parent::__construct($app); $this->api = new Api($this->app); } public function index(){ if(request()->post()){ $length = input('rows',10,'intval'); //每页条数 $page = input('page',1,'intval'); //第几页 $start = ($page - 1) * $length; //分页开始位置 $map[] =['del','=',0]; $map[] =['org_id','=',cur_org_id()]; $map= empty($map) ? true: $map; $lists = Db::name('bracelet_device') ->where($map)->limit($start,$length) ->select(); $deviceInfo = []; foreach ($lists as $k=>$v){ $a = $this->api->getDevice($v['imei']); if(!$a['success']) $this->error($v['imei'].$a['error_desc']); $deviceInfo[] = $a['obj']; } $totalCount = Db::name('bracelet_device') ->where($map) ->count(); $totalPage = ceil($totalCount/$length); $data = []; $ydw = []; $wdw = []; $onlineNumber = 0; foreach ($deviceInfo as $k=>$v){ if(!empty($v['last_address'])){ $a = [ 'lonLat'=>[$v['last_location']['coordinates'][0],$v['last_location']['coordinates'][1]], 'imeI'=>$v['_id'], 'name'=>$v['name'], 'lon'=>$v['last_location']['coordinates'][0], 'lat'=>$v['last_location']['coordinates'][1], 'address'=>$v['last_address'], 'updated_at'=>nDate($v['location_updated_at']['$date']), 'icon'=>$v['online']?$this->zx:$this->lx ]; $ydw[] = [ 'imeI'=>$v['_id'], 'name'=>$v['name'], 'online'=>$v['online']?'在线':'离线', ]; if($v['online']){ $onlineNumber++; } }else{ $wdw[] = [ 'imeI'=>$v['_id'], 'name'=>$v['name'], ]; } } $data['data'][] = $a; $data['online_numbers'] =$onlineNumber; $lpage['page_count'] = $totalPage; $lpage['total'] = $totalCount; $lastPage = $page+1; $proPage = $page-1; $a1 = '<a href="javascript:;" style="color:black;"> 当前第'.$page.'页 </a>'; $a3 = '<a href="javascript:;" style="color:black;"> 共'.$lpage['total'].'条</a>'; if($page==1 && $lpage['page_count'] <=1){ $a0 = '<a style="color:black;" href="javascript:;">上一页 |</a>'; $a2 = '<a style="color:black;" href="javascript:;">| 下一页 |</a>'; }else if ($page==1 && $lpage['page_count'] >1){ $a0 = '<a style="color:black;" href="javascript:;">上一页 |</a>'; $a2 = '<a style="color:black;" onclick="setPage('.$lastPage.')" href="javascript:;">| 下一页 |</a>'; }else if ($page > 1 && $lpage['page_count'] <=1){ $a0 = '<a style="color:black;" onclick="setPage('.$proPage.')" href="javascript:;">上一页 |</a>'; $a2 = '<a style="color:black;" href="javascript:;">| 下一页 |</a>'; }else if ($page > 1 && $lpage['page_count'] >1){ $a0 = '<a style="color:black;" onclick="setPage('.$proPage.')" href="javascript:;">上一页 |</a>'; $a2 = '<a style="color:black;" onclick="setPage('.$lastPage.')" href="javascript:;">| 下一页 |</a>'; } $data['page'] = $a0.$a1.$a2.$a3; $ydwTxt =''; $wdwTxt =''; if(!empty($ydw)){ foreach ($ydw as $v){ $ydwTxt.='<li>'.$v['imeI'].'['.$v['name'].']['.$v['online'].']</li>'; } } if(!empty($wdw)){ foreach ($wdw as $v){ $wdwTxt.='<li>'.$v['imeI'].'['.$v['name'].']</li>'; } } $data['ydw'] = $ydwTxt; $data['wdw'] = $wdwTxt; $this->success('获取成功','',$data); }else{ $this->assign('gdKey',config('app.gdmap')); return $this->fetch(); } } public function getSosNum(){ $count = Db::name('sos_data') ->where('org_id',cur_org_id()) ->count(); $this->success('操作成功','',$count); } public function getSosList(){ if(request()->isAjax()){ //分页参数 $length = input('rows',10,'intval'); //每页条数 $page = 1; //第几页 $start = ($page - 1) * $length; //分页开始位置 $map[] = ['org_id','=',cur_org_id()]; $list = Db::name('sos_data')->where($map) ->limit($start,$length)->order('id','desc') ->select(); foreach ($list as $k=>&$v){ $v['created_at'] = $v['time_begin']; } $totalCount = Db::name('sos_data')->where($map)->count(); $totalPage = ceil($totalCount/$length); $result['page'] = $page; $result['total'] = $totalPage; $result['records'] = $totalCount; $result['rows'] = $list; Db::name('sos_data') ->whereIn('id',array_column($list,'id')) ->delete(); return json($result); }else{ return $this->fetch(); } } }