UserAddress.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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\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. /**
  35. * 用户地址列表
  36. * @return \think\response\Json
  37. * @author wuhaotian
  38. * @email 442384644@qq.com
  39. * @date 2024/7/10
  40. */
  41. public function lst()
  42. {
  43. [$page,$limit] = $this->getPage();
  44. return app('json')->success($this->repository->getList($this->request->uid(),$page,$limit));
  45. }
  46. /**
  47. * 地址详情
  48. * @param $id
  49. * @return \think\response\Json
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. * @author wuhaotian
  54. * @email 442384644@qq.com
  55. * @date 2024/7/10
  56. */
  57. public function detail($id)
  58. {
  59. $uid = $this->request->uid();
  60. if (!$this->repository->existsWhere(['address_id' => $id, 'uid' => $uid])) {
  61. return app('json')->fail('地址不存在');
  62. }
  63. return app('json')->success($this->repository->get($id, $uid));
  64. }
  65. /**
  66. * 新增地址
  67. * @param validate $validate
  68. * @return mixed
  69. * @author Qinii
  70. */
  71. public function create(validate $validate)
  72. {
  73. $data = $this->checkParams($validate);
  74. if ($data['is_default']) {
  75. $this->repository->changeDefault($this->request->uid());
  76. } else {
  77. if (!$this->repository->defaultExists($this->request->uid())) $data['is_default'] = 1;
  78. }
  79. if ($data['address_id']) {
  80. if (!$this->repository->fieldExists($data['address_id'], $this->request->uid()))
  81. return app('json')->fail('信息不存在');
  82. $this->repository->update($data['address_id'], $data);
  83. return app('json')->success('编辑成功');
  84. };
  85. $data['uid'] = $this->request->uid();
  86. $address = $this->repository->create($data);
  87. return app('json')->success('添加成功', $address->toArray());
  88. }
  89. /**
  90. * 修改地址
  91. * @param $id
  92. * @param validate $validate
  93. * @return mixed
  94. * @author Qinii
  95. */
  96. public function update($id, validate $validate)
  97. {
  98. if (!$this->repository->fieldExists($id, $this->request->uid()))
  99. return app('json')->fail('信息不存在');
  100. $data = $this->checkParams($validate);
  101. if ($data['is_default']) $this->repository->changeDefault($this->request->uid());
  102. $this->repository->update($id, $data);
  103. return app('json')->success('编辑成功');
  104. }
  105. /**
  106. * 删除地址
  107. * @param $id
  108. * @return mixed
  109. * @author Qinii
  110. */
  111. public function delete($id)
  112. {
  113. if (!$this->repository->fieldExists($id, $this->request->uid()))
  114. return app('json')->fail('信息不存在');
  115. if ($this->repository->checkDefault($id))
  116. return app('json')->fail('默认地址不能删除');
  117. $this->repository->delete($id);
  118. return app('json')->success('删除成功');
  119. }
  120. /**
  121. * 设置默认地址
  122. * @param $id
  123. * @return \think\response\Json
  124. * @throws \think\db\exception\DbException
  125. * @author wuhaotian
  126. * @email 442384644@qq.com
  127. * @date 2024/7/10
  128. */
  129. public function editDefault($id)
  130. {
  131. if (!$this->repository->fieldExists($id, $this->request->uid()))
  132. return app('json')->fail('信息不存在');
  133. $this->repository->changeDefault($this->request->uid());
  134. $this->repository->update($id, ['is_default' => 1]);
  135. return app('json')->success('修改成功');
  136. }
  137. /**
  138. * 验证参数
  139. * @param validate $validate
  140. * @return array
  141. * @author Qinii
  142. */
  143. public function checkParams(validate $validate)
  144. {
  145. $data = $this->request->params(['address_id', 'real_name', 'phone', 'area', 'detail', 'post_code', 'is_default']);
  146. $validate->check($data);
  147. [$province, $city, $district, $street] = ((array)$data['area']) + [null, null, null, null];
  148. $last = $street ?? $district ?? $city ?? $province;
  149. if (!$last) {
  150. throw new ValidateException('请选择正确的收货地址');
  151. }
  152. $make = app()->make(CityAreaRepository::class);
  153. if (!$make->existsWhere(['id' => $last['id'], 'snum' => 0])) {
  154. throw new ValidateException('请手动选择所在地区');
  155. }
  156. if ($make->search([])->where('id', 'in', array_column($data['area'], 'id'))->count() !== count($data['area'])) {
  157. throw new ValidateException('请选择正确的收货地址');
  158. }
  159. $data['province'] = $province['name'];
  160. $data['province_id'] = $province['id'];
  161. $data['city'] = $city['name'];
  162. $data['city_id'] = $city['id'];
  163. $data['district'] = $district['name'];
  164. $data['district_id'] = $district['id'];
  165. if (isset($street)) {
  166. $data['street'] = $street['name'];
  167. $data['street_id'] = $street['id'];
  168. }
  169. unset($data['area']);
  170. return $data;
  171. }
  172. }