UserAddress.php 4.7 KB

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