GroupBuyController.php 16 KB

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