CityAreaServices.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system;
  12. use app\model\system\CityArea;
  13. use Exception;
  14. use qiniu\basic\BaseServices;
  15. use qiniu\services\CacheService;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. use think\Model;
  20. use Throwable;
  21. /**
  22. * 城市数据(街道)
  23. * Class CityAreaServices
  24. * @package app\services\other
  25. */
  26. class CityAreaServices extends BaseServices
  27. {
  28. /**
  29. * 城市类型
  30. * @var string[]
  31. */
  32. public $type = [
  33. '1' => 'province',
  34. '2' => 'city',
  35. '3' => 'area',
  36. '4' => 'street'
  37. ];
  38. /**
  39. * CityAreaServices constructor.
  40. * @param CityArea $model
  41. */
  42. public function __construct(CityArea $model)
  43. {
  44. $this->model = $model;
  45. }
  46. /**
  47. * 获取某一个城市id相关上级所有ids
  48. * @param int $id
  49. * @return array|int[]
  50. * @throws DataNotFoundException
  51. * @throws DbException
  52. * @throws ModelNotFoundException
  53. */
  54. public function getRelationCityIds(int $id)
  55. {
  56. $cityInfo = $this->model->get($id);
  57. $ids = [];
  58. if ($cityInfo) {
  59. $ids = explode('/', trim($cityInfo['path'], '/'));
  60. }
  61. return array_merge([$id], $ids);
  62. }
  63. /**
  64. * @param int $id
  65. * @param int $expire
  66. * @return bool|mixed|null
  67. * @throws Throwable
  68. */
  69. public function getRelationCityIdsCache(int $id, int $expire = 1800)
  70. {
  71. return CacheService::redisHandler('apiCity')->remember('city_ids_' . $id, function () use ($id) {
  72. $cityInfo = $this->model->get($id);
  73. $ids = [];
  74. if ($cityInfo) {
  75. $ids = explode('/', trim($cityInfo['path'], '/'));
  76. }
  77. return array_merge([$id], $ids);
  78. }, $expire);
  79. }
  80. /**
  81. * 获取城市数据
  82. * @param int $pid
  83. * @param int $type 1:省市 2:省市区 0、3:省市区街道
  84. * @return false|mixed|string|null
  85. * @throws DataNotFoundException
  86. * @throws DbException
  87. * @throws ModelNotFoundException
  88. */
  89. public function getCityTreeList(int $pid = 0, int $type = 0)
  90. {
  91. $parent_name = '中国';
  92. if ($pid) {
  93. $city = $this->model->get($pid);
  94. $parent_name = $city ? $city['name'] : '';
  95. }
  96. $cityList = $this->getCityList(['parent_id' => $pid], 'id as value,id,name as label,parent_id as pid,level', ['children']);
  97. foreach ($cityList as &$item) {
  98. $item['parent_name'] = $parent_name;
  99. if (isset($item['children']) && $item['children']) {
  100. $item['children'] = [];
  101. $item['loading'] = false;
  102. $item['_loading'] = false;
  103. } else {
  104. unset($item['children']);
  105. }
  106. }
  107. if ($cityList) {
  108. switch ($type) {
  109. case 0:
  110. case 3:
  111. break;
  112. case 1://控制children 前端不能请求下一级数据
  113. foreach ($cityList as &$item) {
  114. if ($item['level'] == 2) {
  115. unset($item['children'], $item['loading'], $item['_loading']);
  116. }
  117. }
  118. break;
  119. case 2:
  120. foreach ($cityList as &$item) {
  121. if ($item['level'] == 3) {
  122. unset($item['children'], $item['loading'], $item['_loading']);
  123. }
  124. }
  125. break;
  126. case 4:
  127. foreach ($cityList as &$item) {
  128. if ($item['level'] == 1) {
  129. unset($item['children'], $item['loading'], $item['_loading']);
  130. }
  131. }
  132. break;
  133. }
  134. }
  135. return $cityList;
  136. }
  137. /**
  138. * 搜索某个地址
  139. * @param array $where
  140. * @return array|Model|null
  141. * @throws DataNotFoundException
  142. * @throws DbException
  143. * @throws ModelNotFoundException
  144. */
  145. public function searchCity(array $where)
  146. {
  147. return $this->model->search($where)->order('id DESC')->find();
  148. }
  149. /**
  150. * 获取地址
  151. * @param array $where
  152. * @param string $field
  153. * @param array $with
  154. * @return array
  155. * @throws DataNotFoundException
  156. * @throws DbException
  157. * @throws ModelNotFoundException
  158. */
  159. public function getCityList(array $where, string $field = '*', array $with = [])
  160. {
  161. return $this->model->where($where)->field($field)->with($with)->order('id asc')->select()->toArray();
  162. }
  163. /**
  164. * 删除上级城市和当前城市id
  165. * @param int $cityId
  166. * @return bool
  167. * @throws Exception
  168. */
  169. public function deleteCity(int $cityId)
  170. {
  171. return $this->model->where('id', $cityId)->whereOr('parent_id', $cityId)->delete();
  172. }
  173. }