一、操作流程图
二、代码实现
<?php
/**
* 数字人答题业务流
* User: 龙哥·三年风水
* Date: 2024/9/11
* Time: 14:59
*/
namespace app\controller\shuziren;
use app\controller\Base;
use app\model\param\QuestionParam as PQPModel;
use app\model\answer\QuestionBank;
use app\model\answer\QuestionOpt;
use app\model\answer\StatisticsAnswer;
use app\service\WechatPayNative;
use Redis\Redis;
use app\model\answer\QuestionPrize;
use app\model\answer\QuestionOrder;
use Rabbit\order\Delayed\Pro;
class Answer extends Base
{
// 获取答题
public function index(){
$questParam = PQPModel::dataFind(['id' => 1],'answer_number,answer_accuracy');
$allList = QuestionBank::getAll(['status' => 1],'id','create_time DESC');
if(count($allList) < $questParam['answer_number'])return error('题目数量不够');
$answerArr = array_rand(array_column($allList,'id'),$questParam['answer_number']);
$answerList = [];
foreach($answerArr as $k => $v){
$answerList[$k]['id'] = $k + 1;
$answerList[$k]['title'] = QuestionBank::dataFind(['id' => $v],'title',false);
$answerList[$k]['opt'] = QuestionOpt::getAll(['bank_id' => $v],'opt,content,is_correct','id ASC');
}
$data['list'] = $answerList;
$data['answer_accuracy'] = $questParam['answer_accuracy'];
return success($data);
}
// 记录答题
public function statistics(){
$param = $this->request->param();
$validate = new \app\validate\answer\StatisticsAnswer;
if (!$validate->check($param))return error($validate->getError());//验证参数
$questParam = PQPModel::dataFind(['id' => 1],'answer_number,answer_interval_period');
if(($param['correct_rate'] + $param['error_rate']) != $questParam['answer_number'])return error('非法操作');//验证参数
$data['digitized_human_id'] = $this->token;
$data['digitized_human_name'] = $param['digitized_human_name'];
$data['correct_rate'] = $param['correct_rate'];
$data['error_rate'] = $param['error_rate'];
$data['id_card'] = $param['id_card'];
$data['involved_date'] = date('Y-m-d',time());
$data['status'] = 1;
$data['create_time'] = date('Y-m-d H:i:s',time());
$data['update_time'] = date('Y-m-d H:i:s',time());
StatisticsAnswer::save($data,[]);
$res = [];
if((int)$questParam['answer_interval_period'] > 0){
$redis = Redis::select(config('cache.stores.redis.other_db'));
$keyLog = 'answer_'.$param['id_card'];
$answerLog = $redis->get($keyLog);
if(empty($answerLog)){
$redis->setex($keyLog,(int)$questParam['answer_interval_period']*24*3600,1);
$res['receive_prize'] = 0;
$res['order_no'] = create_order();
}else{
$res['receive_prize'] = $redis->ttl($keyLog);
$res['order_no'] = '';
}
}
return success($res);
}
// 获取奖品
public function prize(){
$idCard = $this->request->param('id_card','');
if(empty($idCard))return error('参数错误');
$redis = Redis::select(config('cache.stores.redis.other_db'));
$keyLog = 'answer_'.$idCard;
$answerLog = $redis->get($keyLog);
if(empty($answerLog))return error('非法访问');
$data = QuestionPrize::dataFind(['id' => 1,'status' => 1],'id,prize_name,prize_price,postage_price,prize_picture',true);
return success($data);
}
// 创建订单
public function order(){
$param = $this->request->param();
$validate = new \app\validate\answer\QuestionOrder;
if(!$validate->check($param))return error($validate->getError());//验证参数
$redis = Redis::select(config('cache.stores.redis.other_db'));
$keyLog = 'answer_'.$param['id_card'];
$answerLog = $redis->get($keyLog);
if(empty($answerLog))return error('非法访问');
$prizeName = QuestionPrize::dataFind(['id' => $param['prize_id'],'status' => 1],'prize_name',false);
if(empty($prizeName))return error('奖品走丢咯~~~~~');
$data['digitized_human_id'] = $this->token;
$data['order_no'] = $param['order_no'];
$data['consignee_name'] = $param['consignee_name'];
$data['consignee_phone'] = $param['consignee_phone'];
$data['prize_name'] = $prizeName;
$data['area'] = $param['area'];
$data['details_addr'] = $param['details_addr'];
$data['id_card'] = $param['id_card'];
$data['receipt_status'] = 1;
$data['express_company'] = '';
$data['express_number'] = '';
$data['status'] = 1;
$data['create_time'] = date('Y-m-d H:i:s',time());
$data['update_time'] = date('Y-m-d H:i:s',time());
$res = QuestionOrder::save($data,[]);
Pro::pushSend($res,20*60);
return success([],'操作成功');
}
//获取支付二维码
public function orderPayment(){
$orderNo = $this->request->param('order_no');
if(empty($orderNo))return error('参数错误');
$receiptStatus = QuestionOrder::dataFind(['order_no' => $orderNo],'receipt_status',false);
if($receiptStatus != 1)return error('没有未支付订单');
$postagePrice = QuestionPrize::dataFind(['id' => 1,'status' => 1],'postage_price',false);
$res = [];
if($postagePrice > 0){
$postagePrice = 0.01;
$nativeService = new WechatPayNative();
$res = $nativeService->pay_payment($orderNo,$postagePrice);
}
return success($res,'操作成功');
}
//获取支付结果
public function orderPaymentResult(){
$orderNo = $this->request->param('order_no');
if(empty($orderNo))return error('参数错误');
$receiptStatus = QuestionOrder::dataFind(['order_no' => $orderNo],'receipt_status',false);
return success(['receipt_status' => empty($receiptStatus) ? 0 : $receiptStatus],'操作成功');
}
//重新生成订单号
public function answerOrderRepeat(){
$orderNo = $this->request->param('order_no');
if(empty($orderNo))return error('参数错误');
$res = QuestionOrder::dataFind(['order_no' => $orderNo],'id,receipt_status',true);
if(!empty($res)){
if($res['receipt_status'] == 4){
$orderNo = create_order();
QuestionOrder::save(['order_no' => $orderNo,'receipt_status' => 1],['id' => $res['id']]);
Pro::pushSend($res['id'],20*60);
}
}
return success(['order_no' => $orderNo]);
}
}