GroupBuyController.php 16 KB

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