UserAddressServices.php 8.6 KB

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