Import.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace Area\Src;
  3. set_time_limit(0);
  4. use Dever;
  5. class Import
  6. {
  7. private $url = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2020/';
  8. /**
  9. * 获取国家统计局最新的地区数据
  10. *
  11. * @return mixed
  12. */
  13. public function load()
  14. {
  15. $url = $this->url . 'index.html';
  16. $html = Dever::curl($url);
  17. $html = mb_convert_encoding($html, "UTF-8", "GB2312");
  18. preg_match_all('/<td><a href=\'(.*?)\'>(.*?)<br\/><\/a><\/td>/i', $html, $result);
  19. Dever::config('base')->hook = true;
  20. # 获取省份
  21. $this->getProvince($result);
  22. return 1;
  23. }
  24. public function getProvince($result)
  25. {
  26. $update = array();
  27. if (isset($result[1]) && isset($result[2])) {
  28. foreach ($result[2] as $k => $v) {
  29. $update['id'] = $this->id(trim($result[1][$k], '.html'));
  30. $update['name'] = strip_tags($v);
  31. $this->pinyin($update);
  32. $id = Dever::upinto('area/province', $update, $update);
  33. # 获取城市
  34. $this->getCity($id, $update['name'], $result[1][$k]);
  35. }
  36. }
  37. }
  38. public function getCity($province, $province_name, $link)
  39. {
  40. $url = $this->url . $link;
  41. $html = Dever::curl($url);
  42. $html = mb_convert_encoding($html, "UTF-8", "GB2312");
  43. preg_match_all('/<tr class=\'citytr\'><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><\/tr>/i', $html, $result);
  44. $update = array();
  45. if (isset($result[3]) && isset($result[4])) {
  46. foreach ($result[4] as $k => $v) {
  47. $v = strip_tags($v);
  48. if ($v == '市辖区') {
  49. $v = $province_name;
  50. }
  51. $update['id'] = $this->id($result[2][$k]);
  52. $update['name'] = $v;
  53. $update['province_id'] = $province;
  54. $this->pinyin($update);
  55. $id = Dever::upinto('area/city', $update, $update);
  56. # 获取县区
  57. $this->getCounty($province, $id, $result[3][$k]);
  58. }
  59. }
  60. }
  61. public function getCounty($province, $city, $link)
  62. {
  63. $url = $this->url . $link;
  64. $temp = explode('/', $link);
  65. $link = $temp[0];
  66. $html = Dever::curl($url);
  67. $html = mb_convert_encoding($html, "UTF-8", "GB2312");
  68. preg_match_all('/<tr class=\'countytr\'><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><\/tr>/i', $html, $result);
  69. $update = array();
  70. if (isset($result[3]) && isset($result[4])) {
  71. foreach ($result[4] as $k => $v) {
  72. $update['id'] = $this->id($result[2][$k]);
  73. $update['name'] = strip_tags($v);
  74. $update['city_id'] = $city;
  75. $update['province_id'] = $province;
  76. $update['area'] = $province . ',' . $city;
  77. $num = substr($update['id'], 4);
  78. # type = 1城区 2郊区 3县城 4经济技术开发 5县级市
  79. if ($v == '门头沟区') {
  80. $update['type'] = 2;
  81. $update['level'] = 2;
  82. } elseif ($num <= 10) {
  83. $update['type'] = 1;
  84. $update['level'] = 1;
  85. } elseif ($num > 10 && $num <= 20) {
  86. $update['type'] = 2;
  87. $update['level'] = 2;
  88. } elseif ($num > 20 && $num <= 70) {
  89. $update['type'] = 3;
  90. $update['level'] = 3;
  91. } elseif ($num > 70 && $num <= 80) {
  92. $update['type'] = 4;
  93. $update['level'] = 2;
  94. } elseif ($num >= 80) {
  95. $update['type'] = 5;
  96. $update['level'] = 2;
  97. }
  98. $this->pinyin($update);
  99. $id = Dever::upinto('area/county', $update, $update);
  100. # 获取街道
  101. $this->getTown($province, $city, $id, $link . '/' . $result[3][$k]);
  102. }
  103. } else {
  104. $city_info = Dever::db('area/city')->find(array('id' => $city, 'clear' => true));
  105. $update['id'] = $city_info['id'];
  106. $update['name'] = $city_info['name'];
  107. $update['city_id'] = $city;
  108. $update['province_id'] = $province;
  109. $update['area'] = $province . ',' . $city;
  110. $update['type'] = 1;
  111. $update['level'] = 1;
  112. $update['pinyin'] = $city_info['pinyin'];
  113. $update['pinyin_first'] = $city_info['pinyin_first'];
  114. $id = Dever::upinto('area/county', $update, $update);
  115. # 获取街道
  116. $this->getTown($province, $city, $id, false, $html);
  117. }
  118. }
  119. public function getTown($province, $city, $county, $link = false, $html = false)
  120. {
  121. if ($link) {
  122. $url = $this->url . $link;
  123. $temp = explode('/', $link);
  124. $link = $temp[0] . '/' . $temp[1];
  125. $html = Dever::curl($url);
  126. $html = mb_convert_encoding($html, "UTF-8", "GB2312");
  127. }
  128. if (!$link && !$html) {
  129. return;
  130. }
  131. preg_match_all('/<tr class=\'towntr\'><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><td><a href=\'(.*?)\'>(.*?)<\/a><\/td><\/tr>/i', $html, $result);
  132. $update = array();
  133. if (isset($result[3]) && isset($result[4])) {
  134. foreach ($result[4] as $k => $v) {
  135. $update['id'] = $this->id($result[2][$k], 9);
  136. $update['name'] = strip_tags($v);
  137. $update['county_id'] = $county;
  138. $update['city_id'] = $city;
  139. $update['province_id'] = $province;
  140. $update['area'] = $province . ',' . $city . ',' . $county;
  141. $this->pinyin($update);
  142. $id = Dever::upinto('area/town', $update, $update);
  143. # 获取社区
  144. //$this->getVillage($province, $city, $county, $id, $link . '/' . $result[3][$k]);
  145. }
  146. }
  147. }
  148. public function getVillage($province, $city, $county, $town, $link)
  149. {
  150. $url = $this->url . $link;
  151. $html = Dever::curl($url);
  152. $html = mb_convert_encoding($html, "UTF-8", "GB2312");
  153. preg_match_all('/<tr class=\'villagetr\'><td>(.*?)<\/td><td>(.*?)<\/td><td>(.*?)<\/td><\/tr>/i', $html, $result);
  154. $update = array();
  155. if (isset($result[1]) && isset($result[2]) && isset($result[3])) {
  156. foreach ($result[3] as $k => $v) {
  157. $update['id'] = $this->id($result[1][$k], 12);
  158. $update['code'] = $result[2][$k];
  159. $update['name'] = strip_tags($v);
  160. $update['town_id'] = $town;
  161. $update['county_id'] = $county;
  162. $update['city_id'] = $city;
  163. $update['province_id'] = $province;
  164. $update['area'] = $province . ',' . $city . ',' . $county . ',' . $town;
  165. $this->pinyin($update);
  166. Dever::upinto('area/village', $update, $update);
  167. }
  168. }
  169. }
  170. public function id($id, $len = 6)
  171. {
  172. return Dever::load('area/api')->code($id, $len);
  173. }
  174. public function pinyin(&$update)
  175. {
  176. if (Dever::import('pinyin') && $update['name']) {
  177. $update['pinyin'] = Dever::getPinyin($update['name']);
  178. $update['pinyin_first'] = Dever::getPinyinFirst($update['name']);
  179. }
  180. }
  181. }