UserAddress.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\api\model;
  10. use app\common\model\Region;
  11. use app\common\model\UserAddress as UserAddressModel;
  12. /**
  13. * 用户收货地址模型
  14. * Class UserAddress
  15. * @package app\common\model
  16. */
  17. class UserAddress extends UserAddressModel
  18. {
  19. /**
  20. * 隐藏字段
  21. * @var array
  22. */
  23. protected $hidden = [
  24. 'wxapp_id',
  25. 'create_time',
  26. 'update_time'
  27. ];
  28. /**
  29. * @param $user_id
  30. * @return false|static[]
  31. * @throws \think\exception\DbException
  32. */
  33. public function getList($user_id)
  34. {
  35. return self::all(compact('user_id'));
  36. }
  37. /**
  38. * 新增收货地址
  39. * @param User $user
  40. * @param $data
  41. * @return mixed
  42. */
  43. public function add($user, $data)
  44. {
  45. return $this->transaction(function () use ($user, $data) {
  46. // 整理地区信息
  47. $region = explode(',', $data['region']);
  48. $provinceId = Region::getIdByName($region[0], 1);
  49. $cityId = Region::getIdByName($region[1], 2, $provinceId);
  50. $regionId = Region::getIdByName($region[2], 3, $cityId);
  51. // 验证城市ID是否合法
  52. if (!$this->checkCityId($cityId)) return false;
  53. // 添加收货地址
  54. $this->allowField(true)->save([
  55. 'name' => $data['name'],
  56. 'phone' => $data['phone'],
  57. 'province_id' => $provinceId,
  58. 'city_id' => $cityId,
  59. 'region_id' => $regionId,
  60. 'detail' => $data['detail'],
  61. 'district' => ($regionId === 0 && !empty($region[2])) ? $region[2] : '',
  62. 'user_id' => $user['user_id'],
  63. 'wxapp_id' => self::$wxapp_id
  64. ]);
  65. // 设为默认收货地址
  66. !$user['address_id'] && $user->save(['address_id' => $this['address_id']]);
  67. return true;
  68. });
  69. }
  70. /**
  71. * 编辑收货地址
  72. * @param $data
  73. * @return false|int
  74. */
  75. public function edit($data)
  76. {
  77. // 整理地区信息
  78. $region = explode(',', $data['region']);
  79. $provinceId = Region::getIdByName($region[0], 1);
  80. $cityId = Region::getIdByName($region[1], 2, $provinceId);
  81. $regionId = Region::getIdByName($region[2], 3, $cityId);
  82. // 验证城市ID是否合法
  83. if (!$this->checkCityId($cityId)) return false;
  84. // 更新收货地址
  85. return $this->allowField(true)->save([
  86. 'name' => $data['name'],
  87. 'phone' => $data['phone'],
  88. 'province_id' => $provinceId,
  89. 'city_id' => $cityId,
  90. 'region_id' => $regionId,
  91. 'detail' => $data['detail'],
  92. 'district' => ($regionId === 0 && !empty($region[2])) ? $region[2] : '',
  93. ]) !== false;
  94. }
  95. /**
  96. * 验证城市ID是否合法
  97. * @param $cityId
  98. * @return bool
  99. */
  100. private function checkCityId($cityId)
  101. {
  102. if ($cityId <= 0) {
  103. \log_write([
  104. 'system_msg' => '选择的城市不存在',
  105. 'param' => \request()->param()
  106. ], 'error');
  107. $this->error = '很抱歉,您选择的城市不存在';
  108. return false;
  109. }
  110. return true;
  111. }
  112. /**
  113. * 设为默认收货地址
  114. * @param User $user
  115. * @return int
  116. */
  117. public function setDefault($user)
  118. {
  119. // 设为默认地址
  120. return $user->save(['address_id' => $this['address_id']]);
  121. }
  122. /**
  123. * 删除收货地址
  124. * @param User $user
  125. * @return int
  126. */
  127. public function remove($user)
  128. {
  129. // 查询当前是否为默认地址
  130. $user['address_id'] == $this['address_id'] && $user->save(['address_id' => 0]);
  131. return $this->delete();
  132. }
  133. /**
  134. * 收货地址详情
  135. * @param $user_id
  136. * @param $address_id
  137. * @return null|static
  138. * @throws \think\exception\DbException
  139. */
  140. public static function detail($user_id, $address_id)
  141. {
  142. return self::get(compact('user_id', 'address_id'));
  143. }
  144. }