dever 3 سال پیش
والد
کامیت
c4ba303d30
7فایلهای تغییر یافته به همراه279 افزوده شده و 133 حذف شده
  1. 28 4
      database/city.php
  2. 28 4
      database/county.php
  3. 28 4
      database/province.php
  4. 28 4
      database/town.php
  5. 28 4
      database/village.php
  6. 125 113
      src/Api.php
  7. 14 0
      src/Import.php

+ 28 - 4
database/city.php

@@ -45,6 +45,30 @@ return array
 			'list'		=> true,
 		),
 
+		'pinyin'		=> array
+		(
+			'type' 		=> 'varchar-300',
+			'name' 		=> '拼音',
+			'default' 	=> '',
+			'desc' 		=> '拼音',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
+		'pinyin_first'		=> array
+		(
+			'type' 		=> 'varchar-30',
+			'name' 		=> '拼音首字母',
+			'default' 	=> '',
+			'desc' 		=> '拼音首字母',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
 		'province_id'		=> array
 		(
 			'type' 		=> 'int-11',
@@ -110,8 +134,8 @@ return array
 				'state' => 1,
 			),
 			'type' => 'all',
-			'order' => array('reorder' => 'desc', 'id' => 'asc'),
-			'col' => 'id,id as value, name',
+			'order' => array('pinyin_first' => 'asc', 'reorder' => 'desc', 'id' => 'asc'),
+			'col' => 'id,id as value, name, pinyin, pinyin_first',
 		),
 
 		'getData' => array
@@ -123,8 +147,8 @@ return array
 				'state' => 1,
 			),
 			'type' => 'all',
-			'order' => array('reorder' => 'desc', 'id' => 'asc'),
-			'col' => 'id, name|id',
+			'order' => array('pinyin_first' => 'asc', 'reorder' => 'desc', 'id' => 'asc'),
+			'col' => 'id, name, pinyin, pinyin_first|id',
 		),
 	)
 );

+ 28 - 4
database/county.php

@@ -45,6 +45,30 @@ return array
 			'list'		=> true,
 		),
 
+		'pinyin'		=> array
+		(
+			'type' 		=> 'varchar-300',
+			'name' 		=> '拼音',
+			'default' 	=> '',
+			'desc' 		=> '拼音',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
+		'pinyin_first'		=> array
+		(
+			'type' 		=> 'varchar-30',
+			'name' 		=> '拼音首字母',
+			'default' 	=> '',
+			'desc' 		=> '拼音首字母',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
 		'city_id'		=> array
 		(
 			'type' 		=> 'int-11',
@@ -110,8 +134,8 @@ return array
 				'state' => 1,
 			),
 			'type' => 'all',
-			'order' => array('reorder' => 'desc', 'id' => 'asc'),
-			'col' => 'id as value, name',
+			'order' => array('pinyin_first' => 'asc', 'reorder' => 'desc', 'id' => 'asc'),
+			'col' => 'id as value, name, pinyin, pinyin_first',
 		),
 
 		'getData' => array
@@ -123,8 +147,8 @@ return array
 				'state' => 1,
 			),
 			'type' => 'all',
-			'order' => array('reorder' => 'desc', 'id' => 'asc'),
-			'col' => 'id, name|id',
+			'order' => array('pinyin_first' => 'asc', 'reorder' => 'desc', 'id' => 'asc'),
+			'col' => 'id, name, pinyin, pinyin_first|id',
 		),
 	)
 );

+ 28 - 4
database/province.php

@@ -39,6 +39,30 @@ return array
 			'list'		=> true,
 		),
 
+		'pinyin'		=> array
+		(
+			'type' 		=> 'varchar-300',
+			'name' 		=> '拼音',
+			'default' 	=> '',
+			'desc' 		=> '拼音',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
+		'pinyin_first'		=> array
+		(
+			'type' 		=> 'varchar-30',
+			'name' 		=> '拼音首字母',
+			'default' 	=> '',
+			'desc' 		=> '拼音首字母',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
 		'reorder'		=> array
 		(
 			'type' 		=> 'int-11',
@@ -91,8 +115,8 @@ return array
 				'state' => 1,
 			),
 			'type' => 'all',
-			'order' => array('reorder' => 'desc', 'id' => 'asc'),
-			'col' => 'id,id as value, name',
+			'order' => array('pinyin_first' => 'asc', 'reorder' => 'desc', 'id' => 'asc'),
+			'col' => 'id,id as value, name, pinyin, pinyin_first',
 		),
 
 		'getData' => array
@@ -103,8 +127,8 @@ return array
 				'state' => 1,
 			),
 			'type' => 'all',
-			'order' => array('reorder' => 'desc', 'id' => 'asc'),
-			'col' => 'id, name',
+			'order' => array('pinyin_first' => 'asc', 'reorder' => 'desc', 'id' => 'asc'),
+			'col' => 'id, name, pinyin, pinyin_first',
 		),
 	)
 );

+ 28 - 4
database/town.php

@@ -45,6 +45,30 @@ return array
 			'list'		=> true,
 		),
 
+		'pinyin'		=> array
+		(
+			'type' 		=> 'varchar-300',
+			'name' 		=> '拼音',
+			'default' 	=> '',
+			'desc' 		=> '拼音',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
+		'pinyin_first'		=> array
+		(
+			'type' 		=> 'varchar-30',
+			'name' 		=> '拼音首字母',
+			'default' 	=> '',
+			'desc' 		=> '拼音首字母',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
 		'county_id'		=> array
 		(
 			'type' 		=> 'int-11',
@@ -110,8 +134,8 @@ return array
 				'state' => 1,
 			),
 			'type' => 'all',
-			'order' => array('reorder' => 'desc', 'id' => 'asc'),
-			'col' => 'id as value, name',
+			'order' => array('pinyin_first' => 'asc', 'reorder' => 'desc', 'id' => 'asc'),
+			'col' => 'id as value, name, pinyin, pinyin_first',
 		),
 
 		'getData' => array
@@ -123,8 +147,8 @@ return array
 				'state' => 1,
 			),
 			'type' => 'all',
-			'order' => array('reorder' => 'desc', 'id' => 'asc'),
-			'col' => 'id, name|id',
+			'order' => array('pinyin_first' => 'asc', 'reorder' => 'desc', 'id' => 'asc'),
+			'col' => 'id, name, pinyin, pinyin_first|id',
 		),
 	)
 );

+ 28 - 4
database/village.php

@@ -45,6 +45,30 @@ return array
 			'list'		=> true,
 		),
 
+		'pinyin'		=> array
+		(
+			'type' 		=> 'varchar-300',
+			'name' 		=> '拼音',
+			'default' 	=> '',
+			'desc' 		=> '拼音',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
+		'pinyin_first'		=> array
+		(
+			'type' 		=> 'varchar-30',
+			'name' 		=> '拼音首字母',
+			'default' 	=> '',
+			'desc' 		=> '拼音首字母',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
 		'code'		=> array
 		(
 			'type' 		=> 'int-11',
@@ -122,8 +146,8 @@ return array
 				'state' => 1,
 			),
 			'type' => 'all',
-			'order' => array('reorder' => 'desc', 'id' => 'asc'),
-			'col' => 'id as value, name',
+			'order' => array('pinyin_first' => 'asc', 'reorder' => 'desc', 'id' => 'asc'),
+			'col' => 'id as value, name, pinyin, pinyin_first',
 		),
 
 		'getData' => array
@@ -135,8 +159,8 @@ return array
 				'state' => 1,
 			),
 			'type' => 'all',
-			'order' => array('reorder' => 'desc', 'id' => 'asc'),
-			'col' => 'id, name|id',
+			'order' => array('pinyin_first' => 'asc', 'reorder' => 'desc', 'id' => 'asc'),
+			'col' => 'id, name, pinyin, pinyin_first|id',
 		),
 	)
 );

+ 125 - 113
src/Api.php

@@ -82,7 +82,131 @@ class Api
     }
 
     /**
-     * 获取所有地区数据:json格式,生成js文件
+     * 获取详细信息
+     *
+     * @return mixed
+     */
+    public function getInfo($area, $col = 'id')
+    {
+        if ($area) {
+            $area = explode(',', $area);
+            $result = array();
+            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 array();
+    }
+
+    /**
+     * 根据地区id转成名称
+     *
+     * @return mixed
+     */
+    public function string($area, $im = ',', $unset = true)
+    {
+        if ($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);
+                    if ($result[0] == $result[1] && $unset) {
+                        unset($result[1]);
+                    }
+                } 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);
+        }
+        return '';
+    }
+
+
+    private function getName($table, $value, $state = false, $col = 'id')
+    {
+        $name = '不限';
+        if (($col == 'id' && $value > 0) || ($col != 'id' && $value)) {
+            $where[$col] = $value;
+            $data = Dever::db('area/' . $table)->one($where);
+            if ($state) {
+                return $data;
+            }
+            if ($data) {
+                $name = $data['name'];
+            }
+        }
+        return $name;
+    }
+
+    # 获取6位地区编码
+    public function code($id, $len = 6)
+    {
+        $id = substr($id, 0, $len);
+        $id = str_pad($id, $len, '0', STR_PAD_RIGHT);
+        return $id;
+    }
+
+    public function getProvince()
+    {
+        return Dever::db('area/province')->getAll();
+    }
+
+    public function getCity($province_id)
+    {
+        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));
+    }
+
+    # 获取城市并根据首字母排序的
+    public function getCityToFirst()
+    {
+        $result = array();
+        $data = Dever::db('area/city')->getAll();
+        if (Dever::import('pinyin')) {
+            $result = Dever::sortPinyinFirst($data, 'pinyin_first');
+        }
+
+        return $result;
+    }
+
+    /**
+     * 获取三级地区数据:json格式,生成js文件
      *
      * @return mixed
      */
@@ -229,116 +353,4 @@ class Api
         $assets = Dever::config('host')->uploadRes;
         return str_replace($path, $assets, $file);
     }
-
-    /**
-     * 获取详细信息
-     *
-     * @return mixed
-     */
-    public function getInfo($area, $col = 'id')
-    {
-        if ($area) {
-            $area = explode(',', $area);
-            $result = array();
-            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 array();
-    }
-
-    /**
-     * 根据地区id转成名称
-     *
-     * @return mixed
-     */
-    public function string($area, $im = ',', $unset = true)
-    {
-        if ($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);
-                    if ($result[0] == $result[1] && $unset) {
-                        unset($result[1]);
-                    }
-                } 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);
-        }
-        return '';
-    }
-
-
-    private function getName($table, $value, $state = false, $col = 'id')
-    {
-        $name = '不限';
-        if (($col == 'id' && $value > 0) || ($col != 'id' && $value)) {
-            $where[$col] = $value;
-            $data = Dever::db('area/' . $table)->one($where);
-            if ($state) {
-                return $data;
-            }
-            if ($data) {
-                $name = $data['name'];
-            }
-        }
-        return $name;
-    }
-
-    # 获取6位地区编码
-    public function code($id, $len = 6)
-    {
-        $id = substr($id, 0, $len);
-        $id = str_pad($id, $len, '0', STR_PAD_RIGHT);
-        return $id;
-    }
-
-    public function getProvince()
-    {
-        return Dever::db('area/province')->getAll();
-    }
-
-    public function getCity($province_id)
-    {
-        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));
-    }
 }

+ 14 - 0
src/Import.php

@@ -35,6 +35,7 @@ class Import
             foreach ($result[2] as $k => $v) {
                 $update['id'] = $this->id(trim($result[1][$k], '.html'));
                 $update['name'] = $v;
+                $this->pinyin($update);
                 $id = Dever::upinto('area/province', $update, $update);
 
                 # 获取城市
@@ -62,6 +63,8 @@ class Import
                 $update['id'] = $this->id($result[2][$k]);
                 $update['name'] = $v;
                 $update['province_id'] = $province;
+
+                $this->pinyin($update);
                 $id = Dever::upinto('area/city', $update, $update);
 
                 # 获取县区
@@ -89,6 +92,7 @@ class Import
                 $update['id'] = $this->id($result[2][$k]);
                 $update['name'] = $v;
                 $update['city_id'] = $city;
+                $this->pinyin($update);
                 $id = Dever::upinto('area/county', $update, $update);
 
                 # 获取街道
@@ -116,6 +120,7 @@ class Import
                 $update['id'] = $this->id($result[2][$k], 9);
                 $update['name'] = $v;
                 $update['county_id'] = $county;
+                $this->pinyin($update);
                 $id = Dever::upinto('area/town', $update, $update);
 
                 # 获取社区
@@ -141,6 +146,7 @@ class Import
                 $update['code'] = $result[2][$k];
                 $update['name'] = $v;
                 $update['town_id'] = $town;
+                $this->pinyin($update);
                 Dever::upinto('area/village', $update, $update);
             }
         }
@@ -150,4 +156,12 @@ class Import
     {
         return Dever::load('area/api')->code($id, $len);
     }
+
+    public function pinyin(&$update)
+    {
+        if (Dever::import('pinyin') && $update['name']) {
+            $update['pinyin'] = Dever::getPinyin($update['name']);
+            $update['pinyin_first'] = Dever::getPinyinFirst($update['name']);
+        }
+    }
 }