| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- <?php
- namespace addons\Scratch\api\modules\v1\controllers;
- use addons\Scratch\common\logic\CommonScratchLogic;
- use addons\Scratch\common\logic\ScratchApiLogic;
- use addons\Scratch\common\logic\ScratchOrderLogic;
- use addons\Scratch\common\logic\ScratchPosterLogic;
- use addons\Scratch\common\models\ScratchLogModel;
- use addons\Scratch\common\models\ScratchSettingModel;
- use common\enums\AppEnum;
- use common\enums\PreviewTypeEnum;
- use common\enums\ShippingTypeEnum;
- use common\logic\order\OrderFormLogic;
- use common\logic\order\OrderInitDataLogic;
- use common\logic\order\OrderLimitLogic;
- use common\logic\order\OrderMarketingLogic;
- use common\models\order\Order;
- use common\models\user\UserAddress;
- use Yii;
- use api\controllers\OnAuthController;
- use yii\base\BaseObject;
- /**
- * Class ScratchController
- * @package addons\Scratch\api\modules\v1\controllers
- */
- class ScratchController extends OnAuthController
- {
- public $modelClass = '';
- public $from_type = ['Scratch'];
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = ['get-data'];
- /**
- * 刮刮卡首页接口
- * @return \yii\web\Response
- */
- public function actionIndex()
- {
- try {
- if(!\Yii::$app->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());
- }
- }
- }
|