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.

CityAreaServices.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace app\services\other;
  3. use app\dao\other\CityAreaDao;
  4. use app\services\BaseServices;
  5. use crmeb\exceptions\AdminException;
  6. use crmeb\services\CacheService;
  7. use crmeb\services\FormBuilder as Form;
  8. /**
  9. * 城市数据(街道)
  10. * Class CityAreaServices
  11. * @package app\services\other
  12. */
  13. class CityAreaServices extends BaseServices
  14. {
  15. /**
  16. * 城市类型
  17. * @var string[]
  18. */
  19. public $type = [
  20. '1' => 'province',
  21. '2' => 'city',
  22. '3' => 'area',
  23. '4' => 'street'
  24. ];
  25. /**
  26. * CityAreaServices constructor.
  27. * @param CityAreaDao $dao
  28. */
  29. public function __construct(CityAreaDao $dao)
  30. {
  31. $this->dao = $dao;
  32. }
  33. /**
  34. * 获取某一个城市id相关上级所有ids
  35. * @param int $id
  36. * @return array|int[]
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function getRelationCityIds(int $id)
  42. {
  43. $cityInfo = $this->dao->get($id);
  44. $ids = [];
  45. if ($cityInfo) {
  46. $ids = explode('/', trim($cityInfo['path'], '/'));
  47. }
  48. return array_merge([$id], $ids);
  49. }
  50. /**
  51. * @param int $id
  52. * @param int $expire
  53. * @return bool|mixed|null
  54. */
  55. public function getRelationCityIdsCache(int $id, int $expire = 1800)
  56. {
  57. return CacheService::redisHandler('apiCity')->remember('city_ids_' . $id, function () use ($id) {
  58. $cityInfo = $this->dao->get($id);
  59. $ids = [];
  60. if ($cityInfo) {
  61. $ids = explode('/', trim($cityInfo['path'], '/'));
  62. }
  63. return array_merge([$id], $ids);
  64. }, $expire);
  65. }
  66. /**
  67. * @return bool|mixed|null
  68. */
  69. public function getCityTreeList(int $pid = 0)
  70. {
  71. $parent_name = '中国';
  72. if ($pid) {
  73. $city = $this->dao->get($pid);
  74. $parent_name = $city ? $city['name'] : '';
  75. }
  76. $cityList = $this->dao->getCityList(['parent_id' => $pid], 'id as value,id,name as label,parent_id as pid,level', ['children']);
  77. foreach ($cityList as &$item) {
  78. $item['parent_name'] = $parent_name;
  79. if (isset($item['children']) && $item['children']) {
  80. $item['children'] = [];
  81. $item['loading'] = false;
  82. $item['_loading'] = false;
  83. } else {
  84. unset($item['children']);
  85. }
  86. }
  87. return $cityList;
  88. }
  89. /**
  90. * 添加城市数据表单
  91. * @param int $parentId
  92. * @return array
  93. * @throws \FormBuilder\Exception\FormBuilderException
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\DbException
  96. * @throws \think\db\exception\ModelNotFoundException
  97. */
  98. public function createCityForm(int $parentId)
  99. {
  100. $info = [];
  101. if ($parentId) {
  102. $info = $this->dao->get($parentId);
  103. }
  104. $field[] = Form::hidden('level', $info['level'] ?? 0);
  105. $field[] = Form::hidden('parent_id', $info['id'] ?? 0);
  106. $field[] = Form::input('parent_name', '父类名称', $info['name'] ?? '中国')->disabled(true);
  107. $field[] = Form::input('name', '名称')->required('请填写城市名称');
  108. return create_form('添加城市', $field, $this->url('/setting/city/save'));
  109. }
  110. /**
  111. * 添加城市数据创建
  112. * @param int $id
  113. * @return array
  114. * @throws \FormBuilder\Exception\FormBuilderException
  115. */
  116. public function updateCityForm(int $id)
  117. {
  118. $info = $this->dao->get($id);
  119. if (!$info) {
  120. throw new AdminException('需改的数据不存在');
  121. }
  122. if ($info['parent_id']) {
  123. $city = $this->dao->get($info['parent_id']);
  124. $info['parent_name'] = $city['name'];
  125. }
  126. $info = $info->toArray();
  127. $field[] = Form::hidden('id', $info['id']);
  128. $field[] = Form::hidden('level', $info['level']);
  129. $field[] = Form::hidden('parent_id', $info['parent_id']);
  130. $field[] = Form::input('parent_name', '父类名称', $info['parent_name'] ?? '中国')->disabled(true);
  131. $field[] = Form::input('name', '名称', $info['name'])->required('请填写城市名称');
  132. return create_form('修改城市', $field, $this->url('/setting/city/save'));
  133. }
  134. }