| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- namespace backend\modules\common\controllers;
- use backend\controllers\BaseController;
- use common\enums\RedisKeyEnum;
- use common\models\common\Region;
- use common\models\user\Town;
- use Yii;
- use yii\filters\AccessControl;
- /**
- * 地区管理
- *
- * @Author bing
- * @DateTime 2021-08-10 17:29:36
- * @copyright: Copyright (c) 2020 广东七件事集团
- */
- class RegionController extends BaseController
- {
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::class,
- 'rules' => [
- [
- 'actions' => ['list', 'get-district-list', 'get-town-list'],
- 'allow' => true,
- ],
- ],
- ],
- ];
- }
- public function actionList()
- {
- if (Yii::$app->request->isAjax) {
- $level = Yii::$app->request->get('level',2);
- $mall_id = Yii::$app->request->get('mall_id',0);
- $is_area_limit_town = Yii::$app->request->get('is_area_limit_town',0);
- if (!empty($mall_id) && empty($is_area_limit_town)) {
- $is_area_limit_town = \Yii::$app->services->setting->mall->get($mall_id, 'area.is_area_limit_town');
- }
- // 如果没有启用4级但是需要的是第四级,那么就将其修改为3级
- if (empty($is_area_limit_town) && $level == 4) $level = 3;
- // 暂时去掉缓存,因为新增了一些行政区,需要重新加载最新的数据
- // $list = Yii::$app->cache->get(RedisKeyEnum::REGION_TREE . $level);
- if(empty($list)){
- $where = [
- ['<=','level',$level],
- ['>','level',0]
- ];
- $regions = Region::lists(['where'=>$where,'select'=>'id,name,parent_id,level','limit'=>10000]);
- if ($level == 4) {
- $towns = Town::lists([
- 'select'=> 'id, name, district_id',
- 'order' => 'id asc'
- ]);
- $t_towns = [];
- foreach ($towns as &$town) {
- $town['level'] = 4;
- $town['parent_id'] = $town['district_id'];
- $town['id'] = "_{$town['id']}"; // 街道表与地区表数据ID存在重复,所以再ID前面加一个下划线
- unset($town['district_id']);
- $t_towns[$town['parent_id']][] = $town;
- }
- }
- $list = Region::getTreeRegion($regions);
- if (!empty($t_towns)) {
- foreach ($list as $k1 => $v1) {
- if (!empty($v1['children'])) {
- foreach ($v1['children'] as $k2 => $v2) {
- if (!empty($v2['children'])) {
- foreach ($v2['children'] as $k3 => $v3) {
- if (!empty($list[$k1]['children'][$k2]['children'])) {
- $list[$k1]['children'][$k2]['children'][$k3]['children'] = $t_towns[$v3['id']] ?? [
- [
- 'id' => "__{$v3['id']}",
- 'name' => $v3['name'],
- 'level' => 4,
- 'parent_id' => $v3['id'],
- ]
- ];
- }
- }
- }
- }
- }
- }
- }
- Yii::$app->cache->set(RedisKeyEnum::REGION_TREE . $level,$list);
- }
- return $this->success('ok',compact('list'));
- }
- }
- public function actionGetDistrictList()
- {
- $params = \Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($params, [
- [['level'], 'required'],
- [['level'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- /**
- * 1省级,2市级,3区,4镇级
- */
- $level_arr = [1];
- if ($params['level'] == 2) $level_arr[] = 2;
- if (in_array($params['level'], [3, 4])) {
- $level_arr[] = 2;
- $level_arr[] = 3;
- }
- $where[] = ['level' => $level_arr];
- $list = Region::getRegionList($where, 'id,name,parent_id,level');
- if (in_array($params['level'], [2, 3, 4])) {
- $tree = [];
- $level_arr = [];
- foreach ($list as $k => $item) {
- if ($item['level'] == 1) $tree[] = $item;
- if (in_array($item['level'], [2, 3])) $level_arr[$item['parent_id']][] = $item;
- }
- foreach ($tree as &$province) {
- if (isset($level_arr[$province['id']])) {
- foreach ($level_arr[$province['id']] as &$city) {
- if (isset($level_arr[$city['id']])) $city['list'] = $level_arr[$city['id']];
- }
- $province['list'] = $level_arr[$province['id']];
- }
- }
- return $this->success('ok',$tree);
- } else {
- return $this->success('ok',$list);
- }
- }
- public function actionGetTownList()
- {
- $params = \Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($params, [
- [['district_id'], 'required'],
- [['district_id'], 'integer'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $district_id = $params['district_id'];
- $list = Town::lists(['where' => [['district_id' => $district_id]], 'select' => 'id,district_id,name']);
- return $this->success('操作成功', ['list' => $list]);
- }
- }
|