Address.php 3.7 KB

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