UserAddressServices.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. */
  78. public function getUserDefaultAddress(int $uid, string $field = '*')
  79. {
  80. return $this->dao->getOne(['uid' => $uid, 'is_default' => 1, 'is_del' => 0], $field);
  81. }
  82. /**
  83. * 获取条数
  84. * @param array $where
  85. * @return int
  86. */
  87. public function getAddresCount(array $where): int
  88. {
  89. return $this->dao->count($where);
  90. }
  91. /**
  92. * 添加地址
  93. * @param array $data
  94. * @return bool
  95. */
  96. public function create(array $data)
  97. {
  98. if (!$this->dao->save($data))
  99. throw new AdminException(100022);
  100. return true;
  101. }
  102. /**
  103. * 修改地址
  104. * @param $id
  105. * @param $data
  106. * @return bool
  107. */
  108. public function updateAddress(int $id, array $data)
  109. {
  110. if (!$this->dao->update($id, $data))
  111. throw new AdminException(100007);
  112. return true;
  113. }
  114. /**
  115. * 设置默认定制
  116. * @param int $uid
  117. * @param int $id
  118. * @return bool
  119. */
  120. public function setDefault(int $uid, int $id)
  121. {
  122. if (!$this->getAddress($id)) {
  123. throw new ApiException(400648);
  124. }
  125. if (!$this->dao->update($uid, ['is_default' => 0], 'uid'))
  126. throw new ApiException(400649);
  127. if (!$this->dao->update($id, ['is_default' => 1]))
  128. throw new ApiException(400650);
  129. return true;
  130. }
  131. /**
  132. * 获取单个地址
  133. * @param int $id
  134. * @return mixed
  135. */
  136. public function address(int $id)
  137. {
  138. $addressInfo = $this->getAddress($id);
  139. if (!$addressInfo || $addressInfo['is_del'] == 1) {
  140. throw new ApiException(100026);
  141. }
  142. return $addressInfo->toArray();
  143. }
  144. /**
  145. * 添加|修改地址
  146. * @param int $uid
  147. * @param array $addressInfo
  148. * @return mixed
  149. */
  150. public function editAddress(int $uid, array $addressInfo)
  151. {
  152. if ($addressInfo['id'] == 0) {
  153. $where = [
  154. ['uid', '=', $uid],
  155. ['real_name', '=', $addressInfo['real_name']],
  156. ['phone', '=', $addressInfo['phone']],
  157. ['detail', '=', $addressInfo['detail']],
  158. ['is_del', '=', 0]
  159. ];
  160. if (isset($addressInfo['address']['city_id'])) {
  161. $where += ['city_id', '=', $addressInfo['address']['city_id']];
  162. }
  163. $res = $this->dao->getCount($where);
  164. if ($res) throw new ApiException(400651);
  165. }
  166. if ($addressInfo['type'] == 1 && !$addressInfo['id']) {
  167. $city = $addressInfo['address']['city'];
  168. /** @var SystemCityServices $systemCity */
  169. $systemCity = app()->make(SystemCityServices::class);
  170. $cityInfo = $systemCity->getOne([['name', '=', $city], ['parent_id', '<>', 0]]);
  171. if ($cityInfo && $cityInfo['city_id']) {
  172. $addressInfo['address']['city_id'] = $cityInfo['city_id'];
  173. } else {
  174. $cityInfo = $systemCity->getOne([['name', 'like', "%$city%"], ['parent_id', '<>', 0]]);
  175. if (!$cityInfo) {
  176. throw new ApiException(400652);
  177. }
  178. $addressInfo['address']['city_id'] = $cityInfo['city_id'];
  179. }
  180. }
  181. if (!isset($addressInfo['address']['city_id']) || $addressInfo['address']['city_id'] == 0) throw new ApiException(100022);
  182. $addressInfo['province'] = $addressInfo['address']['province'];
  183. $addressInfo['city'] = $addressInfo['address']['city'];
  184. $addressInfo['city_id'] = $addressInfo['address']['city_id'] ?? 0;
  185. $addressInfo['district'] = $addressInfo['address']['district'];
  186. $addressInfo['is_default'] = (int)$addressInfo['is_default'] == true ? 1 : 0;
  187. $addressInfo['uid'] = $uid;
  188. unset($addressInfo['address'], $addressInfo['type']);
  189. //数据验证
  190. validate(AddressValidate::class)->check($addressInfo);
  191. $address_check = [];
  192. if ($addressInfo['id']) {
  193. $address_check = $this->getAddress((int)$addressInfo['id']);
  194. }
  195. if ($address_check && $address_check['is_del'] == 0 && $address_check['uid'] = $uid) {
  196. $id = (int)$addressInfo['id'];
  197. unset($addressInfo['id']);
  198. if (!$this->dao->update($id, $addressInfo, 'id')) {
  199. throw new ApiException(100007);
  200. }
  201. if ($addressInfo['is_default']) {
  202. $this->setDefault($uid, $id);
  203. }
  204. return ['type' => 'edit', 'msg' => '编辑地址成功', 'data' => []];
  205. } else {
  206. $addressInfo['add_time'] = time();
  207. //首次添加地址,自动设置为默认地址
  208. $addrCount = $this->getAddresCount(['uid' => $uid]);
  209. if (!$addrCount) $addressInfo['is_default'] = 1;
  210. if (!$address = $this->dao->save($addressInfo)) {
  211. throw new ApiException(100022);
  212. }
  213. if ($addressInfo['is_default']) {
  214. $this->setDefault($uid, (int)$address->id);
  215. }
  216. return ['type' => 'add', 'msg' => '添加地址成功', 'data' => ['id' => $address->id]];
  217. }
  218. }
  219. /**
  220. * 删除地址
  221. * @param int $uid
  222. * @param int $id
  223. * @return bool
  224. */
  225. public function delAddress(int $uid, int $id)
  226. {
  227. $addressInfo = $this->getAddress($id);
  228. if (!$addressInfo || $addressInfo['is_del'] == 1 || $addressInfo['uid'] != $uid) {
  229. throw new ApiException(100026);
  230. }
  231. if ($this->dao->update($id, ['is_del' => '1'], 'id'))
  232. return true;
  233. else
  234. throw new ApiException(100008);
  235. }
  236. /**
  237. * 设置默认用户地址
  238. * @param $id
  239. * @param $uid
  240. * @return bool
  241. */
  242. public function setDefaultAddress(int $id, int $uid)
  243. {
  244. $res1 = $this->dao->update($uid, ['is_default' => 0], 'uid');
  245. $res2 = $this->dao->update(['id' => $id, 'uid' => $uid], ['is_default' => 1]);
  246. $res = $res1 !== false && $res2 !== false;
  247. return $res;
  248. }
  249. }