CityAreaServices.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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\other;
  12. use app\dao\other\CityAreaDao;
  13. use app\services\BaseServices;
  14. use crmeb\exceptions\AdminException;
  15. use crmeb\services\CacheService;
  16. use crmeb\services\FormBuilder as Form;
  17. /**
  18. * 城市数据(街道)
  19. * Class CityAreaServices
  20. * @package app\services\other
  21. * @mixin CityAreaDao
  22. */
  23. class CityAreaServices extends BaseServices
  24. {
  25. /**
  26. * 城市类型
  27. * @var string[]
  28. */
  29. public $type = [
  30. '1' => 'province',
  31. '2' => 'city',
  32. '3' => 'area',
  33. '4' => 'street'
  34. ];
  35. /**
  36. * CityAreaServices constructor.
  37. * @param CityAreaDao $dao
  38. */
  39. public function __construct(CityAreaDao $dao)
  40. {
  41. $this->dao = $dao;
  42. }
  43. /**
  44. * 获取某一个城市id相关上级所有ids
  45. * @param int $id
  46. * @return array|int[]
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. */
  51. public function getRelationCityIds(int $id)
  52. {
  53. $cityInfo = $this->dao->get($id);
  54. $ids = [];
  55. if ($cityInfo) {
  56. $ids = explode('/', trim($cityInfo['path'], '/'));
  57. }
  58. return array_merge([$id], $ids);
  59. }
  60. /**
  61. * @param int $id
  62. * @param int $expire
  63. * @return bool|mixed|null
  64. */
  65. public function getRelationCityIdsCache(int $id, int $expire = 1800)
  66. {
  67. return CacheService::redisHandler('apiCity')->remember('city_ids_' . $id, function () use ($id) {
  68. $cityInfo = $this->dao->get($id);
  69. $ids = [];
  70. if ($cityInfo) {
  71. $ids = explode('/', trim($cityInfo['path'], '/'));
  72. }
  73. return array_merge([$id], $ids);
  74. }, $expire);
  75. }
  76. /**
  77. * 获取城市数据
  78. * @param int $pid
  79. * @param int $type 1:省市 2:省市区 0、3:省市区街道
  80. * @return false|mixed|string|null
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\DbException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. */
  85. public function getCityTreeList(int $pid = 0, int $type = 0)
  86. {
  87. $cityList = $this->dao->cacheStrRemember('pid_' . $pid, function () use ($pid) {
  88. $parent_name = '中国';
  89. if ($pid) {
  90. $city = $this->dao->get($pid);
  91. $parent_name = $city ? $city['name'] : '';
  92. }
  93. $cityList = $this->dao->getCityList(['parent_id' => $pid], 'id as value,id,name as label,parent_id as pid,level', ['children']);
  94. foreach ($cityList as &$item) {
  95. $item['parent_name'] = $parent_name;
  96. if (isset($item['children']) && $item['children']) {
  97. $item['children'] = [];
  98. $item['loading'] = false;
  99. $item['_loading'] = false;
  100. } else {
  101. unset($item['children']);
  102. }
  103. }
  104. return $cityList;
  105. });
  106. if ($cityList) {
  107. switch ($type) {
  108. case 0:
  109. case 3:
  110. break;
  111. case 1://控制children 前端不能请求下一级数据
  112. foreach ($cityList as &$item) {
  113. if ($item['level'] == 2) {
  114. unset($item['children'], $item['loading'], $item['_loading']);
  115. }
  116. }
  117. break;
  118. case 2:
  119. foreach ($cityList as &$item) {
  120. if ($item['level'] == 3) {
  121. unset($item['children'], $item['loading'], $item['_loading']);
  122. }
  123. }
  124. break;
  125. case 4:
  126. foreach ($cityList as &$item) {
  127. if ($item['level'] == 1) {
  128. unset($item['children'], $item['loading'], $item['_loading']);
  129. }
  130. }
  131. break;
  132. }
  133. }
  134. return $cityList;
  135. }
  136. /**
  137. * 添加城市数据表单
  138. * @param int $parentId
  139. * @return array
  140. * @throws \FormBuilder\Exception\FormBuilderException
  141. * @throws \think\db\exception\DataNotFoundException
  142. * @throws \think\db\exception\DbException
  143. * @throws \think\db\exception\ModelNotFoundException
  144. */
  145. public function createCityForm(int $parentId)
  146. {
  147. $info = [];
  148. if ($parentId) {
  149. $info = $this->dao->get($parentId);
  150. }
  151. $field[] = Form::hidden('level', $info['level'] ?? 0);
  152. $field[] = Form::hidden('parent_id', $info['id'] ?? 0);
  153. $field[] = Form::input('parent_name', '父类名称', $info['name'] ?? '中国')->disabled(true);
  154. $field[] = Form::input('name', '名称')->required('请填写城市名称');
  155. return create_form('添加城市', $field, $this->url('/setting/city/save'));
  156. }
  157. /**
  158. * 添加城市数据创建
  159. * @param int $id
  160. * @return array
  161. * @throws \FormBuilder\Exception\FormBuilderException
  162. */
  163. public function updateCityForm(int $id)
  164. {
  165. $info = $this->dao->get($id);
  166. if (!$info) {
  167. throw new AdminException('需改的数据不存在');
  168. }
  169. if ($info['parent_id']) {
  170. $city = $this->dao->get($info['parent_id']);
  171. $info['parent_name'] = $city['name'];
  172. }
  173. $info = $info->toArray();
  174. $field[] = Form::hidden('id', $info['id']);
  175. $field[] = Form::hidden('level', $info['level']);
  176. $field[] = Form::hidden('parent_id', $info['parent_id']);
  177. $field[] = Form::input('parent_name', '父类名称', $info['parent_name'] ?? '中国')->disabled(true);
  178. $field[] = Form::input('name', '名称', $info['name'])->required('请填写城市名称');
  179. return create_form('修改城市', $field, $this->url('/setting/city/save'));
  180. }
  181. }