Area.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\model\Area as ModelArea;
  5. /**
  6. * 地区管理
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Area extends Backend
  11. {
  12. protected $searchFields='id,name';
  13. /**
  14. * Area模型对象
  15. * @var \app\admin\model\Area
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Area;
  22. }
  23. public function import()
  24. {
  25. parent::import();
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. /**
  33. * 查看
  34. */
  35. public function index()
  36. {
  37. //当前是否为关联查询
  38. $this->relationSearch = false;
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags', 'trim']);
  41. if ($this->request->isAjax()) {
  42. //如果发送的来源是Selectpage,则转发到Selectpage
  43. if ($this->request->request('keyField')) {
  44. return $this->selectpage();
  45. }
  46. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  47. $list = $this->model
  48. ->where($where)
  49. ->where('level',2)
  50. ->order($sort, $order)
  51. ->paginate($limit);
  52. $result = array("total" => $list->total(), "rows" => $list->items());
  53. return json($result);
  54. }
  55. return $this->view->fetch();
  56. }
  57. public function air_set($ids){
  58. if($this->request->isGet()){
  59. return view();
  60. }else{
  61. $area=\app\common\model\Area::city()->findOrFail($ids);
  62. $data=input();
  63. $this->validate($data,[
  64. 'longitude'=>['require','number'],
  65. 'latitude'=>['require','number'],
  66. ]);
  67. $area->save([
  68. 'air_longitude'=>$data['longitude'],
  69. 'air_latitude'=>$data['latitude'],
  70. ]);
  71. $this->success();
  72. }
  73. }
  74. public function get_data()
  75. {
  76. $params = [
  77. 'A-G' => ['安徽省', '北京', '重庆', '福建省', '甘肃省', '广东省', '广西壮族自治区', '贵州省'],
  78. 'H-K' => ['海南省', '河北省', '黑龙江省', '河南省', '湖北省', '湖南省', '江苏省', '江西省', '吉林省'],
  79. 'L-S' => ['辽宁省', '内蒙古自治区', '宁夏回族自治区', '青海省', '山东省', '上海', '山西省', '陕西省', '四川省'],
  80. 'T-Z' => ['天津', '新疆维吾尔自治区', '西藏自治区', '云南省', '浙江省'],
  81. '-' => ['台湾', '香港特别行政区', '澳门特别行政区']
  82. ];
  83. $provinceData = [];
  84. $provinceIds = [];
  85. foreach ($params as $key => $value) {
  86. $tempProvinceData = [];
  87. foreach ($value as $val) {
  88. $province = ModelArea::where('name', 'like', $val)->find();
  89. if (empty($province)) {
  90. continue;
  91. }
  92. $tempProvinceData[] = [
  93. 'code' => $province->id ?? 0,
  94. 'address' => $province->name ?? ''
  95. ];
  96. $provinceIds[] = $province->id ?? 0;
  97. }
  98. $provinceData[$key] = $tempProvinceData;
  99. }
  100. $data = [
  101. '86' => $provinceData
  102. ];
  103. foreach ($provinceIds as $provinceId) {
  104. $cities = ModelArea::where('pid', $provinceId)->column('name', 'id');
  105. if (empty($cities)) {
  106. continue;
  107. }
  108. $data[$provinceId] = $cities;
  109. foreach ($cities as $cityId => $city) {
  110. $districts = ModelArea::where('pid', $cityId)->column('name', 'id');
  111. if (empty($districts)) {
  112. continue;
  113. }
  114. $data[$cityId] = $districts;
  115. }
  116. }
  117. echo json_encode($data, JSON_UNESCAPED_UNICODE);
  118. die;
  119. }
  120. }