getDefaultform($this->mall_id, $this->user_id); $form->attributes = Yii::$app->request->post(); $form['action'] = AppEnum::ACTION_PREVIEW; !empty($form->shipping_type) && $form->shipping_type = ShippingTypeEnum::LOGISTICS; $upgradeGiftGoodsService = new UpgradeGiftGoodsService(['mall_id' => $form->mall_id, 'user_id' => $form->user_id]); if (!$upgradeGiftGoodsService->getIsEnable()) { return $this->error('非法请求'); } // 初始化订单数据 $orderInitDataLogic = new OrderInitDataLogic(); $form = $orderInitDataLogic->execute($form, $form->type); if ($form === false) return $this->error($orderInitDataLogic->getError()); // 购买限制验证 $orderLimitLogic = new OrderLimitLogic(); $form = $orderLimitLogic->run($form); if ($form === false) return $this->error($orderLimitLogic->getError()); // 获取收货地址 $address_info = UserAddress::getUserAddress($this->user_id, $form->address_id); $form->address_id = $address_info['id'] ?? 0; // 营销减免活动 $orderMarketingLogic = new OrderMarketingLogic(); $form = $orderMarketingLogic->run($form); if ($form === false) return $this->error($orderMarketingLogic->getError()); $group_order_goods = $form['group_order_goods']; $group_order_goods_list = []; foreach ($group_order_goods as $val) { $group_order_goods_list[] = $val; } $marketing = $form['marketing'] ?? []; $shipping_type_list = ShippingTypeEnum::getMap(); $option_list = []; $group_order_goods = $group_order_goods_list; $extends_fields = []; $info = compact('group_order_goods', 'marketing', 'address_info', 'shipping_type_list', 'option_list', 'extends_fields'); $form['total_pay_money'] = max($form['total_pay_money'], 0); $info['total_pay_money'] = '¥' . $form['total_pay_money']; $info['total_pay_money_not_coupon'] = $form['total_pay_money_not_coupon']; $info['is_address'] = $form['is_address'] ?? 0; $info['is_virtual'] = $form['is_virtual'] ?? 0; $info['is_sign_agreement'] = $form['is_sign_agreement'] ?? 0; $info['shipping_type'] = $form->data['shipping_type'] ?? null; $form->data['type'] = $form->type; return $this->success('操作成功', $info); } catch (Exception $e) { return $this->error($e->getMessage()); } } /** * 创建订单(兑换礼品,0元下单) * * @return mixed|null */ public function actionCreate() { try { $orderFormLogic = new OrderFormLogic(); $form = $orderFormLogic->getDefaultform($this->mall_id, $this->user_id); $post = Yii::$app->request->post(); $post['data']['marketing_type'] = AddonsEnum::ADDONS_NAME_UPGRADEGIFT; // TODO 赠送奖品类型 $form->attributes = $post; $form['action'] = AppEnum::ACTION_CREATE; $upgradeGiftGoodsService = new UpgradeGiftGoodsService(['mall_id' => $form->mall_id, 'user_id' => $form->user_id]); if (!$upgradeGiftGoodsService->getIsEnable()) { return $this->error('非法请求'); } // 初始化订单数据 $orderInitDataLogic = new OrderInitDataLogic(); $form = $orderInitDataLogic->execute($form, $form->type); if ($form === false) return $this->error($orderInitDataLogic->getError()); // 购买限制验证 $orderLimitLogic = new OrderLimitLogic(); $form = $orderLimitLogic->run($form); if ($form === false) return $this->error($orderLimitLogic->getError()); // 数据校验 $flag = OrderValidateLogic::run($form); if (is_string($flag)) return $this->error($flag); // 这个等级是否有奖品可以领取 $giftGoods = $upgradeGiftGoodsService->getUnclaimedGiftGoodsIdByLevel([$form->data['level']]); if (empty($giftGoods)) { return $this->error('非法请求'); } // 验证提交过来的商品是不是可领取的商品 $queryGoodsAttr = GoodsAttr::find()->select('goods_id, name'); GoodsAttr::paramPack(['where' => [['<>', 'status', StatusEnum::DELETE], ['id' => $form->data['attr_id']]]], $queryGoodsAttr); $goodsAttr = $queryGoodsAttr->one(); if (empty($goodsAttr)) { return $this->error('非法请求'); } $attrGoodsId = $goodsAttr->goods_id; if ($attrGoodsId != $giftGoods['goods_id']) { return $this->error('非法请求'); } // 加锁 $createOrderLockKey = RedisKeyEnum::suffix( RedisKeyEnum::CREATE_UPGRADE_GIFT_ORDER_LOCK, sprintf('%d-%d', $this->user_id, $form->data['level']) ); $identification = $this->user_id; if (!LockHelper::lock($createOrderLockKey, $identification, 30, 60)) throw new Exception('当前访问人数过多'); // 当前用户这个等级是否存在待领取的奖品 $unclaimedGiftId = UpgradeGiftGoods::getUnclaimedGiftIdByLevel($form->user_id, $form->data['level']); if (!$unclaimedGiftId) { return $this->error('非法请求'); } // 营销减免活动 $orderMarketingLogic = new OrderMarketingLogic(); $form = $orderMarketingLogic->run($form, true); if ($form === false) return $this->error($orderMarketingLogic->getError()); $t = Yii::$app->db->beginTransaction(); $res = Order::createOrder($form); if ($res === false) { $t->rollBack(); Yii::error(sprintf('方法:%s,创建订单失败,错误原因%s', __METHOD__, Order::getStaticError())); return $this->error(Order::getStaticError()); } // 修改领取状态 $updateRes = UpgradeGiftGoods::setData([ 'id' => $unclaimedGiftId, 'status' => 1, 'receive_time' => time(), 'order_id' => isset($res['transaction']['order_ids']) ? (int)trim($res['transaction']['order_ids'], ',') : 0, // 由于下单是可能存在多个id的,但是目前限制只有一个赠送商品,所以不存在多个订单id,可以直接拿 'goods_id' => $attrGoodsId, 'attr_id' => $form->data['attr_id'], ]); if ($updateRes === false) { $t->rollBack(); Yii::error(sprintf('方法:%s,更新奖品表数据失败,错误原因%s', __METHOD__, UpgradeGiftGoods::getStaticError())); return $this->error('下单失败!'); } UpgradeGiftStatistics::updateAllCounters( ['actual_goods_num' => 1], ['mall_id' => $this->mall_id, 'gift_type' => GiftEnum::GIFT_TYPE_USER, 'status' => StatusEnum::ENABLED, 'level' => $form->data['level']] ); UpgradeGiftGrantLog::setData([ 'mall_id' => $this->mall_id, 'gift_type' => GiftEnum::GIFT_TYPE_USER, 'user_id' => $this->user_id, 'level' => $form->data['level'], 'goods_id' => $attrGoodsId, 'attr_id' => $form->data['attr_id'], 'goods_name' => Goods::find()->select('goods_name')->where(['id' => $attrGoodsId])->scalar(), 'attr_name' => $goodsAttr->name ]); $t->commit(); return $this->success('操作成功', $res); } catch (Exception $e) { if (isset($t)) { $t->rollBack(); } Yii::error(FormatHelper::exception($e, '领取奖品,创建订单失败')); return $this->error($e->getMessage()); } finally { if (isset($createOrderLockKey, $identification)) { LockHelper::uLock($createOrderLockKey, $identification); } } } }