GroupBuyController.php 14 KB

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