rabin vor 1 Tag
Ursprung
Commit
6b11c157b9
7 geänderte Dateien mit 925 neuen und 0 gelöschten Zeilen
  1. 249 0
      app/Api/Data.php
  2. 15 0
      app/Api/Import.php
  3. 157 0
      app/Lib/Data.php
  4. 53 0
      app/Lib/Import/Core.php
  5. 92 0
      app/Lib/Import/Json.php
  6. 205 0
      app/Lib/Import/Web.php
  7. 154 0
      app/Lib/Manage.php

+ 249 - 0
app/Api/Data.php

@@ -0,0 +1,249 @@
+<?php namespace Area\Api;
+use Dever;
+use Area\Lib\Data as Config;
+class Data
+{
+	/**
+     * 获取地区数据
+     *
+     * @return mixed
+     */
+    public function get()
+    {
+        return Dever::load(\Manage\Lib\Util::class)->cascader(3, function($level, $parent) {
+            if ($level == 1) {
+                $data = Dever::load(Config::class)->getProvince();
+            } elseif ($level == 2) {
+                $data = Dever::load(Config::class)->getCity($parent);
+            } elseif ($level == 3) {
+                $data = Dever::load(Config::class)->getCounty($parent);
+            } elseif ($level == 4) {
+                $data = Dever::load(Config::class)->getTown($parent);
+            } else {
+                $data = Dever::load(Config::class)->getVillage($parent);
+            }
+            return $data;
+        });
+    }
+
+    # 获取区域状态
+    public function getStatus($area)
+    {
+        $temp = explode(',', $area);
+        $num = count($temp);
+        if ($num == 4 && isset($temp[3]) && $temp[3] > 0) {
+            # 街道
+            $where['id'] = $temp[3];
+            $table = 'town';
+        } elseif ($num == 3 && isset($temp[2]) && $temp[2] > 0) {
+            # 区县
+            $where['id'] = $temp[2];
+            $table = 'county';
+        } elseif ($num == 2 && isset($temp[1]) && $temp[1] > 0) {
+            # 城市
+            $where['id'] = $temp[1];
+            $table = 'city';
+        }
+        if ($table) {
+            $where['clear'] = true;
+            $info = Dever::db('area/' . $table)->find($where);
+            if ($info && $info['status'] == 2) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    # 修改区域状态
+    public function upStatus($area, $status = 2)
+    {
+        $table = '';
+        $update['status'] = $status;
+        $temp = explode(',', $area);
+        $num = count($temp);
+        if ($num == 4 && isset($temp[3]) && $temp[3] > 0) {
+            # 街道
+            $update['where_id'] = $temp[3];
+            $table = 'town';
+        } elseif ($num == 3 && isset($temp[2]) && $temp[2] > 0) {
+            # 区县
+            $update['where_id'] = $temp[2];
+            $table = 'county';
+        } elseif ($num == 2 && isset($temp[1]) && $temp[1] > 0) {
+            # 城市
+            $update['where_id'] = $temp[1];
+            $table = 'city';
+        }
+
+        $state = false;
+        if ($table) {
+            $update['clear'] = true;
+            $state = Dever::db('area/' . $table)->update($update);  
+        }
+        return $state;
+    }
+
+    /**
+     * 获取三级地区数据:json格式,生成js文件
+     *
+     * @return mixed
+     */
+    public function createJson()
+    {
+        $path = Dever::data() . 'upload/';
+        $create = Dever::input('create');
+        if (!$create) {
+            $create = 1;
+        }
+        $type = Dever::input('type');
+        if (!$type) {
+            $type = 'js';
+        }
+        if ($type == 'klist') {
+            $file = $path . 'city.' . $type . '.js';
+        } else {
+            $file = $path . 'city.' . $type;
+        }
+        
+        if (!is_file($file)) {
+            $create = 2;
+        }
+        if ($create == 2) {
+            $array = [];
+
+            $klist = Dever::load(Config::class)->getProvince();
+
+            if ($type == 'klist') {
+                $province = $klist;
+            } else {
+                $province = array_merge($array, $klist);
+            }
+            $province_data = [];
+            $city_data = [];
+            $county_data = [];
+            $town_data = [];
+
+            foreach ($province as $k => $v) {
+                $province_data[$k]['name'] = $v['name'];
+                $province_data[$k]['id'] = $v['value'];
+
+                if ($v['value'] <= 0) {
+                    continue;
+                }
+
+                $klist[$k]['text'] = $v['name'];
+                $klist[$k]['value'] = $v['value'];
+                $klist[$k]['children'] = Dever::load(Config::class)->getCity($v['value']);
+
+                if ($type == 'klist') {
+                    $city = $klist[$k]['children'];
+                } else {
+                    $city = array_merge($array, $klist[$k]['children']);
+                }
+
+                foreach ($city as $k1 => $v1) {
+                    $city_data[$v['value']][$k1]['province'] = $v['name'];
+                    $city_data[$v['value']][$k1]['name'] = $v1['name'];
+                    $city_data[$v['value']][$k1]['id'] = $v1['value'];
+
+                    if ($v1['value'] <= 0) {
+                        continue;
+                    }
+
+                    $klist[$k]['children'][$k1]['text'] = $v1['name'];
+                    $klist[$k]['children'][$k1]['value'] = $v1['value'];
+                    $klist[$k]['children'][$k1]['children'] = Dever::load(Config::class)->getCounty($v1['value']);
+
+                    if ($type == 'klist') {
+                        $county = $klist[$k]['children'][$k1]['children'];
+                    } else {
+                        $county = array_merge($array, $klist[$k]['children'][$k1]['children']);
+                    }
+
+                    foreach ($county as $k2 => $v2) {
+                        $county_data[$v1['value']][$k2]['city'] = $v1['name'];
+                        $county_data[$v1['value']][$k2]['name'] = $v2['name'];
+                        $county_data[$v1['value']][$k2]['id'] = $v2['value'];
+
+                        if ($v2['value'] <= 0) {
+                            continue;
+                        }
+
+                        $klist[$k]['children'][$k1]['children'][$k2]['text'] = $v2['name'];
+                        $klist[$k]['children'][$k1]['children'][$k2]['value'] = $v2['value'];
+                        $klist[$k]['children'][$k1]['children'][$k2]['children'] = Dever::load(Config::class)->getTown($v2['value']);
+
+                        if ($type == 'klist') {
+                            $town = $klist[$k]['children'][$k1]['children'][$k2]['children'];
+                        } else {
+                            $town = array_merge($array, $klist[$k]['children'][$k1]['children'][$k2]['children']);
+                        }
+
+                        foreach ($town as $k3 => $v3) {
+                            $town_data[$v2['value']][$k3]['county'] = $v2['name'];
+                            $town_data[$v2['value']][$k3]['name'] = $v3['name'];
+                            $town_data[$v2['value']][$k3]['id'] = $v3['value'];
+
+                            if ($v3['value'] <= 0) {
+                                continue;
+                            }
+
+                            $klist[$k]['children'][$k1]['children'][$k2]['children'][$k3]['text'] = $v3['name'];
+                            $klist[$k]['children'][$k1]['children'][$k2]['children'][$k3]['value'] = $v3['value'];
+                        }
+                    }
+                }
+            }
+
+            if ($type == 'klist') {
+                 $content = 'var cities = ' . Dever::json_encode($klist) . ';';
+            } elseif ($type == 'js') {
+                $content = 'var provinces = ' . Dever::json_encode($province_data) . ';';
+                $content .= 'var citys = ' . Dever::json_encode($city_data) . ';';
+                $content .= 'var areas = ' . Dever::json_encode($county_data) . ';';
+                $content .= 'var towns = ' . Dever::json_encode($town_data) . ';';
+            } elseif ($type == 'plist') {
+                $content = '<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<array>' . "\r\n";
+
+                foreach ($province_data as $k => $v) {
+                    $content .= '    <dict>
+        <key>province</key>
+        <string>'.$v['name'].'</string>
+        <key>citys</key>
+        <array>';
+
+                    if (isset($city_data[$v['id']])) {
+                        foreach ($city_data[$v['id']] as $k1 => $v1) {
+                            $content .= "\r\n" . '            <dict>
+                <key>city</key>
+                <string>'.$v1['name'].'</string>
+                <key>districts</key>
+                <array>';
+
+                            if (isset($county_data[$v1['id']])) {
+                                foreach ($county_data[$v1['id']] as $k2 => $v2) {
+                                    $content .= "\r\n" . '                    <string>'.$v2['name'].'</string>';
+                                }
+
+                                $content .= "\r\n                ";
+                            }
+
+                            $content .= '</array>' . "\r\n" . '            </dict>';
+                        }
+
+                        $content .= "\r\n        ";
+                    }
+                    
+
+                    $content .= '</array>' . "\r\n" . '    </dict>' . "\r\n";
+                }
+                $content .= '</array>' . "\r\n" . '</plist>';
+            }
+            file_put_contents($file, $content);
+        }
+        return $file;
+    }
+}

+ 15 - 0
app/Api/Import.php

@@ -0,0 +1,15 @@
+<?php namespace Area\Api;
+use Dever;
+
+class Import
+{
+    public function web()
+    {
+        return Dever::load(\Area\Lib\Import\Web::class)->get();
+    }
+
+    public function json()
+    {
+        return Dever::load(\Area\Lib\Import\Json::class)->get();
+    }
+}

+ 157 - 0
app/Lib/Data.php

@@ -0,0 +1,157 @@
+<?php
+namespace Area\Lib;
+use Dever;
+class Data 
+{
+    public function getProvince()
+    {
+        return Dever::db('area/province')->select(['status' => 1], ['col' => 'id,id as value,name']);
+    }
+
+    public function getCity($province_id)
+    {
+        if ($province_id) {
+            $where['province_id'] = $province_id;
+        }
+        $where['status'] = 1;
+        return Dever::db('area/city')->select($where, ['col' => 'id,id as value,name']);
+    }
+
+    public function getCounty($city_id)
+    {
+        if ($city_id) {
+            $where['city_id'] = $city_id;
+        }
+        $where['status'] = 1;
+        return Dever::db('area/county')->select($where, ['col' => 'id,id as value,name']);
+    }
+
+    public function getTown($county_id)
+    {
+        if ($county_id) {
+            $where['county_id'] = $county_id;
+        }
+        $where['status'] = 1;
+        return Dever::db('area/town')->select($where, ['col' => 'id,id as value,name']);
+    }
+
+    public function getVillage($town_id)
+    {
+        if ($town_id) {
+            $where['town_id'] = $town_id;
+        }
+        $where['status'] = 1;
+        return Dever::db('area/village')->select($where, ['col' => 'id,id as value,name']);
+    }
+
+    # 获取城市并根据首字母排序的
+    public function getCityToFirst()
+    {
+        $result = [];
+        $data = $this->getCity(false);
+        if (Dever::import('pinyin')) {
+            $result = Dever::sortPinyinFirst($data, 'pinyin_first');
+        }
+        return $result;
+    }
+
+    /**
+     * 获取详细信息
+     *
+     * @return mixed
+     */
+    public function getInfo($area, $col = 'id')
+    {
+        if ($area) {
+            $area = explode(',', $area);
+            $result = [];
+            foreach ($area as $k => $v) {
+                if ($k == 0) {
+                    $result[$k] = $this->getName('province', $v, true, $col);
+                } elseif ($k == 1) {
+                    $result[$k] = $this->getName('city', $v, true, $col);
+                    if ($col == 'id' && isset($result[1]['name']) && $result[0]['name'] == $result[1]['name']) {
+                        unset($result[1]);
+                    }
+                } elseif ($k == 2) {
+                    $result[$k] = $this->getName('county', $v, true, $col);
+                } elseif ($k == 3) {
+                    $result[$k] = $this->getName('town', $v, true, $col);
+                } elseif ($k == 4) {
+                    $result[$k] = $this->getName('village', $v, true, $col);
+                }
+            }
+            return $result;
+        }
+        return [];
+    }
+
+    /**
+     * 根据地区id转成名称
+     *
+     * @return mixed
+     */
+    public function string($area, $im = ',', $name = '不限', $unset = true, $check = false)
+    {
+        if ($area) {
+            if (is_string($area)) {
+                $area = explode(',', $area);
+            }
+            
+            $result = [];
+            foreach ($area as $k => $v) {
+                if ($k == 0) {
+                    $result[$k] = $this->getName('province', $v, false, 'id', $name);
+                } elseif ($k == 1) {
+                    $result[$k] = $this->getName('city', $v, false, 'id', $name);
+                    if (isset($result[0]) && $result[0] == $result[1] && $unset) {
+                        unset($result[1]);
+                    }
+                } elseif ($k == 2) {
+                    $parent = $area[0] . ',' . $area[1];
+                    $result[$k] = $this->getName('county', $v, false, 'id', $name, $check, $parent);
+                } elseif ($k == 3) {
+                    $parent = $area[0] . ',' . $area[1] . ',' . $area[2];
+                    $result[$k] = $this->getName('town', $v, false, 'id', $name, $check, $parent);
+                } elseif ($k == 4) {
+                    $result[$k] = $this->getName('village', $v, false, 'id', $name);
+                } else {
+                    $result[$k] = '';
+                }
+                if (isset($result[$k]) && !$result[$k]) {
+                    unset($result[$k]);
+                }
+            }
+            return implode($im, $result);
+        }
+        return '';
+    }
+
+
+    private function getName($table, $value, $state = false, $col = 'id', $name = '不限', $check = false, $area = [])
+    {
+        if (($col == 'id' && $value > 0) || ($col != 'id' && $value)) {
+            $where[$col] = $value;
+            $data = Dever::db('area/' . $table)->find($where);
+            if ($state) {
+                return $data;
+            }
+            if ($data) {
+                $name = $data['name'];
+                if ($check && $area && $data['area'] != $area) {
+                    $name = '<font style="color:red">'.$name.'(错误)</font>';
+                }
+            }
+        }
+        return $name;
+    }
+
+    public function pinyin($data)
+    {
+        if (Dever::project('pinyin') && $data['name']) {
+            $data['pinyin'] = Dever::load(\Pinyin\Lib\Convert::class)->getPinyin($data['name']);
+            $data['pinyin_first'] = Dever::load(\Pinyin\Lib\Convert::class)->getPinyinFirst($data['name']);
+        }
+        return $data;
+    }
+}

+ 53 - 0
app/Lib/Import/Core.php

@@ -0,0 +1,53 @@
+<?php namespace Area\Lib\Import;
+use Dever;
+class Core
+{
+    # 获取唯一id 已废弃
+    public function id($id, $len = 6)
+    {
+        return $id;
+        $id = substr($id, 0, $len);
+        $id = str_pad($id, $len, '0', STR_PAD_RIGHT);
+        return $id;
+    }
+
+    # 设置区县的等级
+    public function setLevelCounty(&$update)
+    {
+        $num = substr($update['id'], 4);
+
+        # type = 1城区 2郊区 3县城 4经济技术开发 5县级市
+        if ($update['name'] == '门头沟区') {
+            $update['type'] = 2;
+            $update['level'] = 2;
+        } elseif ($num <= 10) {
+            $update['type'] = 1;
+            $update['level'] = 1;
+        } elseif ($num > 10 && $num <= 20) {
+            $update['type'] = 2;
+            $update['level'] = 2;
+        } elseif ($num > 20 && $num <= 70) {
+            $update['type'] = 3;
+            $update['level'] = 3;
+        } elseif ($num > 70 && $num <= 80) {
+            $update['type'] = 4;
+            $update['level'] = 2;
+        } elseif ($num >= 80) {
+            $update['type'] = 5;
+            $update['level'] = 2;
+        }
+    }
+
+    # 更新数据
+    public function up($table, $id, $data)
+    {
+        $db = Dever::db('area/ ' .$table);
+        $info = $db->find($id);
+        if (!$info) {
+            $db->insert($data);
+        } else {
+            $db->update($info['id'], $data);
+        }
+        return $id;
+    }
+}

+ 92 - 0
app/Lib/Import/Json.php

@@ -0,0 +1,92 @@
+<?php namespace Area\Lib\Import;
+set_time_limit(0);
+use Dever;
+use Area\Lib\Data;
+class Json extends Core
+{
+    private $url = 'https://github.com/modood/Administrative-divisions-of-China';
+
+    public function getUrl()
+    {
+        return $this->url;
+    }
+    
+    public function get()
+    {
+        $this->getProvince();
+        $this->getCity();
+        $this->getCounty();
+        $this->getTown();
+        return 'ok';
+    }
+
+    private function load($type)
+    {
+        $file = DEVER_APP_PATH . 'file/'.$type.'.json';
+        $content = file_get_contents($file);
+        $content = json_decode($content, true);
+        return $content;
+    }
+
+    public function getProvince()
+    {
+        $data = $this->load('provinces');
+        if ($data) {
+            foreach ($data as $k => $v) {
+                $update['id'] = $this->id($v['code']);
+                $update['name'] = $v['name'];
+                $update = Dever::load(Data::class)->pinyin($update);
+                $this->up('province', $update['id'], $update);
+            }
+        }
+    }
+
+    public function getCity()
+    {
+        $data = $this->load('cities');
+        if ($data) {
+            foreach ($data as $k => $v) {
+                $update['id'] = $this->id($v['code']);
+                $update['name'] = $v['name'];
+                $update['province_id'] = $this->id($v['provinceCode']);
+                $update = Dever::load(Data::class)->pinyin($update);
+                $this->up('city', $update['id'], $update);
+            }
+        }
+    }
+
+    public function getCounty()
+    {
+        $data = $this->load('areas');
+        if ($data) {
+            foreach ($data as $k => $v) {
+                $update['id'] = $this->id($v['code']);
+                $update['name'] = $v['name'];
+                $update['city_id'] = $this->id($v['cityCode']);
+                $update['province_id'] = $this->id($v['provinceCode']);
+                $update['area'] = $update['province_id'] . ',' . $update['city_id'];
+                $this->setLevelCounty($update);
+                $update = Dever::load(Data::class)->pinyin($update);
+                $this->up('county', $update['id'], $update);
+            }
+        }
+    }
+
+    public function getTown()
+    {
+        $data = $this->load('streets');
+        if ($data) {
+            foreach ($data as $k => $v) {
+                $update['id'] = $this->id($v['code'], 9);
+                $update['name'] = $v['name'];
+                $update['county_id'] = $this->id($v['areaCode']);
+                $update['city_id'] = $this->id($v['cityCode']);
+                $update['province_id'] = $this->id($v['provinceCode']);
+                $update['area'] = $update['province_id'] . ',' . $update['city_id'] . ',' . $update['county_id'];
+                $this->setLevelCounty($update);
+                $update = Dever::load(Data::class)->pinyin($update);
+                $this->up('town', $update['id'], $update);
+            }
+        }
+    }
+}

+ 205 - 0
app/Lib/Import/Web.php

@@ -0,0 +1,205 @@
+<?php namespace Area\Lib\Import;
+set_time_limit(0);
+use Dever;
+use Area\Lib\Data;
+/**
+ * 获取国家统计局最新的地区数据
+ *
+ * @return mixed
+ */
+class Web extends Core
+{
+    private $url = 'https://www.stats.gov.cn/sj/tjbz/tjyqhdmhcxhfdm/2023/';
+
+    public function getUrl()
+    {
+        return $this->url;
+    }
+    
+    public function get()
+    {
+        $url = $this->url . 'index.html';
+        
+        $html = $this->html($url);
+
+        preg_match_all('/<td><a href="(.*?)">(.*?)<br \/><\/a><\/td>/i', $html, $result);
+
+        # 获取省份
+        $this->getProvince($result);
+
+        return 1;
+    }
+
+    public function getProvince($result)
+    {
+        $province = Dever::input('province');
+        $update = [];
+        if (isset($result[1]) && isset($result[2]) && $result[2]) {
+            foreach ($result[2] as $k => $v) {
+                $update['id'] = $this->id(trim($result[1][$k], '.html'));
+                $update['name'] = strip_tags($v);
+                $update = Dever::load(Data::class)->pinyin($update);
+                $id = $this->up('province', $update['id'], $update);
+
+                # 获取城市
+                if ($province) {
+                    if ($update['name'] == $province) {
+                        $this->getCity($id, $update['name'], $result[1][$k]);
+                    }
+                } else {
+                    $this->getCity($id, $update['name'], $result[1][$k]);
+                }
+            }
+        }
+    }
+
+    public function getCity($province, $province_name, $link)
+    {
+        $city = Dever::input('city');
+
+        $url = $this->url . $link;
+        
+        $html = $this->html($url);
+
+        preg_match_all('/<tr class="citytr"><td><a href="(.*?)">(.*?)<\/a><\/td><td><a href="(.*?)">(.*?)<\/a><\/td><\/tr>/is', $html, $result);
+
+        $update = [];
+        if (isset($result[3]) && isset($result[4]) && $result[4]) {
+            foreach ($result[4] as $k => $v) {
+                $v = strip_tags($v);
+                if ($v == '市辖区') {
+                    $v = $province_name;
+                }
+                $update['id'] = $this->id($result[2][$k]);
+                $update['name'] = $v;
+                $update['province_id'] = $province;
+
+                $update = Dever::load(Data::class)->pinyin($update);
+                $id = $this->up('city', $update['id'], $update);
+
+                if ($city) {
+                    if ($update['name'] == $city) {
+                        $this->getCounty($province, $id, $result[3][$k]);
+                    }
+                } else {
+                    $this->getCounty($province, $id, $result[3][$k]);
+                }
+            }
+        }
+    }
+
+    public function getCounty($province, $city, $source_link)
+    {
+        $url = $this->url . $source_link;
+
+        $temp = explode('/', $source_link);
+        $link = $temp[0];
+        
+        $html = $this->html($url);
+
+        preg_match_all('/<tr class="countytr"><td><a href="(.*?)">(.*?)<\/a><\/td><td><a href="(.*?)">(.*?)<\/a><\/td><\/tr>/i', $html, $result);
+
+        $update = [];
+        if (isset($result[3]) && isset($result[4]) && $result[4]) {
+            foreach ($result[4] as $k => $v) {
+                $update['id'] = $this->id($result[2][$k]);
+                $update['name'] = strip_tags($v);
+                $update['city_id'] = $city;
+                $update['province_id'] = $province;
+                $update['area'] = $province . ',' . $city;
+                $this->setLevelCounty($update);
+                $update = Dever::load(Data::class)->pinyin($update);
+                $id = $this->up('county', $update['id'], $update);
+
+                # 获取街道
+                $this->getTown($province, $city, $id, $link . '/' . $result[3][$k]);
+            }
+        } else {
+            $city_info = Dever::db('area/city')->find($city);
+            $update['id'] = $city_info['id'];
+            $update['name'] = $city_info['name'] . '辖区';
+            $update['city_id'] = $city;
+            $update['province_id'] = $province;
+            $update['area'] = $province . ',' . $city;
+            $update['type'] = 1;
+            $update['level'] = 1;
+            $update['pinyin'] = $city_info['pinyin'];
+            $update['pinyin_first'] = $city_info['pinyin_first'];
+
+            $id = $this->up('county', $update['id'], $update);
+
+            # 获取街道
+            $this->getTown($province, $city, $id, $source_link, $html);
+        }
+    }
+
+    public function getTown($province, $city, $county, $link = false, $html = false)
+    {
+        if ($link) {
+            $url = $this->url . $link;
+
+            $temp = explode('/', $link);
+            $link = $temp[0] . '/' . $temp[1];
+            
+            $html = $this->html($url);
+        }
+        if (!$link && !$html) {
+            return;
+        }
+
+        preg_match_all('/<tr class="towntr"><td><a href="(.*?)">(.*?)<\/a><\/td><td><a href="(.*?)">(.*?)<\/a><\/td><\/tr>/i', $html, $result);
+
+        $update = [];
+        if (isset($result[3]) && isset($result[4]) && $result[4]) {
+            foreach ($result[4] as $k => $v) {
+                $update['id'] = $this->id($result[2][$k], 9);
+                $update['name'] = strip_tags($v);
+                $update['county_id'] = $county;
+                $update['city_id'] = $city;
+                $update['province_id'] = $province;
+                $update['area'] = $province . ',' . $city . ',' . $county;
+                $update = Dever::load(Data::class)->pinyin($update);
+                $id = $this->up('town', $update['id'], $update);
+
+                # 获取社区
+                //$this->getVillage($province, $city, $county, $id, $link . '/' . $result[3][$k]);
+            }
+        }
+    }
+
+    public function getVillage($province, $city, $county, $town, $link)
+    {
+        $url = $this->url . $link;
+        
+        $html = $this->html($url);
+
+        preg_match_all('/<tr class="villagetr"><td>(.*?)<\/td><td>(.*?)<\/td><td>(.*?)<\/td><\/tr>/i', $html, $result);
+
+        $update = [];
+        if (isset($result[1]) && isset($result[2]) && isset($result[3])) {
+            foreach ($result[3] as $k => $v) {
+                $update['id'] = $this->id($result[1][$k], 12);
+                $update['code'] = $result[2][$k];
+                $update['name'] = strip_tags($v);
+                $update['town_id'] = $town;
+                $update['county_id'] = $county;
+                $update['city_id'] = $city;
+                $update['province_id'] = $province;
+                $update['area'] = $province . ',' . $city . ',' . $county . ',' . $town;
+                $update = Dever::load(Data::class)->pinyin($update);
+                $this->up('village', $update['id'], $update);
+            }
+        }
+    }
+
+    private function html($url)
+    {
+        $html = Dever::curl($url)->result();
+
+        //$html = Dever::convert($html, "UTF-8", "GBK");
+        $html = preg_replace('//', '', $html); // 去掉HTML注释
+        $html = preg_replace('/\s+/', ' ', $html); // 清除多余的空格
+        $html = preg_replace('/>\s</', '><', $html); // 去掉标记之间的空格
+        return $html;
+    }
+}

+ 154 - 0
app/Lib/Manage.php

@@ -0,0 +1,154 @@
+<?php
+namespace Area\Lib;
+use Dever;
+class Manage
+{
+    public function up($db, $data)
+    {
+        if (isset($data['area'])) {
+            $temp = is_string($data['area']) ? explode(',', $data['area']) : $data['area'];
+            $data['province_id'] = $temp[0];
+            $data['city_id'] = $temp[1];
+            if (isset($temp[2])) {
+                $data['county_id'] = $temp[2];
+            }
+            if (isset($temp[3])) {
+                $data['town_id'] = $temp[3];
+            }
+        }
+        $data = Dever::load(Data::class)->pinyin($data);
+        return $data;
+    }
+
+    public function upLevel($db, $data)
+    {
+        $update = [];
+        $city = $data['city'];
+        if ($city) {
+            Dever::db('area/city')->update(['level_id' => $data['id']], ['level_id' => -1]);
+            $city = explode('、', $city);
+            foreach ($city as $k => $v) {
+                $v = trim($v);
+                if (!$v) {
+                    continue;
+                }
+                Dever::db('area/city')->update(['name' => ['like', $v]], ['level_id' => $data['id']]);
+            }
+            if ($data['id'] == 1) {
+                $this->setTown();
+            }
+        }
+    }
+
+    # 设置国家镇级市、超级街道
+    public function setTown()
+    {
+        $data = [
+            # 镇级市
+            '河南省,安阳市,安阳县,水冶镇',
+            '河南省,信阳市,平桥区,明港镇',
+            '河南省,郑州市,巩义市,回郭镇',//'河南省,巩义市,安阳县,回郭镇',
+
+            '甘肃省,武威市,凉州区,黄羊镇',
+            '甘肃省,定西市,陇西县,首阳镇',
+            '甘肃省,天水市,甘谷县,磐安镇',
+
+            '广西,南宁市,宾阳县,黎塘镇',
+            '广西,梧州市,藤县,太平镇',
+            '广西,北海市,合浦县,公馆镇',
+            '广西,贵港市,平南县,大安镇',
+
+            '吉林省,长春市,公主岭市,范家屯镇',//'吉林省,四平市,公主岭市,范家屯镇',
+            '吉林省,四平市,梨树县,郭家店镇',
+            '吉林省,吉林市,吉林中国新加坡食品区,岔路河镇',// '吉林省,吉林市,永吉县,岔路河镇',
+            '吉林省,白山市,抚松县,松江河镇',//'吉林省,吉林市,抚松县,松江河镇',
+
+            '辽宁省,锦州市,北镇市,沟帮子街道  ',//'辽宁省,锦州市,北镇市,沟帮子镇',
+            '辽宁省,辽阳市,辽阳县,刘二堡镇',//'辽宁省,辽阳市,辽阳县,刘二堡新市镇',
+            '辽宁省,铁岭市,昌图县,八面城镇',
+
+            '黑龙江省,哈尔滨市,巴彦县,兴隆镇',
+            '黑龙江省,牡丹江市,海林市,柴河镇',
+            '黑龙江省,牡丹江市,宁安市,东京城镇',
+            '黑龙江省,伊春市,大箐山县,朗乡镇',//'黑龙江省,伊春市,铁力市,朗乡镇',
+            '黑龙江省,黑龙江农垦总局,建三江农垦分局,三江镇',//未知
+            '黑龙江省,红兴隆市,红兴隆农垦分局,兴隆镇',//未知
+            '黑龙江省,鹤岗市,萝北县,宝泉岭农场',//'黑龙江省,宝泉岭市,宝泉岭农垦分局,宝泉岭镇',
+            '黑龙江省,鸡西市,密山市,裴德镇',//'黑龙江省,牡丹江市,牡丹江农垦分局,裴德镇',
+            '黑龙江省,黑河市,嫩江市,双山镇',//'黑龙江省,九三垦区分局,九三垦区分局,双山镇',
+
+            '江苏省,苏州市,吴江区,盛泽镇',
+            '江苏省,无锡市,宜兴市,丁蜀镇',
+            '江苏省,南通市,启东市,启东吕四镇',//未知
+
+            '河北省,廊坊市,三河市,燕郊镇',
+            '河北省,廊坊市,霸州市,胜芳镇',
+
+            '江西省,南昌市,南昌县,向塘镇',
+
+            '安徽省,宿州市,萧县,黄口镇',
+            '安徽省,亳州市,谯城区,古城镇',
+
+            '湖南省,郴州市,永兴县,马田镇',
+            '湖南省,怀化市,洪江市,安江镇',
+            '湖南省,益阳市,桃江县,灰山港镇',
+
+            '湖北省,襄阳市,樊城区,太平店镇',//'湖北省,襄阳市,襄阳区,太平店镇',
+            '湖北省,荆门市,钟祥市,胡集镇',
+            '湖北省,孝感市,汉川市,马口镇',
+            '湖北省,宜昌市,夷陵区,龙泉镇',
+
+            '福建省,漳州市,龙海市,角美镇',
+            '福建省,泉州市,南安市,水头镇',
+            '福建省,泉州市,晋江市,东石镇',
+            '福建省,莆田市,秀屿区,忠门镇',
+
+            '浙江省,温州,苍南县,龙港镇',
+            '浙江省,绍兴市,诸暨市,店口镇',
+
+            '重庆市,重庆市,江津区,白沙镇',
+            '重庆市,重庆市,大足区,龙水镇',
+            '重庆市,重庆市,九龙坡区,西彭镇',
+
+            '四川省,南充市,仪陇县,金城镇',
+            '四川省,内江市,资中县,重龙镇',
+            '四川省,广元市,利州区,宝轮镇',
+            '四川省,达州市,宣汉县,南坝镇',
+
+            '广东省,佛山市,南海区,狮山镇',
+            '广东省,东莞市,东莞市,长安镇',
+            '广东省,东莞市,东莞市,虎门镇',
+            '广东省,东莞市,东莞市,塘厦镇',
+
+            # 超级街道
+            '广东省,深圳市,南山区,粤海街道',
+            '广东省,深圳市,龙岗区,坂田街道',
+            '广东省,深圳市,龙岗区,布吉街道',
+            '广东省,深圳市,龙岗区,龙城街道',
+            '广东省,深圳市,龙岗区,平湖街道',
+            '广东省,深圳市,龙岗区,龙岗街道',
+        ];
+
+        foreach ($data as $k => $v) {
+            $temp = explode(',', $v);
+            if (isset($temp[3])) {
+                $province = Dever::db('area/province')->find(['name' => ['like', $temp[0]]]);
+                if ($province) {
+                    $city = Dever::db('area/city')->find(['name' => ['like', $temp[1]], 'province_id' => $province['id']]);
+                    if ($city) {
+                        $county = Dever::db('area/county')->find(['name' => ['like', $temp[2]], 'city_id' => $city['id']]);
+                        if ($county) {
+                            if (strstr($temp[3], '街道')) {
+                                $type = 3;
+                            } else {
+                                $type = 2;
+                            }
+                            $town = Dever::db('area/town')->find(['name' => ['like', $temp[3]], 'county_id' => $county['id'], 'type' => $type]);
+                        }
+                    }
+                }
+            }
+        }
+        return 'ok';
+    }
+}