初始化项目:添加后端代码、ThinkPHP框架、前端资源

This commit is contained in:
amb
2026-09-03 12:42:39 +08:00
commit 482bece22e
3726 changed files with 416708 additions and 0 deletions
@@ -0,0 +1,72 @@
<?php
namespace app\api\model\app\exam;
use think\Model;
use think\Db;
use app\api\model\app\exam\WarehouseQuestion;
class Exercises extends Model
{
// 表名
protected $name = 'app_exam_exercises';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'integer';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = false;
protected $deleteTime = false;
public function course()
{
return $this->belongsTo('app\api\model\course\Course', 'bind_course', 'id', [], 'LEFT')->setEagerlyType(0);
}
/**
* 获取练习包含的题目数量
* @param $exercisesId
* @return mixed
*/
public function getExercisesQuestionCount($exercisesId){
$notInExercisesGroupIds = \app\api\model\app\exam\ExercisesBindWarehouseGroup::getNotInExercisesGroupIds($exercisesId);
return WarehouseQuestion::where([
'group_id'=>['NOT IN',$notInExercisesGroupIds]
])->count();
}
/**
* 获取订单中的会员卡与规格对应的信息
* @param $cardIdSku 格式 ID_SKU
* @return false
*/
public static function getPayDetail($exercisesId){
$data = self::where([
'id'=>$exercisesId,
'status'=>1,
'sales_type'=>2
])->field('detail',true)->find();
if(!$data){
return false;
}
$data = $data->toArray();
$data['price_marking'] = $data['price'] = $data['pay_price'];
$data['type'] = 'exercises';
$extendInfo = \app\common\model\goods\Handle::parseGoodsExtendInfo($data['id'],'exercises');
$data = array_merge($data,$extendInfo);
return $data;
}
}
@@ -0,0 +1,61 @@
<?php
namespace app\api\model\app\exam;
use think\Model;
use app\api\model\app\exam\WarehouseGroup;
class ExercisesBindWarehouseGroup extends Model
{
// 表名
protected $name = 'app_exam_exercises_bind_warehouse_group';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'integer';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = false;
protected $deleteTime = false;
// 追加属性
protected $append = [
];
/**
* 获取练习绑定的题库分组
* @param $exercisesId
* @return mixed
*/
public static function getExercisesBindWarehouseGroupIds($exercisesId){
return self::where([
'exercises_id'=>$exercisesId
])->field('warehouse_group_id')->column('warehouse_group_id');
}
/**
* 获取没有被练习绑定的分组ID
* 为什么要查这个?
* 当分组被删除时,绑定被删除的分组的题目的分组ID未变更,
* 当练习绑定了未分组的题目集合是,仅搜索分组为0的数据查不到上面情景的题目,故获取该类型的分组ID进行not in 反查询
* @return void
*/
public static function getNotInExercisesGroupIds($exercisesId){
$bindIds = self::getExercisesBindWarehouseGroupIds($exercisesId);
$group = WarehouseGroup::where([
'id'=>['not in',$bindIds]
])->field('id')->column('id');
if(!in_array('0',$bindIds)){
$group[] = 0;
}
return $group;
}
}
@@ -0,0 +1,30 @@
<?php
namespace app\api\model\app\exam;
use think\Model;
use think\Db;
use app\api\model\app\exam\WarehouseQuestion;
class ExercisesGroup extends Model
{
// 表名
protected $name = 'app_exam_exercises_group';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'integer';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = false;
protected $deleteTime = false;
public function getList(){
return self::where([
'status'=>1
])
->order('sort','asc')->order('createtime','desc')->select();
}
}
@@ -0,0 +1,248 @@
<?php
namespace app\api\model\app\exam;
use think\Model;
use app\api\model\app\exam\Exercises;
use app\api\model\app\exam\WarehouseQuestion;
use app\api\model\app\exam\ExercisesBindWarehouseGroup;
/**
* 题库练习记录
*/
class ExercisesLog extends Model
{
// 表名,不含前缀
protected $name = 'app_exam_exercises_log';
// 自动写入时间戳字段
protected $autoWriteTimestamp = false;
// 定义时间戳字段名
protected $createTime = false;
protected $updateTime = false;
// 追加属性
protected $append = [
];
/**
* 出题
* @return void
*/
public function buildLog($userId,$exercisesId)
{
//获取出题配置
$exercisesInfo = Exercises::where([
'id'=>$exercisesId,
'status'=>1
])->find();
if(!$exercisesInfo){
return false;
}
$questionData = $this->getQuestionIds($userId,$exercisesId,$exercisesInfo['question_mode'],$exercisesInfo['question_count']);
if(!$questionData['ids']){
return false;
}
$logId = self::insertGetId([
'user_id'=>$userId,
'uniacid'=>UNIACID,
'exercises_id'=>$exercisesId,
'question_ids'=>implode(",",$questionData['ids']),
'data_index'=>$questionData['data_index'],
'question_count'=>count($questionData['ids']),
'result_true_count'=>0,
'createtime'=>time()
]);
if(!$logId){
return false;
}
return $logId;
}
/**
* 获取练习记录的问题列表
* @param $logId
* @return false
*/
public function getQuestionList($logId){
//获取出题配置
$logData = self::where([
'id'=>$logId
])->find();
if(!$logData){
return false;
}
$logData['question_ids'] = explode(",",$logData['question_ids']);
$questionList = $this->idsGetQestion($logData['question_ids']);
return $questionList;
}
/**
* 通过问题ID集合获取问题列表
* @param $questionIds
* @return false
*/
public function idsGetQestion($questionIds){
$questionList = WarehouseQuestion::where([
'id'=>['in',$questionIds]
])->orderRaw("find_in_set(id,'".implode(",",$questionIds)."')")->select();
if(!$questionList){
return [];
}
foreach ($questionList as &$item){
$item->option = json_decode($item->option,true);
switch ($item['type']){
case 'multiple':
case 'indefinite':
$item->answer = explode(",",$item->answer);
break;
case 'fillblank':
$item->answer = json_decode($item->answer,true);
break;
case 'essay':
// 问答题答案为富文本字符串,保持原样
break;
default:
$item->answer = intval($item->answer);
}
}
return $questionList;
}
/**
* 获取问题集合ID
* @param $userId
* @param $exercisesId
* @param $mode
* @param $count
* @return false
*/
public function getQuestionIds($userId,$exercisesId,$mode,$count){
// 判断是否已经过题了
$exercisesLog = self::where([
'exercises_id'=>$exercisesId,
'user_id'=>$userId
])->order('createtime','desc')->find();
$dataIndex = 0;
if($exercisesLog){
$dataIndex = $exercisesLog['data_index'];
}
if($mode == 'random'){
$questionData = $this->getRandomQuestion($exercisesId,$dataIndex,$count);
}else{
$questionData = $this->getSortQuestion($exercisesId,$dataIndex,$count);
}
if(!$questionData['ids']){
return false;
}
return $questionData;
}
/**
* 随机出题
* @return void
*/
public function getRandomQuestion($exercisesId,$dataIndex,$count){
$groupIds = ExercisesBindWarehouseGroup::getExercisesBindWarehouseGroupIds($exercisesId);
$where = [];
if($groupIds){
$where = ['group_id'=>['in',$groupIds]];
}
$questionIds = WarehouseQuestion::where($where)->orderRaw('rand()')->limit($count)->field('id')->column('id');
if($questionIds){
shuffle($questionIds);
}
return ['ids'=>$questionIds,'data_index'=>$dataIndex];
}
/**
* 顺序出题
* @param $exercisesId 练习
* @param $dataIndex 上次查询的下标
* @param $count 获取数量
* @return void
*/
public function getSortQuestion($exercisesId,$dataIndex,$count){
$groupIds = ExercisesBindWarehouseGroup::getExercisesBindWarehouseGroupIds($exercisesId);
$where = [];
if($groupIds){
$where = ['group_id'=>['in',$groupIds]];
}
$questionIds = WarehouseQuestion::where($where)->limit("{$dataIndex},$count")->field('id')->column('id');
if(count($questionIds) < $count){
//说明问题不够了 需要重头再来
$patchCount = $count - count($questionIds);
$dataIndex = $patchCount;
$patchQuestionIds = WarehouseQuestion::where($where)->limit("0,$patchCount")->field('id')->column('id');
if($patchQuestionIds){
$questionIds = array_merge($questionIds,$patchQuestionIds);
}
$questionIds = array_unique($questionIds);
}else{
$dataIndex += $count;
}
return ['ids'=>$questionIds,'data_index'=>$dataIndex];
}
/**
* 获取做过的题目数量
* @return void
*/
public function getCompleteQuestionCount($exercisesId){
$questioIds = self::where([
'exercises_id'=>$exercisesId
])->field('question_ids')->column('question_ids');
if(!$questioIds){
return 0;
}
$ids = [];
foreach ($questioIds as $questioId){
$ids = array_merge($ids,explode(",",$questioId));
}
$ids = array_unique($ids);
return count($ids);
}
}
@@ -0,0 +1,292 @@
<?php
namespace app\api\model\app\exam;
use think\Model;
use app\api\model\app\exam\Exercises;
use app\api\model\app\exam\WarehouseQuestion;
use app\api\model\app\exam\ExercisesBindWarehouseGroup;
/**
* 题库练习记录答案
*/
class ExercisesLogAnswer extends Model
{
// 表名,不含前缀
protected $name = 'app_exam_exercises_log_answer';
// 自动写入时间戳字段
protected $autoWriteTimestamp = false;
// 定义时间戳字段名
protected $createTime = false;
protected $updateTime = false;
// 追加属性
protected $append = [
];
/**
* 效验答案
* @param $logId
* @param $questionList 问题列表
* @param $answerList 用户答案
* @return void
*/
public function checkAnswer($questionList,$answerList){
if(empty($answerList) || empty($questionList)){
return [];
}
$questionKeyVals = [];
foreach ($questionList as $question){
$questionKeyVals[$question['id']] = $question;
}
$result = [];
$questionKeys = array_keys($questionKeyVals);
$trueCount = 0;
$errorCount = 0;
foreach ($answerList as $key => $answer){
//判断答案的题目ID 是不是全在问题列表里
if(!in_array($key,$questionKeys)){
throw new \app\common\exception\Exception('答案数据异常');
}
$type = $questionKeyVals[$key]['type'];
//判断答案的范围是否正确
switch ($type){
case 'judge':
if($answer > 1 || $answer < 0){
throw new \app\common\exception\Exception('判断题答案数据异常');
}
break;
case 'single':
if($answer > (count($questionKeyVals[$key]['option']) - 1) || $answer < 0){
throw new \app\common\exception\Exception('单选题答案数据异常');
}
break;
case 'multiple':
case 'indefinite':
if(!is_array($answer)){
throw new \app\common\exception\Exception('多选题答案数据异常');
}
foreach ($answer as $option){
if($option > (count($questionKeyVals[$key]['option']) - 1) || $option < 0){
throw new \app\common\exception\Exception('多选题答案数据异常');
}
}
break;
case 'fillblank':
if(!is_array($answer)){
throw new \app\common\exception\Exception('填空题答案数据异常');
}
break;
case 'essay':
break;
}
$temp = [
'question_id'=>$key,
'createtime'=>time()
];
$isCorrect = false;
switch ($type){
case 'single':
case 'judge':
$temp['right_answer'] = $questionKeyVals[$key]['answer'];
$temp['user_answer'] = $answer;
$isCorrect = ($temp['right_answer'] == $temp['user_answer']);
break;
case 'multiple':
case 'indefinite':
$rightAnswer = $this->normalizeChoiceAnswer($questionKeyVals[$key]['answer']);
$answer = $this->normalizeChoiceAnswer($answer);
$temp['right_answer'] = implode(",",$rightAnswer);
$temp['user_answer'] = implode(",",$answer);
$isCorrect = ($rightAnswer == $answer);
break;
case 'fillblank':
$rightAnswer = is_array($questionKeyVals[$key]['answer']) ? $questionKeyVals[$key]['answer'] : json_decode($questionKeyVals[$key]['answer'],true);
$temp['right_answer'] = json_encode($rightAnswer,JSON_UNESCAPED_UNICODE);
$temp['user_answer'] = json_encode($answer,JSON_UNESCAPED_UNICODE);
$isCorrect = $this->checkFillblankAnswer($answer,$rightAnswer);
break;
case 'essay':
$temp['right_answer'] = $questionKeyVals[$key]['answer'];
$temp['user_answer'] = (is_array($answer) || is_object($answer))
? json_encode($answer, JSON_UNESCAPED_UNICODE)
: $answer;
$isCorrect = false;
break;
}
if($isCorrect){
$trueCount++;
$temp['result'] = 1;
$temp['error_book_show'] = 0;
}else{
$temp['result'] = 0;
// 问答题属于主观题,不纳入错题本
$temp['error_book_show'] = ($type == 'essay') ? 2 : 1;
if($type != 'essay'){
$errorCount++;
}
}
$result[] = $temp;
}
return [
'answer_list'=>$result,
'error_count'=>$errorCount,
'true_count'=>$trueCount,
];
}
/**
* 校验填空题答案
* @param array $userAnswer 用户答案
* @param array $rightAnswer 正确答案
* @return bool
*/
protected function checkFillblankAnswer($userAnswer,$rightAnswer){
if(empty($rightAnswer) || empty($userAnswer)){
return false;
}
if(count($userAnswer) != count($rightAnswer)){
return false;
}
foreach ($rightAnswer as $index => $acceptedAnswers){
$userBlankAnswer = isset($userAnswer[$index]) ? trim($userAnswer[$index]) : '';
$blankCorrect = false;
if(!is_array($acceptedAnswers)){
$acceptedAnswers = [$acceptedAnswers];
}
foreach ($acceptedAnswers as $acceptedAnswer){
if(strcasecmp(trim($acceptedAnswer),$userBlankAnswer) === 0){
$blankCorrect = true;
break;
}
}
if(!$blankCorrect){
return false;
}
}
return true;
}
/**
* 规范化选择题答案,避免多选/不定项因顺序不同误判。
* @param mixed $answer
* @return array
*/
protected function normalizeChoiceAnswer($answer)
{
if(!is_array($answer)){
$answer = ($answer === '' || $answer === null) ? [] : explode(",",$answer);
}
$result = [];
foreach ($answer as $item){
if($item === '' || $item === null){
continue;
}
$result[] = intval($item);
}
$result = array_values(array_unique($result));
sort($result, SORT_NUMERIC);
return $result;
}
/**
* 博阿村答案
* @param $userId
* @param $logId
* @param $answerList
* @return void
*/
public function saveAnswer($userId,$exercisesId,$logId,$answerList){
foreach ($answerList as &$item){
$item['createtime'] = time();
$item['exercises_log_id'] = $logId;
$item['exercises_id'] = $exercisesId;
$item['user_id'] = $userId;
$item['uniacid'] = UNIACID;
}
return self::insertAll($answerList);
}
/**
* 获取用户提交的答案
* @param $userId
* @param $exercisesId
* @param $logId
* @return array
*/
public function getUserAnswer($userId,$exercisesId,$logId = 0){
$where = [
'user_id'=>$userId
];
if($exercisesId){
$where['exercises_id'] = $exercisesId;
}
if($logId){
$where['exercises_log_id'] = $logId;
}
$list = self::with(['warehouse_question'])->where($where)->group('question_id')->order('id','desc')->select();
$data = [];
if(!$list){
return $data;
}
$list = collection($list)->toArray();
foreach ($list as $item){
$type = isset($item['warehouse_question']['type']) ? $item['warehouse_question']['type'] : '';
switch ($type){
case 'multiple':
case 'indefinite':
$data[$item['question_id']] = [];
foreach (explode(",",$item['user_answer']) as $option){
$data[$item['question_id']][] = intval($option);
}
break;
case 'fillblank':
$data[$item['question_id']] = json_decode($item['user_answer'],true);
break;
case 'essay':
$decoded = json_decode($item['user_answer'], true);
$data[$item['question_id']] = is_array($decoded) ? $decoded : $item['user_answer'];
break;
default:
$data[$item['question_id']] = intval($item['user_answer']);
}
}
return $data;
}
public function warehouseQuestion()
{
return $this->belongsTo('app\api\model\app\exam\WarehouseQuestion', 'question_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
}
@@ -0,0 +1,30 @@
<?php
namespace app\api\model\app\exam;
use think\Model;
use think\Db;
class ExercisesSubscribe extends Model
{
// 表名
protected $name = 'app_exam_exercises_subscribe';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'integer';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = false;
protected $deleteTime = false;
public function exercises()
{
return $this->belongsTo('app\api\model\app\exam\Exercises', 'exercises_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
}
@@ -0,0 +1,34 @@
<?php
namespace app\api\model\app\exam;
use think\Model;
class WarehouseGroup extends Model
{
// 表名
protected $name = 'app_exam_warehouse_group';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'integer';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = false;
protected $deleteTime = false;
/**
* 获取所有的分组ID
* @return mixed
*/
public static function getAllGroupIds(){
return self::field('id')->column('id');
}
}
@@ -0,0 +1,27 @@
<?php
namespace app\api\model\app\exam;
use think\Model;
class WarehouseQuestion extends Model
{
// 表名
protected $name = 'app_exam_warehouse_question';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'integer';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = false;
protected $deleteTime = false;
// 追加属性
protected $append = [
];
}