CityArea.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\store;
  12. use app\common\repositories\store\CityAreaRepository;
  13. use think\App;
  14. use crmeb\basic\BaseController;
  15. use think\exception\ValidateException;
  16. /**
  17. * 省市区
  18. */
  19. class CityArea extends BaseController
  20. {
  21. protected $repository;
  22. /**
  23. * City constructor.
  24. * @param App $app
  25. * @param repository $repository
  26. */
  27. public function __construct(App $app, CityAreaRepository $repository)
  28. {
  29. parent::__construct($app);
  30. $this->repository = $repository;
  31. }
  32. /**
  33. * 列表
  34. * @Author:Qinii
  35. * @Date: 2020/5/8
  36. * @Time: 14:40
  37. * @return mixed
  38. */
  39. public function lst($id)
  40. {
  41. $where['parent_id'] = $id;
  42. return app('json')->success($this->repository->getList($where));
  43. }
  44. /**
  45. * 创建表单
  46. * @param $id
  47. * @return \think\response\Json
  48. * @author Qinii
  49. */
  50. public function createForm($id)
  51. {
  52. return app('json')->success(formToData($this->repository->form(0, $id)));
  53. }
  54. /**
  55. * 创建
  56. * @return \think\response\Json
  57. * @author Qinii.
  58. */
  59. public function create()
  60. {
  61. $data = $this->checkParams();
  62. $this->repository->create($data);
  63. return app('json')->success('添加成功');
  64. }
  65. /**
  66. * 修改表单
  67. * @param $id
  68. * @return \think\response\Json
  69. * @author Qinii
  70. */
  71. public function updateForm($id)
  72. {
  73. return app('json')->success(formToData($this->repository->form($id, null)));
  74. }
  75. /**
  76. * 修改
  77. * @param $id
  78. * @return \think\response\Json
  79. * @author Qinii
  80. */
  81. public function update($id)
  82. {
  83. $data = $this->checkParams();
  84. if (!$res = $this->repository->get($id)) {
  85. return app('json')->fail('数据不存在');
  86. }
  87. $this->repository->update($id, $data);
  88. return app('json')->success('编辑成功');
  89. }
  90. public function checkParams()
  91. {
  92. $type = [
  93. 1 => 'province',
  94. 2 => 'city',
  95. 3 => 'area',
  96. 4 => 'street',
  97. ];
  98. $data = $this->request->params(['parent_id', 'level', 'name', ['path', '/']]);
  99. if ($data['parent_id']) {
  100. $parent = $this->repository->get($data['parent_id']);
  101. if (!$parent) throw new ValidateException('上级数据不存在');
  102. $data['path'] = $parent['path'] . $parent['id'] . '/';
  103. }
  104. $data['type'] = $type[$data['level']];
  105. if (!$data['name']) throw new ValidateException('请填写城市名称');
  106. return $data;
  107. }
  108. /**
  109. * 删除
  110. * @param $id
  111. * @return \think\response\Json
  112. * @author Qinii
  113. */
  114. public function delete($id)
  115. {
  116. $res = $this->repository->getWhere(['parent_id' => $id]);
  117. if ($res) {
  118. return app('json')->fail('数据存在子集不能删除');
  119. }
  120. $this->repository->delete($id);
  121. return app('json')->success('删除成功');
  122. }
  123. }