UserAddress.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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\index\model;
  13. use app\index\model\User as UserModel;
  14. use app\index\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. 'last_name' => $data['last_name'],
  76. 'phone' => $data['phone'],
  77. 'province_id' => $data['province_id'],
  78. 'city_id' => $data['city_id'],
  79. 'region_id' => $data['region_id'],
  80. 'detail' => $data['detail'],
  81. 'user_id' => $user['user_id'],
  82. 'zip_code' => $data['zip_code'],
  83. 'email' => $data['email'],
  84. 'store_id' => self::$storeId
  85. ]);
  86. // 设为默认收货地址
  87. !$user['address_id'] && $this->setDefault((int)$this['address_id']);
  88. //return true;
  89. $regionValue = $data['region'][2]['label'] ?? '';
  90. return [
  91. 'address_id' => $this['address_id'],
  92. 'full' => $data['detail'] . ',' . $regionValue . '(' . $data['zip_code'] . '),US',
  93. 'zip_code' => $data['zip_code'],
  94. 'name' => $data['name'],
  95. 'last_name' => $data['last_name'],
  96. 'phone' => $data['phone'],
  97. 'email' => $data['email'],
  98. ];
  99. });
  100. }
  101. /**
  102. * 格式化用户上传的省市区数据
  103. * @param array $data
  104. * @return array
  105. * @throws BaseException
  106. */
  107. private function getRegionId(array $data)
  108. {
  109. if (!isset($data['region'])) {
  110. throwError('省市区不能为空');
  111. }
  112. if (count($data['region']) != 3) {
  113. throwError('省市区数据不合法');
  114. }
  115. return array_map(function ($item) {
  116. return $item['value'];
  117. }, $data['region']);
  118. }
  119. /**
  120. * 编辑收货地址
  121. * @param array $data
  122. * @return bool
  123. * @throws BaseException
  124. */
  125. public function edit(array $data)
  126. {
  127. // 省市区ID
  128. list($data['province_id'], $data['city_id'], $data['region_id']) = $this->getRegionId($data);
  129. // 更新收货地址
  130. return $this->save([
  131. 'name' => $data['name'],
  132. 'phone' => $data['phone'],
  133. 'province_id' => $data['province_id'],
  134. 'city_id' => $data['city_id'],
  135. 'region_id' => $data['region_id'],
  136. 'detail' => $data['detail']
  137. ]) !== false;
  138. }
  139. /**
  140. * 设为默认收货地址
  141. * @param int $addressIid
  142. * @return bool
  143. * @throws BaseException
  144. */
  145. public function setDefault(int $addressIid)
  146. {
  147. // 设为默认地址
  148. $userId = UserService::getCurrentLoginUserId();
  149. return UserModel::updateBase(['address_id' => $addressIid], ['user_id' => $userId]);
  150. }
  151. /**
  152. * 删除收货地址
  153. * @return bool
  154. * @throws BaseException
  155. */
  156. public function remove()
  157. {
  158. // 查询当前是否为默认地址
  159. $user = UserService::getCurrentLoginUser(true);
  160. // 清空默认地址
  161. if ($user['address_id'] == $this['address_id']) {
  162. UserModel::updateBase(['address_id' => 0], ['user_id' => $this['user_id']]);
  163. }
  164. // 标记为已删除
  165. return $this->save(['is_delete' => 1]);
  166. }
  167. /**
  168. * 收货地址详情
  169. * @param int $addressId
  170. * @return UserAddress|array|null
  171. * @throws BaseException
  172. */
  173. public static function detail(int $addressId)
  174. {
  175. $userId = UserService::getCurrentLoginUserId();
  176. $detail = self::get(['user_id' => $userId, 'address_id' => $addressId]);
  177. if (empty($detail)) {
  178. throwError('未找到该收货地址');
  179. return false;
  180. }
  181. return $detail;
  182. }
  183. }