UserAddressServices.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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\model\user\UserAddress;
  14. use app\services\system\CityAreaServices;
  15. use app\validate\api\user\AddressValidate;
  16. use qiniu\basic\BaseServices;
  17. use qiniu\exceptions\AdminException;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\DbException;
  20. use think\db\exception\ModelNotFoundException;
  21. use think\Exception;
  22. use think\exception\ValidateException;
  23. use think\Model;
  24. /**
  25. *
  26. * Class UserAddressServices
  27. * @package app\services\user
  28. * @mixin UserAddress
  29. */
  30. class UserAddressServices extends BaseServices
  31. {
  32. /**
  33. * UserAddressServices constructor.
  34. * @param UserAddress $model
  35. */
  36. public function __construct(UserAddress $model)
  37. {
  38. $this->model = $model;
  39. }
  40. /**
  41. * 获取单个地址
  42. * @param $id
  43. * @param array $field
  44. * @return array
  45. * @throws DataNotFoundException
  46. * @throws DbException
  47. * @throws ModelNotFoundException
  48. */
  49. public function getAddress($id, $field = [])
  50. {
  51. return $this->get($id, $field);
  52. }
  53. /**
  54. * 获取所有地址
  55. * @param array $where
  56. * @param string $field
  57. * @return array
  58. * @throws DbException
  59. */
  60. public function getAddressList(array $where, string $field = '*'): array
  61. {
  62. [$page, $limit] = $this->getPageValue();
  63. $list = $this->getList($where, $field, $page, $limit);
  64. $count = $this->getCount($where);
  65. return compact('list', 'count');
  66. }
  67. /**
  68. * 获取某个用户的所有地址
  69. * @param int $uid
  70. * @param string $field
  71. * @return array
  72. */
  73. public function getUserAddressList(int $uid, string $field = '*'): array
  74. {
  75. [$page, $limit] = $this->getPageValue();
  76. $where = ['uid' => $uid];
  77. return $this->getList($where, $field, $page, $limit);
  78. }
  79. /**
  80. * @param int $uid
  81. * @param string $field
  82. * @return array|Model|null
  83. * @throws DataNotFoundException
  84. * @throws DbException
  85. * @throws ModelNotFoundException
  86. */
  87. public function getUserDefaultAddress(int $uid, string $field = '*')
  88. {
  89. return $this->getOne(['uid' => $uid, 'is_default' => 1], $field);
  90. }
  91. /**
  92. * 添加地址
  93. * @param array $data
  94. * @return bool
  95. */
  96. public function createAddress(array $data)
  97. {
  98. if (!$this->save($data))
  99. throw new AdminException('写入失败');
  100. return true;
  101. }
  102. /**
  103. * 修改地址
  104. * @param int $id
  105. * @param array $data
  106. * @return bool
  107. */
  108. public function updateAddress(int $id, array $data)
  109. {
  110. if (!$this->update($id, $data))
  111. throw new AdminException('修改失败');
  112. return true;
  113. }
  114. /**
  115. * 设置默认定制
  116. * @param int $uid
  117. * @param int $id
  118. * @return bool
  119. * @throws Exception
  120. */
  121. public function setDefault(int $uid, int $id)
  122. {
  123. if (!$this->be($id)) {
  124. throw new ValidateException('地址不存在');
  125. }
  126. if (!$this->update($uid, ['is_default' => 0], 'uid'))
  127. throw new Exception('取消原来默认地址');
  128. if (!$this->update($id, ['is_default' => 1]))
  129. throw new Exception('设置默认地址失败');
  130. return true;
  131. }
  132. /**
  133. * 获取单个地址
  134. * @param int $id
  135. * @return mixed
  136. * @throws DataNotFoundException
  137. * @throws DbException
  138. * @throws ModelNotFoundException
  139. */
  140. public function address(int $id)
  141. {
  142. $addressInfo = $this->getAddress($id);
  143. if (!$addressInfo) {
  144. throw new ValidateException('数据不存在');
  145. }
  146. return $addressInfo;
  147. }
  148. /**
  149. * 添加|修改地址
  150. * @param int $uid
  151. * @param array $addressInfo
  152. * @return array
  153. * @throws DataNotFoundException
  154. * @throws DbException
  155. * @throws Exception
  156. * @throws ModelNotFoundException
  157. */
  158. public function editAddress(int $uid, array $addressInfo)
  159. {
  160. if ($addressInfo['type'] == 1 && !$addressInfo['id']) {
  161. $city = $addressInfo['address']['city'];
  162. /** @var CityAreaServices $systemCity */
  163. $systemCity = app()->make(CityAreaServices::class);
  164. $cityInfo = $systemCity->getOne([['name', '=', $city], ['parent_id', '<>', 0]]);
  165. if ($cityInfo && $cityInfo['code']) {
  166. $addressInfo['address']['city_id'] = $cityInfo['code'];
  167. } else {
  168. $cityInfo = $systemCity->getOne([['name', 'like', "%$city%"], ['parent_id', '<>', 0]]);
  169. if (!$cityInfo) {
  170. throw new ValidateException('收货地址格式错误!修改后请重新导入!');
  171. }
  172. $addressInfo['address']['city_id'] = $cityInfo['code'];
  173. }
  174. $addressInfo['province_id'] = $systemCity->getOne(['id' => $cityInfo['pid']])['code'] ?? '';
  175. }
  176. if (!isset($addressInfo['address']['city_id']) || $addressInfo['address']['city_id'] == 0) throw new ValidateException('添加收货地址失败!');
  177. $addressInfo['province'] = $addressInfo['address']['province'] ?? '';
  178. $addressInfo['city'] = $addressInfo['address']['city'] ?? '';
  179. $addressInfo['city_id'] = $addressInfo['address']['city_id'] ?? 0;
  180. $addressInfo['district'] = $addressInfo['address']['district'] ?? '';
  181. $addressInfo['street'] = $addressInfo['address']['street'] ?? '';
  182. $addressInfo['is_default'] = (int)$addressInfo['is_default'] == true ? 1 : 0;
  183. $addressInfo['uid'] = $uid;
  184. unset($addressInfo['address'], $addressInfo['type']);
  185. //数据验证
  186. validate(AddressValidate::class)->check($addressInfo);
  187. $address_check = [];
  188. if ($addressInfo['id']) {
  189. $address_check = $this->getAddress((int)$addressInfo['id']);
  190. }
  191. if ($address_check && $address_check['is_del'] == 0 && $address_check['uid'] = $uid) {
  192. $id = (int)$addressInfo['id'];
  193. unset($addressInfo['id']);
  194. if (!$this->update($id, $addressInfo, 'id')) {
  195. throw new ValidateException('编辑收货地址失败');
  196. }
  197. if ($addressInfo['is_default']) {
  198. $this->setDefault($uid, $id);
  199. }
  200. return ['type' => 'edit', 'msg' => '编辑地址成功', 'data' => []];
  201. } else {
  202. $addressInfo['add_time'] = time();
  203. if (!$address = $this->create($addressInfo)) {
  204. throw new ValidateException('添加收货地址失败');
  205. }
  206. if ($addressInfo['is_default']) {
  207. $this->setDefault($uid, (int)$address->id);
  208. }
  209. return ['type' => 'add', 'msg' => '添加地址成功', 'data' => ['id' => $address->id]];
  210. }
  211. }
  212. /**
  213. * 删除地址
  214. * @param int $uid
  215. * @param int $id
  216. * @return bool
  217. * @throws DataNotFoundException
  218. * @throws DbException
  219. * @throws ModelNotFoundException
  220. */
  221. public function delAddress(int $uid, int $id)
  222. {
  223. $addressInfo = $this->getAddress($id);
  224. if (!$addressInfo || $addressInfo['uid'] != $uid) {
  225. throw new ValidateException('数据不存在');
  226. }
  227. if ($this->delete($id)) {
  228. return true;
  229. } else
  230. throw new ValidateException('删除地址失败!');
  231. }
  232. /**
  233. * 设置默认用户地址
  234. * @param int $id
  235. * @param int $uid
  236. * @return bool
  237. */
  238. public function setDefaultAddress(int $id, int $uid)
  239. {
  240. $res1 = $this->update($uid, ['is_default' => 0], 'uid');
  241. $res2 = $this->update(['id' => $id, 'uid' => $uid], ['is_default' => 1]);
  242. return $res1 !== false && $res2 !== false;
  243. }
  244. }