浏览代码

行政区划从百度更新

theo 3 年之前
父节点
当前提交
f345fc3f5d

+ 4 - 1
backendApi/config/urlManagerRules.php

@@ -296,6 +296,9 @@ return [
             'POST,GET sms' => 'sms',
             'POST,GET transfer' => 'transfer',
             'POST,GET score' => 'score',
+            'GET region' => 'region',
+            'GET region-js' => 'region-js',
+            'GET init-region-xls' => 'init-region-xls',
         ],
     ],
     [
@@ -543,4 +546,4 @@ return [
             'GET user-login-export' => 'user-login-export',
         ],
     ],
-];
+];

+ 147 - 1
backendApi/modules/v1/controllers/ConfigController.php

@@ -45,6 +45,8 @@ use common\models\UserClose;
 use common\models\UserInfo;
 use common\models\UserNetwork;
 use common\models\UserRelation;
+use common\models\BaiduRegion;
+use common\models\Region;
 use common\models\WithdrawLevel;
 use Yii;
 use common\models\Config;
@@ -822,4 +824,148 @@ class ConfigController extends BaseController {
         return static::notice('更新服务协议成功');
     }
 
-}
+    public static function actionInitRegionXls() {
+        BaiduRegion::importXls(0,1000);
+        return static::notice('导入成功');
+    }
+
+    public static function actionRegion() {
+        // 开始查找bd区划,先找县级和以上
+//        $bdProv = BaiduRegion::_getBdProv();
+//        foreach ($bdProv as $prov) {
+//
+//        }
+
+        $bdCity = BaiduRegion::_getBdCity();
+        print_r("检查新增地市".PHP_EOL);
+        foreach ($bdCity as $city) {
+            $p = BaiduRegion::_checkInNc($city['CITY_CODE']);
+            if (!$p) {
+                print_r($city['CITY_NAME'].'找不到,添加'.PHP_EOL);
+//                print_r($city);
+                if(BaiduRegion::addNcRegion($region_code=$city['CITY_CODE'], $region_name=$city['CITY_NAME'], $pid=$city['PROV_CODE'], $deep=3)){
+                    print_r("添加成功".PHP_EOL);
+                }else{
+                    $regionM = Region::findOne(["REGION_CODE"=>$city['CITY_CODE']]);
+                    $regionM->STATUS = 1;
+                    $regionM->save();
+                    print_r("启用成功".PHP_EOL);
+                }
+            }
+        }
+
+        $bdCounty = BaiduRegion::_getBdCounty();
+        print_r("检查新增县".PHP_EOL);
+        foreach ($bdCounty as $county) {
+            $p = BaiduRegion::_checkInNc($county['COUNTY_CODE']);
+            if (!$p){
+                print_r($county['COUNTY_NAME'].' 区县找不到,添加'.PHP_EOL);
+//                print_r($county);
+                if(BaiduRegion::addNcRegion($region_code=$county['COUNTY_CODE'], $region_name=$county['COUNTY_NAME'], $pid=$county['CITY_CODE'], $deep=4)){
+                    print_r("添加成功".PHP_EOL);
+                }else{
+                    $regionM = Region::findOne(["REGION_CODE"=>$county['COUNTY_CODE']]);
+                    $regionM->STATUS = 1;
+                    $regionM->save();
+                    print_r("启用成功".PHP_EOL);
+                }
+            }
+        }
+
+        $bdTown = BaiduRegion::_getBdTown(); // 只取省直辖县,直筒子市的乡镇
+        print_r("检查新增地乡镇".PHP_EOL);
+        foreach ($bdTown as $town) {
+            $p = BaiduRegion::_checkInNc($town['TOWN_CODE']);
+//            print_r($p);
+            if (!$p) {
+                print_r($town['TOWN_CODE'].$town['TOWN_NAME'].' 镇 找不到,添加'.PHP_EOL);
+//                print_r($town);
+                if(BaiduRegion::addNcRegion($region_code=$town['TOWN_CODE'], $region_name=$town['TOWN_NAME'], $pid=$town['COUNTY_CODE'], $deep=5)){
+                    print_r("添加成功".PHP_EOL);
+                }else{
+                    $regionM = Region::findOne(["REGION_CODE"=>$town['TOWN_CODE']]);
+                    $regionM->STATUS = 1;
+                    $regionM->save();
+                    print_r("启用成功".PHP_EOL);
+                }
+            }
+        }
+
+        $ncAllRegion = BaiduRegion::_getAllNcRegion();
+
+        foreach ($ncAllRegion as $ncRegi) {
+            if(strlen($ncRegi['REGION_CODE'])==9){
+                $model = Region::findOne(["REGION_CODE"=>$ncRegi['REGION_CODE'], "STATUS"=>1]);
+                $model->DEEP = 5;
+                $model->save();
+                $ncRegi['DEEP'] = 5;
+            }
+            $t = BaiduRegion::_checkInBd($ncRegi['REGION_CODE'], $ncRegi['REGION_NAME'], $ncRegi['PID'], $ncRegi['DEEP']);
+            if(!$t){
+                print_r($ncRegi['REGION_CODE'].$ncRegi['REGION_NAME']." 在百度没有,标记删除".$ncRegi['DEEP'].PHP_EOL);
+                $regionM = Region::findOne(["REGION_CODE"=>$ncRegi['REGION_CODE'], "STATUS"=>1]);
+                $regionM->STATUS = '0';
+                $regionM->save();
+            } else {
+                if($t['REGION_NAME']!=$ncRegi['REGION_NAME']) {
+                    print_r($ncRegi['REGION_CODE'] . $ncRegi['REGION_NAME'] . " 更名,需修改" . PHP_EOL);
+                    $regionM = Region::findOne(["REGION_CODE"=>$ncRegi['REGION_CODE'], "STATUS"=>1]);
+                    $regionM->REGION_NAME = $t['REGION_NAME'];
+                    $regionM->save();
+                }
+                if($t['PID']!=$ncRegi['PID']){
+                    print_r($ncRegi['REGION_CODE'] . $ncRegi['REGION_NAME'] . " 上级".$ncRegi['PID']."需修改". $t['PID'] . PHP_EOL);
+                    $regionM = Region::findOne(["REGION_CODE"=>$ncRegi['REGION_CODE'], "STATUS"=>1]);
+                    $regionM->PID = $t['PID'];
+                    $regionM->save();
+                }
+            }
+        }
+
+        die;
+    }
+
+    public function actionRegionJs() {
+        $ncProv = BaiduRegion::_getAllNcProv();
+//        print_r($ncProv);
+
+        $prov_list = [];
+        $ar_region_array = [
+            '86' => $prov_list
+        ];
+
+        foreach ($ncProv as $prov) {
+            $ar_region_array['86'][$prov['REGION_CODE']] = $prov['REGION_NAME'];
+            $ar_region_array[$prov['REGION_CODE']] = [];
+        }
+
+        // 普通地市,直筒子市
+        $ncCity = BaiduRegion::_getAllNcCity();
+        foreach($ncCity as $city){
+            $ar_region_array[$city['PID']][$city['REGION_CODE']] = $city['REGION_NAME'];
+            $ncCounty = BaiduRegion::_getAllNcCountyByCity($city['REGION_CODE']);
+            foreach ($ncCounty as $county) {
+                $ar_region_array[$city['REGION_CODE']][$county['REGION_CODE']] = $county['REGION_NAME'];
+            }
+        }
+
+        // 直辖县级市
+        $ncHighCounty = BaiduRegion::_getAllNcCounty();
+        foreach ($ncHighCounty as $county) {
+            $ar_region_array[$county['PID']][$county['REGION_CODE']] = $county['REGION_NAME'];
+            $ncTown = BaiduRegion::_getAllNcTownByCity($county['REGION_CODE']);
+            foreach ($ncTown as $town) {
+                $ar_region_array[$county['REGION_CODE']][$town['REGION_CODE']] = $town['REGION_NAME'];
+            }
+        }
+
+        $ar_region_data = json_encode($ar_region_array,JSON_UNESCAPED_UNICODE);
+        $str = (string)$ar_region_data;
+        $str = "const AR_REGION_DATA = ".$str;
+
+        file_put_contents("/Volumes/HDD/workshop/old/ar.upload.ming/cdn/jsdata/ar_region_data.js", $str);
+
+        return static::notice('成功');
+    }
+
+}

+ 35 - 1
backendEle/src/views/config/base.vue

@@ -55,6 +55,15 @@
         <el-form-item>
           <el-button type="primary" @click="onSubmit" :loading="submitButtonStat">更新</el-button>
         </el-form-item>
+        <el-form-item>
+          <el-button type="primary" @click="initRegionXls" :loading="initRegionXlsStat">导入地址xls</el-button>
+        </el-form-item>
+        <el-form-item>
+          <el-button type="primary" @click="renewRegion" :loading="renewRegionStat">地址</el-button>
+        </el-form-item>
+        <el-form-item>
+          <el-button type="primary" @click="genRegionJs" :loading="genRegionJsStat">生成js</el-button>
+        </el-form-item>
       </el-form>
     </div>
   </div>
@@ -73,6 +82,9 @@ export default {
       loading: true,
       submitButtonStat: false,
       configData: null,
+      initRegionXlsStat: false,
+      renewRegionStat: false,
+      genRegionJsStat: false,
     }
   },
   computed: {
@@ -85,6 +97,28 @@ export default {
     }
   },
   methods: {
+    initRegionXls () {
+      network.getData('config/init-region-xls').then(response => {
+        console.log(1)
+      }).catch(response => {
+        console.log(2)
+      })
+    },
+    renewRegion () {
+      network.getData('config/region').then(response => {
+        console.log(1)
+      }).catch(response => {
+        console.log(2)
+      })
+    },
+    genRegionJs () {
+      network.getData('config/region-js').then(response => {
+        let data = JSON.parse(response)
+        console.log(data)
+      }).catch(response => {
+        console.log(2)
+      })
+    },
     onSubmit () {
       this.submitButtonStat = true
       console.log(this.form)
@@ -116,4 +150,4 @@ export default {
   font-size:13px;
   margin-left:10px;
 }
-</style>
+</style>

+ 245 - 0
common/models/BaiduRegion.php

@@ -0,0 +1,245 @@
+<?php
+
+namespace common\models;
+
+use common\models\Region;
+use sunmoon\phpspreadsheet\Excel;
+
+class BaiduRegion extends \common\components\ActiveRecord
+{
+    /**
+     * {@inheritdoc}
+     */
+    public static function tableName()
+    {
+        return '{{%BAIDU_REGION}}';
+    }
+
+    public static function importXls($startRow, $limit, $offset=0){
+//        $offset = 0;
+        $inData = BaiduRegion::getXlsData($startRow, $limit);
+        if($inData){
+            $offset += $limit;
+//            print_r($inData);
+//            print_r("插入数据");
+            BaiduRegion::batchInsert($inData);
+            unset($inData);
+            self::importXls($startRow+$offset, $limit);
+        }
+    }
+
+    public static function getXlsData($startRow, $limit) {
+        $filePath = \Yii::getAlias('@common/runtime/uploads/area.xlsx');
+        $tempFileName = \Yii::getAlias('@common/runtime/uploads/' . 'importTemp.txt');
+        $xlsData = Excel::import($filePath, [
+            'setFirstRecordAsKeys' => true,
+            'readStartRow' => $startRow + 1,
+            'readEndRow' => $startRow + $limit,
+            'storeFile' => $tempFileName,
+            'dropKeysRow' => $startRow == 1 ? false : true,
+        ]);
+        $dataArray = [];
+        foreach ($xlsData as $dl) {
+            if ($dl['乡镇代码']) {
+                $dataArray[] = [
+                    'PROV_NAME' => $dl['省份名称'],
+                    'PROV_CODE' => $dl['省份代码'],
+                    'CITY_NAME' => $dl['城市名称'],
+                    'CITY_CODE' => $dl['城市代码'],
+                    'COUNTY_NAME' => $dl['区县名称'],
+                    'COUNTY_CODE' => $dl['区县代码'],
+                    'TOWN_NAME' => $dl['乡镇名称'],
+                    'TOWN_CODE' => $dl['乡镇代码'],
+                ];
+            }else{
+                break;
+            }
+        }
+        return $dataArray;
+    }
+
+    public static function _getAllNcRegion() {
+        $ncRegion = Region::find()->select("REGION_CODE, REGION_NAME, PID, DEEP")
+            ->where("STATUS = 1 AND DEEP <= 5")
+            ->orderBy("REGION_CODE")
+            ->asArray()->all();
+        return $ncRegion;
+    }
+
+    public static function _getBdProv() {
+        $bdProv = self::find()->select('PROV_CODE,PROV_NAME')
+//            ->where("CITY_CODE like :CITY_CODE", [':CITY_CODE'=>'%00'])
+//            ->andWhere("PROV_CODE !=:PROV_CODE", [':PROV_CODE'=>'710000'])
+            ->groupBy('PROV_CODE')
+            ->asArray()->all();
+        return $bdProv;
+    }
+
+    public static function _getBdCity() {
+        $bdCity = self::find()->select('CITY_CODE,CITY_NAME,PROV_CODE')
+            ->where("PROV_CODE <=:PROV_CODE", [':PROV_CODE'=>800000])
+            ->groupBy('CITY_CODE')
+            ->asArray()->all();
+        return $bdCity;
+    }
+
+    public static function _getBdCounty() {
+        $bdCounty = self::find()->select('COUNTY_CODE,COUNTY_NAME,CITY_CODE')
+            ->where("PROV_CODE <=:PROV_CODE", [':PROV_CODE'=>800000])
+            ->groupBy('COUNTY_CODE')
+            ->asArray()->all();
+        return $bdCounty;
+    }
+
+    public static function _getBdTown() {
+        $bdTown = self::find()->select('TOWN_CODE,TOWN_NAME,COUNTY_CODE')
+            ->where(["in","CITY_CODE",[
+                '419001', '429004', '429005', '429006', '429021', // 济源、仙天潜神
+                '441900', '442000', '460400', '469001', '620200', // 东莞、中山、儋州、嘉峪关
+                '469001','469002','469005','469006','469007','469021','469022','469023','469024','469025','469026','469027','469028','469029','469030', // 海南
+                '659001', '659002', '659003', '659004', '659005', '659006', '659007', '659008', '659009', // 石河子、阿拉尔、图木舒克、五家渠、北屯、铁门关、双河、可克达拉、昆玉
+                '659010', '659011', // 胡杨河、新星市
+            ]])
+            ->asArray()->all();
+        return $bdTown;
+    }
+
+    public static function _checkInNc($region_code){
+        $p = Region::find()->select('REGION_CODE, REGION_NAME')
+            ->where('STATUS = 1 AND REGION_CODE = :REGION_CODE', [':REGION_CODE'=>$region_code])
+            ->asArray()->one();
+        return $p;
+    }
+
+    public static function addNcRegion($region_code, $region_name, $pid, $deep){
+        $newRegion = new Region();
+        $newRegion->REGION_CODE = $region_code;
+        $newRegion->REGION_NAME = $region_name;
+        $newRegion->PID = $pid;
+        $newRegion->DEEP = $deep;
+        $newRegion->STATUS = 1;
+        return $newRegion->save();
+    }
+
+    public static function _checkInBd($region_code, $region_name, $pid, $deep) {
+        $ncOtherRegion = [
+            '419001', '429004', '429005', '429006', '429021', // 济源,仙天潜神
+//            '441900', '442000', '460400', '469001', '620200', // 东莞、中山、儋州、嘉峪关
+            '469001','469002','469005','469006','469007','469021','469022','469023','469024','469025','469026','469027','469028','469029','469030', // 海南
+            '659001', '659002', '659003', '659004', '659005', '659006', '659007', '659008', '659009', // 石河子、阿拉尔、图木舒克、五家渠、北屯、铁门关、双河、可克达拉、昆玉
+            '659010', '659011', // 胡杨河、新星市
+        ];
+        $ret = [];
+        if ($deep==2){ // 省级
+            $t = BaiduRegion::find()->select('PROV_CODE, PROV_NAME')
+                ->where("PROV_CODE = :PROV_CODE", [':PROV_CODE'=>$region_code])
+//                ->orderBy('PROV_CODE')->groupBy('PROV_CODE')
+                ->asArray()->one();
+            if($t){
+                $ret = [
+                    'REGION_CODE' => $region_code
+                ];
+                $ret['REGION_NAME'] = $t['PROV_NAME'];
+                $ret['PID'] = '1';
+            }
+
+        }else if($deep==3){ // 地级
+            $t = BaiduRegion::find()->select('CITY_CODE, CITY_NAME, PROV_CODE')
+                ->where("CITY_CODE = :CITY_CODE", [':CITY_CODE'=>$region_code])
+//                ->orderBy('CITY_CODE')->groupBy('CITY_CODE')
+                ->asArray()->one();
+            if($t){
+                $ret = [
+                    'REGION_CODE' => $region_code
+                ];
+                $ret['REGION_NAME'] = $t['CITY_NAME'];
+                $ret['PID'] = $t['PROV_CODE'];
+            }
+
+        }else if($deep==4){ // 县级
+            $t = BaiduRegion::find()->select('COUNTY_CODE, COUNTY_NAME, CITY_CODE, PROV_CODE')
+                ->where("COUNTY_CODE = :COUNTY_CODE", [':COUNTY_CODE'=>$region_code])
+//                ->orderBy('COUNTY_CODE')->groupBy('COUNTY_CODE')
+                ->asArray()->one();
+            if($t){
+                $ret = [
+                    'REGION_CODE' => $region_code
+                ];
+                $ret['REGION_NAME'] = $t['COUNTY_NAME'];
+                $ret['PID'] = $t['CITY_CODE'];
+                if(in_array($region_code, $ncOtherRegion)){
+                    $ret['PID'] = $t['PROV_CODE'];
+                }
+            }
+
+        }else if($deep==5){ // 乡镇
+            $t = BaiduRegion::find()->select('TOWN_CODE, TOWN_NAME, COUNTY_CODE')
+                ->where("TOWN_CODE = :TOWN_CODE", [':TOWN_CODE'=>$region_code])
+//                ->orderBy('COUNTY_CODE')->groupBy('COUNTY_CODE')
+                ->asArray()->one();
+            if($t){
+                $ret = [
+                    'REGION_CODE' => $region_code
+                ];
+                $ret['REGION_NAME'] = $t['TOWN_NAME'];
+                $ret['PID'] = $t['COUNTY_CODE'];
+            }
+
+        }
+        if(!$t){
+            $ret = null;
+        }
+        return $ret;
+    }
+
+    // 构建js用到:
+
+    public static function _getAllNcProv() {
+        $ncRegion = Region::find()->select("REGION_CODE, REGION_NAME, PID, DEEP")
+            ->where("STATUS = 1 AND DEEP = 2")
+            ->orderBy("REGION_CODE")
+            ->asArray()->all();
+        return $ncRegion;
+    }
+
+    public static function _getAllNcCity() {
+        $ncRegion = Region::find()->select("REGION_CODE, REGION_NAME, PID, DEEP")
+            ->where("STATUS = 1 AND DEEP = 3 AND REGION_CODE!='999900'")
+            ->orderBy("REGION_CODE")
+            ->asArray()->all();
+        return $ncRegion;
+    }
+
+    public static function _getAllNcCounty() {
+        $ncRegion = Region::find()->select("REGION_CODE, REGION_NAME, PID, DEEP")
+            ->where(['in', 'REGION_CODE', [
+                '419001', '429004', '429005', '429006', '429021', // 济源、仙天潜神
+//                '441900', '442000', '460400', '469001', '620200', // 东莞、中山、儋州、嘉峪关
+                '469001','469002','469005','469006','469007','469021','469022','469023','469024','469025','469026','469027','469028','469029','469030', // 海南
+                '659001', '659002', '659003', '659004', '659005', '659006', '659007', '659008', '659009', // 石河子、阿拉尔、图木舒克、五家渠、北屯、铁门关、双河、可克达拉、昆玉
+                '659010', '659011', // 胡杨河、新星市
+            ]])
+            ->andWhere("STATUS = 1 AND DEEP = 4")
+            ->orderBy("REGION_CODE")
+            ->asArray()->all();
+        return $ncRegion;
+    }
+
+    public static function _getAllNcCountyByCity($pid) {
+        $ncRegion = Region::find()->select("REGION_CODE, REGION_NAME, PID, DEEP")
+            ->where("STATUS = 1 AND PID = :PID", [':PID'=>$pid])
+            ->orderBy("REGION_CODE")
+            ->asArray()->all();
+        return $ncRegion;
+    }
+
+    public static function _getAllNcTownByCity($pid) {
+        $ncRegion = Region::find()->select("REGION_CODE, REGION_NAME, PID, DEEP")
+            ->where("STATUS = 1 AND DEEP = 5 AND PID = :PID", [':PID'=>$pid])
+            ->orderBy("REGION_CODE")
+            ->asArray()->all();
+        return $ncRegion;
+    }
+
+
+}

+ 3 - 0
composer.json

@@ -36,6 +36,9 @@
         "process-timeout": 1800,
         "fxp-asset": {
             "enabled": false
+        },
+        "allow-plugins": {
+            "yiisoft/yii2-composer": true
         }
     },
     "repositories": {