CityArea.php 4.2 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\services\other\CityAreaServices;
  14. use crmeb\services\CacheService;
  15. use think\facade\App;
  16. /**
  17. * 城市数据
  18. * Class CityArea
  19. * @package app\controller\admin\v1\setting
  20. */
  21. class CityArea extends AuthController
  22. {
  23. /**
  24. * 构造方法
  25. * CityArea constructor.
  26. * @param App $app
  27. * @param CityAreaServices $services
  28. */
  29. public function __construct(App $app, CityAreaServices $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. [$parent_id] = $this->request->getMore([
  42. [['parent_id', 'd'], 0]
  43. ], true);
  44. return $this->success($this->services->getCityTreeList((int)$parent_id));
  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 $this->success($this->services->createCityForm((int)$parentId));
  60. }
  61. /**
  62. * 保存
  63. */
  64. public function save()
  65. {
  66. $data = $this->request->postMore([
  67. [['id', 'd'], 0],
  68. [['name', 's'], ''],
  69. [['level', 'd'], 0],
  70. [['parent_id', 'd'], 0],
  71. ]);
  72. if ($data['id'] == 0) {
  73. if ($data['parent_id']) {
  74. $parent = $this->services->get($data['parent_id']);
  75. if ($parent) {
  76. $data['path'] = $parent['path'] . $parent['id'] .'/';
  77. }
  78. } else {
  79. $data['path'] = '/';
  80. }
  81. unset($data['id']);
  82. $data['level'] = $data['level'] + 1;
  83. $data['type'] = $this->services->type[$data['level']] ?? '';
  84. $data['create_time'] = date('Y-m-d H:i:s');
  85. $this->services->save($data);
  86. $this->services->bcInc($data['parent_id'], 'snum', 1);
  87. return $this->success('添加城市成功!');
  88. } else {
  89. unset($data['level']);
  90. unset($data['parent_id']);
  91. $this->services->update($data['id'], $data);
  92. return $this->success('修改城市成功!');
  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 $this->success($this->services->updateCityForm((int)$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. $city = $this->services->get((int)$id);
  119. if ($city) {
  120. $this->services->deleteCity($id);
  121. if ($city['parent_id']) $this->services->bcDec($city['parent_id'], 'snum', 1);
  122. }
  123. return $this->success('删除成功!');
  124. }
  125. /**
  126. * 清除城市缓存
  127. * @throws \Psr\SimpleCache\InvalidArgumentException
  128. */
  129. public function clean_cache()
  130. {
  131. CacheService::delete('CITY_LIST');
  132. return $this->success('清除成功!');
  133. }
  134. }