verify_sign(); // 检查登录 $uid = $this->checkLogin(); // 获取参数 $activeId = request('active_id',0); $questionId = request('question_id',0); $answerId = request('answer_id',0); // 获取客户城市的数据 $data = $Active->getOne($activeId); // 如果存在的话 if( !$data ) return json_send(['code'=>'error','msg'=>'活动已下线或不存在','data'=>'']); // 查询条件 $map = []; // 判断周期 if( !empty($data['freq']) ) { if( $data['freq'] == 1 ) $map = [['insert_time','>=',now()->startOfDay()->getTimestamp()],['insert_time','<=',now()->endOfDay()->getTimestamp()]]; if( $data['freq'] == 2 ) $map = [['insert_time','>=',now()->startOfWeek()->getTimestamp()],['insert_time','<=',now()->endOfWeek()->getTimestamp()]]; if( $data['freq'] == 3 ) $map = [['insert_time','>=',now()->startOfMonth()->getTimestamp()],['insert_time','<=',now()->endOfMonth()->getTimestamp()]]; } // 获取已参与次数 $joinTotal = $ActiveRecord->query()->where([['active_id','=',$activeId],['custom_uid','=',$uid]])->where($map)->count(); // 获取已分享次数 $shareTotal = $ActiveShare->query()->where([['active_id','=',$activeId],['custom_uid','=',$uid]])->where($map)->count(); // 计算总答题次数 $data['join_total'] = $data['join_total'] + ( $shareTotal >= $data['join_share'] ? $data['join_share'] : $shareTotal); // 计算剩余参与次数 $data['join_last'] = $data['join_total'] - $joinTotal; // 如果次数不够 if( $data['join_last'] <= 0 ) return json_send(['code'=>'error','msg'=>'您已经没有参与次数了','data'=>'']); // 获取问题列表 $answer = $Model->getOne($questionId,$answerId); // 组合数据,写入订单表,子表 DB::beginTransaction(); try { // 记录答题记录 $result = $ActiveRecord->add(['active_id'=>$activeId,'custom_uid'=>$uid,'question_id'=>$questionId,'answer_id'=>$answerId,'is_answer'=>$answer['is_answer']]); // 如果记录失败 if( !$result ) { // 回滚事务 DB::rollBack(); // 返回结果 return json_send(['code'=>'error','msg'=>'提交失败','data'=>['error'=>'答题记录失败']]); } // 判断答案是否正确 if( $answer['is_answer'] ) { /** * @todo * 对应的抽奖活动 为用户增加一次抽奖机会 */ $usable = $RiddleUsable->query()->where([['custom_uid','=',$uid],['lottery_id','=',$data['lottery_id']]])->first(); if (!$usable) { $result = $RiddleUsable->query()->insert(['custom_uid'=>$uid,'lottery_id'=>$data['lottery_id'],'number'=>1,'insert_time'=>time(),'update_time'=>time()]); }else{ $result = $RiddleUsable->query()->where([['custom_uid','=',$uid],['lottery_id','=',$data['lottery_id']]])->increment('number',1); } if( !$result ) { DB::rollBack(); // 返回结果 return json_send(['code'=>'error','msg'=>'提交失败','data'=>['error'=>'抽奖机会增加失败']]); } } // 提交事务 DB::commit(); // 返回结果 return json_send(['code'=>'success','msg'=>'提交成功','data'=>['is_answer'=>$answer['is_answer']]]); } catch (\Throwable $th) { // 返回结果 return json_send(['code'=>'success','msg'=>'提交失败','data'=>['error'=>$th->getMessage()]]); } } }