You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

169 lines
5.5 KiB

<?php
declare(strict_types=1);
/**
* Quiz.php
* Created by PhpStorm.
* Date: 2026/10/17
* Time: 09:40
* Author: qian.guo
* E-mail: yiyezhiqiutel@gmail.com
*/
namespace app\api\controller;
use app\BaseController;
use app\model\QuizModel;
use app\service\ScoreService;
use think\facade\Log;
use think\Request;
/**
* 测评控制器
* 设计说明:
* - 取卷与交卷都免登录:本期不建库、不收集手机号,登录只是「存用户资料」的前提,
* 而测评本身不需要知道你是谁。匿名能把门槛降到最低
* 代价:无法防重复提交,靠限流 + 无状态计算兑付
* - 刻意不返回轴定义与题目的计分归属:前端渲染只需要题干与选项文字
* 多给一个字段就多一条反推答案方向的线索
* - 交卷不落库、不写文件:答案是「随传随算」的
* 多机部署下写本地文件会不一致,反而比不算更糟
* 代价:用户关掉小程序就丢失结果,所以前端把结果存在本地存储里
*/
class Quiz extends BaseController
{
// 评分服务
private $score_service;
public function __construct(ScoreService $score_service)
{
parent::__construct();
$this->score_service = $score_service;
}
/**
* 获取试卷与题目
* getPaperInfo
* @return \think\response\Json
*/
public function getPaperInfo()
{
$paper = QuizModel::getPaperInfo();
if (empty($paper)) {
return json([
'code' => config('code.fail'),
'msg' => '试卷不存在',
'data' => [
'err_code' => config('code.err.paper_not_found')
]
]);
}
$question_list = QuizModel::getPublicQuestionList();
if (empty($question_list)) {
return json([
'code' => config('code.success'),
'msg' => '暂无数据',
'data' => [],
'total' => 0
]);
}
$data = [];
$data['paper_code'] = (string) ($paper['code'] ?? '');
$data['title'] = (string) ($paper['title'] ?? '');
$data['version'] = (string) ($paper['version'] ?? '');
$data['question_count'] = QuizModel::getQuestionTotal();
$data['notice'] = (string) ($paper['notice'] ?? '');
$data['questions'] = $question_list;
return json([
'code' => config('code.success'),
'msg' => 'success',
'data' => $data
]);
}
/**
* 交卷并评分
* submitAnswers
*
* 请求体:answers = { "1": "A", "2": "B", ... }(键为题号,值为选项 key)
* 免登录,不落库,不写文件:答案是「随传随算」的。
*
* 为什么不存:本期不建库,而写本地文件在多机部署下会不一致;
* 存一份对不上号的数据,比不存更糟。
* 代价是用户关掉小程序就丢失结果,所以前端把结果存本地并允许从结果页重进。
*
* @param Request $request 请求对象
* @return \think\response\Json
*/
public function submitAnswers(Request $request)
{
$answer_list = $request->post('answers', []);
// 参数必须是「题号 => 选项 key」的映射
if (!is_array($answer_list)) {
return json([
'code' => config('code.fail'),
'msg' => '作答格式不正确',
'data' => [
'err_code' => config('code.err.param')
]
]);
}
if (count($answer_list) === 0) {
return json([
'code' => config('code.fail'),
'msg' => '还没有作答',
'data' => [
'err_code' => config('code.err.quiz_unfinished')
]
]);
}
$question_total = QuizModel::getQuestionTotal();
/*
* 缺题直接失败,不静默补默认值。
* 补默认值会让「只答了 30 题」也算出一个看上去合法的人格类型,
* 那是把「数据不完整」伪装成「结果有效」—— 比报错危险得多。
*/
if (count($answer_list) < $question_total) {
return json([
'code' => config('code.fail'),
'msg' => '还有 ' . ($question_total - count($answer_list)) . ' 题未作答',
'data' => [
'err_code' => config('code.err.quiz_unfinished'),
'unfinished_count' => $question_total - count($answer_list)
]
]);
}
try {
$result = $this->score_service->score($answer_list);
} catch (\Throwable $e) {
// 评分失败的原因多是配置问题(策略未注册 / 题库文件损坏),
// 记日志便于定位,但对外只给「评分失败」,不把内部细节吐给客户端
Log::error('[Quiz::submitAnswers] ' . $e->getMessage());
return json([
'code' => config('code.error'),
'msg' => '评分失败,请稍后重试',
'data' => [
'err_code' => config('code.err.score_failed')
]
]);
}
return json([
'code' => config('code.success'),
'msg' => 'success',
'data' => $result->getData()
]);
}
}