rabin 3 달 전
부모
커밋
40351a4f4e
18개의 변경된 파일417개의 추가작업 그리고 284개의 파일을 삭제
  1. 34 16
      api/Data.php
  2. 5 241
      api/Import.php
  3. 0 0
      file/areas.json
  4. 0 0
      file/cities.json
  5. 3 0
      file/data.txt
  6. 1 0
      file/provinces.json
  7. 0 0
      file/streets.json
  8. 10 10
      lib/Data.php
  9. 53 0
      lib/Import/Core.php
  10. 91 0
      lib/Import/Json.php
  11. 204 0
      lib/Import/Web.php
  12. 9 10
      lib/Manage.php
  13. 2 2
      manage/province.php
  14. 1 1
      table/city.php
  15. 1 1
      table/county.php
  16. 1 1
      table/province.php
  17. 1 1
      table/town.php
  18. 1 1
      table/village.php

+ 34 - 16
api/Data.php

@@ -109,14 +109,7 @@ class Data
             $create = 2;
         }
         if ($create == 2) {
-            $array = array
-            (
-                array
-                (
-                    'value' => "-1",
-                    'name' => '请选择',
-                ),
-            );
+            $array = [];
 
             $klist = Dever::load('data', 'area')->getProvince();
 
@@ -125,9 +118,10 @@ class Data
             } else {
                 $province = array_merge($array, $klist);
             }
-            $province_data = array();
-            $city_data = array();
-            $county_data = array();
+            $province_data = [];
+            $city_data = [];
+            $county_data = [];
+            $town_data = [];
 
             foreach ($province as $k => $v) {
                 $province_data[$k]['name'] = $v['name'];
@@ -167,12 +161,36 @@ class Data
                     }
 
                     foreach ($county as $k2 => $v2) {
-                        $klist[$k]['children'][$k1]['children'][$k2]['text'] = $v2['name'];
-                        $klist[$k]['children'][$k1]['children'][$k2]['value'] = $v2['value'];
-
                         $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('data', 'area')->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'];
+                        }
                     }
                 }
             }
@@ -183,6 +201,7 @@ class Data
                 $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">
@@ -225,7 +244,6 @@ class Data
             }
             file_put_contents($file, $content);
         }
-        $assets = Dever::config('host')->uploadRes;
-        return str_replace($path, $assets, $file);
+        return $file;
     }
 }

+ 5 - 241
api/Import.php

@@ -1,251 +1,15 @@
-<?php
-namespace Area\Api;
-set_time_limit(0);
+<?php namespace Area\Api;
 use Dever;
 
 class Import
 {
-    private $url = 'https://www.stats.gov.cn/sj/tjbz/tjyqhdmhcxhfdm/2023/';
-
-    public function getUrl()
-    {
-        return $this->url;
-    }
-
-	/**
-     * 获取国家统计局最新的地区数据
-     *
-     * @return mixed
-     */
-    public function load()
-    {
-        $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 = array();
-        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', 'area')->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)
+    public function web()
     {
-        $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 = array();
-        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', 'area')->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]);
-                }
-            }
-        }
+        return Dever::load('import/web', 'area')->get();
     }
 
-    public function getCounty($province, $city, $source_link)
+    public function json()
     {
-        $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 = array();
-        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', 'area')->pinyin($update);
-                $id = $this->up('county', $update['id'], $update);
-
-                # 获取街道
-                $this->getTown($province, $city, $id, $link . '/' . $result[3][$k]);
-            }
-        } else {
-            $city_info = Dever::db('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 = array();
-        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', 'area')->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 = array();
-        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', 'area')->pinyin($update);
-                $this->up('village', $update['id'], $update);
-            }
-        }
-    }
-
-    public function id($id, $len = 6)
-    {
-        $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($table, 'area');
-        $info = $db->find($id);
-        if (!$info) {
-            $db->insert($data);
-        } else {
-            $db->update($info['id'], $data);
-        }
-        return $id;
-    }
-
-    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;
+        return Dever::load('import/json', 'area')->get();
     }
 }

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
file/areas.json


파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
file/cities.json


+ 3 - 0
file/data.txt

@@ -0,0 +1,3 @@
+数据来源:
+https://github.com/modood/Administrative-divisions-of-China
+https://github.com/YangH9/China_area

+ 1 - 0
file/provinces.json

@@ -0,0 +1 @@
+[{"code":"11","name":"北京市"},{"code":"12","name":"天津市"},{"code":"13","name":"河北省"},{"code":"14","name":"山西省"},{"code":"15","name":"内蒙古自治区"},{"code":"21","name":"辽宁省"},{"code":"22","name":"吉林省"},{"code":"23","name":"黑龙江省"},{"code":"31","name":"上海市"},{"code":"32","name":"江苏省"},{"code":"33","name":"浙江省"},{"code":"34","name":"安徽省"},{"code":"35","name":"福建省"},{"code":"36","name":"江西省"},{"code":"37","name":"山东省"},{"code":"41","name":"河南省"},{"code":"42","name":"湖北省"},{"code":"43","name":"湖南省"},{"code":"44","name":"广东省"},{"code":"45","name":"广西壮族自治区"},{"code":"46","name":"海南省"},{"code":"50","name":"重庆市"},{"code":"51","name":"四川省"},{"code":"52","name":"贵州省"},{"code":"53","name":"云南省"},{"code":"54","name":"西藏自治区"},{"code":"61","name":"陕西省"},{"code":"62","name":"甘肃省"},{"code":"63","name":"青海省"},{"code":"64","name":"宁夏回族自治区"},{"code":"65","name":"新疆维吾尔自治区"}]

파일 크기가 너무 크기때문에 변경 상태를 표시하지 않습니다.
+ 0 - 0
file/streets.json


+ 10 - 10
lib/Data.php

@@ -5,7 +5,7 @@ class Data
 {
     public function getProvince()
     {
-        return Dever::db('province', 'area')->select(array('status' => 1));
+        return Dever::db('province', 'area')->select(['status' => 1], ['col' => 'id,id as value,name']);
     }
 
     public function getCity($province_id)
@@ -14,7 +14,7 @@ class Data
             $where['province_id'] = $province_id;
         }
         $where['status'] = 1;
-        return Dever::db('city', 'area')->select($where);
+        return Dever::db('city', 'area')->select($where, ['col' => 'id,id as value,name']);
     }
 
     public function getCounty($city_id)
@@ -23,7 +23,7 @@ class Data
             $where['city_id'] = $city_id;
         }
         $where['status'] = 1;
-        return Dever::db('county', 'area')->select($where);
+        return Dever::db('county', 'area')->select($where, ['col' => 'id,id as value,name']);
     }
 
     public function getTown($county_id)
@@ -32,7 +32,7 @@ class Data
             $where['county_id'] = $county_id;
         }
         $where['status'] = 1;
-        return Dever::db('town', 'area')->select($where);
+        return Dever::db('town', 'area')->select($where, ['col' => 'id,id as value,name']);
     }
 
     public function getVillage($town_id)
@@ -41,13 +41,13 @@ class Data
             $where['town_id'] = $town_id;
         }
         $where['status'] = 1;
-        return Dever::db('village', 'area')->select($where);
+        return Dever::db('village', 'area')->select($where, ['col' => 'id,id as value,name']);
     }
 
     # 获取城市并根据首字母排序的
     public function getCityToFirst()
     {
-        $result = array();
+        $result = [];
         $data = $this->getCity(false);
         if (Dever::import('pinyin')) {
             $result = Dever::sortPinyinFirst($data, 'pinyin_first');
@@ -64,7 +64,7 @@ class Data
     {
         if ($area) {
             $area = explode(',', $area);
-            $result = array();
+            $result = [];
             foreach ($area as $k => $v) {
                 if ($k == 0) {
                     $result[$k] = $this->getName('province', $v, true, $col);
@@ -83,7 +83,7 @@ class Data
             }
             return $result;
         }
-        return array();
+        return [];
     }
 
     /**
@@ -98,7 +98,7 @@ class Data
                 $area = explode(',', $area);
             }
             
-            $result = array();
+            $result = [];
             foreach ($area as $k => $v) {
                 if ($k == 0) {
                     $result[$k] = $this->getName('province', $v, false, 'id', $name);
@@ -128,7 +128,7 @@ class Data
     }
 
 
-    private function getName($table, $value, $state = false, $col = 'id', $name = '不限', $check = false, $area = array())
+    private function getName($table, $value, $state = false, $col = 'id', $name = '不限', $check = false, $area = [])
     {
         if (($col == 'id' && $value > 0) || ($col != 'id' && $value)) {
             $where[$col] = $value;

+ 53 - 0
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($table, 'area');
+        $info = $db->find($id);
+        if (!$info) {
+            $db->insert($data);
+        } else {
+            $db->update($info['id'], $data);
+        }
+        return $id;
+    }
+}

+ 91 - 0
lib/Import/Json.php

@@ -0,0 +1,91 @@
+<?php namespace Area\Lib\Import;
+set_time_limit(0);
+use Dever;
+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', 'area')->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', 'area')->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', 'area')->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', 'area')->pinyin($update);
+                $this->up('town', $update['id'], $update);
+            }
+        }
+    }
+}

+ 204 - 0
lib/Import/Web.php

@@ -0,0 +1,204 @@
+<?php namespace Area\Lib\Import;
+set_time_limit(0);
+use Dever;
+/**
+ * 获取国家统计局最新的地区数据
+ *
+ * @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', 'area')->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', 'area')->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', 'area')->pinyin($update);
+                $id = $this->up('county', $update['id'], $update);
+
+                # 获取街道
+                $this->getTown($province, $city, $id, $link . '/' . $result[3][$k]);
+            }
+        } else {
+            $city_info = Dever::db('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', 'area')->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', 'area')->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;
+    }
+}

+ 9 - 10
lib/Manage.php

@@ -22,17 +22,17 @@ class Manage
 
     public function upLevel($db, $data)
     {
-        $update = array();
+        $update = [];
         $city = $data['city'];
         if ($city) {
-            Dever::db('city', 'area')->update(array('level_id' => $data['id']), array('level_id' => -1));
+            Dever::db('city', 'area')->update(['level_id' => $data['id']], ['level_id' => -1]);
             $city = explode('、', $city);
             foreach ($city as $k => $v) {
                 $v = trim($v);
                 if (!$v) {
                     continue;
                 }
-                Dever::db('city', 'area')->update(array('name' => array('like', $v)), array('level_id' => $data['id']));
+                Dever::db('city', 'area')->update(['name' => ['like', $v]], ['level_id' => $data['id']]);
             }
             if ($data['id'] == 1) {
                 $this->setTown();
@@ -43,8 +43,7 @@ class Manage
     # 设置国家镇级市、超级街道
     public function setTown()
     {
-        $data = array
-        (
+        $data = [
             # 镇级市
             '河南省,安阳市,安阳县,水冶镇',
             '河南省,信阳市,平桥区,明港镇',
@@ -128,23 +127,23 @@ class Manage
             '广东省,深圳市,龙岗区,龙城街道',
             '广东省,深圳市,龙岗区,平湖街道',
             '广东省,深圳市,龙岗区,龙岗街道',
-        );
+        ];
 
         foreach ($data as $k => $v) {
             $temp = explode(',', $v);
             if (isset($temp[3])) {
-                $province = Dever::db('province', 'area')->find(array('name' => array('like', $temp[0])));
+                $province = Dever::db('province', 'area')->find(['name' => ['like', $temp[0]]]);
                 if ($province) {
-                    $city = Dever::db('city', 'area')->find(array('name' => array('like', $temp[1]), 'province_id' => $province['id']));
+                    $city = Dever::db('city', 'area')->find(['name' => ['like', $temp[1]], 'province_id' => $province['id']]);
                     if ($city) {
-                        $county = Dever::db('county', 'area')->find(array('name' => array('like', $temp[2]), 'city_id' => $city['id']));
+                        $county = Dever::db('county', 'area')->find(['name' => ['like', $temp[2]], 'city_id' => $city['id']]);
                         if ($county) {
                             if (strstr($temp[3], '街道')) {
                                 $type = 3;
                             } else {
                                 $type = 2;
                             }
-                            $town = Dever::db('town', 'area')->find(array('name' => array('like', $temp[3]), 'county_id' => $county['id'], 'type' => $type));
+                            $town = Dever::db('town', 'area')->find(['name' => ['like', $temp[3]], 'county_id' => $county['id'], 'type' => $type]);
                         }
                     }
                 }

+ 2 - 2
manage/province.php

@@ -5,8 +5,8 @@ return array
 (
     'list' => array
     (
-        'desc' => '导入数据请在服务器执行:php '.$path.'index.php \'{"l":"import.load"}\';
-        数据来源:' . Dever::load('import', 'area', 'api')->getUrl(),
+        'desc' => '导入数据请在服务器执行:php '.$path.'index.php \'{"l":"import.json"}\';
+        数据来源:' . Dever::load('import/json', 'area')->getUrl(),
         'field'      => array
         (
             'id',

+ 1 - 1
table/city.php

@@ -2,7 +2,7 @@
 return array
 (
 	'name' => '城市',
-	'order' => 'sort asc',
+	'order' => 'sort asc,id asc',
 	'struct' => array
 	(
 		'name'		=> array

+ 1 - 1
table/county.php

@@ -2,7 +2,7 @@
 return array
 (
 	'name' => '区县',
-	'order' => 'sort asc',
+	'order' => 'sort asc,id asc',
 	'struct' => array
 	(
 		'name'		=> array

+ 1 - 1
table/province.php

@@ -2,7 +2,7 @@
 return array
 (
 	'name' => '省份',
-	'order' => 'sort asc',
+	'order' => 'sort asc,id asc',
 	'struct' => array
 	(
 		'name'		=> array

+ 1 - 1
table/town.php

@@ -2,7 +2,7 @@
 return array
 (
 	'name' => '街镇',
-	'order' => 'sort asc',
+	'order' => 'sort asc,id asc',
 	'struct' => array
 	(
 		'name'		=> array

+ 1 - 1
table/village.php

@@ -2,7 +2,7 @@
 return array
 (
 	'name' => '社区',
-	'order' => 'sort asc',
+	'order' => 'sort asc,id asc',
 	'struct' => array
 	(
 		'name'		=> array

이 변경점에서 너무 많은 파일들이 변경되어 몇몇 파일들은 표시되지 않았습니다.