Address.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\api\controller;
  10. use app\api\model\UserAddress;
  11. /**
  12. * 收货地址管理
  13. * Class Address
  14. * @package app\api\controller
  15. */
  16. class Address extends Controller
  17. {
  18. /**
  19. * 收货地址列表
  20. * @return array
  21. * @throws \app\common\exception\BaseException
  22. * @throws \think\exception\DbException
  23. */
  24. public function lists()
  25. {
  26. $user = $this->getUser();
  27. $model = new UserAddress;
  28. $list = $model->getList($user['user_id']);
  29. return $this->renderSuccess([
  30. 'list' => $list,
  31. 'default_id' => $user['address_id'],
  32. ]);
  33. }
  34. /**
  35. * 添加收货地址
  36. * @return array
  37. * @throws \app\common\exception\BaseException
  38. * @throws \think\exception\DbException
  39. */
  40. public function add()
  41. {
  42. $model = new UserAddress;
  43. if ($model->add($this->getUser(), $this->request->post())) {
  44. return $this->renderSuccess([], '添加成功');
  45. }
  46. return $this->renderError($model->getError() ?: '添加失败');
  47. }
  48. /**
  49. * 收货地址详情
  50. * @param $address_id
  51. * @return array
  52. * @throws \app\common\exception\BaseException
  53. * @throws \think\exception\DbException
  54. */
  55. public function detail($address_id)
  56. {
  57. $user = $this->getUser();
  58. $detail = UserAddress::detail($user['user_id'], $address_id);
  59. $region = array_values($detail['region']);
  60. return $this->renderSuccess(compact('detail', 'region'));
  61. }
  62. /**
  63. * 编辑收货地址
  64. * @param $address_id
  65. * @return array
  66. * @throws \app\common\exception\BaseException
  67. * @throws \think\exception\DbException
  68. */
  69. public function edit($address_id)
  70. {
  71. $user = $this->getUser();
  72. $model = UserAddress::detail($user['user_id'], $address_id);
  73. if ($model->edit($this->request->post())) {
  74. return $this->renderSuccess([], '更新成功');
  75. }
  76. return $this->renderError($model->getError() ?: '更新失败');
  77. }
  78. /**
  79. * 设为默认地址
  80. * @param $address_id
  81. * @return array
  82. * @throws \app\common\exception\BaseException
  83. * @throws \think\exception\DbException
  84. */
  85. public function setDefault($address_id)
  86. {
  87. $user = $this->getUser();
  88. $model = UserAddress::detail($user['user_id'], $address_id);
  89. if ($model->setDefault($user)) {
  90. return $this->renderSuccess([], '设置成功');
  91. }
  92. return $this->renderError($model->getError() ?: '设置失败');
  93. }
  94. /**
  95. * 删除收货地址
  96. * @param $address_id
  97. * @return array
  98. * @throws \app\common\exception\BaseException
  99. * @throws \think\exception\DbException
  100. */
  101. public function delete($address_id)
  102. {
  103. $user = $this->getUser();
  104. $model = UserAddress::detail($user['user_id'], $address_id);
  105. if ($model->remove($user)) {
  106. return $this->renderSuccess([], '删除成功');
  107. }
  108. return $this->renderError($model->getError() ?: '删除失败');
  109. }
  110. }