GroupBuyController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <?php
  2. namespace mall\controllers;
  3. use bizHd\custom\classes\CustomClass;
  4. use bizHd\goods\classes\GoodsClass;
  5. use bizHd\groupBuy\classes\GroupBuyActivityClass;
  6. use bizHd\groupBuy\classes\GroupBuyClass;
  7. use bizHd\groupBuy\classes\GroupBuyMemberClass;
  8. use bizHd\order\services\OrderService;
  9. use bizHd\shop\classes\ShopClass;
  10. use bizMall\hd\classes\HdClass;
  11. use common\components\business;
  12. use common\components\dict;
  13. use common\components\orderSn;
  14. use common\components\stringUtil;
  15. use common\components\util;
  16. use mall\models\groupBuy\CreateGroupBuyForm;
  17. use mall\models\groupBuy\JoinGroupBuyForm;
  18. use Yii;
  19. /**
  20. * 商城端拼团接口
  21. * 落地页 / 团详情 / 开团 / 参团 / 我的拼团
  22. */
  23. class GroupBuyController extends BaseController
  24. {
  25. public $guestAccess = [
  26. 'goods-landing',
  27. 'detail',
  28. ];
  29. /**
  30. * 团购商品落地页:活动商品信息 + 当前最快成团开放团
  31. * GET goodsId
  32. */
  33. public function actionGoodsLanding()
  34. {
  35. $mainId = intval($this->mainId);
  36. if ($mainId <= 0) {
  37. util::fail('无效门店');
  38. }
  39. $goodsId = intval(Yii::$app->request->get('goodsId', 0));
  40. if ($goodsId <= 0) {
  41. util::fail('请选择商品');
  42. }
  43. $row = GroupBuyActivityClass::getActiveGoodsRowByGoodsId($mainId, $goodsId);
  44. if (empty($row)) {
  45. util::fail('团购活动已结束或商品已下架');
  46. }
  47. $goods = GoodsClass::getById($goodsId, true);
  48. $cover = strval($row['cover'] ?? '');
  49. $coverUrl = $cover;
  50. if ($cover !== '' && strpos($cover, 'http') !== 0) {
  51. $formatted = business::formatUploadImg($cover);
  52. $coverUrl = $formatted['url'] ?? $cover;
  53. }
  54. $detailImages = [];
  55. if (!empty($goods)) {
  56. $pics = $goods->detail ?? ($goods->pic ?? '');
  57. if (is_string($pics) && $pics !== '') {
  58. $decoded = json_decode($pics, true);
  59. if (is_array($decoded)) {
  60. foreach ($decoded as $p) {
  61. if (is_string($p) && $p !== '') {
  62. if (strpos($p, 'http') === 0) {
  63. $detailImages[] = $p;
  64. } else {
  65. $fmt = business::formatUploadImg($p);
  66. $detailImages[] = $fmt['url'] ?? $p;
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73. $openGroup = GroupBuyClass::getHottestOpenGroup(
  74. $mainId,
  75. $goodsId,
  76. intval($row['activityGoodsId'] ?? 0)
  77. );
  78. util::success([
  79. 'activityGoodsId' => intval($row['activityGoodsId']),
  80. 'activityId' => intval($row['activityId']),
  81. 'goodsId' => $goodsId,
  82. 'name' => strval($row['name']),
  83. 'cover' => $cover,
  84. 'coverUrl' => $coverUrl,
  85. 'price' => floatval($row['price']),
  86. 'originPrice' => floatval($row['originPrice']),
  87. 'groupSize' => intval($row['groupSize']),
  88. 'stock' => intval($row['stock']),
  89. 'limit' => intval($row['limit']),
  90. 'virtualGroup' => intval($row['virtualGroup']),
  91. 'virtualMinutes' => intval($row['virtualMinutes']),
  92. 'autoRefund' => 1,
  93. 'startTime' => intval($row['startTime']),
  94. 'endTime' => intval($row['endTime']),
  95. 'title' => strval($row['title']),
  96. 'subtitle' => strval($row['subtitle']),
  97. 'desc' => strval($row['desc']),
  98. 'showCountdown' => intval($row['showCountdown']),
  99. 'detailImages' => $detailImages,
  100. 'openGroup' => $openGroup,
  101. ]);
  102. }
  103. /**
  104. * 拼团详情
  105. * GET id
  106. */
  107. public function actionDetail()
  108. {
  109. $mainId = intval($this->mainId);
  110. if ($mainId <= 0) {
  111. util::fail('无效门店');
  112. }
  113. $id = intval(Yii::$app->request->get('id', 0));
  114. if ($id <= 0) {
  115. util::fail('请选择拼团');
  116. }
  117. $detail = GroupBuyClass::getDetail($mainId, $id);
  118. // 附带活动商品落地信息,便于分享进入后完整展示
  119. $landing = GroupBuyActivityClass::getActiveGoodsRowById($mainId, intval($detail['activityGoodsId']));
  120. if (empty($landing)) {
  121. $landing = GroupBuyActivityClass::getActiveGoodsRowByGoodsId($mainId, intval($detail['goodsId']));
  122. }
  123. $detail['activityGoods'] = $landing;
  124. $currentShopId = intval(Yii::$app->request->get('account', 0));
  125. $hd = HdClass::getByCondition(['shopId' => $currentShopId, 'userId' => $this->userId], false, null, 'id, customId');
  126. if (empty($hd)) {
  127. // util::error(errCode::GROUP_BUY_HD_NOT_BOUND, '客户暂无关联此店');
  128. // 客户与当前门店无关系时,立即建立
  129. if (empty($this->user)) {
  130. util::fail('请先登录');
  131. }
  132. $shop = ShopClass::getById($currentShopId, true);
  133. if (empty($shop)) {
  134. util::fail('门店不存在');
  135. }
  136. $respond = CustomClass::buildRelation($shop, $this->user);
  137. if (empty($respond) || empty($respond['hd'])) {
  138. util::fail('关联门店失败');
  139. }
  140. $hdId = intval($respond['hd']->id);
  141. } else {
  142. $hdId = intval($hd['id']);
  143. }
  144. $detail['hdId'] = $hdId;
  145. util::success($detail);
  146. }
  147. /**
  148. * 我要开团:创建团 + 创建待支付订单 + 挂接团长成员
  149. */
  150. public function actionCreate()
  151. {
  152. $form = new CreateGroupBuyForm();
  153. $form->loadAndValidate();
  154. $mainId = intval($this->mainId);
  155. $custom = $this->custom;
  156. if (empty($custom)) {
  157. util::fail('请先登录');
  158. }
  159. $customId = intval($custom->id);
  160. $activityGoodsId = intval($form->activityGoodsId);
  161. $goodsNum = max(1, intval($form->goodsNum));
  162. $row = GroupBuyActivityClass::getActiveGoodsRowById($mainId, $activityGoodsId);
  163. if (empty($row)) {
  164. util::fail('团购活动已结束或商品已下架');
  165. }
  166. if ($goodsNum > intval($row['limit'])) {
  167. util::fail('超出单人限购数量');
  168. }
  169. $connection = Yii::$app->db;
  170. $transaction = $connection->beginTransaction();
  171. try {
  172. $group = GroupBuyClass::createGroup($mainId, intval($this->shopId), $customId, $row);
  173. $groupBuyId = intval($group['id']);
  174. $order = $this->createGroupBuyOrder($form, $row, $groupBuyId, $goodsNum);
  175. GroupBuyMemberClass::bindOrderMember(
  176. $groupBuyId,
  177. $mainId,
  178. $customId,
  179. intval($order->id),
  180. strval($order->orderSn),
  181. GroupBuyMemberClass::ROLE_LEADER
  182. );
  183. $transaction->commit();
  184. util::success([
  185. 'groupBuyId' => $groupBuyId,
  186. 'orderSn' => $order->orderSn,
  187. 'totalPrice' => $order->actPrice,
  188. 'id' => $order->id,
  189. ]);
  190. } catch (\Exception $e) {
  191. $transaction->rollBack();
  192. Yii::error('开团失败:' . $e->getMessage(), __METHOD__);
  193. $msg = $e->getMessage();
  194. if ($msg !== '' && mb_strlen($msg) < 80 && strpos($msg, 'SQLSTATE') === false) {
  195. util::fail($msg);
  196. }
  197. util::fail('开团失败');
  198. }
  199. }
  200. /**
  201. * 我要参团:加入已有团 + 创建待支付订单
  202. */
  203. public function actionJoin()
  204. {
  205. $form = new JoinGroupBuyForm();
  206. $form->loadAndValidate();
  207. $mainId = intval($this->mainId);
  208. $custom = $this->custom;
  209. $currentShopId = intval(Yii::$app->request->get('account', 0));
  210. if (empty($custom)) {
  211. $hd = HdClass::getByCondition(['shopId' => $currentShopId, 'userId' => $this->userId], false, null, 'id, customId');
  212. if (empty($hd)) {
  213. // 客户与当前门店无关系时,立即建立
  214. if (empty($this->user) || empty($this->shop)) {
  215. util::fail('请先登录');
  216. }
  217. $respond = CustomClass::buildRelation($this->shop, $this->user);
  218. if (empty($respond) || empty($respond['custom'])) {
  219. util::fail('关联门店失败');
  220. }
  221. $custom = $respond['custom'];
  222. } else {
  223. $customId = $hd['customId'];
  224. $custom = CustomClass::getById($customId, true);
  225. }
  226. }
  227. $customId = intval($custom->id);
  228. $groupBuyId = intval($form->groupBuyId);
  229. $goodsNum = max(1, intval($form->goodsNum));
  230. $connection = Yii::$app->db;
  231. $transaction = $connection->beginTransaction();
  232. try {
  233. $group = GroupBuyClass::assertCanJoin($mainId, $groupBuyId, $customId);
  234. $row = GroupBuyActivityClass::getActiveGoodsRowById($mainId, intval($group->activityGoodsId));
  235. if (empty($row)) {
  236. // 活动结束后仍允许在截止前参团:用团上快照价
  237. $row = [
  238. 'activityId' => intval($group->activityId),
  239. 'activityGoodsId' => intval($group->activityGoodsId),
  240. 'goodsId' => intval($group->goodsId),
  241. 'price' => floatval($group->price),
  242. 'limit' => 1,
  243. 'endTime' => intval($group->deadline),
  244. 'groupSize' => intval($group->needNum),
  245. 'virtualGroup' => intval($group->virtualGroup),
  246. 'virtualMinutes' => 0,
  247. ];
  248. }
  249. if ($goodsNum > intval($row['limit'] ?? 1)) {
  250. util::fail('超出单人限购数量');
  251. }
  252. $order = $this->createGroupBuyOrder($form, $row, $groupBuyId, $goodsNum, $custom);
  253. GroupBuyMemberClass::bindOrderMember(
  254. $groupBuyId,
  255. $mainId,
  256. $customId,
  257. intval($order->id),
  258. strval($order->orderSn),
  259. GroupBuyMemberClass::ROLE_MEMBER
  260. );
  261. $transaction->commit();
  262. util::success([
  263. 'groupBuyId' => $groupBuyId,
  264. 'orderSn' => $order->orderSn,
  265. 'totalPrice' => $order->actPrice,
  266. 'id' => $order->id,
  267. ]);
  268. } catch (\Exception $e) {
  269. $transaction->rollBack();
  270. Yii::error('参团失败:' . $e->getMessage(), __METHOD__);
  271. $msg = $e->getMessage();
  272. if ($msg !== '' && mb_strlen($msg) < 80 && strpos($msg, 'SQLSTATE') === false) {
  273. util::fail($msg);
  274. }
  275. util::fail('参团失败');
  276. }
  277. }
  278. /**
  279. * 我的拼团列表
  280. */
  281. public function actionMyList()
  282. {
  283. $mainId = intval($this->mainId);
  284. $custom = $this->custom;
  285. if (empty($custom)) {
  286. util::fail('请先登录');
  287. }
  288. util::success([
  289. 'list' => GroupBuyClass::getMyList($mainId, intval($custom->id)),
  290. ]);
  291. }
  292. /**
  293. * 组装并创建拼团订单(核价以后端活动商品为准)
  294. *
  295. * @param object $form
  296. * @param array $row
  297. * @param int $groupBuyId
  298. * @param int $goodsNum
  299. * @return object
  300. */
  301. private function createGroupBuyOrder($form, $row, $groupBuyId, $goodsNum, $currenCustom = [])
  302. {
  303. $shop = $this->shop;
  304. $openShop = ShopClass::getOpenShop($shop);
  305. if ($openShop == 0) {
  306. util::fail('已休店');
  307. }
  308. $mainId = intval($this->mainId);
  309. $custom = $this->custom;
  310. if (empty($custom) && !empty($currenCustom)) {
  311. $custom = $currenCustom;
  312. }
  313. $hd = $this->hd;
  314. $user = $this->user;
  315. $saleGoodsId = intval($row['goodsId']);
  316. $goodsInfo = GoodsClass::getById($saleGoodsId, true);
  317. if (empty($goodsInfo) || intval($goodsInfo->mainId) !== $mainId) {
  318. util::fail('没有找到商品');
  319. }
  320. $stock = intval($goodsInfo->stock ?? 0);
  321. if ($stock < $goodsNum) {
  322. util::fail('库存不足');
  323. }
  324. $unitPrice = floatval($row['price'] ?? 0);
  325. if ($unitPrice <= 0) {
  326. util::fail('团购价格异常');
  327. }
  328. $goodsPrice = bcmul($unitPrice, $goodsNum, 2);
  329. $sendType = intval($form->sendType ?? dict::getDict('sendType', 'shopGet'));
  330. $sendCost = 0;
  331. // 跑腿配送暂不在拼团链路单独报价,统一引导自取或送货上门(运费0,与花店协商)
  332. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  333. util::fail('拼团暂不支持跑腿,请选择自取或送货上门');
  334. }
  335. if ($sendType == dict::getDict('sendType', 'carGet')) {
  336. if (empty($form->receiveUserName)) {
  337. util::fail('请填写收花人名字');
  338. }
  339. if (empty($form->receiveMobile) || !stringUtil::isMobile($form->receiveMobile)) {
  340. util::fail('请填写正确的收花人手机号');
  341. }
  342. }
  343. $post = [
  344. 'sendType' => $sendType,
  345. 'receiveUserName' => strval($form->receiveUserName ?? ''),
  346. 'receiveMobile' => strval($form->receiveMobile ?? ''),
  347. 'address' => strval($form->address ?? ''),
  348. 'floor' => strval($form->floor ?? ''),
  349. 'city' => strval($form->city ?? ''),
  350. 'long' => strval($form->long ?? ''),
  351. 'lat' => strval($form->lat ?? ''),
  352. 'showAddress' => strval($form->showAddress ?? ''),
  353. 'remark' => strval($form->remark ?? ''),
  354. 'reachDate' => !empty($form->reachDate) ? $form->reachDate : date('Y-m-d'),
  355. 'reachPeriod' => strval($form->reachPeriod ?? ''),
  356. 'sendCost' => $sendCost,
  357. 'sendDistance' => 0,
  358. 'sjId' => intval($this->sjId ?: ($shop->sjId ?? 0)),
  359. 'shopId' => $this->shopId,
  360. 'mainId' => $mainId,
  361. 'userId' => $this->userId,
  362. 'customId' => intval($custom->id),
  363. 'customName' => strval($custom->name ?? ''),
  364. 'customNamePy' => stringUtil::py(strval($custom->name ?? '')),
  365. 'hdId' => intval($hd->id ?? 0),
  366. 'hdName' => strval($hd->name ?? ''),
  367. 'bookName' => strval($user->name ?? ''),
  368. 'bookMobile' => !empty($form->bookMobile) && stringUtil::isMobile($form->bookMobile)
  369. ? $form->bookMobile
  370. : strval($user->mobile ?? ''),
  371. 'payWay' => 0,
  372. 'store' => 0,
  373. 'modifyPrice' => stringUtil::calcAdd($sendCost, $goodsPrice),
  374. 'deadline' => time() + 1800,
  375. 'orderSn' => orderSn::getOrderSn(),
  376. 'fromType' => dict::getDict('fromType', 'mall'),
  377. 'cash' => 0,
  378. 'needPrint' => dict::getDict('needPrint', 'need'),
  379. 'groupBuyId' => intval($groupBuyId),
  380. 'product' => [[
  381. 'productId' => $saleGoodsId,
  382. 'unitType' => 0,
  383. 'property' => 0,
  384. 'unitPrice' => $unitPrice,
  385. 'num' => $goodsNum,
  386. ]],
  387. ];
  388. if ($sendType != 1 && empty($post['reachPeriod'])) {
  389. util::fail('请选择配送时间');
  390. }
  391. return OrderService::createHdOrder($post, $custom, 0);
  392. }
  393. }