CityArea.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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. class CityArea extends BaseController
  17. {
  18. protected $repository;
  19. /**
  20. * City constructor.
  21. * @param App $app
  22. * @param repository $repository
  23. */
  24. public function __construct(App $app, CityAreaRepository $repository)
  25. {
  26. parent::__construct($app);
  27. $this->repository = $repository;
  28. }
  29. /**
  30. * @Author:Qinii
  31. * @Date: 2020/5/8
  32. * @Time: 14:40
  33. * @return mixed
  34. */
  35. public function lst($id)
  36. {
  37. $where['parent_id'] = $id;
  38. return app('json')->success($this->repository->getList($where));
  39. }
  40. public function createForm($id)
  41. {
  42. return app('json')->success(formToData($this->repository->form(0, $id)));
  43. }
  44. public function create()
  45. {
  46. $data = $this->checkParams();
  47. $this->repository->create($data);
  48. return app('json')->success('添加成功');
  49. }
  50. public function updateForm($id)
  51. {
  52. return app('json')->success(formToData($this->repository->form($id,null)));
  53. }
  54. public function update($id)
  55. {
  56. $data = $this->checkParams();
  57. if (!$res = $this->repository->get($id)){
  58. return app('json')->fail('数据不存在');
  59. }
  60. $this->repository->update($id, $data);
  61. return app('json')->success('编辑成功');
  62. }
  63. public function checkParams()
  64. {
  65. $type = [
  66. 1 => 'province',
  67. 2 => 'city',
  68. 3 => 'area',
  69. 4 => 'street',
  70. ];
  71. $data = $this->request->params(['parent_id','level','name',['path','/']]);
  72. if ($data['parent_id']) {
  73. $parent = $this->repository->get($data['parent_id']);
  74. if (!$parent) throw new ValidateException('上级数据不存在');
  75. $data['path'] = $parent['path'] . $parent['id'].'/';
  76. }
  77. $data['type'] = $type[$data['level']];
  78. if (!$data['name']) throw new ValidateException('请填写城市名称');
  79. return $data;
  80. }
  81. public function delete($id)
  82. {
  83. $res = $this->repository->getWhere(['parent_id' => $id]);
  84. if ($res) {
  85. return app('json')->fail('数据存在子集不能删除');
  86. }
  87. $this->repository->delete($id);
  88. return app('json')->success('删除成功');
  89. }
  90. }