Area.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace app\admin\controller\base\other;
  3. /**
  4. * @title: 字典-行政区域
  5. */
  6. use app\admin\controller\Base;
  7. use app\common\model\base\other\Area as AreaModel;
  8. class Area extends Base
  9. {
  10. protected $model = null;
  11. protected $noNeedLogin = ['checkpath','getList','getAreaByLoc','checkFirstLetter','changeCommon'];
  12. protected $noNeedAuth = ['checkpath'];
  13. public function initialize()
  14. {
  15. parent::initialize();
  16. $this->model = new AreaModel;
  17. }
  18. /**
  19. * @title: 行政区域列表
  20. * @param {int} {pageNo} {非必填,默认值为1} {页码}
  21. * @param {int} {pageSize} {非必填,默认值为100} {每页数量}
  22. * @param {int} {pid} {非必填,默认为1} {父级id}
  23. * @param {string} {ename} {} {地区缩写}
  24. * @param {string} {firstLetter} {} {首字母}
  25. * @param {int} {common} {} {是否常用,1=常用}
  26. * @return array
  27. * @Author: wangkewei
  28. * @Date: 2021/5/17 15:21
  29. */
  30. public function getList($pid = 1,$firstLetter='',$ename='',$common=0,$pageNo = 1,$pageSize = 100)
  31. {
  32. $where = [];
  33. $where[] = ['id','<>',1];
  34. $where[] = ['level','<',3];
  35. $where[] = ['name','NOT IN',['市辖区','县']];
  36. if(!empty($ename)){
  37. $where[] = ['ename','LIKE',"$ename%"];
  38. }else{
  39. $where[] = ['pid','=',$pid];
  40. }
  41. if(!empty($firstLetter)){
  42. $where[] = ['firstLetter','=',$firstLetter];
  43. }
  44. if($common == 1){
  45. $list = $this->model->where('common','=',$common)->paginate(['page'=>$pageNo,'list_rows'=>$pageSize]);
  46. }else{
  47. $list = $this->model->where($where)->paginate(['page'=>$pageNo,'list_rows'=>$pageSize]);
  48. }
  49. return pageRes(1,"获取成功",$list->total(),$list->items());
  50. }
  51. public function getTree($pid=207)
  52. {
  53. $list = $this->model->where('pid',$pid)->select()->toArray();
  54. $tree = array2tree($list,'pid','id',$pid);
  55. return res(1,"获取成功",$tree);
  56. }
  57. /**
  58. * @title: 行政区域编辑
  59. * @param array
  60. * @return array
  61. * @Author: wangkewei
  62. * @Date: 2021/5/17 15:23
  63. */
  64. public function doEdit()
  65. {
  66. $data = $this->request->param();
  67. $res = $this->model->replace()->save($data);
  68. $pid = $this->model->pid;
  69. $id = $this->model->id;
  70. if($pid>1){
  71. $pInfo = $this->model->where('id',$pid)->find();
  72. $path = explode(',',$pInfo->path);
  73. $path[] = $id;
  74. $pathTxt = implode(',',$path);
  75. $this->model->where('id',$id)->save(['path'=>$pathTxt]);
  76. }else{
  77. $this->model->where('id',$id)->save(['path'=>$id]);
  78. }
  79. slog(1,"编辑了行政区域");
  80. return res(1,"保存成功");
  81. }
  82. /**
  83. * @title: 行政区域变更是否常用
  84. * @param array
  85. * @return array
  86. * @Author: wangkewei
  87. * @Date: 2021/5/17 15:23
  88. */
  89. public function changeCommon()
  90. {
  91. $data = $this->request->param();
  92. if(empty($data['id']) || !isset($data['common'])){
  93. return res(2,'缺少参数');
  94. }
  95. $this->model->where('id',$data['id'])->update(['common'=>$data['common']]);
  96. slog(1,"编辑了行政区域");
  97. return res(1,"保存成功");
  98. }
  99. /**
  100. * @title: 行政区域删除
  101. * @param {string} {code} {必填} {行政区域code}
  102. * @return array
  103. * @Author: wangkewei
  104. * @Date: 2021/5/17 15:24
  105. */
  106. public function doDelete($code = "")
  107. {
  108. $data = $this->request->param();
  109. $this->model->where("code",$code)->delete();
  110. slog(1,"删除了行政区域");
  111. return res(1,"删除成功");
  112. }
  113. /**
  114. * @title: 检查行政区域路径并更新
  115. * @desc:
  116. * @param {*}
  117. * @return {*}
  118. * @Author: Rock
  119. * @Date: 2021-07-12 20:13:41
  120. * @LastEditTime: Do not edit
  121. */
  122. public function checkPath()
  123. {
  124. $list = $this->model->select();
  125. foreach($list as $item){
  126. $parent = $this->model->where('id',$item->pid)->find();
  127. if($parent){
  128. $pathAry = explode(',',$parent->path);
  129. $pathAry[] = $item->id;
  130. $pathAry = array_filter($pathAry);
  131. $item->save(['path'=>implode(',',$pathAry),'level'=>count($pathAry)]);
  132. }
  133. }
  134. }
  135. /**
  136. * @title: 检查首字母并更新
  137. * @desc:
  138. * @param {*}
  139. * @return {*}
  140. * @Author: Rock
  141. * @Date: 2021-07-21 16:00:33
  142. * @LastEditTime: Do not edit
  143. */
  144. public function checkFirstLetter()
  145. {
  146. $list = $this->model->where('ename','not null')->select();
  147. foreach($list as $item){
  148. $item->firstLetter = strtoupper(substr($item->ename,0,1));
  149. $item->save();
  150. }
  151. }
  152. /**
  153. * @title: 根据定位信息获取地区信息
  154. * @desc:
  155. * @param {float} {lng} {} {经度}
  156. * @param {float} {lat} {} {纬度}
  157. * @return {*}
  158. * @Author: Rock
  159. * @Date: 2021-07-12 20:15:10
  160. * @LastEditTime: Do not edit
  161. */
  162. public function getAreaByLoc($lat,$lng)
  163. {
  164. $map = new \baidu\Map;
  165. $result = $map->location_to_address(['lat'=>$lat,'lng'=>$lng]);
  166. $code = $result['addressComponent']['adcode'];
  167. $area = $this->model->where('code',$code)->find();
  168. return res(1,"获取成功",$area);
  169. }
  170. }