request->isGet){ throw new \ErrorException('NOT IS GET!'); } $page = \Yii::$app->request->get('page',1); return $this->success('ok', ScratchApiLogic::getList( $this->mall_id, $this->user, $page )); }catch (\ErrorException $errorException){ $this->error($errorException->getMessage()); }catch (\Exception $exception){ $this->error($exception->getMessage()); } } /** * 刮刮卡首页接口 * @return \yii\web\Response */ public function actionGetData() { try { if(!\Yii::$app->request->isGet){ throw new \ErrorException('NOT IS GET!'); } return $this->success('ok', ScratchApiLogic::getData( $this->mall_id, $this->user )); }catch (\ErrorException $errorException){ $this->error($errorException->getMessage()); }catch (\Exception $exception){ $this->error($exception->getMessage()); } } /** * 抽奖 * @return \yii\web\Response */ public function actionLottery(){ try { if(!\Yii::$app->request->isGet){ throw new \ErrorException('NOT IS GET!'); } return $this->success('ok', ScratchApiLogic::scratchCard( $this->mall_id, $this->user )); }catch (\ErrorException $errorException){ $this->error($errorException->getMessage()); }catch (\Exception $exception){ $this->error($exception->getMessage()); } } /** * 领取奖品 * @return mixed|void */ public function actionReceive() { try { if(!\Yii::$app->request->isGet){ throw new \ErrorException('NOT IS GET!'); } $id = \Yii::$app->request->get('id', 0); return $this->success('ok', ScratchApiLogic::toReceive( $this->user, $id )); }catch (\ErrorException $errorException){ $this->error($errorException->getMessage()); }catch (\Exception $exception){ $this->error($exception->getMessage()); } } /** * 抽奖规则 * @return \yii\web\Response */ public function actionSetting() { try { if(!\Yii::$app->request->isGet){ throw new \ErrorException('NOT IS GET!'); } $setting = ScratchSettingModel::find() ->where(['mall_id' => $this->mall_id]) ->asArray() ->select(["rule","start_at","end_at","payment_type","send_type"]) ->one(); if (!empty($setting)) { if (!empty($setting['payment_type'])) { $setting['payment_type'] = json_decode($setting['payment_type'], true); } else { $setting['payment_type'] = ['online_pay']; } $setting['send_type'] = CommonScratchLogic::sendType($setting['send_type']); $setting["start_at"] = date("Y-m-d H:i:s",$setting["start_at"]); $setting["end_at"] = date("Y-m-d H:i:s",$setting["end_at"]); } $data = [ 'setting' => $setting ]; return $this->success('ok', $data); }catch (\ErrorException $errorException){ $this->error($errorException->getMessage()); }catch (\Exception $exception){ $this->error($exception->getMessage()); } } /** * 中奖记录 * @return \yii\web\Response */ public function actionPrize() { try { if(!\Yii::$app->request->isGet){ throw new \ErrorException('NOT IS GET!'); } $page = \Yii::$app->request->get('page',1); return $this->success('ok', ScratchApiLogic::getLogList( $this->mall_id, $this->user, $page )); }catch (\ErrorException $errorException){ $this->error($errorException->getMessage()); }catch (\Exception $exception){ $this->error($exception->getMessage()); } } /** * 兑换奖品预览 * @return \yii\web\Response */ public function actionOrderPreview() { try { if(!\Yii::$app->request->isPost){ throw new \ErrorException('NOT IS POST!'); } $scratch_log_id = \Yii::$app->request->post('scratch_log_id',0); $scratchLog = ScratchLogModel::findOne([ 'id' => $scratch_log_id, 'user_id' => $this->user_id, ]); if(empty($scratchLog)){ throw new \ErrorException('中奖信息不存在'); } if($scratchLog->status != ScratchLogModel::STATUS_NO_RECEIVED){ $statusData = ScratchLogModel::LOG_STATUS_ARR; $t = $statusData[$scratchLog->status] ?? '未知'; throw new \ErrorException('中奖信息状态是:' . $t . ',不能操作'); } $attributes = [ 'type' => PreviewTypeEnum::BUY_NOW, 'data' => [ 'address_id' => 0, 'num' => 1, 'attr_id' => $scratchLog->attr_id ], ]; $orderFormLogic = new OrderFormLogic(); $form = $orderFormLogic->getDefaultform($this->mall_id, $this->user_id); $form->attributes = $attributes; $form['action'] = AppEnum::ACTION_PREVIEW; !empty($form->shipping_type) && $form->shipping_type = ShippingTypeEnum::LOGISTICS; // 初始化订单数据 $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']; $marketing = $form['marketing']; $pay_type_list = \Yii::$app->services->pay->getEnablePayment($this->mall_id, $this->platform); $shipping_type_list = ShippingTypeEnum::getMap(); $info = compact('group_order_goods', 'marketing', 'address_info', 'pay_type_list', 'shipping_type_list'); //运费 $shipping_fee_money = $form['marketing']['deduct']['shipping_fee']['money'] ?? 0; $form['total_pay_money'] = $shipping_fee_money; $info['total_pay_money'] = '¥' . $form['total_pay_money']; $info['is_address'] = $form['is_address'] ?? 0; return $this->success('操作成功', $info); }catch (\ErrorException $errorException){ $this->error($errorException->getMessage()); }catch (\Exception $exception){ $this->error($exception->getMessage()); } } /** * 兑换奖品下单 * @return mixed|void */ public function actionOrderSubmit() { try { if(!\Yii::$app->request->isPost){ throw new \ErrorException('NOT IS POST!'); } $params = \Yii::$app->request->post(); $res = \Yii::$app->services->validate->validate($params, [ [['scratch_log_id'], 'required', 'message' => 'scratch_log_id,必传'], [['address_id'], 'required', 'message' => 'address_id,必传'], [['remark'], 'safe'], ]); if ($res === false){ throw new \ErrorException(\Yii::$app->services->validate->getErrorMessage()); } $scratchLog = ScratchLogModel::findOne([ 'id' => $params['scratch_log_id'], 'user_id' => $this->user_id, ]); if(empty($scratchLog)){ throw new \ErrorException('中奖信息不存在'); } if($scratchLog->status != ScratchLogModel::STATUS_NO_RECEIVED){ $statusData = ScratchLogModel::LOG_STATUS_ARR; $t = $statusData[$scratchLog->status] ?? '未知'; throw new \ErrorException('中奖信息状态是:' . $t . ',不能操作'); } $attributes = [ 'type' => PreviewTypeEnum::BUY_NOW, 'data' => [ 'address_id' => $params['address_id'], 'num' => 1, 'remark' => $params['remark'], 'attr_id' => $scratchLog->attr_id ], ]; $orderFormLogic = new OrderFormLogic(); $form = $orderFormLogic->getDefaultform($this->mall_id, $this->user_id); $form->attributes = $attributes; $form['action'] = AppEnum::ACTION_CREATE; // 初始化订单数据 $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()); // 营销减免活动 $orderMarketingLogic = new OrderMarketingLogic(); $form = $orderMarketingLogic->run($form, true); if ($form === false) return $this->error($orderMarketingLogic->getError()); $res = ScratchOrderLogic::createOrder($form, $scratchLog); if ($res === false) return $this->error(Order::getStaticError()); return $this->success('操作成功', $res); }catch (\ErrorException $errorException){ $this->error($errorException->getMessage()); }catch (\Exception $exception){ $this->error($exception->getMessage()); } } }