UserAddressServices.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. namespace app\services\user;
  13. use app\api\validate\user\AddressValidate;
  14. use app\services\BaseServices;
  15. use app\dao\user\UserAddressDao;
  16. use app\services\shipping\SystemCityServices;
  17. use crmeb\exceptions\AdminException;
  18. use think\Exception;
  19. use think\exception\ValidateException;
  20. /**
  21. *
  22. * Class UserAddressServices
  23. * @package app\services\user
  24. * @method getOne(array $where, ?string $field = '*', array $with = []) 获取一条数据
  25. * @method be($map, string $field = '') 验证数据是否存在
  26. */
  27. class UserAddressServices extends BaseServices
  28. {
  29. /**
  30. * UserAddressServices constructor.
  31. * @param UserAddressDao $dao
  32. */
  33. public function __construct(UserAddressDao $dao)
  34. {
  35. $this->dao = $dao;
  36. }
  37. /**
  38. * 获取单个地址
  39. * @param $id
  40. * @param $field
  41. * @return array
  42. */
  43. public function getAddress($id, $field = [])
  44. {
  45. return $this->dao->get($id, $field);
  46. }
  47. /**
  48. * 获取所有地址
  49. * @param array $where
  50. * @param string $field
  51. * @return array
  52. */
  53. public function getAddressList(array $where, string $field = '*'): array
  54. {
  55. [$page, $limit] = $this->getPageValue();
  56. $list = $this->dao->getList($where, $field, $page, $limit);
  57. $count = $this->getAddresCount($where);
  58. return compact('list', 'count');
  59. }
  60. /**
  61. * 获取某个用户的所有地址
  62. * @param int $uid
  63. * @param string $field
  64. * @return array
  65. */
  66. public function getUserAddressList(int $uid, string $field = '*'): array
  67. {
  68. [$page, $limit] = $this->getPageValue();
  69. $where = ['uid' => $uid];
  70. $where['is_del'] = 0;
  71. return $this->dao->getList($where, $field, $page, $limit);
  72. }
  73. /**
  74. * 获取用户默认地址
  75. * @param int $uid
  76. * @param string $field
  77. * @return array
  78. */
  79. public function getUserDefaultAddress(int $uid, string $field = '*')
  80. {
  81. return $this->dao->getOne(['uid' => $uid, 'is_default' => 1, 'is_del' => 0], $field);
  82. }
  83. /**
  84. * 获取条数
  85. * @param array $where
  86. * @return int
  87. */
  88. public function getAddresCount(array $where): int
  89. {
  90. return $this->dao->count($where);
  91. }
  92. /**
  93. * 添加地址
  94. * @param array $data
  95. * @return bool
  96. */
  97. public function create(array $data)
  98. {
  99. if (!$this->dao->save($data))
  100. throw new AdminException('写入失败');
  101. return true;
  102. }
  103. /**
  104. * 修改地址
  105. * @param $id
  106. * @param $data
  107. * @return bool
  108. */
  109. public function updateAddress(int $id, array $data)
  110. {
  111. if (!$this->dao->update($id, $data))
  112. throw new AdminException('修改失败');
  113. return true;
  114. }
  115. /**
  116. * 设置默认定制
  117. * @param int $uid
  118. * @param int $id
  119. * @return bool
  120. */
  121. public function setDefault(int $uid, int $id)
  122. {
  123. if (!$this->getAddress($id)) {
  124. throw new ValidateException('地址不存在');
  125. }
  126. if (!$this->dao->update($uid, ['is_default' => 0], 'uid'))
  127. throw new Exception('取消原来默认地址');
  128. if (!$this->dao->update($id, ['is_default' => 1]))
  129. throw new Exception('设置默认地址失败');
  130. return true;
  131. }
  132. /**
  133. * 获取单个地址
  134. * @param int $id
  135. * @return mixed
  136. */
  137. public function address(int $id)
  138. {
  139. $addressInfo = $this->getAddress($id);
  140. if (!$addressInfo || $addressInfo['is_del'] == 1) {
  141. throw new ValidateException('数据不存在');
  142. }
  143. return $addressInfo->toArray();
  144. }
  145. /**
  146. * 添加|修改地址
  147. * @param int $uid
  148. * @param array $addressInfo
  149. * @return mixed
  150. */
  151. public function editAddress(int $uid, array $addressInfo)
  152. {
  153. if ($addressInfo['id'] == 0) {
  154. if (isset($addressInfo['address']['city_id'])) {
  155. $where[] = ['city_id', '=', $addressInfo['address']['city_id']];
  156. } else {
  157. $where = [];
  158. }
  159. $res = $this->dao->getCount([
  160. ['uid', '=', $uid],
  161. ['real_name', '=', $addressInfo['real_name']],
  162. ['phone', '=', $addressInfo['phone']],
  163. ['detail', '=', $addressInfo['detail']],
  164. ['is_del', '=', 0]
  165. ] + $where);
  166. if ($res) throw new ValidateException('地址已存在,请勿重复添加');
  167. }
  168. if ($addressInfo['type'] == 1 && !$addressInfo['id']) {
  169. $city = $addressInfo['address']['city'];
  170. /** @var SystemCityServices $systemCity */
  171. $systemCity = app()->make(SystemCityServices::class);
  172. $cityInfo = $systemCity->getOne([['name', '=', $city], ['parent_id', '<>', 0]]);
  173. if ($cityInfo && $cityInfo['city_id']) {
  174. $addressInfo['address']['city_id'] = $cityInfo['city_id'];
  175. } else {
  176. $cityInfo = $systemCity->getOne([['name', 'like', "%$city%"], ['parent_id', '<>', 0]]);
  177. if (!$cityInfo) {
  178. throw new ValidateException('收货地址格式错误!修改后请重新导入!');
  179. }
  180. $addressInfo['address']['city_id'] = $cityInfo['city_id'];
  181. }
  182. }
  183. if (!isset($addressInfo['address']['city_id']) || $addressInfo['address']['city_id'] == 0) throw new ValidateException('添加收货地址失败!');
  184. $addressInfo['province'] = $addressInfo['address']['province'];
  185. $addressInfo['city'] = $addressInfo['address']['city'];
  186. $addressInfo['city_id'] = $addressInfo['address']['city_id'] ?? 0;
  187. $addressInfo['district'] = $addressInfo['address']['district'];
  188. $addressInfo['is_default'] = (int)$addressInfo['is_default'] == true ? 1 : 0;
  189. $addressInfo['uid'] = $uid;
  190. unset($addressInfo['address'], $addressInfo['type']);
  191. //数据验证
  192. validate(AddressValidate::class)->check($addressInfo);
  193. $address_check = [];
  194. if ($addressInfo['id']) {
  195. $address_check = $this->getAddress((int)$addressInfo['id']);
  196. }
  197. if ($address_check && $address_check['is_del'] == 0 && $address_check['uid'] = $uid) {
  198. $id = (int)$addressInfo['id'];
  199. unset($addressInfo['id']);
  200. if (!$this->dao->update($id, $addressInfo, 'id')) {
  201. throw new ValidateException('编辑收货地址失败');
  202. }
  203. if ($addressInfo['is_default']) {
  204. $this->setDefault($uid, $id);
  205. }
  206. return ['type' => 'edit', 'msg' => '编辑地址成功', 'data' => []];
  207. } else {
  208. $addressInfo['add_time'] = time();
  209. //首次添加地址,自动设置为默认地址
  210. $addrCount = $this->getAddresCount(['uid' => $uid]);
  211. if (!$addrCount) $addressInfo['is_default'] = 1;
  212. if (!$address = $this->dao->save($addressInfo)) {
  213. throw new ValidateException('添加收货地址失败');
  214. }
  215. if ($addressInfo['is_default']) {
  216. $this->setDefault($uid, (int)$address->id);
  217. }
  218. return ['type' => 'add', 'msg' => '添加地址成功', 'data' => ['id' => $address->id]];
  219. }
  220. }
  221. /**
  222. * 删除地址
  223. * @param int $uid
  224. * @param int $id
  225. * @return bool
  226. */
  227. public function delAddress(int $uid, int $id)
  228. {
  229. $addressInfo = $this->getAddress($id);
  230. if (!$addressInfo || $addressInfo['is_del'] == 1 || $addressInfo['uid'] != $uid) {
  231. throw new ValidateException('数据不存在');
  232. }
  233. if ($this->dao->update($id, ['is_del' => '1'], 'id'))
  234. return true;
  235. else
  236. throw new ValidateException('删除地址失败!');
  237. }
  238. /**
  239. * 设置默认用户地址
  240. * @param $id
  241. * @param $uid
  242. * @return bool
  243. */
  244. public function setDefaultAddress(int $id, int $uid)
  245. {
  246. $res1 = $this->dao->update($uid, ['is_default' => 0], 'uid');
  247. $res2 = $this->dao->update(['id' => $id, 'uid' => $uid], ['is_default' => 1]);
  248. $res = $res1 !== false && $res2 !== false;
  249. return $res;
  250. }
  251. }