UserAddressServices.php 9.3 KB

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