UserAddress.php 6.2 KB

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