SystemCity.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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\controller\admin\v1\other;
  12. use app\controller\admin\AuthController;
  13. use app\controller\admin\v1\out\SystemStoreServices;
  14. use app\services\other\SystemCityServices;
  15. use crmeb\services\{CacheService};
  16. use think\facade\App;
  17. /**
  18. * 城市数据
  19. * Class SystemCity
  20. * @package app\controller\admin\v1\setting
  21. */
  22. class SystemCity extends AuthController
  23. {
  24. /**
  25. * 构造方法
  26. * SystemCity constructor.
  27. * @param App $app
  28. * @param SystemCityServices $services
  29. */
  30. public function __construct(App $app, SystemCityServices $services)
  31. {
  32. parent::__construct($app);
  33. $this->services = $services;
  34. }
  35. /**
  36. * 城市列表
  37. * @return string
  38. * @throws \Exception
  39. */
  40. public function index()
  41. {
  42. $where = $this->request->getMore([
  43. [['parent_id', 'd'], 0]
  44. ]);
  45. return $this->success($this->services->getCityList($where));
  46. }
  47. /**
  48. * 添加城市
  49. * @return mixed
  50. * @throws \FormBuilder\Exception\FormBuilderException
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function add()
  56. {
  57. [$parentId] = $this->request->getMore([
  58. [['parent_id', 'd'], 0]
  59. ], true);
  60. return $this->success($this->services->createCityForm($parentId));
  61. }
  62. /**
  63. * 保存
  64. */
  65. public function save()
  66. {
  67. $data = $this->request->postMore([
  68. [['id', 'd'], 0],
  69. [['name', 's'], ''],
  70. [['merger_name', 's'], ''],
  71. [['area_code', 's'], ''],
  72. [['lng', 's'], ''],
  73. [['lat', 's'], ''],
  74. [['level', 'd'], 0],
  75. [['parent_id', 'd'], 0],
  76. ]);
  77. $this->validate($data, \app\validate\admin\setting\SystemCityValidate::class, 'save');
  78. if ($data['parent_id'] == 0) {
  79. $data['merger_name'] = $data['name'];
  80. } else {
  81. $data['merger_name'] = $this->services->value(['id' => $data['parent_id']], 'name') . ',' . $data['name'];
  82. }
  83. CacheService::delete($this->services->tree_city_key);
  84. if ($data['id'] == 0) {
  85. unset($data['id']);
  86. $data['level'] = $data['level'] + 1;
  87. $data['city_id'] = intval($this->services->getCityIdMax() + 1);
  88. $this->services->save($data);
  89. return $this->success('添加城市成功!');
  90. } else {
  91. unset($data['level']);
  92. unset($data['parent_id']);
  93. $this->services->update($data['id'], $data);
  94. return $this->success('修改城市成功!');
  95. }
  96. }
  97. /**
  98. * 修改城市
  99. * @return string
  100. * @throws \think\db\exception\DataNotFoundException
  101. * @throws \think\db\exception\DbException
  102. * @throws \think\db\exception\ModelNotFoundException
  103. */
  104. public function edit()
  105. {
  106. [$id] = $this->request->getMore([
  107. [['id', 'd'], 0]
  108. ], true);
  109. return $this->success($this->services->updateCityForm($id));
  110. }
  111. /**
  112. * 删除城市
  113. * @throws \Exception
  114. */
  115. public function delete()
  116. {
  117. [$id] = $this->request->getMore([
  118. [['city_id', 'd'], 0]
  119. ], true);
  120. $this->services->deleteCity($id);
  121. CacheService::delete($this->services->tree_city_key);
  122. return $this->success('删除成功!');
  123. }
  124. /**
  125. * 清除城市缓存
  126. * @throws \Psr\SimpleCache\InvalidArgumentException
  127. */
  128. public function clean_cache()
  129. {
  130. CacheService::delete($this->services->tree_city_key);
  131. CacheService::delete('CITY_LIST');
  132. return $this->success('清除成功!');
  133. }
  134. }