UserAddress.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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\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 app\common\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. // * @param $value
  37. // * @param $data
  38. // * @return array
  39. // */
  40. // public function getRegionAttr($value, $data)
  41. // {
  42. // return array_values(parent::getRegionAttr($value, $data));
  43. // }
  44. /**
  45. * 获取收货地址列表
  46. * @return \think\Collection
  47. * @throws \think\db\exception\DataNotFoundException
  48. * @throws \think\db\exception\DbException
  49. * @throws \think\db\exception\ModelNotFoundException
  50. * @throws BaseException
  51. */
  52. public function getList()
  53. {
  54. $userId = UserService::getCurrentLoginUserId();
  55. return $this->where('user_id', '=', $userId)
  56. ->where('is_delete', '=', 0)
  57. ->select();
  58. }
  59. /**
  60. * 新增收货地址
  61. * @param array $data
  62. * @return mixed
  63. * @throws BaseException
  64. */
  65. public function add(array $data)
  66. {
  67. // 当前用户信息
  68. $user = UserService::getCurrentLoginUser(true);
  69. // 省市区ID
  70. list($data['province_id'], $data['city_id'], $data['region_id']) = $this->getRegionId($data);
  71. // 添加收货地址
  72. return $this->transaction(function () use ($user, $data) {
  73. $this->save([
  74. 'name' => $data['name'],
  75. 'phone' => $data['phone'],
  76. 'province_id' => $data['province_id'],
  77. 'city_id' => $data['city_id'],
  78. 'region_id' => $data['region_id'],
  79. 'detail' => $data['detail'],
  80. 'user_id' => $user['user_id'],
  81. 'store_id' => self::$storeId
  82. ]);
  83. // 设为默认收货地址
  84. !$user['address_id'] && $this->setDefault((int)$this['address_id']);
  85. return true;
  86. });
  87. }
  88. /**
  89. * 格式化用户上传的省市区数据
  90. * @param array $data
  91. * @return array
  92. * @throws BaseException
  93. */
  94. private function getRegionId(array $data)
  95. {
  96. if (!isset($data['region'])) {
  97. throwError('省市区不能为空');
  98. }
  99. if (count($data['region']) != 3) {
  100. throwError('省市区数据不合法');
  101. }
  102. return array_map(function ($item) {
  103. return $item['value'];
  104. }, $data['region']);
  105. }
  106. /**
  107. * 编辑收货地址
  108. * @param array $data
  109. * @return bool
  110. * @throws BaseException
  111. */
  112. public function edit(array $data)
  113. {
  114. // 省市区ID
  115. list($data['province_id'], $data['city_id'], $data['region_id']) = $this->getRegionId($data);
  116. // 更新收货地址
  117. return $this->save([
  118. 'name' => $data['name'],
  119. 'phone' => $data['phone'],
  120. 'province_id' => $data['province_id'],
  121. 'city_id' => $data['city_id'],
  122. 'region_id' => $data['region_id'],
  123. 'detail' => $data['detail']
  124. ]) !== false;
  125. }
  126. /**
  127. * 设为默认收货地址
  128. * @param int $addressIid
  129. * @return bool
  130. * @throws BaseException
  131. */
  132. public function setDefault(int $addressIid)
  133. {
  134. // 设为默认地址
  135. $userId = UserService::getCurrentLoginUserId();
  136. return UserModel::updateBase(['address_id' => $addressIid], ['user_id' => $userId]);
  137. }
  138. /**
  139. * 删除收货地址
  140. * @return bool
  141. * @throws BaseException
  142. */
  143. public function remove()
  144. {
  145. // 查询当前是否为默认地址
  146. $user = UserService::getCurrentLoginUser(true);
  147. // 清空默认地址
  148. if ($user['address_id'] == $this['address_id']) {
  149. UserModel::updateBase(['address_id' => 0], ['user_id' => $this['user_id']]);
  150. }
  151. // 标记为已删除
  152. return $this->save(['is_delete' => 1]);
  153. }
  154. /**
  155. * 收货地址详情
  156. * @param int $addressId
  157. * @return UserAddress|array|null
  158. * @throws BaseException
  159. */
  160. public static function detail(int $addressId)
  161. {
  162. $userId = UserService::getCurrentLoginUserId();
  163. $detail = self::get(['user_id' => $userId, 'address_id' => $addressId]);
  164. if (empty($detail)) {
  165. throwError('未找到该收货地址');
  166. return false;
  167. }
  168. return $detail;
  169. }
  170. }