dever 6 years ago
commit
c7cbb38bc9
7 changed files with 533 additions and 0 deletions
  1. 7 0
      daemon/import.php
  2. 117 0
      database/city.php
  3. 117 0
      database/county.php
  4. 98 0
      database/province.php
  5. 8 0
      index.php
  6. 96 0
      src/Api.php
  7. 90 0
      src/Import.php

+ 7 - 0
daemon/import.php

@@ -0,0 +1,7 @@
+<?php
+
+define('DEVER_DAEMON', true);
+
+include(dirname(__FILE__) . DIRECTORY_SEPARATOR . '../index.php');
+
+Dever::load('area/import.load');

+ 117 - 0
database/city.php

@@ -0,0 +1,117 @@
+<?php
+
+$province = function()
+{
+	$array = array();
+	$data = Dever::load('area/province-state');
+	if($data)
+	{
+		$array += $data;
+	}
+	return $array;
+};
+
+return array
+(
+	# 表名
+	'name' => 'city',
+	# 显示给用户看的名称
+	'lang' => '城市',
+	# 是否显示在后台菜单
+	'order' => 11,
+	# 数据结构
+	'struct' => array
+	(
+		'id' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+			'order'		=> 'asc',
+			'list'		=> true,
+		),
+
+		'name'		=> array
+		(
+			'type' 		=> 'varchar-50',
+			'name' 		=> '城市名称',
+			'default' 	=> '',
+			'desc' 		=> '城市名称',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
+		'province_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '省份',
+			'default' 	=> '0',
+			'desc' 		=> '省份',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'select',
+			'option'	=> $province,
+			'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
+			(
+				'province_id' => 'yes',
+				'state' => 1,
+			),
+			'type' => 'all',
+			'order' => array('reorder' => 'desc', 'id' => 'asc'),
+			'col' => 'id as value, name',
+		),
+	)
+);

+ 117 - 0
database/county.php

@@ -0,0 +1,117 @@
+<?php
+
+$city = function()
+{
+	$array = array();
+	$data = Dever::load('area/city-state');
+	if($data)
+	{
+		$array += $data;
+	}
+	return $array;
+};
+
+return array
+(
+	# 表名
+	'name' => 'county',
+	# 显示给用户看的名称
+	'lang' => '县区',
+	# 是否显示在后台菜单
+	'order' => 12,
+	# 数据结构
+	'struct' => array
+	(
+		'id' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+			'order'		=> 'asc',
+			'list'		=> true,
+		),
+
+		'name'		=> array
+		(
+			'type' 		=> 'varchar-50',
+			'name' 		=> '县区名称',
+			'default' 	=> '',
+			'desc' 		=> '县区名称',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
+		'city_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '城市',
+			'default' 	=> '0',
+			'desc' 		=> '城市',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'select',
+			'option'	=> $city,
+			'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
+			(
+				'city_id' => 'yes',
+				'state' => 1,
+			),
+			'type' => 'all',
+			'order' => array('reorder' => 'desc', 'id' => 'asc'),
+			'col' => 'id as value, name',
+		),
+	)
+);

+ 98 - 0
database/province.php

@@ -0,0 +1,98 @@
+<?php
+
+$info = Dever::load('manage/project.get');
+
+$path = $info['area']['path'];
+
+return array
+(
+	# 表名
+	'name' => 'province',
+	# 显示给用户看的名称
+	'lang' => '省份',
+	# 是否显示在后台菜单
+	'order' => 10,
+	'desc' => '导入数据请访问:' . Dever::url('area/import.load') . '<br />或者在服务器执行:php '.$path.'daemon/import.php',
+	# 数据结构
+	'struct' => array
+	(
+		'id' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+			'order'		=> 'asc',
+			'list'		=> true,
+		),
+
+		'name'		=> array
+		(
+			'type' 		=> 'varchar-50',
+			'name' 		=> '省份名称',
+			'default' 	=> '',
+			'desc' 		=> '省份名称',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'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
+			(
+				'state' => 1,
+			),
+			'type' => 'all',
+			'order' => array('reorder' => 'desc', 'id' => 'asc'),
+			'col' => 'id as value, name',
+		),
+	)
+);

+ 8 - 0
index.php

@@ -0,0 +1,8 @@
+<?php
+
+define('DEVER_APP_NAME', 'area');
+define('DEVER_APP_LANG', '地区设置');
+define('DEVER_APP_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);
+define('DEVER_MANAGE_ORDER', -10);
+define('DEVER_MANAGE_ICON', 'glyphicon glyphicon-compressed');
+include(DEVER_APP_PATH . '../boot.php');

+ 96 - 0
src/Api.php

@@ -0,0 +1,96 @@
+<?php
+
+namespace Area\Src;
+
+use Dever;
+
+class Api
+{
+    private $default = array
+    (
+        'value' => -1,
+        'name' => '不限',
+    );
+
+    private $search_default = array
+    (
+        'value' => -1,
+        'name' => '不选择',
+    );
+
+	/**
+     * 获取地区数据
+     *
+     * @return mixed
+     */
+    public function get()
+    {
+        # 联动总数
+        $level_total = 3;
+
+        # 当前联动级别
+        $level_num = Dever::input('level_num');
+
+        # 一般为id
+        $level_id = Dever::input('level_id');
+
+        # 是否是搜索列表页
+        $level_search = Dever::input('level_search');
+        if ($level_search) {
+            $default = $this->search_default;
+        } 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 {
+            $data = Dever::db('area/county')->getAll(array('city_id' => $level_id));
+        }
+
+        if ($level_search || $level_num > 2) {
+            array_unshift($data, $default);
+        }
+
+        $result['level_total'] = $level_total;
+        $result['list'] = $data;
+        return $result;
+    }
+
+    /**
+     * 根据地区id转成名称
+     *
+     * @return mixed
+     */
+    public function string($area)
+    {
+        $area = explode(',', $area);
+        $result = array();
+        foreach ($area as $k => $v) {
+            if ($k == 0) {
+                $result[$k] = $this->getName('province', $v);
+            } elseif ($k == 1) {
+                $result[$k] = $this->getName('city', $v);
+            } elseif ($k == 2) {
+                $result[$k] = $this->getName('county', $v);
+            }
+        }
+        return implode(',', $result);
+    }
+
+
+    private function getName($table, $id)
+    {
+        $name = '不限';
+        if ($id > 0) {
+            $data = Dever::db('area/' . $table)->one($id);
+            if ($data) {
+                $name = $data['name'];
+            }
+        }
+        return $name;
+    }
+}

+ 90 - 0
src/Import.php

@@ -0,0 +1,90 @@
+<?php
+
+namespace Area\Src;
+
+use Dever;
+
+class Import
+{
+    private $url = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/';
+	/**
+     * 获取国家统计局最新的地区数据
+     *
+     * @return mixed
+     */
+    public function load()
+    {
+        $url = $this->url . 'index.html';
+        
+        $html = Dever::curl($url);
+
+        $html = mb_convert_encoding($html, "UTF-8", "GB2312");
+
+        preg_match_all('/<td><a href=\'(.*?)\'>(.*?)<br\/><\/a><\/td>/i', $html, $result);
+
+        # 获取省份
+        $this->getProvince($result);
+
+        return 1;
+    }
+
+    public function getProvince($result)
+    {
+        $update = array();
+        if (isset($result[1]) && isset($result[2])) {
+            foreach ($result[2] as $k => $v) {
+                $update['name'] = $v;
+                $id = Dever::upinto('area/province', $update, $update);
+
+                # 获取城市
+                $this->getCity($id, $update['name'], $result[1][$k]);
+            }
+        }
+    }
+
+    public function getCity($province, $province_name, $link)
+    {
+        $url = $this->url . $link;
+        
+        $html = Dever::curl($url);
+
+        $html = mb_convert_encoding($html, "UTF-8", "GB2312");
+
+        preg_match_all('/<tr class=\'citytr\'><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) {
+                if ($v == '市辖区') {
+                    $v = $province_name;
+                }
+                $update['name'] = $v;
+                $update['province_id'] = $province;
+                $id = Dever::upinto('area/city', $update, $update);
+
+                # 获取县区
+                $this->getCounty($id, $result[3][$k]);
+            }
+        }
+    }
+
+    public function getCounty($city, $link)
+    {
+        $url = $this->url . $link;
+        
+        $html = Dever::curl($url);
+
+        $html = mb_convert_encoding($html, "UTF-8", "GB2312");
+
+        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])) {
+            foreach ($result[4] as $k => $v) {
+                $update['name'] = $v;
+                $update['city_id'] = $city;
+                Dever::upinto('area/county', $update, $update);
+            }
+        }
+    }
+}