Address.php 3.0 KB

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