Json.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php namespace Area\Lib\Import;
  2. set_time_limit(0);
  3. use Dever;
  4. use Area\Lib\Data;
  5. class Json extends Core
  6. {
  7. private $url = 'https://github.com/modood/Administrative-divisions-of-China';
  8. public function getUrl()
  9. {
  10. return $this->url;
  11. }
  12. public function get()
  13. {
  14. $this->getProvince();
  15. $this->getCity();
  16. $this->getCounty();
  17. $this->getTown();
  18. return 'ok';
  19. }
  20. private function load($type)
  21. {
  22. $file = DEVER_APP_PATH . 'file/'.$type.'.json';
  23. $content = file_get_contents($file);
  24. $content = json_decode($content, true);
  25. return $content;
  26. }
  27. public function getProvince()
  28. {
  29. $data = $this->load('provinces');
  30. if ($data) {
  31. foreach ($data as $k => $v) {
  32. $update['id'] = $this->id($v['code']);
  33. $update['name'] = $v['name'];
  34. $update = Dever::load(Data::class)->pinyin($update);
  35. $this->up('province', $update['id'], $update);
  36. }
  37. }
  38. }
  39. public function getCity()
  40. {
  41. $data = $this->load('cities');
  42. if ($data) {
  43. foreach ($data as $k => $v) {
  44. $update['id'] = $this->id($v['code']);
  45. $update['name'] = $v['name'];
  46. $update['province_id'] = $this->id($v['provinceCode']);
  47. $update = Dever::load(Data::class)->pinyin($update);
  48. $this->up('city', $update['id'], $update);
  49. }
  50. }
  51. }
  52. public function getCounty()
  53. {
  54. $data = $this->load('areas');
  55. if ($data) {
  56. foreach ($data as $k => $v) {
  57. $update['id'] = $this->id($v['code']);
  58. $update['name'] = $v['name'];
  59. $update['city_id'] = $this->id($v['cityCode']);
  60. $update['province_id'] = $this->id($v['provinceCode']);
  61. $update['area'] = $update['province_id'] . ',' . $update['city_id'];
  62. $this->setLevelCounty($update);
  63. $update = Dever::load(Data::class)->pinyin($update);
  64. $this->up('county', $update['id'], $update);
  65. }
  66. }
  67. }
  68. public function getTown()
  69. {
  70. $data = $this->load('streets');
  71. if ($data) {
  72. foreach ($data as $k => $v) {
  73. $update['id'] = $this->id($v['code'], 9);
  74. $update['name'] = $v['name'];
  75. $update['county_id'] = $this->id($v['areaCode']);
  76. $update['city_id'] = $this->id($v['cityCode']);
  77. $update['province_id'] = $this->id($v['provinceCode']);
  78. $update['area'] = $update['province_id'] . ',' . $update['city_id'] . ',' . $update['county_id'];
  79. $this->setLevelCounty($update);
  80. $update = Dever::load(Data::class)->pinyin($update);
  81. $this->up('town', $update['id'], $update);
  82. }
  83. }
  84. }
  85. }