SystemCity.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\setting;
  12. use app\adminapi\controller\AuthController;
  13. use app\services\shipping\SystemCityServices;
  14. use think\facade\App;
  15. use crmeb\services\{CacheService};
  16. /**
  17. * 城市数据
  18. * Class SystemCity
  19. * @package app\adminapi\controller\v1\setting
  20. */
  21. class SystemCity extends AuthController
  22. {
  23. /**
  24. * 构造方法
  25. * SystemCity constructor.
  26. * @param App $app
  27. * @param SystemCityServices $services
  28. */
  29. public function __construct(App $app, SystemCityServices $services)
  30. {
  31. parent::__construct($app);
  32. $this->services = $services;
  33. }
  34. /**
  35. * 城市列表
  36. * @return string
  37. * @throws \Exception
  38. */
  39. public function index()
  40. {
  41. $where = $this->request->getMore([
  42. [['parent_id', 'd'], 0]
  43. ]);
  44. return app('json')->success($this->services->getCityList($where));
  45. }
  46. /**
  47. * 添加城市
  48. * @return mixed
  49. * @throws \FormBuilder\Exception\FormBuilderException
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. public function add()
  55. {
  56. [$parentId] = $this->request->getMore([
  57. [['parent_id', 'd'], 0]
  58. ], true);
  59. return app('json')->success($this->services->createCityForm($parentId));
  60. }
  61. /**
  62. * 保存
  63. */
  64. public function save()
  65. {
  66. $data = $this->request->postMore([
  67. [['id', 'd'], 0],
  68. [['name', 's'], ''],
  69. [['merger_name', 's'], ''],
  70. [['area_code', 's'], ''],
  71. [['lng', 's'], ''],
  72. [['lat', 's'], ''],
  73. [['level', 'd'], 0],
  74. [['parent_id', 'd'], 0],
  75. ]);
  76. $this->validate($data, \app\adminapi\validate\setting\SystemCityValidate::class, 'save');
  77. if ($data['parent_id'] == 0) {
  78. $data['merger_name'] = $data['name'];
  79. } else {
  80. $data['merger_name'] = $this->services->value(['id' => $data['parent_id']], 'name') . ',' . $data['name'];
  81. }
  82. if ($data['id'] == 0) {
  83. unset($data['id']);
  84. $data['level'] = $data['level'] + 1;
  85. $data['city_id'] = intval($this->services->getCityIdMax() + 1);
  86. $this->services->save($data);
  87. return app('json')->success(100000);
  88. } else {
  89. unset($data['level']);
  90. unset($data['parent_id']);
  91. $this->services->update($data['id'], $data);
  92. return app('json')->success(100001);
  93. }
  94. }
  95. /**
  96. * 修改城市
  97. * @return string
  98. * @throws \think\db\exception\DataNotFoundException
  99. * @throws \think\db\exception\DbException
  100. * @throws \think\db\exception\ModelNotFoundException
  101. */
  102. public function edit()
  103. {
  104. [$id] = $this->request->getMore([
  105. [['id', 'd'], 0]
  106. ], true);
  107. return app('json')->success($this->services->updateCityForm($id));
  108. }
  109. /**
  110. * 删除城市
  111. * @throws \Exception
  112. */
  113. public function delete()
  114. {
  115. [$id] = $this->request->getMore([
  116. [['city_id', 'd'], 0]
  117. ], true);
  118. $this->services->deleteCity($id);
  119. return app('json')->success(100002);
  120. }
  121. /**
  122. * 清除城市缓存
  123. * @throws \Psr\SimpleCache\InvalidArgumentException
  124. */
  125. public function clean_cache()
  126. {
  127. CacheService::delete('CITY_LIST');
  128. CacheService::delete('CITY_FULL_LIST');
  129. return app('json')->success(400185);
  130. }
  131. /**
  132. * 获取城市数据完整列表
  133. * @return \think\Response
  134. * @author 吴汐
  135. * @email 442384644@qq.com
  136. * @date 2023/04/10
  137. */
  138. public function fullList()
  139. {
  140. return app('json')->success($this->services->fullList('parent_id,name as label,city_id as value'));
  141. }
  142. }