GroupBuyController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 = Yii::$app->request->get('account', 0);
  125. $hd = HdClass::getByCondition(['shopId'=>$currentShopId, 'userId'=>$this->userId], false, null, 'id, customId');
  126. if (empty($hd)) {
  127. // 创建关系???
  128. util::fail('客户暂无关联此店');
  129. }
  130. $hdId = $hd['id'];
  131. $detail['hdId'] = $hdId;
  132. util::success($detail);
  133. }
  134. /**
  135. * 我要开团:创建团 + 创建待支付订单 + 挂接团长成员
  136. */
  137. public function actionCreate()
  138. {
  139. $form = new CreateGroupBuyForm();
  140. $form->loadAndValidate();
  141. $mainId = intval($this->mainId);
  142. $custom = $this->custom;
  143. if (empty($custom)) {
  144. util::fail('请先登录');
  145. }
  146. $customId = intval($custom->id);
  147. $activityGoodsId = intval($form->activityGoodsId);
  148. $goodsNum = max(1, intval($form->goodsNum));
  149. $row = GroupBuyActivityClass::getActiveGoodsRowById($mainId, $activityGoodsId);
  150. if (empty($row)) {
  151. util::fail('团购活动已结束或商品已下架');
  152. }
  153. if ($goodsNum > intval($row['limit'])) {
  154. util::fail('超出单人限购数量');
  155. }
  156. $connection = Yii::$app->db;
  157. $transaction = $connection->beginTransaction();
  158. try {
  159. $group = GroupBuyClass::createGroup($mainId, intval($this->shopId), $customId, $row);
  160. $groupBuyId = intval($group['id']);
  161. $order = $this->createGroupBuyOrder($form, $row, $groupBuyId, $goodsNum);
  162. GroupBuyMemberClass::bindOrderMember(
  163. $groupBuyId,
  164. $mainId,
  165. $customId,
  166. intval($order->id),
  167. strval($order->orderSn),
  168. GroupBuyMemberClass::ROLE_LEADER
  169. );
  170. $transaction->commit();
  171. util::success([
  172. 'groupBuyId' => $groupBuyId,
  173. 'orderSn' => $order->orderSn,
  174. 'totalPrice' => $order->actPrice,
  175. 'id' => $order->id,
  176. ]);
  177. } catch (\Exception $e) {
  178. $transaction->rollBack();
  179. Yii::error('开团失败:' . $e->getMessage(), __METHOD__);
  180. $msg = $e->getMessage();
  181. if ($msg !== '' && mb_strlen($msg) < 80 && strpos($msg, 'SQLSTATE') === false) {
  182. util::fail($msg);
  183. }
  184. util::fail('开团失败');
  185. }
  186. }
  187. /**
  188. * 我要参团:加入已有团 + 创建待支付订单
  189. */
  190. public function actionJoin()
  191. {
  192. $form = new JoinGroupBuyForm();
  193. $form->loadAndValidate();
  194. $mainId = intval($this->mainId);
  195. $custom = $this->custom;
  196. $currentShopId = Yii::$app->request->get('account', 0);
  197. if (empty($custom)) {
  198. //util::fail('请先登录');
  199. $hd = HdClass::getByCondition(['shopId'=>$currentShopId, 'userId'=>$this->userId], false, null, 'id, customId');
  200. if (empty($hd)) {
  201. // 创建关系???
  202. util::fail('客户暂无关联此店');
  203. }
  204. $customId = $hd['customId'];
  205. $custom = CustomClass::getById($customId, true);
  206. }
  207. $customId = intval($custom->id);
  208. $groupBuyId = intval($form->groupBuyId);
  209. $goodsNum = max(1, intval($form->goodsNum));
  210. $connection = Yii::$app->db;
  211. $transaction = $connection->beginTransaction();
  212. try {
  213. $group = GroupBuyClass::assertCanJoin($mainId, $groupBuyId, $customId);
  214. $row = GroupBuyActivityClass::getActiveGoodsRowById($mainId, intval($group->activityGoodsId));
  215. if (empty($row)) {
  216. // 活动结束后仍允许在截止前参团:用团上快照价
  217. $row = [
  218. 'activityId' => intval($group->activityId),
  219. 'activityGoodsId' => intval($group->activityGoodsId),
  220. 'goodsId' => intval($group->goodsId),
  221. 'price' => floatval($group->price),
  222. 'limit' => 1,
  223. 'endTime' => intval($group->deadline),
  224. 'groupSize' => intval($group->needNum),
  225. 'virtualGroup' => intval($group->virtualGroup),
  226. 'virtualMinutes' => 0,
  227. ];
  228. }
  229. if ($goodsNum > intval($row['limit'] ?? 1)) {
  230. util::fail('超出单人限购数量');
  231. }
  232. $order = $this->createGroupBuyOrder($form, $row, $groupBuyId, $goodsNum, $custom);
  233. GroupBuyMemberClass::bindOrderMember(
  234. $groupBuyId,
  235. $mainId,
  236. $customId,
  237. intval($order->id),
  238. strval($order->orderSn),
  239. GroupBuyMemberClass::ROLE_MEMBER
  240. );
  241. $transaction->commit();
  242. util::success([
  243. 'groupBuyId' => $groupBuyId,
  244. 'orderSn' => $order->orderSn,
  245. 'totalPrice' => $order->actPrice,
  246. 'id' => $order->id,
  247. ]);
  248. } catch (\Exception $e) {
  249. $transaction->rollBack();
  250. Yii::error('参团失败:' . $e->getMessage(), __METHOD__);
  251. $msg = $e->getMessage();
  252. if ($msg !== '' && mb_strlen($msg) < 80 && strpos($msg, 'SQLSTATE') === false) {
  253. util::fail($msg);
  254. }
  255. util::fail('参团失败');
  256. }
  257. }
  258. /**
  259. * 我的拼团列表
  260. */
  261. public function actionMyList()
  262. {
  263. $mainId = intval($this->mainId);
  264. $custom = $this->custom;
  265. if (empty($custom)) {
  266. util::fail('请先登录');
  267. }
  268. util::success([
  269. 'list' => GroupBuyClass::getMyList($mainId, intval($custom->id)),
  270. ]);
  271. }
  272. /**
  273. * 组装并创建拼团订单(核价以后端活动商品为准)
  274. *
  275. * @param object $form
  276. * @param array $row
  277. * @param int $groupBuyId
  278. * @param int $goodsNum
  279. * @return object
  280. */
  281. private function createGroupBuyOrder($form, $row, $groupBuyId, $goodsNum, $currenCustom = [])
  282. {
  283. $shop = $this->shop;
  284. $openShop = ShopClass::getOpenShop($shop);
  285. if ($openShop == 0) {
  286. util::fail('已休店');
  287. }
  288. $mainId = intval($this->mainId);
  289. $custom = $this->custom;
  290. if (empty($custom) && !empty($currenCustom)) {
  291. $custom = $currenCustom;
  292. }
  293. $hd = $this->hd;
  294. $user = $this->user;
  295. $saleGoodsId = intval($row['goodsId']);
  296. $goodsInfo = GoodsClass::getById($saleGoodsId, true);
  297. if (empty($goodsInfo) || intval($goodsInfo->mainId) !== $mainId) {
  298. util::fail('没有找到商品');
  299. }
  300. $stock = intval($goodsInfo->stock ?? 0);
  301. if ($stock < $goodsNum) {
  302. util::fail('库存不足');
  303. }
  304. $unitPrice = floatval($row['price'] ?? 0);
  305. if ($unitPrice <= 0) {
  306. util::fail('团购价格异常');
  307. }
  308. $goodsPrice = bcmul($unitPrice, $goodsNum, 2);
  309. $sendType = intval($form->sendType ?? dict::getDict('sendType', 'shopGet'));
  310. $sendCost = 0;
  311. // 跑腿配送暂不在拼团链路单独报价,统一引导自取或送货上门(运费0,与花店协商)
  312. if ($sendType == dict::getDict('sendType', 'thirdSend')) {
  313. util::fail('拼团暂不支持跑腿,请选择自取或送货上门');
  314. }
  315. if ($sendType == dict::getDict('sendType', 'carGet')) {
  316. if (empty($form->receiveUserName)) {
  317. util::fail('请填写收花人名字');
  318. }
  319. if (empty($form->receiveMobile) || !stringUtil::isMobile($form->receiveMobile)) {
  320. util::fail('请填写正确的收花人手机号');
  321. }
  322. }
  323. $post = [
  324. 'sendType' => $sendType,
  325. 'receiveUserName' => strval($form->receiveUserName ?? ''),
  326. 'receiveMobile' => strval($form->receiveMobile ?? ''),
  327. 'address' => strval($form->address ?? ''),
  328. 'floor' => strval($form->floor ?? ''),
  329. 'city' => strval($form->city ?? ''),
  330. 'long' => strval($form->long ?? ''),
  331. 'lat' => strval($form->lat ?? ''),
  332. 'showAddress' => strval($form->showAddress ?? ''),
  333. 'remark' => strval($form->remark ?? ''),
  334. 'reachDate' => !empty($form->reachDate) ? $form->reachDate : date('Y-m-d'),
  335. 'reachPeriod' => strval($form->reachPeriod ?? ''),
  336. 'sendCost' => $sendCost,
  337. 'sendDistance' => 0,
  338. 'sjId' => intval($this->sjId ?: ($shop->sjId ?? 0)),
  339. 'shopId' => $this->shopId,
  340. 'mainId' => $mainId,
  341. 'userId' => $this->userId,
  342. 'customId' => intval($custom->id),
  343. 'customName' => strval($custom->name ?? ''),
  344. 'customNamePy' => stringUtil::py(strval($custom->name ?? '')),
  345. 'hdId' => intval($hd->id ?? 0),
  346. 'hdName' => strval($hd->name ?? ''),
  347. 'bookName' => strval($user->name ?? ''),
  348. 'bookMobile' => !empty($form->bookMobile) && stringUtil::isMobile($form->bookMobile)
  349. ? $form->bookMobile
  350. : strval($user->mobile ?? ''),
  351. 'payWay' => 0,
  352. 'store' => 0,
  353. 'modifyPrice' => stringUtil::calcAdd($sendCost, $goodsPrice),
  354. 'deadline' => time() + 1800,
  355. 'orderSn' => orderSn::getOrderSn(),
  356. 'fromType' => dict::getDict('fromType', 'mall'),
  357. 'cash' => 0,
  358. 'needPrint' => dict::getDict('needPrint', 'need'),
  359. 'groupBuyId' => intval($groupBuyId),
  360. 'product' => [[
  361. 'productId' => $saleGoodsId,
  362. 'unitType' => 0,
  363. 'property' => 0,
  364. 'unitPrice' => $unitPrice,
  365. 'num' => $goodsNum,
  366. ]],
  367. ];
  368. if ($sendType != 1 && empty($post['reachPeriod'])) {
  369. util::fail('请选择配送时间');
  370. }
  371. return OrderService::createHdOrder($post, $custom, 0);
  372. }
  373. }