UserAddress.php 6.1 KB

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