UserAddress.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. use app\api\model\DeliveryLimit as DeliveryLimitModel;
  18. /**
  19. * 用户收货地址模型
  20. * Class UserAddress
  21. * @package app\common\model
  22. */
  23. class UserAddress extends UserAddressModel
  24. {
  25. /**
  26. * 隐藏字段
  27. * @var array
  28. */
  29. protected $hidden = [
  30. 'is_delete',
  31. 'store_id',
  32. 'create_time',
  33. 'update_time'
  34. ];
  35. // /**
  36. // * 地区名称
  37. // * @param $value
  38. // * @param $data
  39. // * @return array
  40. // */
  41. // public function getRegionAttr($value, $data)
  42. // {
  43. // return array_values(parent::getRegionAttr($value, $data));
  44. // }
  45. /**
  46. * 获取收货地址列表
  47. * @return \think\Collection
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. * @throws BaseException
  52. */
  53. public function getList()
  54. {
  55. $userId = UserService::getCurrentLoginUserId();
  56. return $this->where('user_id', '=', $userId)
  57. ->where('is_delete', '=', 0)
  58. ->order('default','desc')
  59. ->select();
  60. }
  61. /**
  62. * 新增收货地址
  63. * @param array $data
  64. * @return mixed
  65. * @throws BaseException
  66. */
  67. public function add(array $data)
  68. {
  69. // 当前用户信息
  70. $user = UserService::getCurrentLoginUser(true);
  71. // 省市区ID
  72. $this->getRegionId($data);
  73. // 添加收货地址
  74. return $this->transaction(function () use ($user, $data) {
  75. $this->save([
  76. 'name' => $data['name'],
  77. 'phone' => $data['phone'],
  78. 'province_id' => $data['province_id'],
  79. 'city_id' => $data['city_id'],
  80. 'region_id' => $data['region_id'],
  81. 'detail' => $data['detail'],
  82. 'default' => $data['default'],
  83. 'user_id' => $user['user_id'],
  84. 'store_id' => self::$storeId
  85. ]);
  86. // 如果没有默认地址或者设置为默认(default=1),就设为默认收货地址
  87. if(!$user['address_id'] || $data['default']==1){
  88. $this->setDefault((int)$this['address_id']);
  89. }
  90. return true;
  91. });
  92. }
  93. /**
  94. * 格式化用户上传的省市区数据
  95. * @param array $data
  96. * @return array
  97. * @throws BaseException
  98. */
  99. private function getRegionId(array $data)
  100. {
  101. if (!isset($data['province_id']) || !isset($data['city_id']) || !isset($data['region_id'])) {
  102. throwError('省市区不能为空');
  103. }
  104. $detail = DeliveryLimitModel::detail(DeliveryLimitModel::DEFAULT_ID);
  105. if (in_array($data['city_id'],$detail['region'])) {
  106. throwError('限购地区省市不能设置为地址');
  107. }
  108. // return array_map(function ($item) {
  109. // return $item['value'];
  110. // }, $data['region']);
  111. }
  112. /**
  113. * 编辑收货地址
  114. * @param array $data
  115. * @return bool
  116. * @throws BaseException
  117. */
  118. public function edit(array $data)
  119. {
  120. // 省市区ID
  121. $this->getRegionId($data);
  122. // 更新收货地址
  123. return $this->transaction(function () use ($data) {
  124. $this->save([
  125. 'name' => $data['name'],
  126. 'phone' => $data['phone'],
  127. 'province_id' => $data['province_id'],
  128. 'city_id' => $data['city_id'],
  129. 'region_id' => $data['region_id'],
  130. 'default' => $data['default'],
  131. 'detail' => $data['detail']
  132. ]);
  133. // 如果没有默认地址或者设置为默认(default=1),就设为默认收货地址
  134. if($data['default']==1){
  135. $this->setDefault((int)$this['address_id']);
  136. }
  137. return true;
  138. });
  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 $this->transaction(function () use ($addressIid, $userId) {
  151. $this->updateBase(['default' => 1], [['address_id', '=', $addressIid],['user_id', '=', $userId]]);
  152. $this->updateBase(['default' => 0], [['address_id', '<>', $addressIid],['user_id', '=', $userId]]);
  153. UserModel::updateBase(['address_id' => $addressIid], ['user_id' => $userId]);
  154. return true;
  155. });
  156. }
  157. /**
  158. * 删除收货地址
  159. * @return bool
  160. * @throws BaseException
  161. */
  162. public function remove()
  163. {
  164. // 查询当前是否为默认地址
  165. $user = UserService::getCurrentLoginUser(true);
  166. // 清空默认地址
  167. if ($user['address_id'] == $this['address_id']) {
  168. UserModel::updateBase(['address_id' => 0], ['user_id' => $this['user_id']]);
  169. }
  170. // 标记为已删除
  171. return $this->save(['is_delete' => 1]);
  172. // return $this->delete();
  173. }
  174. /**
  175. * 收货地址详情
  176. * @param int $addressId
  177. * @return UserAddress|array|null
  178. * @throws BaseException
  179. */
  180. public static function detail(int $addressId)
  181. {
  182. $userId = UserService::getCurrentLoginUserId();
  183. $detail = self::get(['user_id' => $userId, 'address_id' => $addressId]);
  184. if (empty($detail)) {
  185. throwError('未找到该收货地址');
  186. return false;
  187. }
  188. return $detail;
  189. }
  190. }