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