UserAddress.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\api\user;
  12. use app\common\repositories\store\CityAreaRepository;
  13. use think\App;
  14. use crmeb\basic\BaseController;
  15. use app\validate\api\UserAddressValidate as validate;
  16. use app\common\repositories\user\UserAddressRepository as repository;
  17. use think\exception\ValidateException;
  18. class UserAddress extends BaseController
  19. {
  20. /**
  21. * @var repository
  22. */
  23. protected $repository;
  24. /**
  25. * UserAddress constructor.
  26. * @param App $app
  27. * @param repository $repository
  28. */
  29. public function __construct(App $app, repository $repository)
  30. {
  31. parent::__construct($app);
  32. $this->repository = $repository;
  33. }
  34. public function lst()
  35. {
  36. return app('json')->success($this->repository->getList($this->request->uid()));
  37. }
  38. public function detail($id)
  39. {
  40. $uid = $this->request->uid();
  41. if (!$this->repository->existsWhere(['address_id' => $id, 'uid' => $uid])) {
  42. return app('json')->fail('地址不存在');
  43. }
  44. return app('json')->success($this->repository->get($id, $uid));
  45. }
  46. /**
  47. * @param validate $validate
  48. * @return mixed
  49. * @author Qinii
  50. */
  51. public function create(validate $validate)
  52. {
  53. $data = $this->checkParams($validate);
  54. if ($data['is_default']) {
  55. $this->repository->changeDefault($this->request->uid());
  56. } else {
  57. if (!$this->repository->defaultExists($this->request->uid())) $data['is_default'] = 1;
  58. }
  59. if ($data['address_id']) {
  60. if (!$this->repository->fieldExists($data['address_id'], $this->request->uid()))
  61. return app('json')->fail('信息不存在');
  62. $this->repository->update($data['address_id'], $data);
  63. return app('json')->success('编辑成功');
  64. };
  65. $data['uid'] = $this->request->uid();
  66. $address = $this->repository->create($data);
  67. return app('json')->success('添加成功', $address->toArray());
  68. }
  69. /**
  70. * @param $id
  71. * @param validate $validate
  72. * @return mixed
  73. * @author Qinii
  74. */
  75. public function update($id, validate $validate)
  76. {
  77. if (!$this->repository->fieldExists($id, $this->request->uid()))
  78. return app('json')->fail('信息不存在');
  79. $data = $this->checkParams($validate);
  80. if ($data['is_default']) $this->repository->changeDefault($this->request->uid());
  81. $this->repository->update($id, $data);
  82. return app('json')->success('编辑成功');
  83. }
  84. /**
  85. * @param $id
  86. * @return mixed
  87. * @author Qinii
  88. */
  89. public function delete($id)
  90. {
  91. if (!$this->repository->fieldExists($id, $this->request->uid()))
  92. return app('json')->fail('信息不存在');
  93. if ($this->repository->checkDefault($id))
  94. return app('json')->fail('默认地址不能删除');
  95. $this->repository->delete($id);
  96. return app('json')->success('删除成功');
  97. }
  98. public function editDefault($id)
  99. {
  100. if (!$this->repository->fieldExists($id, $this->request->uid()))
  101. return app('json')->fail('信息不存在');
  102. $this->repository->changeDefault($this->request->uid());
  103. $this->repository->update($id, ['is_default' => 1]);
  104. return app('json')->success('修改成功');
  105. }
  106. /**
  107. * @param validate $validate
  108. * @return array
  109. * @author Qinii
  110. */
  111. public function checkParams(validate $validate)
  112. {
  113. $data = $this->request->params(['address_id', 'real_name', 'phone', 'area', 'detail', 'post_code', 'is_default']);
  114. $validate->check($data);
  115. [$province, $city, $district, $street] = ((array)$data['area']) + [null, null, null, null];
  116. $last = $street ?? $district ?? $city ?? $province;
  117. if (!$last) {
  118. throw new ValidateException('请选择正确的收货地址');
  119. }
  120. $make = app()->make(CityAreaRepository::class);
  121. if (!$make->existsWhere(['id' => $last['id'], 'snum' => 0])) {
  122. throw new ValidateException('请手动选择所在地区');
  123. }
  124. if ($make->search([])->where('id', 'in', array_column($data['area'], 'id'))->count() !== count($data['area'])) {
  125. throw new ValidateException('请选择正确的收货地址');
  126. }
  127. $data['province'] = $province['name'];
  128. $data['province_id'] = $province['id'];
  129. $data['city'] = $city['name'];
  130. $data['city_id'] = $city['id'];
  131. $data['district'] = $district['name'];
  132. $data['district_id'] = $district['id'];
  133. if (isset($street)) {
  134. $data['street'] = $street['name'];
  135. $data['street_id'] = $street['id'];
  136. }
  137. unset($data['area']);
  138. return $data;
  139. }
  140. }