UserAddress.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2025 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\model;
  13. use app\api\model\User as UserModel;
  14. use app\api\service\User as UserService;
  15. use app\common\model\UserAddress as UserAddressModel;
  16. use cores\exception\BaseException;
  17. /**
  18. * 用户收货地址模型
  19. * Class UserAddress
  20. * @package app\common\model
  21. */
  22. class UserAddress extends UserAddressModel
  23. {
  24. /**
  25. * 隐藏字段
  26. * @var array
  27. */
  28. protected $hidden = [
  29. 'is_delete',
  30. 'store_id',
  31. 'create_time',
  32. 'update_time'
  33. ];
  34. /**
  35. * 获取收货地址列表
  36. * @return \think\Collection
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. * @throws BaseException
  41. */
  42. public function getList(): \think\Collection
  43. {
  44. $userId = UserService::getCurrentLoginUserId();
  45. return $this->where('user_id', '=', $userId)
  46. ->where('is_delete', '=', 0)
  47. ->select();
  48. }
  49. /**
  50. * 新增收货地址
  51. * @param array $data
  52. * @return mixed
  53. * @throws BaseException
  54. */
  55. public function add(array $data)
  56. {
  57. // 当前用户信息
  58. $user = UserService::getCurrentLoginUser(true);
  59. // 省市区ID
  60. [$data['province_id'], $data['city_id'], $data['region_id']] = $this->getRegionId($data);
  61. // 添加收货地址
  62. return $this->transaction(function () use ($user, $data) {
  63. $this->save([
  64. 'name' => $data['name'],
  65. 'phone' => $data['phone'],
  66. 'province_id' => $data['province_id'],
  67. 'city_id' => $data['city_id'],
  68. 'region_id' => $data['region_id'],
  69. 'detail' => $data['detail'],
  70. 'user_id' => $user['user_id'],
  71. 'store_id' => self::$storeId
  72. ]);
  73. // 设为默认收货地址
  74. !$user['address_id'] && $this->setDefault((int)$this['address_id']);
  75. return true;
  76. });
  77. }
  78. /**
  79. * 格式化用户上传的省市区数据
  80. * @param array $data
  81. * @return array
  82. * @throws BaseException
  83. */
  84. private function getRegionId(array $data): array
  85. {
  86. if (!isset($data['region'])) {
  87. throwError('省市区不能为空');
  88. }
  89. if (count($data['region']) != 3) {
  90. throwError('省市区数据不正确');
  91. }
  92. return \array_map(fn($item) => $item['value'], $data['region']);
  93. }
  94. /**
  95. * 编辑收货地址
  96. * @param array $data
  97. * @return bool
  98. * @throws BaseException
  99. */
  100. public function edit(array $data): bool
  101. {
  102. // 省市区ID
  103. [$data['province_id'], $data['city_id'], $data['region_id']] = $this->getRegionId($data);
  104. // 更新收货地址
  105. return $this->save([
  106. 'name' => $data['name'],
  107. 'phone' => $data['phone'],
  108. 'province_id' => $data['province_id'],
  109. 'city_id' => $data['city_id'],
  110. 'region_id' => $data['region_id'],
  111. 'detail' => $data['detail']
  112. ]) !== false;
  113. }
  114. /**
  115. * 设为默认收货地址
  116. * @param int $addressIid
  117. * @return bool
  118. * @throws BaseException
  119. */
  120. public function setDefault(int $addressIid): bool
  121. {
  122. // 设为默认地址
  123. $userId = UserService::getCurrentLoginUserId();
  124. return UserModel::updateBase(['address_id' => $addressIid], $userId);
  125. }
  126. /**
  127. * 删除收货地址
  128. * @return bool
  129. * @throws BaseException
  130. */
  131. public function remove(): bool
  132. {
  133. // 查询当前是否为默认地址
  134. $user = UserService::getCurrentLoginUser(true);
  135. // 清空默认地址
  136. if ($user['address_id'] == $this['address_id']) {
  137. UserModel::updateBase(['address_id' => 0], $this['user_id']);
  138. }
  139. // 标记为已删除
  140. return $this->save(['is_delete' => 1]);
  141. }
  142. /**
  143. * 收货地址详情
  144. * @param int $addressId
  145. * @return static|array|null
  146. * @throws BaseException
  147. */
  148. public static function detail(int $addressId)
  149. {
  150. $userId = UserService::getCurrentLoginUserId();
  151. $detail = self::get(['user_id' => $userId, 'address_id' => $addressId]);
  152. empty($detail) && throwError('未找到该收货地址');
  153. return $detail;
  154. }
  155. }