GroupBuyController.php 15 KB

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