dever 3 vuotta sitten
vanhempi
commit
57546291cc
4 muutettua tiedostoa jossa 369 lisäystä ja 11 poistoa
  1. 130 0
      database/town.php
  2. 142 0
      database/village.php
  3. 38 7
      src/Api.php
  4. 59 4
      src/Import.php

+ 130 - 0
database/town.php

@@ -0,0 +1,130 @@
+<?php
+
+$county = function()
+{
+	$array = array();
+	$data = Dever::load('area/county-state');
+	if($data)
+	{
+		$array += $data;
+	}
+	return $array;
+};
+
+return array
+(
+	# 表名
+	'name' => 'town',
+	# 显示给用户看的名称
+	'lang' => '街道',
+	# 是否显示在后台菜单
+	'order' => 13,
+	# 数据结构
+	'struct' => array
+	(
+		'id' 		=> array
+		(
+			'type' 		=> 'bigint-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+			'order'		=> 'asc',
+			'list'		=> true,
+		),
+
+		'name'		=> array
+		(
+			'type' 		=> 'varchar-150',
+			'name' 		=> '街道名称',
+			'default' 	=> '',
+			'desc' 		=> '街道名称',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
+		'county_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '县区',
+			'default' 	=> '0',
+			'desc' 		=> '县区',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'select',
+			'option'	=> $county,
+			'list'		=> true,
+		),
+
+		'reorder'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '排序(数值越大越靠前)',
+			'default' 	=> '1',
+			'desc' 		=> '请输入排序',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			'search'	=> 'order',
+			'list_name' => '排序',
+			'list'		=> true,
+			'order'		=> 'desc',
+			'edit'		=> true,
+		),
+		
+		'state'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '状态',
+			'default' 	=> '1',
+			'desc' 		=> '请选择状态',
+			'match' 	=> 'is_numeric',
+		),
+		
+		'cdate'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '录入时间',
+			'match' 	=> array('is_numeric', time()),
+			'desc' 		=> '',
+			# 只有insert时才生效
+			'insert'	=> true,
+			'search'	=> 'date',
+			'list'		=> 'date("Y-m-d H:i:s", {cdate})',
+		),
+	),
+
+	'manage' => array
+	(
+		
+	),
+
+	'request' => array
+	(
+		'getAll' => array
+		(
+			# 匹配的正则或函数 选填项
+			'option' => array
+			(
+				'county_id' => 'yes',
+				'state' => 1,
+			),
+			'type' => 'all',
+			'order' => array('reorder' => 'desc', 'id' => 'asc'),
+			'col' => 'id as value, name',
+		),
+
+		'getData' => array
+		(
+			# 匹配的正则或函数 选填项
+			'option' => array
+			(
+				'county_id' => 'yes',
+				'state' => 1,
+			),
+			'type' => 'all',
+			'order' => array('reorder' => 'desc', 'id' => 'asc'),
+			'col' => 'id, name|id',
+		),
+	)
+);

+ 142 - 0
database/village.php

@@ -0,0 +1,142 @@
+<?php
+
+$town = function()
+{
+	$array = array();
+	$data = Dever::load('area/town-state');
+	if($data)
+	{
+		$array += $data;
+	}
+	return $array;
+};
+
+return array
+(
+	# 表名
+	'name' => 'village',
+	# 显示给用户看的名称
+	'lang' => '社区',
+	# 是否显示在后台菜单
+	'order' => 14,
+	# 数据结构
+	'struct' => array
+	(
+		'id' 		=> array
+		(
+			'type' 		=> 'bigint-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+			'order'		=> 'asc',
+			'list'		=> true,
+		),
+
+		'name'		=> array
+		(
+			'type' 		=> 'varchar-150',
+			'name' 		=> '社区名称',
+			'default' 	=> '',
+			'desc' 		=> '社区名称',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
+		'code'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '城乡分类代码',
+			'default' 	=> '',
+			'desc' 		=> '城乡分类代码',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
+		'town_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '街道',
+			'default' 	=> '0',
+			'desc' 		=> '街道',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'select',
+			'option'	=> $town,
+			'list'		=> true,
+		),
+
+		'reorder'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '排序(数值越大越靠前)',
+			'default' 	=> '1',
+			'desc' 		=> '请输入排序',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			'search'	=> 'order',
+			'list_name' => '排序',
+			'list'		=> true,
+			'order'		=> 'desc',
+			'edit'		=> true,
+		),
+		
+		'state'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '状态',
+			'default' 	=> '1',
+			'desc' 		=> '请选择状态',
+			'match' 	=> 'is_numeric',
+		),
+		
+		'cdate'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '录入时间',
+			'match' 	=> array('is_numeric', time()),
+			'desc' 		=> '',
+			# 只有insert时才生效
+			'insert'	=> true,
+			'search'	=> 'date',
+			'list'		=> 'date("Y-m-d H:i:s", {cdate})',
+		),
+	),
+
+	'manage' => array
+	(
+		
+	),
+
+	'request' => array
+	(
+		'getAll' => array
+		(
+			# 匹配的正则或函数 选填项
+			'option' => array
+			(
+				'town_id' => 'yes',
+				'state' => 1,
+			),
+			'type' => 'all',
+			'order' => array('reorder' => 'desc', 'id' => 'asc'),
+			'col' => 'id as value, name',
+		),
+
+		'getData' => array
+		(
+			# 匹配的正则或函数 选填项
+			'option' => array
+			(
+				'town_id' => 'yes',
+				'state' => 1,
+			),
+			'type' => 'all',
+			'order' => array('reorder' => 'desc', 'id' => 'asc'),
+			'col' => 'id, name|id',
+		),
+	)
+);

+ 38 - 7
src/Api.php

@@ -25,7 +25,7 @@ class Api
      */
     public function get()
     {
-        # 联动总数
+        # 联动总数,默认到县区
         $level_total = Dever::input('level_total', 3);
 
         # 当前联动级别
@@ -42,20 +42,28 @@ class Api
                 $default['name'] = '省份选择';
             } elseif ($level_num == 2) {
                 $default['name'] = '城市选择';
+            } elseif ($level_num == 3) {
+                $default['name'] = '县区选择';
+            } elseif ($level_num == 4) {
+                $default['name'] = '街道选择';
             } else {
-                $default['name'] = '地区选择';
+                $default['name'] = '区选择';
             }
         } else {
             $default = $this->default;
         }
 
-        # 级联动
+        # 级联动
         if ($level_num == 1) {
             $data = Dever::db('area/province')->getAll();
         } elseif ($level_num == 2) {
             $data = Dever::db('area/city')->getAll(array('province_id' => $level_id));
-        } else {
+        } elseif ($level_num == 3) {
             $data = Dever::db('area/county')->getAll(array('city_id' => $level_id));
+        } elseif ($level_num == 4) {
+            $data = Dever::db('area/town')->getAll(array('county_id' => $level_id));
+        } else {
+            $data = Dever::db('area/village')->getAll(array('town_id' => $level_id));
         }
 
         if (!$data) {
@@ -242,6 +250,10 @@ class Api
                     }
                 } 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;
@@ -269,6 +281,10 @@ class Api
                     }
                 } elseif ($k == 2) {
                     $result[$k] = $this->getName('county', $v);
+                } elseif ($k == 3) {
+                    $result[$k] = $this->getName('town', $v);
+                } elseif ($k == 4) {
+                    $result[$k] = $this->getName('village', $v);
                 }
             }
             return implode($im, $result);
@@ -294,10 +310,10 @@ class Api
     }
 
     # 获取6位地区编码
-    public function code($id)
+    public function code($id, $len = 6)
     {
-        $id = substr($id, 0, 6);
-        $id = str_pad($id, 6, '0', STR_PAD_RIGHT);
+        $id = substr($id, 0, $len);
+        $id = str_pad($id, $len, '0', STR_PAD_RIGHT);
         return $id;
     }
 
@@ -310,4 +326,19 @@ class Api
     {
         return Dever::db('area/city')->getAll(array('province_id' => $province_id));
     }
+
+    public function getCounty($city_id)
+    {
+        return Dever::db('area/county')->getAll(array('city_id' => $city_id));
+    }
+
+    public function getTown($county_id)
+    {
+        return Dever::db('area/town')->getAll(array('county_id' => $county_id));
+    }
+
+    public function getVillage($town_id)
+    {
+        return Dever::db('area/village')->getAll(array('town_id' => $town_id));
+    }
 }

+ 59 - 4
src/Import.php

@@ -6,7 +6,7 @@ use Dever;
 
 class Import
 {
-    private $url = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2018/';
+    private $url = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2020/';
 	/**
      * 获取国家统计局最新的地区数据
      *
@@ -73,6 +73,9 @@ class Import
     public function getCounty($city, $link)
     {
         $url = $this->url . $link;
+
+        $temp = explode('/', $link);
+        $link = $temp[0];
         
         $html = Dever::curl($url);
 
@@ -86,13 +89,65 @@ class Import
                 $update['id'] = $this->id($result[2][$k]);
                 $update['name'] = $v;
                 $update['city_id'] = $city;
-                Dever::upinto('area/county', $update, $update);
+                $id = Dever::upinto('area/county', $update, $update);
+
+                # 获取街道
+                $this->getTown($id, $link . '/' . $result[3][$k]);
+            }
+        }
+    }
+
+    public function getTown($county, $link)
+    {
+        $url = $this->url . $link;
+
+        $temp = explode('/', $link);
+        $link = $temp[0] . '/' . $temp[1];
+        
+        $html = Dever::curl($url);
+
+        $html = mb_convert_encoding($html, "UTF-8", "GB2312");
+
+        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])) {
+            foreach ($result[4] as $k => $v) {
+                $update['id'] = $this->id($result[2][$k], 9);
+                $update['name'] = $v;
+                $update['county_id'] = $county;
+                $id = Dever::upinto('area/town', $update, $update);
+
+                # 获取社区
+                $this->getVillage($id, $link . '/' . $result[3][$k]);
+            }
+        }
+    }
+
+    public function getVillage($town, $link)
+    {
+        $url = $this->url . $link;
+        
+        $html = Dever::curl($url);
+
+        $html = mb_convert_encoding($html, "UTF-8", "GB2312");
+
+        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'] = $v;
+                $update['town_id'] = $town;
+                Dever::upinto('area/village', $update, $update);
             }
         }
     }
 
-    public function id($id)
+    public function id($id, $len = 6)
     {
-        return Dever::load('area/api')->code($id);
+        return Dever::load('area/api')->code($id, $len);
     }
 }