model = new AreaModel; } /** * @title: 行政区域列表 * @param {int} {pageNo} {非必填,默认值为1} {页码} * @param {int} {pageSize} {非必填,默认值为100} {每页数量} * @param {int} {pid} {非必填,默认为1} {父级id} * @param {string} {ename} {} {地区缩写} * @param {string} {firstLetter} {} {首字母} * @param {int} {common} {} {是否常用,1=常用} * @return array * @Author: wangkewei * @Date: 2021/5/17 15:21 */ public function getList($pid = 1,$firstLetter='',$ename='',$common=0,$pageNo = 1,$pageSize = 100) { $where = []; $where[] = ['id','<>',1]; $where[] = ['level','<',3]; $where[] = ['name','NOT IN',['市辖区','县']]; if(!empty($ename)){ $where[] = ['ename','LIKE',"$ename%"]; }else{ $where[] = ['pid','=',$pid]; } if(!empty($firstLetter)){ $where[] = ['firstLetter','=',$firstLetter]; } if($common == 1){ $list = $this->model->where('common','=',$common)->paginate(['page'=>$pageNo,'list_rows'=>$pageSize]); }else{ $list = $this->model->where($where)->paginate(['page'=>$pageNo,'list_rows'=>$pageSize]); } return pageRes(1,"获取成功",$list->total(),$list->items()); } public function getTree($pid=207) { $list = $this->model->where('pid',$pid)->select()->toArray(); $tree = array2tree($list,'pid','id',$pid); return res(1,"获取成功",$tree); } /** * @title: 行政区域编辑 * @param array * @return array * @Author: wangkewei * @Date: 2021/5/17 15:23 */ public function doEdit() { $data = $this->request->param(); $res = $this->model->replace()->save($data); $pid = $this->model->pid; $id = $this->model->id; if($pid>1){ $pInfo = $this->model->where('id',$pid)->find(); $path = explode(',',$pInfo->path); $path[] = $id; $pathTxt = implode(',',$path); $this->model->where('id',$id)->save(['path'=>$pathTxt]); }else{ $this->model->where('id',$id)->save(['path'=>$id]); } slog(1,"编辑了行政区域"); return res(1,"保存成功"); } /** * @title: 行政区域变更是否常用 * @param array * @return array * @Author: wangkewei * @Date: 2021/5/17 15:23 */ public function changeCommon() { $data = $this->request->param(); if(empty($data['id']) || !isset($data['common'])){ return res(2,'缺少参数'); } $this->model->where('id',$data['id'])->update(['common'=>$data['common']]); slog(1,"编辑了行政区域"); return res(1,"保存成功"); } /** * @title: 行政区域删除 * @param {string} {code} {必填} {行政区域code} * @return array * @Author: wangkewei * @Date: 2021/5/17 15:24 */ public function doDelete($code = "") { $data = $this->request->param(); $this->model->where("code",$code)->delete(); slog(1,"删除了行政区域"); return res(1,"删除成功"); } /** * @title: 检查行政区域路径并更新 * @desc: * @param {*} * @return {*} * @Author: Rock * @Date: 2021-07-12 20:13:41 * @LastEditTime: Do not edit */ public function checkPath() { $list = $this->model->select(); foreach($list as $item){ $parent = $this->model->where('id',$item->pid)->find(); if($parent){ $pathAry = explode(',',$parent->path); $pathAry[] = $item->id; $pathAry = array_filter($pathAry); $item->save(['path'=>implode(',',$pathAry),'level'=>count($pathAry)]); } } } /** * @title: 检查首字母并更新 * @desc: * @param {*} * @return {*} * @Author: Rock * @Date: 2021-07-21 16:00:33 * @LastEditTime: Do not edit */ public function checkFirstLetter() { $list = $this->model->where('ename','not null')->select(); foreach($list as $item){ $item->firstLetter = strtoupper(substr($item->ename,0,1)); $item->save(); } } /** * @title: 根据定位信息获取地区信息 * @desc: * @param {float} {lng} {} {经度} * @param {float} {lat} {} {纬度} * @return {*} * @Author: Rock * @Date: 2021-07-12 20:15:10 * @LastEditTime: Do not edit */ public function getAreaByLoc($lat,$lng) { $map = new \baidu\Map; $result = $map->location_to_address(['lat'=>$lat,'lng'=>$lng]); $code = $result['addressComponent']['adcode']; $area = $this->model->where('code',$code)->find(); return res(1,"获取成功",$area); } }