RegionController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace backend\modules\common\controllers;
  3. use backend\controllers\BaseController;
  4. use common\enums\RedisKeyEnum;
  5. use common\models\common\Region;
  6. use common\models\user\Town;
  7. use Yii;
  8. use yii\filters\AccessControl;
  9. /**
  10. * 地区管理
  11. *
  12. * @Author bing
  13. * @DateTime 2021-08-10 17:29:36
  14. * @copyright: Copyright (c) 2020 广东七件事集团
  15. */
  16. class RegionController extends BaseController
  17. {
  18. public function behaviors()
  19. {
  20. return [
  21. 'access' => [
  22. 'class' => AccessControl::class,
  23. 'rules' => [
  24. [
  25. 'actions' => ['list', 'get-district-list', 'get-town-list'],
  26. 'allow' => true,
  27. ],
  28. ],
  29. ],
  30. ];
  31. }
  32. public function actionList()
  33. {
  34. if (Yii::$app->request->isAjax) {
  35. $level = Yii::$app->request->get('level',2);
  36. $mall_id = Yii::$app->request->get('mall_id',0);
  37. $is_area_limit_town = Yii::$app->request->get('is_area_limit_town',0);
  38. if (!empty($mall_id) && empty($is_area_limit_town)) {
  39. $is_area_limit_town = \Yii::$app->services->setting->mall->get($mall_id, 'area.is_area_limit_town');
  40. }
  41. // 如果没有启用4级但是需要的是第四级,那么就将其修改为3级
  42. if (empty($is_area_limit_town) && $level == 4) $level = 3;
  43. // 暂时去掉缓存,因为新增了一些行政区,需要重新加载最新的数据
  44. // $list = Yii::$app->cache->get(RedisKeyEnum::REGION_TREE . $level);
  45. if(empty($list)){
  46. $where = [
  47. ['<=','level',$level],
  48. ['>','level',0]
  49. ];
  50. $regions = Region::lists(['where'=>$where,'select'=>'id,name,parent_id,level','limit'=>10000]);
  51. if ($level == 4) {
  52. $towns = Town::lists([
  53. 'select'=> 'id, name, district_id',
  54. 'order' => 'id asc'
  55. ]);
  56. $t_towns = [];
  57. foreach ($towns as &$town) {
  58. $town['level'] = 4;
  59. $town['parent_id'] = $town['district_id'];
  60. $town['id'] = "_{$town['id']}"; // 街道表与地区表数据ID存在重复,所以再ID前面加一个下划线
  61. unset($town['district_id']);
  62. $t_towns[$town['parent_id']][] = $town;
  63. }
  64. }
  65. $list = Region::getTreeRegion($regions);
  66. if (!empty($t_towns)) {
  67. foreach ($list as $k1 => $v1) {
  68. if (!empty($v1['children'])) {
  69. foreach ($v1['children'] as $k2 => $v2) {
  70. if (!empty($v2['children'])) {
  71. foreach ($v2['children'] as $k3 => $v3) {
  72. if (!empty($list[$k1]['children'][$k2]['children'])) {
  73. $list[$k1]['children'][$k2]['children'][$k3]['children'] = $t_towns[$v3['id']] ?? [
  74. [
  75. 'id' => "__{$v3['id']}",
  76. 'name' => $v3['name'],
  77. 'level' => 4,
  78. 'parent_id' => $v3['id'],
  79. ]
  80. ];
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }
  87. }
  88. Yii::$app->cache->set(RedisKeyEnum::REGION_TREE . $level,$list);
  89. }
  90. return $this->success('ok',compact('list'));
  91. }
  92. }
  93. public function actionGetDistrictList()
  94. {
  95. $params = \Yii::$app->request->get();
  96. $res = Yii::$app->services->validate->validate($params, [
  97. [['level'], 'required'],
  98. [['level'], 'integer'],
  99. ]);
  100. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  101. /**
  102. * 1省级,2市级,3区,4镇级
  103. */
  104. $level_arr = [1];
  105. if ($params['level'] == 2) $level_arr[] = 2;
  106. if (in_array($params['level'], [3, 4])) {
  107. $level_arr[] = 2;
  108. $level_arr[] = 3;
  109. }
  110. $where[] = ['level' => $level_arr];
  111. $list = Region::getRegionList($where, 'id,name,parent_id,level');
  112. if (in_array($params['level'], [2, 3, 4])) {
  113. $tree = [];
  114. $level_arr = [];
  115. foreach ($list as $k => $item) {
  116. if ($item['level'] == 1) $tree[] = $item;
  117. if (in_array($item['level'], [2, 3])) $level_arr[$item['parent_id']][] = $item;
  118. }
  119. foreach ($tree as &$province) {
  120. if (isset($level_arr[$province['id']])) {
  121. foreach ($level_arr[$province['id']] as &$city) {
  122. if (isset($level_arr[$city['id']])) $city['list'] = $level_arr[$city['id']];
  123. }
  124. $province['list'] = $level_arr[$province['id']];
  125. }
  126. }
  127. return $this->success('ok',$tree);
  128. } else {
  129. return $this->success('ok',$list);
  130. }
  131. }
  132. public function actionGetTownList()
  133. {
  134. $params = \Yii::$app->request->get();
  135. $res = Yii::$app->services->validate->validate($params, [
  136. [['district_id'], 'required'],
  137. [['district_id'], 'integer'],
  138. ]);
  139. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  140. $district_id = $params['district_id'];
  141. $list = Town::lists(['where' => [['district_id' => $district_id]], 'select' => 'id,district_id,name']);
  142. return $this->success('操作成功', ['list' => $list]);
  143. }
  144. }