SystemCityServices.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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\SystemCityDao;
  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 SystemCityServices
  20. * @package app\services\other
  21. * @mixin SystemCityDao
  22. */
  23. class SystemCityServices extends BaseServices
  24. {
  25. /**
  26. * 城市数据
  27. * @var string
  28. */
  29. public $tree_city_key = 'tree_city_list';
  30. /**
  31. * 构造方法
  32. * SystemCityServices constructor.
  33. * @param SystemCityDao $dao
  34. */
  35. public function __construct(SystemCityDao $dao)
  36. {
  37. $this->dao = $dao;
  38. }
  39. /**
  40. * 获取城市数据
  41. * @param array $where
  42. * @return array
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\DbException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. */
  47. public function getCityList(array $where)
  48. {
  49. return CacheService::get($this->tree_city_key, function () {
  50. return $this->getSonCityList();
  51. }, 86400);
  52. }
  53. /**
  54. * tree形城市列表
  55. * @param int $pid
  56. * @param string $parent_name
  57. * @return array
  58. * @throws \think\db\exception\DataNotFoundException
  59. * @throws \think\db\exception\DbException
  60. * @throws \think\db\exception\ModelNotFoundException
  61. */
  62. public function getSonCityList($pid = 0, $parent_name = '中国')
  63. {
  64. $list = $this->dao->getCityList(['parent_id' => $pid], 'id,city_id,level,name');
  65. $arr = [];
  66. if ($list) {
  67. foreach ($list as $item) {
  68. $item['parent_id'] = $parent_name;
  69. $item['children'] = $this->getSonCityList($item['city_id'], $item['name']);
  70. $arr [] = $item;
  71. }
  72. }
  73. return $arr;
  74. }
  75. /**
  76. * 添加城市数据表单
  77. * @param int $parentId
  78. * @return array
  79. * @throws \FormBuilder\Exception\FormBuilderException
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\DbException
  82. * @throws \think\db\exception\ModelNotFoundException
  83. */
  84. public function createCityForm(int $parentId)
  85. {
  86. if ($parentId) {
  87. $info = $this->dao->getOne(['city_id' => $parentId], 'level,city_id,name');
  88. } else {
  89. $info = ["level" => 0, "city_id" => 0, "name" => '中国'];
  90. }
  91. $field[] = Form::hidden('level', $info['level']);
  92. $field[] = Form::hidden('parent_id', $info['city_id']);
  93. $field[] = Form::input('parent_name', '上级名称', $info['name'])->readonly(true);
  94. $field[] = Form::input('name', '名称')->required('请填写城市名称');
  95. return create_form('添加城市', $field, $this->url('/setting/city/save'));
  96. }
  97. /**
  98. * 添加城市数据创建
  99. * @param int $id
  100. * @return array
  101. * @throws \FormBuilder\Exception\FormBuilderException
  102. */
  103. public function updateCityForm(int $id)
  104. {
  105. $info = $this->dao->get($id);
  106. if (!$info) {
  107. throw new AdminException('需改的数据不存在');
  108. }
  109. $info = $info->toArray();
  110. $info['parent_name'] = $this->dao->value(['city_id' => $info['parent_id']], 'name') ?: '中国';
  111. $field[] = Form::hidden('id', $info['id']);
  112. $field[] = Form::hidden('level', $info['level']);
  113. $field[] = Form::hidden('parent_id', $info['parent_id']);
  114. $field[] = Form::input('parent_name', '上级名称', $info['parent_name'])->readonly(true);
  115. $field[] = Form::input('name', '名称', $info['name'])->required('请填写城市名称');
  116. $field[] = Form::input('merger_name', '合并名称', $info['merger_name'])->placeholder('格式:陕西,西安,雁塔')->required('请填写合并名称');
  117. return create_form('修改城市', $field, $this->url('/setting/city/save'));
  118. }
  119. /**
  120. * 获取城市数据
  121. * @return mixed
  122. */
  123. public function cityList()
  124. {
  125. return CacheService::get('CITY_LIST', function () {
  126. $allCity = $this->dao->getCityList([], 'city_id as v,name as n,parent_id');
  127. return sort_city_tier($allCity, 0);
  128. }, 0);
  129. }
  130. /**
  131. * @return bool|mixed|null
  132. */
  133. public function getCityTreeList()
  134. {
  135. return CacheService::get('CITY_TREE_LIST', function () {
  136. return get_tree_children($this->dao->getCityList(['is_show' => 1], 'city_id as value,name as label,parent_id as pid'), 'children', 'value');
  137. });
  138. }
  139. }