rabin 6 gadi atpakaļ
vecāks
revīzija
c6de4c3bd3
2 mainītis faili ar 17 papildinājumiem un 1 dzēšanām
  1. 8 0
      src/Api.php
  2. 9 1
      src/Import.php

+ 8 - 0
src/Api.php

@@ -259,4 +259,12 @@ class Api
         }
         return $name;
     }
+
+    # 获取6位地区编码
+    public function code($id)
+    {
+        $id = substr($id, 0, 6);
+        $id = str_pad($id, 6, '0', STR_PAD_RIGHT);
+        return $id;
+    }
 }

+ 9 - 1
src/Import.php

@@ -6,7 +6,7 @@ use Dever;
 
 class Import
 {
-    private $url = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/';
+    private $url = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2018/';
 	/**
      * 获取国家统计局最新的地区数据
      *
@@ -33,6 +33,7 @@ class Import
         $update = array();
         if (isset($result[1]) && isset($result[2])) {
             foreach ($result[2] as $k => $v) {
+                $update['id'] = $this->id(trim($result[1][$k], '.html'));
                 $update['name'] = $v;
                 $id = Dever::upinto('area/province', $update, $update);
 
@@ -58,6 +59,7 @@ class Import
                 if ($v == '市辖区') {
                     $v = $province_name;
                 }
+                $update['id'] = $this->id($result[2][$k]);
                 $update['name'] = $v;
                 $update['province_id'] = $province;
                 $id = Dever::upinto('area/city', $update, $update);
@@ -81,10 +83,16 @@ class Import
         $update = array();
         if (isset($result[3]) && isset($result[4])) {
             foreach ($result[4] as $k => $v) {
+                $update['id'] = $this->id($result[2][$k]);
                 $update['name'] = $v;
                 $update['city_id'] = $city;
                 Dever::upinto('area/county', $update, $update);
             }
         }
     }
+
+    public function id($id)
+    {
+        return Dever::load('area/api')->code($id);
+    }
 }