Address.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. use app\common\service\delivery\Express;
  17. /**
  18. * 收货地址管理
  19. * Class Address
  20. * @package app\api\controller
  21. */
  22. class Address extends Controller
  23. {
  24. /**
  25. * 收货地址列表
  26. * @return array|\think\response\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()
  33. {
  34. // 获取收货地址列表
  35. $model = new UserAddressModel;
  36. $list = $model->getList();
  37. return $this->renderSuccess(compact('list'));
  38. }
  39. /**
  40. * 获取当前用户默认收货地址
  41. * @return array|\think\response\Json
  42. * @throws BaseException
  43. */
  44. public function defaultId()
  45. {
  46. $useInfo = UserService::getCurrentLoginUser(true);
  47. return $this->renderSuccess(['defaultId' => $useInfo['address_id']]);
  48. }
  49. /**
  50. * 收货地址详情
  51. * @param int $addressId 地址ID
  52. * @return array|\think\response\Json
  53. * @throws BaseException
  54. */
  55. public function detail(int $addressId)
  56. {
  57. $detail = UserAddressModel::detail($addressId);
  58. return $this->renderSuccess(compact('detail'));
  59. }
  60. /**
  61. * 添加收货地址
  62. * @return array|\think\response\Json
  63. * @throws BaseException
  64. */
  65. public function add()
  66. {
  67. $model = new UserAddressModel;
  68. if ($model->add($this->request->post())) {
  69. return $this->renderSuccess([], '添加成功');
  70. }
  71. return $this->renderError($model->getError() ?: '添加失败');
  72. }
  73. /**
  74. * 编辑收货地址
  75. * @param int $addressId 地址ID
  76. * @return array|\think\response\Json
  77. * @throws BaseException
  78. */
  79. public function edit(int $addressId)
  80. {
  81. $model = UserAddressModel::detail($addressId);
  82. if ($model->edit($this->request->post())) {
  83. return $this->renderSuccess([], '更新成功');
  84. }
  85. return $this->renderError($model->getError() ?: '更新失败');
  86. }
  87. /**
  88. * 设为默认地址
  89. * @param int $addressId 地址ID
  90. * @return array|\think\response\Json
  91. * @throws BaseException
  92. */
  93. public function setDefault(int $addressId)
  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. public function checkLimit(int $addressId=0){
  103. $userInfo = UserService::getCurrentLoginUser(true);
  104. $model = UserAddressModel::detail($addressId);
  105. if(!$model){
  106. return $this->renderError("地址不存在");
  107. }
  108. if($model->user_id!=$userInfo->user_id){
  109. return $this->renderError("地址不正确");
  110. }
  111. $city_id = $model['city_id'];
  112. $e = new Express($city_id,[]);
  113. $flag = $e->isDeliveryLimit();
  114. return $this->renderSuccess(compact('flag'));
  115. }
  116. /**
  117. * 删除收货地址
  118. * @param int $addressId 地址ID
  119. * @return array|\think\response\Json
  120. * @throws BaseException
  121. */
  122. public function remove(int $addressId)
  123. {
  124. $model = UserAddressModel::detail($addressId);
  125. if ($model->remove()) {
  126. return $this->renderSuccess([], '删除成功');
  127. }
  128. return $this->renderError($model->getError() ?: '删除失败');
  129. }
  130. }