UserAddress.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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\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(function ($item) {
  93. return $item['value'];
  94. }, $data['region']);
  95. }
  96. /**
  97. * 编辑收货地址
  98. * @param array $data
  99. * @return bool
  100. * @throws BaseException
  101. */
  102. public function edit(array $data): bool
  103. {
  104. // 省市区ID
  105. [$data['province_id'], $data['city_id'], $data['region_id']] = $this->getRegionId($data);
  106. // 更新收货地址
  107. return $this->save([
  108. 'name' => $data['name'],
  109. 'phone' => $data['phone'],
  110. 'province_id' => $data['province_id'],
  111. 'city_id' => $data['city_id'],
  112. 'region_id' => $data['region_id'],
  113. 'detail' => $data['detail']
  114. ]) !== false;
  115. }
  116. /**
  117. * 设为默认收货地址
  118. * @param int $addressIid
  119. * @return bool
  120. * @throws BaseException
  121. */
  122. public function setDefault(int $addressIid): bool
  123. {
  124. // 设为默认地址
  125. $userId = UserService::getCurrentLoginUserId();
  126. return UserModel::updateBase(['address_id' => $addressIid], $userId);
  127. }
  128. /**
  129. * 删除收货地址
  130. * @return bool
  131. * @throws BaseException
  132. */
  133. public function remove(): bool
  134. {
  135. // 查询当前是否为默认地址
  136. $user = UserService::getCurrentLoginUser(true);
  137. // 清空默认地址
  138. if ($user['address_id'] == $this['address_id']) {
  139. UserModel::updateBase(['address_id' => 0], $this['user_id']);
  140. }
  141. // 标记为已删除
  142. return $this->save(['is_delete' => 1]);
  143. }
  144. /**
  145. * 收货地址详情
  146. * @param int $addressId
  147. * @return static|array|null
  148. * @throws BaseException
  149. */
  150. public static function detail(int $addressId)
  151. {
  152. $userId = UserService::getCurrentLoginUserId();
  153. $detail = self::get(['user_id' => $userId, 'address_id' => $addressId]);
  154. empty($detail) && throwError('未找到该收货地址');
  155. return $detail;
  156. }
  157. }