UserCollageController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. namespace app\controller\api\v2\activity;
  12. use app\Request;
  13. use app\services\activity\collage\UserCollagePartakeServices;
  14. use app\services\activity\collage\UserCollageServices;
  15. use app\services\store\SystemStoreServices;
  16. use app\services\user\UserAddressServices;
  17. /**
  18. *
  19. * Class UserCollageController
  20. * @package app\controller\api\v2\activity
  21. */
  22. class UserCollageController
  23. {
  24. protected $services;
  25. public function __construct(UserCollageServices $services)
  26. {
  27. $this->services = $services;
  28. }
  29. /**
  30. * 验证是否在配送范围
  31. * @param Request $request
  32. * @return \think\Response
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public function isWithinScopeDistribution(Request $request)
  38. {
  39. [$store_id, $address_id] = $request->getMore([
  40. ['store_id', 0],
  41. ['address_id', 0],
  42. ], true);
  43. $uid = (int)$request->uid();
  44. if (!$store_id || !$address_id) return app('json')->fail('参数有误!');
  45. /** @var UserAddressServices $addressServices */
  46. $addressServices = app()->make(UserAddressServices::class);
  47. if (!$addressInfo = $addressServices->getOne(['uid' => $uid, 'id' => $address_id, 'is_del' => 0]))
  48. return app('json')->fail('地址选择有误!');
  49. $addressInfo = $addressInfo->toArray();
  50. /** @var SystemStoreServices $storeService */
  51. $storeService = app()->make(SystemStoreServices::class);
  52. $res = $storeService->checkStoreDeliveryScope((int)$store_id, $addressInfo);
  53. if ($res) {
  54. return app('json')->successful('ok');
  55. } else {
  56. return app('json')->fail('不在配送范围');
  57. }
  58. }
  59. /**
  60. * 发起拼单
  61. * @param Request $request
  62. * @return \think\Response
  63. */
  64. public function userInitiateCollage(Request $request)
  65. {
  66. [$store_id, $address_id, $shipping_type] = $request->getMore([
  67. ['store_id', 0],
  68. ['address_id', 0],
  69. ['shipping_type', 1]
  70. ], true);
  71. $uid = (int)$request->uid();
  72. if ($shipping_type == 1 && !$address_id) {
  73. return app('json')->fail('请选择收货地址!');
  74. } else if ($shipping_type == 2 && !$store_id) {
  75. return app('json')->fail('请选择门店!');
  76. } else if ($shipping_type == 3 && !$address_id) {
  77. return app('json')->fail('请选择收货地址!');
  78. }
  79. if (!$this->services->checkCollageStatus()) return app('json')->fail('门店或拼单未开启!');
  80. try {
  81. $res = $this->services->setUserCollage($uid, (int)$store_id, (int)$address_id, (int)$shipping_type);
  82. return app('json')->successful('ok', ['collageId' => $res->id]);
  83. } catch (\Throwable $e) {
  84. $msg = $e->getMessage();
  85. \think\facade\Log::error('发起拼单失败,原因:' . $msg . $e->getFile() . $e->getLine());
  86. return app('json')->fail('发起拼单失败');
  87. }
  88. }
  89. /**
  90. * 检查用户是否发起拼单
  91. * @param Request $request
  92. * @return \think\Response
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\DbException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. */
  97. public function isUserInitiateCollage(Request $request)
  98. {
  99. $uid = (int)$request->uid();
  100. $collageId = 0;
  101. $where = ['uid' => $uid, 'type' => 9, 'status' => ['0', '1']];
  102. $collage = $this->services->getUserCollage($where);
  103. if ($collage) {
  104. $collageId = $collage->id;
  105. }
  106. return app('json')->successful(['collageId' => $collageId]);
  107. }
  108. /**
  109. * 检查拼单
  110. * @param Request $request
  111. * @return \think\Response
  112. * @throws \think\db\exception\DataNotFoundException
  113. * @throws \think\db\exception\DbException
  114. * @throws \think\db\exception\ModelNotFoundException
  115. */
  116. public function isInitiateCollage(Request $request)
  117. {
  118. [$collage_id] = $request->getMore([
  119. ['collage_id', 0]
  120. ], true);
  121. if (!$collage_id) return app('json')->fail('参数有误!');
  122. $where = ['id' => $collage_id, 'type' => 9];
  123. $collage = $this->services->getUserCollage($where);
  124. if (!$collage) return app('json')->fail('拼单不存在');
  125. return app('json')->successful(['collage' => $collage]);
  126. }
  127. /**
  128. * 取消拼单
  129. * @param Request $request
  130. * @return \think\Response
  131. */
  132. public function cancelInitiateCollage(Request $request)
  133. {
  134. [$collage_id] = $request->getMore([
  135. ['collage_id', 0]
  136. ], true);
  137. $where = ['status' => -1];
  138. if (!$collage_id) return app('json')->fail('参数有误!');
  139. $res = $this->services->userUpdate((int)$collage_id, $where);
  140. if ($res) {
  141. return app('json')->successful('ok');
  142. } else {
  143. return app('json')->fail('取消失败');
  144. }
  145. }
  146. /**
  147. * 购物车 统计 数量
  148. * @param Request $request
  149. * @return mixed
  150. */
  151. public function count(Request $request)
  152. {
  153. [$numType, $collage_id, $store_id] = $request->getMore([
  154. ['numType', false],//购物车编号
  155. ['collage_id', 0],
  156. ['store_id', 0]
  157. ], true);
  158. $uid = (int)$request->uid();
  159. if (!$collage_id || !$store_id) return app('json')->fail('参数有误!');
  160. /** @var UserCollagePartakeServices $partakeService */
  161. $partakeService = app()->make(UserCollagePartakeServices::class);
  162. $where = ['uid' => $uid, 'collate_code_id' => $collage_id, 'store_id' => $store_id, 'status'=> 1];
  163. return app('json')->success('ok', $partakeService->getUserPartakeCount($where, (string)$numType, (int)$collage_id, (int)$store_id));
  164. }
  165. /**
  166. * 获取用户购物车
  167. * @param Request $request
  168. * @return mixed
  169. */
  170. public function getCartList(Request $request)
  171. {
  172. $uid = (int)$request->uid();
  173. [$collage_id, $store_id] = $request->getMore([
  174. ['collage_id', 0],
  175. ['store_id', 0]
  176. ], true);
  177. if (!$collage_id || !$store_id) return app('json')->fail('参数有误!');
  178. /** @var UserCollagePartakeServices $partakeService */
  179. $partakeService = app()->make(UserCollagePartakeServices::class);
  180. $data = $partakeService->getPartakeList(['uid' => $uid, 'collate_code_id' => $collage_id, 'store_id' => $store_id, 'status'=> 1], 0, 0, ['productInfo', 'attrInfo']);
  181. [$data, $valid, $invalid] = $partakeService->handleCartList($uid, $data, -1, $store_id);
  182. return app('json')->successful($valid);
  183. }
  184. /**
  185. * 获取自提门店信息
  186. * @param Request $request
  187. * @return \think\Response
  188. * @throws \think\db\exception\DataNotFoundException
  189. * @throws \think\db\exception\DbException
  190. * @throws \think\db\exception\ModelNotFoundException
  191. */
  192. public function getStoredata(Request $request)
  193. {
  194. [$store_id, $latitude, $longitude] = $request->getMore([
  195. ['store_id', 0],
  196. ['latitude', ''],
  197. ['longitude', '']
  198. ], true);
  199. if (!$store_id || !$latitude || !$longitude) return app('json')->fail('参数有误!');
  200. /** @var SystemStoreServices $storeService */
  201. $storeService = app()->make(SystemStoreServices::class);
  202. $storeInfo = $storeService->getStoreInfo((int)$store_id);
  203. $distance = $storeService->distance($latitude, $longitude, $storeInfo['latitude'], $storeInfo['longitude']);
  204. if ($distance) {
  205. $storeInfo['range'] = bcdiv($distance, '1000', 1);
  206. } else {
  207. $storeInfo['range'] = 0;
  208. }
  209. return app('json')->successful($storeInfo);
  210. }
  211. /**
  212. * 用户添加拼单商品
  213. * @param Request $request
  214. * @return \think\Response
  215. * @throws \think\db\exception\DataNotFoundException
  216. * @throws \think\db\exception\DbException
  217. * @throws \think\db\exception\ModelNotFoundException
  218. */
  219. public function addCollagePartake(Request $request)
  220. {
  221. $where = $request->postMore([
  222. ['productId', 0],//普通商品编号
  223. [['cartNum', 'd'], 1], //购物车数量
  224. ['uniqueId', ''],//属性唯一值
  225. ['collageId', 0],//拼单ID
  226. ['storeId', 0],//门店ID
  227. ['isAdd', 1],//购物车数量加减 1 加 0 减
  228. ]);
  229. if (!$where['productId'] || !$where['storeId'] || !$where['collageId']) {
  230. return app('json')->fail('参数错误');
  231. }
  232. $uid = (int)$request->uid();
  233. $wheredata = ['id' => $where['collageId'], 'type' => 9];
  234. $collage = $this->services->getUserCollage($wheredata);
  235. if ($collage['store_id'] != $where['storeId']) return app('json')->fail('选择门店有误!');
  236. if ($collage['status'] == 1) return app('json')->fail('订单提交中,不能在添加商品!');
  237. if ($collage['status'] >= 2) return app('json')->fail('拼单完成,不能在添加商品!');
  238. /** @var UserCollagePartakeServices $partakeService */
  239. $partakeService = app()->make(UserCollagePartakeServices::class);
  240. $res = $partakeService->addUserPartakeProduct($uid, (int)$where['productId'], $where['cartNum'], $where['uniqueId'], $where['collageId'], $where['storeId'], 9, $where['isAdd']);
  241. if ($res) {
  242. return app('json')->successful('ok');
  243. } else {
  244. return app('json')->fail('添加失败');
  245. }
  246. }
  247. /**
  248. * 获取拼单数据
  249. * @param Request $request
  250. * @return \think\Response
  251. * @throws \think\db\exception\DataNotFoundException
  252. * @throws \think\db\exception\DbException
  253. * @throws \think\db\exception\ModelNotFoundException
  254. */
  255. public function getUserCollagePartake(Request $request)
  256. {
  257. [$collage_id] = $request->getMore([
  258. ['collage_id', 0]
  259. ], true);
  260. if (!$collage_id) return app('json')->fail('参数有误!');
  261. $uid = (int)$request->uid();
  262. /** @var UserCollagePartakeServices $partakeService */
  263. $partakeService = app()->make(UserCollagePartakeServices::class);
  264. $cartList = $partakeService->getUserPartakeProduct($collage_id, $uid);
  265. return app('json')->successful($cartList);
  266. }
  267. /**
  268. * 用户清空拼单数据
  269. * @param Request $request
  270. * @return \think\Response
  271. */
  272. public function emptyCollagePartake(Request $request)
  273. {
  274. [$collage_id] = $request->getMore([
  275. ['collage_id', 0]
  276. ], true);
  277. if (!$collage_id) return app('json')->fail('参数有误!');
  278. $uid = (int)$request->uid();
  279. /** @var UserCollagePartakeServices $partakeService */
  280. $partakeService = app()->make(UserCollagePartakeServices::class);
  281. $res = $partakeService->emptyUserCollagePartake((int)$collage_id, $uid);
  282. if ($res) {
  283. return app('json')->successful('ok');
  284. } else {
  285. return app('json')->fail('清空失败');
  286. }
  287. }
  288. /**
  289. * 复制他人拼单商品
  290. * @param Request $request
  291. * @return \think\Response
  292. * @throws \think\db\exception\DataNotFoundException
  293. * @throws \think\db\exception\DbException
  294. * @throws \think\db\exception\ModelNotFoundException
  295. */
  296. public function duplicateCollagePartake(Request $request)
  297. {
  298. [$collage_id, $c_uid] = $request->getMore([
  299. ['collage_id', 0],
  300. ['c_uid', 0],
  301. ], true);
  302. if (!$collage_id || !$c_uid) return app('json')->fail('参数有误!');
  303. $uid = (int)$request->uid();
  304. if ($c_uid == $uid) return app('json')->fail('您不能复制自己的商品!');
  305. $where = ['id' => $collage_id, 'type' => 9];
  306. $collage = $this->services->getUserCollage($where);
  307. if ($collage['status'] >= 2) return app('json')->fail('拼单完成,不能在复制商品!');
  308. /** @var UserCollagePartakeServices $partakeService */
  309. $partakeService = app()->make(UserCollagePartakeServices::class);
  310. $res = $partakeService->duplicateUserCollagePartake((int)$collage_id, (int)$c_uid, $uid);
  311. if ($res) {
  312. return app('json')->successful('ok');
  313. } else {
  314. return app('json')->fail('复制失败');
  315. }
  316. }
  317. /**
  318. * 结算拼单
  319. * @param Request $request
  320. * @return \think\Response
  321. * @throws \think\db\exception\DataNotFoundException
  322. * @throws \think\db\exception\DbException
  323. * @throws \think\db\exception\ModelNotFoundException
  324. */
  325. public function userSettleAccountsCollage(Request $request)
  326. {
  327. [$collage_id] = $request->getMore([
  328. ['collage_id', 0],
  329. ], true);
  330. if (!$collage_id) return app('json')->fail('参数有误!');
  331. $uid = (int)$request->uid();
  332. /** @var UserCollagePartakeServices $partakeService */
  333. $partakeService = app()->make(UserCollagePartakeServices::class);
  334. $cartIds = $partakeService->allUserSettleAccountsCollage((int)$collage_id, $uid, 9);
  335. if ($cartIds) {
  336. return app('json')->successful('ok', ['cartIds' => $cartIds]);
  337. } else {
  338. return app('json')->fail('结算失败');
  339. }
  340. }
  341. /**用户删除拼单商品
  342. * @param Request $request
  343. * @return \think\Response
  344. */
  345. public function delUserCollagePartake(Request $request)
  346. {
  347. $where = $request->postMore([
  348. ['collage_id', 0],
  349. ['storeId', 0],
  350. ['productId', 0],//普通商品编号
  351. ['uniqueId', ''],//属性唯一值
  352. ]);
  353. if (!$where['collage_id'] || !$where['storeId'] || !$where['productId'] || !$where['uniqueId']) return app('json')->fail('参数有误!');
  354. /** @var UserCollagePartakeServices $partakeService */
  355. $partakeService = app()->make(UserCollagePartakeServices::class);
  356. $res = $partakeService->delUserCatePartake((int)$where['collage_id'], (int)$where['storeId'], (int)$where['productId'], $where['uniqueId']);
  357. if ($res) {
  358. return app('json')->successful('删除成功');
  359. } else {
  360. return app('json')->fail('删除失败');
  361. }
  362. }
  363. }