Address.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\api\controller;
  13. use app\api\service\User as UserService;
  14. use app\api\model\UserAddress as UserAddressModel;
  15. use app\common\exception\BaseException;
  16. /**
  17. * 收货地址管理
  18. * Class Address
  19. * @package app\api\controller
  20. */
  21. class Address extends Controller
  22. {
  23. /**
  24. * 收货地址列表
  25. * @return array|\think\response\Json
  26. * @throws BaseException
  27. * @throws \think\db\exception\DataNotFoundException
  28. * @throws \think\db\exception\DbException
  29. * @throws \think\db\exception\ModelNotFoundException
  30. */
  31. public function list()
  32. {
  33. // 获取收货地址列表
  34. $model = new UserAddressModel;
  35. $list = $model->getList();
  36. return $this->renderSuccess(compact('list'));
  37. }
  38. /**
  39. * 获取当前用户默认收货地址
  40. * @return array|\think\response\Json
  41. * @throws BaseException
  42. */
  43. public function defaultId()
  44. {
  45. $useInfo = UserService::getCurrentLoginUser(true);
  46. return $this->renderSuccess(['defaultId' => $useInfo['address_id']]);
  47. }
  48. /**
  49. * 收货地址详情
  50. * @param int $addressId 地址ID
  51. * @return array|\think\response\Json
  52. * @throws BaseException
  53. */
  54. public function detail(int $addressId)
  55. {
  56. $detail = UserAddressModel::detail($addressId);
  57. return $this->renderSuccess(compact('detail'));
  58. }
  59. /**
  60. * 添加收货地址
  61. * @return array|\think\response\Json
  62. * @throws BaseException
  63. */
  64. public function add()
  65. {
  66. $model = new UserAddressModel;
  67. if ($model->add($this->postForm())) {
  68. return $this->renderSuccess([], '添加成功');
  69. }
  70. return $this->renderError($model->getError() ?: '添加失败');
  71. }
  72. /**
  73. * 编辑收货地址
  74. * @param int $addressId 地址ID
  75. * @return array|\think\response\Json
  76. * @throws BaseException
  77. */
  78. public function edit(int $addressId)
  79. {
  80. $model = UserAddressModel::detail($addressId);
  81. if ($model->edit($this->postForm())) {
  82. return $this->renderSuccess([], '更新成功');
  83. }
  84. return $this->renderError($model->getError() ?: '更新失败');
  85. }
  86. /**
  87. * 设为默认地址
  88. * @param int $addressId 地址ID
  89. * @return array|\think\response\Json
  90. * @throws BaseException
  91. */
  92. public function setDefault(int $addressId)
  93. {
  94. $model = UserAddressModel::detail($addressId);
  95. if ($model->setDefault((int)$model['address_id'])) {
  96. return $this->renderSuccess([], '设置成功');
  97. }
  98. return $this->renderError($model->getError() ?: '设置失败');
  99. }
  100. /**
  101. * 删除收货地址
  102. * @param int $addressId 地址ID
  103. * @return array|\think\response\Json
  104. * @throws BaseException
  105. */
  106. public function remove(int $addressId)
  107. {
  108. $model = UserAddressModel::detail($addressId);
  109. if ($model->remove()) {
  110. return $this->renderSuccess([], '删除成功');
  111. }
  112. return $this->renderError($model->getError() ?: '删除失败');
  113. }
  114. }