控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Area.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 地区管理
  13. // +----------------------------------------------------------------------
  14. namespace app\admin\controller;
  15. use app\admin\model\Area as AreaModel;
  16. use app\common\controller\Adminbase;
  17. use think\Db;
  18. class Area extends Adminbase
  19. {
  20. protected $modelValidate = true;
  21. protected function initialize()
  22. {
  23. parent::initialize();
  24. $this->modelClass = new AreaModel;
  25. }
  26. //更新地区表
  27. public function refresh()
  28. {
  29. $config = get_addon_config('area');
  30. $amapKey = $config['amap_webapi_key'];
  31. $url = "http://restapi.amap.com/v3/config/district?key={$amapKey}&keywords=&subdistrict=3&extensions=base";
  32. $result = \util\Http::get($url);
  33. $resultArr = json_decode($result, true);
  34. if (isset($resultArr['status']) && $resultArr['status'] == 1) {
  35. Db::startTrans();
  36. try {
  37. //清空数据
  38. $this->modelClass->where('id', '<>', 0)->delete();
  39. $pinyin = new \Overtrue\Pinyin\Pinyin('Overtrue\Pinyin\MemoryFileDictLoader');
  40. $areArr = [];
  41. array_multisort(array_column($resultArr['districts'][0]['districts'], 'adcode'), SORT_ASC, $resultArr['districts'][0]['districts']);
  42. //省
  43. foreach ($resultArr['districts'][0]['districts'] as $i => $province) {
  44. [$lng, $lat] = explode(',', $province['center']);
  45. $level1 = [
  46. 'adcode' => $province['adcode'],
  47. 'level' => 1,
  48. 'name' => $province['name'],
  49. 'pid' => 0,
  50. 'pinyin' => $pinyin->permalink($province['name'], ''),
  51. 'first' => $pinyin->abbr($province['name']),
  52. 'lng' => $lng,
  53. 'lat' => $lat,
  54. 'child' => [],
  55. ];
  56. array_multisort(array_column($province['districts'], 'adcode'), SORT_ASC, $province['districts']);
  57. //市
  58. foreach ($province['districts'] as $j => $city) {
  59. [$lng, $lat] = explode(',', $city['center']);
  60. $level2 = [
  61. 'adcode' => $city['adcode'],
  62. 'level' => 2,
  63. 'name' => $city['name'],
  64. 'pinyin' => $pinyin->permalink($city['name'], ''),
  65. 'first' => $pinyin->abbr($city['name']),
  66. 'lng' => $lng,
  67. 'lat' => $lat,
  68. 'child' => [],
  69. ];
  70. array_multisort(array_column($city['districts'], 'adcode'), SORT_ASC, $city['districts']);
  71. //区
  72. foreach ($city['districts'] as $k => $area) {
  73. [$lng, $lat] = explode(',', $area['center']);
  74. $level3 = [
  75. 'adcode' => $area['adcode'],
  76. 'level' => 3,
  77. 'name' => $area['name'],
  78. 'pinyin' => $pinyin->permalink($area['name'], ''),
  79. 'first' => $pinyin->abbr($area['name']),
  80. 'lng' => $lng,
  81. 'lat' => $lat,
  82. 'child' => [],
  83. ];
  84. $level2['child'][$k] = $level3;
  85. }
  86. $level1['child'][$j] = $level2;
  87. }
  88. $areArr[$i] = $level1;
  89. }
  90. $this->areaUpdate($areArr);
  91. Db::commit();
  92. } catch (\Exception $e) {
  93. Db::rollback();
  94. $this->error("更新失败:" . $e->getMessage());
  95. }
  96. } else {
  97. if (isset($resultArr['infocode']) && $resultArr['infocode'] == 10001) {
  98. $this->error('高德地图key不正确或过期');
  99. } elseif (isset($resultArr['infocode']) && $resultArr['infocode']) {
  100. $this->error('错误码infocode:' . $resultArr['infocode']);
  101. } else {
  102. $this->error('异常错误!');
  103. }
  104. }
  105. $this->success("更新成功!");
  106. }
  107. protected function areaUpdate($arr, $pid = 0)
  108. {
  109. foreach ($arr as $k => $v) {
  110. $v['pid'] = $pid;
  111. $area = AreaModel::create($v, true);
  112. if ($v['child']) {
  113. $this->areaUpdate($v['child'], $area['id']);
  114. }
  115. }
  116. }
  117. }