rabin 3 年之前
父节点
当前提交
a1ed260f04
共有 1 个文件被更改,包括 40 次插入15 次删除
  1. 40 15
      src/Import.php

+ 40 - 15
src/Import.php

@@ -18,7 +18,7 @@ class Import
         
         $html = Dever::curl($url);
 
-        $html = mb_convert_encoding($html, "UTF-8", "GB2312");
+        $html = Dever::convert($html, "UTF-8", "GBK");
 
         preg_match_all('/<td><a href=\'(.*?)\'>(.*?)<br\/><\/a><\/td>/i', $html, $result);
 
@@ -32,27 +32,36 @@ class Import
 
     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);
                 $this->pinyin($update);
-                $id = Dever::upinto('area/province', $update, $update);
+                $id = $this->upinto('area/province', $update['id'], $update);
 
                 # 获取城市
-                $this->getCity($id, $update['name'], $result[1][$k]);
+                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 = Dever::curl($url);
 
-        $html = mb_convert_encoding($html, "UTF-8", "GB2312");
+        $html = Dever::convert($html, "UTF-8", "GBK");
 
         preg_match_all('/<tr class=\'citytr\'><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><\/tr>/i', $html, $result);
 
@@ -68,10 +77,15 @@ class Import
                 $update['province_id'] = $province;
 
                 $this->pinyin($update);
-                $id = Dever::upinto('area/city', $update, $update);
-
-                # 获取县区
-                $this->getCounty($province, $id, $result[3][$k]);
+                $id = $this->upinto('area/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]);
+                }
             }
         }
     }
@@ -85,7 +99,7 @@ class Import
         
         $html = Dever::curl($url);
 
-        $html = mb_convert_encoding($html, "UTF-8", "GB2312");
+        $html = Dever::convert($html, "UTF-8", "GBK");
 
         preg_match_all('/<tr class=\'countytr\'><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><\/tr>/i', $html, $result);
 
@@ -99,7 +113,7 @@ class Import
                 $update['area'] = $province . ',' . $city;
                 $this->setLevelCounty($update);
                 $this->pinyin($update);
-                $id = Dever::upinto('area/county', $update, $update);
+                $id = $this->upinto('area/county', $update['id'], $update);
 
                 # 获取街道
                 $this->getTown($province, $city, $id, $link . '/' . $result[3][$k]);
@@ -116,7 +130,7 @@ class Import
             $update['pinyin'] = $city_info['pinyin'];
             $update['pinyin_first'] = $city_info['pinyin_first'];
 
-            $id = Dever::upinto('area/county', $update, $update);
+            $id = $this->upinto('area/county', $update['id'], $update);
 
             # 获取街道
             $this->getTown($province, $city, $id, $source_link, $html);
@@ -133,7 +147,7 @@ class Import
             
             $html = Dever::curl($url);
 
-            $html = mb_convert_encoding($html, "UTF-8", "GB2312");
+            $html = Dever::convert($html, "UTF-8", "GBK");
         }
         if (!$link && !$html) {
             return;
@@ -151,7 +165,7 @@ class Import
                 $update['province_id'] = $province;
                 $update['area'] = $province . ',' . $city . ',' . $county;
                 $this->pinyin($update);
-                $id = Dever::upinto('area/town', $update, $update);
+                $id = $this->upinto('area/town', $update['id'], $update);
 
                 # 获取社区
                 //$this->getVillage($province, $city, $county, $id, $link . '/' . $result[3][$k]);
@@ -165,7 +179,7 @@ class Import
         
         $html = Dever::curl($url);
 
-        $html = mb_convert_encoding($html, "UTF-8", "GB2312");
+        $html = Dever::convert($html, "UTF-8", "GBK");
 
         preg_match_all('/<tr class=\'villagetr\'><td>(.*?)<\/td><td>(.*?)<\/td><td>(.*?)<\/td><\/tr>/i', $html, $result);
 
@@ -181,7 +195,7 @@ class Import
                 $update['province_id'] = $province;
                 $update['area'] = $province . ',' . $city . ',' . $county . ',' . $town;
                 $this->pinyin($update);
-                Dever::upinto('area/village', $update, $update);
+                $this->upinto('area/village', $update['id'], $update);
             }
         }
     }
@@ -224,4 +238,15 @@ class Import
             $update['level'] = 2;
         }
     }
+
+    public function upinto($table, $id, $data)
+    {
+        $info = Dever::db($table)->one($id);
+        if (!$info) {
+            return Dever::db($table)->insert($data);
+        } else {
+            $data['where_id'] = $info['id'];
+            return Dever::db($table)->update($data);
+        }
+    }
 }