// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\api\controller\groupon; use think\response\Json; use app\api\controller\Controller; use app\api\model\groupon\Goods as GrouponGoodsModel; use app\api\model\groupon\Setting as SettingModel; use app\api\service\order\Checkout as CheckoutService; use app\common\enum\DiscountType as DiscountTypeEnum; use app\common\enum\order\OrderSource as OrderSourceEnum; /** * 拼团订单结算控制器 * Class Checkout * @package app\api\controller\groupon */ class Checkout extends Controller { /** * 拼团订单结算 * @return Json * @throws \cores\exception\BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function order(): Json { // 实例化结算台服务 $Checkout = new CheckoutService; // 订单结算api参数 $params = $Checkout->setParam($this->getParam([ 'grouponGoodsId' => 0, 'goodsSkuId' => '', 'goodsNum' => 0, 'taskId' => 0, // 拼单ID (仅参与拼单时传入) 'stepPeople' => 0, // 拼团人数 (仅阶梯团时传入) ])); // 获取拼团商品信息 $model = new GrouponGoodsModel; $goodsList = $model->getCheckoutGoodsList( (int)$params['grouponGoodsId'], (string)$params['goodsSkuId'], (int)$params['goodsNum'], (int)$params['taskId'], (int)$params['stepPeople'] ); // 设置订单来源 $Checkout->setOrderSource([ 'source' => OrderSourceEnum::GROUPON, 'sourceId' => (int)$params['taskId'], 'sourceData' => $params['stepPeople'] > 0 ? ['stepPeople' => (int)$params['stepPeople']] : [], ]); // 拼团商品是否参与 等级折扣、积分抵扣和优惠券折扣 $Checkout->setCheckoutRule([ 'isCoupon' => SettingModel::existDiscount(DiscountTypeEnum::COUPON), 'isUsePoints' => SettingModel::existDiscount(DiscountTypeEnum::POINTS), 'isUserGrade' => false, 'isDealer' => SettingModel::getIsDealer(), ]); // 获取订单结算信息 $orderInfo = $Checkout->onCheckout($goodsList); if ($this->request->isGet()) { return $this->renderSuccess([ 'order' => $orderInfo, 'personal' => $Checkout->getPersonal(), 'setting' => $Checkout->getSetting(), ]); } // 订单结算提交 if ($Checkout->hasError()) { return $this->renderError($Checkout->getError()); } // 创建订单 if (!$Checkout->createOrder($orderInfo)) { return $this->renderError($Checkout->getError() ?: '订单创建失败'); } // 返回结算信息 return $this->renderSuccess(['orderId' => $Checkout->model['order_id']], '订单创建成功'); } /** * 订单提交 * @return Json * @throws \cores\exception\BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function submit(): Json { return $this->order(); } /** * 订单结算提交的参数 * @param array $define * @return array */ private function getParam(array $define = []): array { return array_merge($define, $this->request->param()); } }