Kaynağa Gözat

Merge branch 'master' of http://git.derkj.com:9095/waibao/NN2025081602

* 'master' of http://git.derkj.com:9095/waibao/NN2025081602:
  兼容小数点分数
  题目富文本编辑
  no message
  修改编号显示ID
  科目添加考试时长
  处理试题总分为0问题

# Conflicts:
#	app/Services/Api/PaperService.php
wesmiler 5 ay önce
ebeveyn
işleme
7b08253f80

+ 3 - 0
.gitignore

@@ -16,3 +16,6 @@ check_table_structure.php
 MODIFICATION_SUMMARY.md
 start-dev.bat
 test_db_fields.php
+COMMIT_SUMMARY.md
+sql/add_answer_times_to_subjects.sql
+sql/update_paper_statistics.sql

+ 1 - 0
addons/admin/src/config/setting.js

@@ -5,6 +5,7 @@ export default {
   version: "1.0",
   name: "后台管理系统", // 项目名称
   loginName: "后台登录", // 项目名称
+  // baseURL: "http://123.57.137.186:8090", // 本地接口地址
   baseURL: "https://api.gxlmcs.com/", // 本地接口地址
   // chatURL: 'ws://127.0.4.82:8660',  // 聊天地址
   signKey: "stapp&688", // 签名密钥

+ 8 - 1
addons/admin/src/views/exam/component/SubjectManager.vue

@@ -23,6 +23,7 @@
                         </template>
                     </el-table-column>
                     <el-table-column prop="sort" label="排序" width="100" />
+                    <el-table-column prop="answer_times" label="答题时长(分钟)" width="100" />
                     <el-table-column prop="status" label="状态" width="100">
                         <template slot-scope="{ row }">
                             <el-tag type="success" v-if="row.status === 1">有效</el-tag>
@@ -65,6 +66,9 @@
                             :value="Number(key)" />
                     </el-select>
                 </el-form-item>
+                <el-form-item label="答题时长(分钟)" prop="answer_times">
+                    <el-input-number v-model="formData.answer_times" :min="0" :max="12000" placeholder="请输入答题时长(分钟)" />
+                </el-form-item>
                 <el-form-item label="排序" prop="sort">
                     <el-input-number v-model="formData.sort" :min="0" />
                 </el-form-item>
@@ -111,6 +115,7 @@ export default {
                 attr_type: this.defaultType === 1 ? 3 : null,
                 description: '',
                 icon: '',
+                answer_times: 45,
                 sort: 0,
                 status: 1
             },
@@ -152,7 +157,8 @@ export default {
                     ...row,
                     attr_type: row.attr_type ? Number(row.attr_type) : null,
                     description: row.description || '',
-                    icon: row.icon || ''
+                    icon: row.icon || '',
+                    answer_times: row.answer_times || 45
                 })
             } else {
                 this.resetForm()
@@ -215,6 +221,7 @@ export default {
                 attr_type: this.defaultType === 1 ? 3 : null, // 单招默认为3(职业适应性测试视频课),对口和专升本为null
                 description: '',
                 icon: '',
+                answer_times: 45,
                 sort: 0,
                 status: 1
             })

+ 41 - 7
addons/admin/src/views/exam/component/TopicManager.vue

@@ -24,7 +24,7 @@
                             <template slot-scope="{ row }">
                                 <div v-if="row.show_type === 1" class="topic-content clickable"
                                     @click="showContentDialog('题目内容', row.topic_name)">
-                                    {{ row.topic_name }}
+                                    <div class="topic-html-content" v-html="row.topic_name"></div>
                                 </div>
                                 <div v-else class="topic-content clickable"
                                     @click="showImageDialog('题目内容', row.topic_name)">
@@ -102,7 +102,7 @@
                 </el-form-item>
 
                 <el-form-item label="题目内容" prop="topic_name" v-if="form.show_type === 1">
-                    <el-input type="textarea" :rows="3" v-model="form.topic_name" placeholder="请输入题目内容"></el-input>
+                    <tinymce-editor v-model="form.topic_name" :init="editorConfig" />
                 </el-form-item>
 
                 <el-form-item label="题目图片" prop="topic_name" v-else>
@@ -213,9 +213,7 @@
 
         <!-- 内容查看弹窗 -->
         <el-dialog :visible.sync="contentDialogVisible" :title="contentDialogTitle" width="600px">
-            <div class="content-display">
-                <pre>{{ contentDialogContent }}</pre>
-            </div>
+            <div class="content-display" v-html="contentDialogContent"></div>
         </el-dialog>
 
         <!-- 图片查看弹窗 -->
@@ -230,10 +228,12 @@
 <script>
 import Sortable from 'sortablejs';
 import uploadImage from '../../../components/uploadImage'
+import TinymceEditor from '@/components/TinymceEditor/index.vue'
 
 export default {
     components: {
-        uploadImage
+        uploadImage,
+        TinymceEditor
     },
     name: 'TopicManager',
     props: {
@@ -291,7 +291,19 @@ export default {
             // 图片查看弹窗
             imageDialogVisible: false,
             imageDialogTitle: '',
-            imageDialogImage: ''
+            imageDialogImage: '',
+            // 富文本编辑器配置
+            editorConfig: {
+                height: 300,
+                branding: false,
+                skin_url: '/tinymce/skins/ui/oxide',
+                content_css: '/tinymce/skins/content/default/content.css',
+                language_url: '/tinymce/langs/zh_CN.js',
+                language: 'zh_CN',
+                plugins: 'code print preview fullscreen paste searchreplace link autolink image imagetools media table codesample lists advlist hr charmap emoticons anchor directionality quickbars nonbreaking visualblocks visualchars wordcount',
+                toolbar: 'fullscreen preview code | undo redo | forecolor backcolor | bold italic underline strikethrough | alignleft aligncenter alignright alignjustify | outdent indent | numlist bullist | formatselect fontselect fontsizeselect | link image media emoticons charmap anchor codesample',
+                toolbar_drawer: 'sliding'
+            }
         };
     },
     mounted() {
@@ -628,9 +640,28 @@ export default {
     text-overflow: ellipsis;
     display: -webkit-box;
     -webkit-line-clamp: 3;
+    line-clamp: 3;
     -webkit-box-orient: vertical;
 }
 
+.topic-html-content {
+    max-height: 60px;
+    overflow: hidden;
+    word-break: break-word;
+}
+
+.topic-html-content>>>p {
+    margin: 0;
+    padding: 0;
+}
+
+.topic-html-content>>>img {
+    max-width: 100%;
+    height: auto;
+    max-height: 50px;
+    object-fit: contain;
+}
+
 .topic-content.clickable {
     cursor: pointer;
     transition: all 0.3s ease;
@@ -645,6 +676,9 @@ export default {
 .content-display {
     max-height: 400px;
     overflow-y: auto;
+    padding: 10px;
+    background-color: #f8f9fa;
+    border-radius: 4px;
 }
 
 .content-display pre {

+ 1 - 1
addons/admin/src/views/exam/examList.vue

@@ -63,7 +63,7 @@
                 highlight-current-row>
                 <template slot-scope="{ index }">
                     <el-table-column type="selection" width="45" align="center" fixed="left" />
-                    <el-table-column type="index" :index="index" label="编号" width="60" align="center" fixed="left" />
+                    <el-table-column prop="id" label="ID" width="60" align="center" fixed="left" />
                     <el-table-column label="试卷名称" width="240">
                         <template slot-scope="{ row }">
                             <text-ellipsis :text="row.name" :max-length="16" />

+ 8 - 4
addons/admin/src/views/subject/subject.vue

@@ -77,7 +77,7 @@
                     <el-table-column prop="attr_type" label="类别属性" width="120" v-if="showAttrTypeColumn">
                         <template slot-scope="{ row }">
                             <el-tag type="success" v-if="row.attr_type">{{ attrTypeOptions[row.attr_type] || '未知'
-                                }}</el-tag>
+                            }}</el-tag>
                             <span v-else>-</span>
                         </template>
                     </el-table-column>
@@ -124,6 +124,9 @@
                             :value="Number(key)" />
                     </el-select>
                 </el-form-item>
+                <el-form-item label="答题时长(分钟)" prop="answer_times">
+                    <el-input-number v-model="formData.answer_times" :min="0" :max="12000" placeholder="请输入答题时长(分钟)" />
+                </el-form-item>
                 <el-form-item label="排序" prop="sort">
                     <el-input-number v-model="formData.sort" :min="0" />
                 </el-form-item>
@@ -155,7 +158,7 @@ export default {
                 where: { keyword: "", type: null, attr_type: null, status: null }
             },
             choose: [],
-            formData: { id: null, subject_name: "", type: 1, attr_type: 3, description: "", icon: "", sort: 0, status: 1 },
+            formData: { id: null, subject_name: "", type: 1, attr_type: 3, description: "", icon: "", answer_times: 45, sort: 0, status: 1 },
             isEdit: false,
             formVisible: false,
             formRules: {
@@ -233,11 +236,12 @@ export default {
                     type: Number(row.type),
                     attr_type: row.attr_type ? Number(row.attr_type) : (Number(row.type) === 1 ? 3 : null),
                     description: row.description || "",
-                    icon: row.icon || ""
+                    icon: row.icon || "",
+                    answer_times: row.answer_times || 45
                 }; // 转成数字
             } else {
                 this.isEdit = false;
-                this.formData = { id: null, subject_name: "", type: 1, attr_type: 3, description: "", icon: "", sort: 0, status: 1 };
+                this.formData = { id: null, subject_name: "", type: 1, attr_type: 3, description: "", icon: "", answer_times: 45, sort: 0, status: 1 };
             }
             this.formVisible = true;
         },

+ 1 - 0
app/Models/ExamSubjectsModel.php

@@ -16,6 +16,7 @@ class ExamSubjectsModel extends BaseModel
         'attr_type',
         'description',
         'icon',
+        'answer_times',
         'sort',
         'create_time',
         'update_time',

+ 157 - 157
app/Services/Api/PaperService.php

@@ -60,45 +60,46 @@ class PaperService extends BaseService
      */
     public function getDataList($params, $pageSize = 15)
     {
-        $page = isset($params['page'])? $params['page'] : 1;
-        $cacheKey = "caches:paper:list_{$page}_{$pageSize}:".md5(json_encode($params));
+        $page = isset($params['page']) ? $params['page'] : 1;
+        $cacheKey = "caches:paper:list_{$page}_{$pageSize}:" . md5(json_encode($params));
         $datas = RedisService::get($cacheKey);
 
         // 访问次数统计
-        $sc = isset($params['sc'])? $params['sc'] : 0;
-        $type = isset($params['type'])? $params['type'] : 0;
-        $sceneType = isset($params['scene_type'])? $params['scene_type'] : 0;
-        if (empty($sc) && !in_array($sceneType, [3,4,6])) {
+        $sc = isset($params['sc']) ? $params['sc'] : 0;
+        $type = isset($params['type']) ? $params['type'] : 0;
+        $sceneType = isset($params['scene_type']) ? $params['scene_type'] : 0;
+        if (empty($sc) && !in_array($sceneType, [3, 4, 6])) {
             ExamAccessLogModel::saveLog(date('Y-m-d'), $type, $sceneType);
         }
-        if($datas){
+        if ($datas) {
             return $datas;
         }
-        $userId = isset($params['user_id'])? $params['user_id'] : 0;
+        $userId = isset($params['user_id']) ? $params['user_id'] : 0;
         $query = $this->getQuery($params);
-        $list = $query->with(['answer'=>function($query) use($userId){
+        $list = $query->with(['answer' => function ($query) use ($userId) {
             $query->where('user_id', $userId);
-        }])->where('a.topic_count','>', 0)->select(['a.*','b.id as rid', 'b.score', 'b.accurate_count','b.user_id', 'b.answer_times', 'b.answer_count', 'b.is_submit'])
-            ->orderBy('a.create_time','desc')
+        }])
+            ->where('a.topic_count', '>', 0)->select(['a.*', 'b.id as rid', 'b.score', 'b.accurate_count', 'b.user_id', 'b.answer_times', 'b.answer_count', 'b.is_submit'])
+            ->orderBy('a.sort', 'desc')->orderBy('a.id', 'desc')
             ->paginate($pageSize > 0 ? $pageSize : 9999999);
-        $list = $list? $list->toArray() :[];
-        if($list){
-            foreach($list['data'] as &$item){
-                $item['score'] = !empty($item['score'])? $item['score'] : 0;
-                $item['is_submit'] = !empty($item['is_submit'])? $item['is_submit'] : 0;
-                $item['answer_count'] = !empty($item['answer_count'])? $item['answer_count'] : 0;
-                $item['accurate_count'] = !empty($item['accurate_count'])? $item['accurate_count'] : 0;
+        $list = $list ? $list->toArray() : [];
+        if ($list) {
+            foreach ($list['data'] as &$item) {
+                $item['score'] = !empty($item['score']) ? $item['score'] : 0;
+                $item['is_submit'] = !empty($item['is_submit']) ? $item['is_submit'] : 0;
+                $item['answer_count'] = !empty($item['answer_count']) ? $item['answer_count'] : 0;
+                $item['accurate_count'] = !empty($item['accurate_count']) ? $item['accurate_count'] : 0;
                 $item['date'] = $item['create_time'] ? datetime($item['create_time'], 'Y-m-d') : '';
             }
         }
 
-        $rows = isset($list['data'])? $list['data'] : [];
+        $rows = isset($list['data']) ? $list['data'] : [];
         $datas = [
-            'pageSize'=> $pageSize,
-            'total'=> isset($list['total'])? $list['total'] : 0,
-            'list'=> $rows
+            'pageSize' => $pageSize,
+            'total' => isset($list['total']) ? $list['total'] : 0,
+            'list' => $rows
         ];
-        if($rows){
+        if ($rows) {
             RedisService::set($cacheKey, $datas, rand(300, 600));
         }
 
@@ -112,36 +113,36 @@ class PaperService extends BaseService
      */
     public function getQuery($params)
     {
-        $where = ['a.status'=>1,'a.mark'=>1];
-        $status = isset($params['status'])? $params['status'] : 0;
-        $type = isset($params['type'])? $params['type'] : 0;
-        $sceneType = isset($params['scene_type'])? $params['scene_type'] : 0;
-        $subjectId = isset($params['subject_id'])? $params['subject_id'] : 0;
-        if($status>0){
+        $where = ['a.status' => 1, 'a.mark' => 1];
+        $status = isset($params['status']) ? $params['status'] : 0;
+        $type = isset($params['type']) ? $params['type'] : 0;
+        $sceneType = isset($params['scene_type']) ? $params['scene_type'] : 0;
+        $subjectId = isset($params['subject_id']) ? $params['subject_id'] : 0;
+        if ($status > 0) {
             $where['a.status'] = $status;
         }
-        if($type>0){
+        if ($type > 0) {
             $where['a.type'] = $type;
         }
 
-        if($sceneType>0){
+        if ($sceneType > 0) {
             $where['a.scene_type'] = $sceneType;
         }
 
-        if($subjectId>0){
+        if ($subjectId > 0) {
             $where['a.subject_id'] = $subjectId;
         }
-        $userId = isset($params['user_id'])? $params['user_id'] : 0;
+        $userId = isset($params['user_id']) ? $params['user_id'] : 0;
         return $this->model->from('exam_papers as a')
-            ->leftJoin('exam_answers as b', function($join) use($userId){
+            ->leftJoin('exam_answers as b', function ($join) use ($userId) {
                 // 未完成交卷的答题数据
-                $join->on('b.paper_id','=','a.id')->where(['b.user_id'=>$userId,'b.status'=>1,'b.mark'=>1]);
+                $join->on('b.paper_id', '=', 'a.id')->where(['b.user_id' => $userId, 'b.status' => 1, 'b.mark' => 1]);
             })
             ->where($where)
-            ->where(function ($query) use($params){
-                $keyword = isset($params['keyword'])? $params['keyword'] : '';
-                if($keyword){
-                    $query->where('a.name','like',"%{$keyword}%");
+            ->where(function ($query) use ($params) {
+                $keyword = isset($params['keyword']) ? $params['keyword'] : '';
+                if ($keyword) {
+                    $query->where('a.name', 'like', "%{$keyword}%");
                 }
             });
     }
@@ -153,29 +154,29 @@ class PaperService extends BaseService
      * @param int $submit 是否已交卷,1-是,0-否
      * @return array|mixed
      */
-    public function getLastAnswer($userId, $paperId, $submit=0)
+    public function getLastAnswer($userId, $paperId, $submit = 0)
     {
         $cacheKey = "caches:paper:answer_last_{$userId}:{$paperId}_{$submit}";
         $data = RedisService::get($cacheKey);
-        if($data){
+        if ($data) {
             return $data;
         }
 
         $lastTime = ConfigService::make()->getConfigByCode('submit_paper_time', 30);
-        $lastTime = $lastTime>=1 && $lastTime <= 150? $lastTime : 30;
-        $data = ExamAnswerModel::where(['user_id'=>$userId,'is_submit'=> $submit,'paper_id'=> $paperId,'status'=>1,'mark'=>1])
-            ->where(function($query) use($lastTime, $submit){
-                if($submit<=0){
+        $lastTime = $lastTime >= 1 && $lastTime <= 150 ? $lastTime : 30;
+        $data = ExamAnswerModel::where(['user_id' => $userId, 'is_submit' => $submit, 'paper_id' => $paperId, 'status' => 1, 'mark' => 1])
+            ->where(function ($query) use ($lastTime, $submit) {
+                if ($submit <= 0) {
                     // 未交卷
-                    $query->where('is_submit', 0)->orWhere('answer_last_at','>=', time() - $lastTime * 60);
-                }else{
-                    $query->where('is_submit', $submit)->orWhere('answer_last_at','<', time() - $lastTime * 60);
+                    $query->where('is_submit', 0)->orWhere('answer_last_at', '>=', time() - $lastTime * 60);
+                } else {
+                    $query->where('is_submit', $submit)->orWhere('answer_last_at', '<', time() - $lastTime * 60);
                 }
             })
-            ->select(['id','paper_id','score','answer_last_at','answer_last_id'])
+            ->select(['id', 'paper_id', 'score', 'answer_last_at', 'answer_last_id'])
             ->first();
-        $data = $data? $data->toArray() : [];
-        if($data){
+        $data = $data ? $data->toArray() : [];
+        if ($data) {
             RedisService::set($cacheKey, $data, rand(5, 10));
         }
         return $data;
@@ -187,18 +188,18 @@ class PaperService extends BaseService
      * @param $paperId
      * @return array|mixed
      */
-    public function getLastAnswerLogId($userId,$paperId)
+    public function getLastAnswerLogId($userId, $paperId)
     {
         $cacheKey = "caches:paper:log_{$userId}_{$paperId}";
         $id = RedisService::get($cacheKey);
-        if($id){
+        if ($id) {
             return $id;
         }
 
-        $id = ExamAnswerModel::where(['user_id'=> $userId,'paper_id'=> $paperId,'status'=>1,'mark'=>1])
-            ->where('create_time','>=', strtotime(date('Y-m-d')))
+        $id = ExamAnswerModel::where(['user_id' => $userId, 'paper_id' => $paperId, 'status' => 1, 'mark' => 1])
+            ->where('create_time', '>=', strtotime(date('Y-m-d')))
             ->value('id');
-        if($id){
+        if ($id) {
             RedisService::set($cacheKey, $id, rand(5, 10));
         }
 
@@ -210,71 +211,71 @@ class PaperService extends BaseService
      * @param $id
      * @return array|mixed
      */
-    public function getInfo($userId, $paperId, $params=[])
+    public function getInfo($userId, $paperId, $params = [])
     {
-        $lid = isset($params['lid'])? intval($params['lid']) : 0;
-        $tid = isset($params['tid'])? intval($params['tid']) : 0;
-        $rid = isset($params['rid'])? intval($params['rid']) : 0;
-        $type = isset($params['type'])? intval($params['type']) : 1;
+        $lid = isset($params['lid']) ? intval($params['lid']) : 0;
+        $tid = isset($params['tid']) ? intval($params['tid']) : 0;
+        $rid = isset($params['rid']) ? intval($params['rid']) : 0;
+        $type = isset($params['type']) ? intval($params['type']) : 1;
         $cacheKey = "caches:paper:info_{$userId}:p{$paperId}_t{$tid}_r{$rid}_l{$lid}";
         $info = RedisService::get($cacheKey);
-        if($info){
+        if ($info) {
             return $info;
         }
 
         // 若进行答题
-        if($rid<=0) {
-            if($type != 1){
+        if ($rid <= 0) {
+            if ($type != 1) {
                 // 判断N分钟内是否有未交卷的答题
                 $lastAnswerInfo = $this->getLastAnswer($userId, $paperId, 0);
                 $rid = isset($lastAnswerInfo['id']) ? $lastAnswerInfo['id'] : 0;
-            }else {
+            } else {
                 $rid = $this->getLastAnswerLogId($userId, $paperId);
             }
         }
-        $where = ['a.id'=> $paperId,'a.status'=>1,'a.mark'=>1];
+        $where = ['a.id' => $paperId, 'a.status' => 1, 'a.mark' => 1];
         $info = $this->model->from('exam_papers as a')
-            ->leftJoin('exam_answers as b',function($join) use($rid,$userId){
-                $join->on('b.paper_id','=','a.id')->where(['b.id'=>$rid,'b.user_id'=>$userId,'b.status'=>1]);
+            ->leftJoin('exam_answers as b', function ($join) use ($rid, $userId) {
+                $join->on('b.paper_id', '=', 'a.id')->where(['b.id' => $rid, 'b.user_id' => $userId, 'b.status' => 1]);
             })
             ->where($where)
-            ->select(['a.id as paper_id','b.id as rid','b.score','b.accurate_count','b.is_submit','b.answer_count','b.answer_last_id','b.answer_times','a.name','a.type','a.scene_type','a.subject_id','a.score_total','a.topic_count','a.total_time','a.is_charge','a.create_time','a.status'])
+            ->select(['a.id as paper_id', 'b.id as rid', 'b.score', 'b.accurate_count', 'b.is_submit', 'b.answer_count', 'b.answer_last_id', 'b.answer_times', 'a.name', 'a.type', 'a.scene_type', 'a.subject_id', 'a.score_total', 'a.topic_count', 'a.total_time', 'a.is_charge', 'a.create_time', 'a.status'])
             ->first();
-        $info = $info? $info->toArray() : [];
+        $info = $info ? $info->toArray() : [];
 
-        if($info){
-            $info['create_time'] = $info['create_time']? datetime($info['create_time'],'Y-m-d') : '';
-            $info['answer_count'] = isset($info['answer_count'])? intval($info['answer_count']) : 0;
-            $info['accurate_count'] = isset($info['accurate_count'])? intval($info['accurate_count']) : 0;
-            $info['score'] = isset($info['score'])? intval($info['score']) : 0;
-            $info['rid'] = isset($info['rid'])? intval($info['rid']) : 0;
+        if ($info) {
+            $info['create_time'] = $info['create_time'] ? datetime($info['create_time'], 'Y-m-d') : '';
+            $info['answer_count'] = isset($info['answer_count']) ? intval($info['answer_count']) : 0;
+            $info['accurate_count'] = isset($info['accurate_count']) ? intval($info['accurate_count']) : 0;
+            $info['score'] = isset($info['score']) ? intval($info['score']) : 0;
+            $info['rid'] = isset($info['rid']) ? intval($info['rid']) : 0;
 
             // 剩余时间
-            $isSubmit = isset($info['is_submit'])? $info['is_submit'] : 0;
-            $totalTime = isset($info['total_time'])? intval($info['total_time']) : 0;
-            $totalTime = $totalTime>0? $totalTime : ConfigService::make()->getConfigByCode('answer_total_time', 1800);
-            $info['answer_times'] = isset($info['answer_times'])? intval($info['answer_times']) : 0;
-            $info['remain_time'] = $totalTime>$info['answer_times']? $totalTime-$info['answer_times'] : 0;
-            $info['remain_time'] = $isSubmit? 0 : $info['remain_time'];
-            $info['remain_time_text'] = $info['remain_time']? format_times($info['remain_time']) : '00:00';
-            $info['progress'] = $info['topic_count']?intval($info['answer_count']/$info['topic_count'] * 100) : 0;
+            $isSubmit = isset($info['is_submit']) ? $info['is_submit'] : 0;
+            $totalTime = isset($info['total_time']) ? intval($info['total_time']) : 0;
+            $totalTime = $totalTime > 0 ? $totalTime : ConfigService::make()->getConfigByCode('answer_total_time', 1800);
+            $info['answer_times'] = isset($info['answer_times']) ? intval($info['answer_times']) : 0;
+            $info['remain_time'] = $totalTime > $info['answer_times'] ? $totalTime - $info['answer_times'] : 0;
+            $info['remain_time'] = $isSubmit ? 0 : $info['remain_time'];
+            $info['remain_time_text'] = $info['remain_time'] ? format_times($info['remain_time']) : '00:00';
+            $info['progress'] = $info['topic_count'] ? intval($info['answer_count'] / $info['topic_count'] * 100) : 0;
             // 当前题目
             //$prefix = env('DB_PREFIX','_lev');
             $model = ExamTopicModel::from('exam_topics as a')
-                ->leftJoin('exam_answers_topics as b', function($join) use($rid, $userId){
+                ->leftJoin('exam_answers_topics as b', function ($join) use ($rid, $userId) {
                     // 是否有最近答题记录
-                    $join->on('b.topic_id','=',"a.id")->where("b.answer_log_id",'=', $rid)->where(['b.user_id'=>$userId,'b.status'=>1,"b.mark"=> 1]);
+                    $join->on('b.topic_id', '=', "a.id")->where("b.answer_log_id", '=', $rid)->where(['b.user_id' => $userId, 'b.status' => 1, "b.mark" => 1]);
                 })
-                ->where(['a.paper_id'=> $paperId,'a.status'=>1,'a.mark'=>1])
-                ->where(function($query) use($tid){
+                ->where(['a.paper_id' => $paperId, 'a.status' => 1, 'a.mark' => 1])
+                ->where(function ($query) use ($tid) {
                     // 答题卡选择的题目,否则默认按题目排序返回第一题
-                    if($tid>0){
+                    if ($tid > 0) {
                         $query->where('a.id', $tid);
                     }
                 });
-            $info['topic'] = $model->select(['a.*','b.id as answer_topic_id','b.answer_log_id','b.answer as submit_answer','b.answer_analysis','b.answer_type as submit_answer_type','b.score as submit_score','b.accurate'])
-                ->orderBy('a.sort','desc')
-                ->orderBy('a.id','asc')
+            $info['topic'] = $model->select(['a.*', 'b.id as answer_topic_id', 'b.answer_log_id', 'b.answer as submit_answer', 'b.answer_analysis', 'b.answer_type as submit_answer_type', 'b.score as submit_score', 'b.accurate'])
+                ->orderBy('a.sort', 'desc')
+                ->orderBy('a.id', 'asc')
                 ->first();
             $info['topic'] = $info['topic']? $info['topic']->toArray() : [];
             $topicId = isset($info['topic']['id'])? $info['topic']['id'] : 0;
@@ -288,88 +289,88 @@ class PaperService extends BaseService
                 $info['topic']['submit_answer_type'] = isset($info['topic']['submit_answer_type'])?$info['topic']['submit_answer_type'] : 1;
                 $info['topic']['submit_answers'] = '';
                 $info['topic']['collect_answers'] = [];
-                if($info['topic']['submit_answer_type'] == 3){
+                if ($info['topic']['submit_answer_type'] == 3) {
                     $info['topic']['submit_answer'] = get_image_url($info['topic']['submit_answer']);
-                }else if($info['topic']['submit_answer_type'] == 4){
-                    $info['topic']['submit_answer'] = $info['topic']['submit_answer']?json_decode($info['topic']['submit_answer'], true):[];
-                }else{
+                } else if ($info['topic']['submit_answer_type'] == 4) {
+                    $info['topic']['submit_answer'] = $info['topic']['submit_answer'] ? json_decode($info['topic']['submit_answer'], true) : [];
+                } else {
                     $info['topic']['submit_answer'] = format_content($info['topic']['submit_answer']);
                 }
 
-                if($info['topic']['show_type'] == 1){
+                if ($info['topic']['show_type'] == 1) {
                     $info['topic']['topic_name'] = format_content($info['topic']['topic_name']);
-                }else if($info['topic']['show_type'] == 2){
+                } else if ($info['topic']['show_type'] == 2) {
                     $info['topic']['topic_name'] = get_image_url($info['topic']['topic_name']);
 
                     // 已经答题,返回答案
-                    if($rid>0 || $info['topic']['submit_answer']){
+                    if ($rid > 0 || $info['topic']['submit_answer']) {
                         $info['topic']['topic_analysis'] = get_image_url($info['topic']['topic_analysis']);
-                        if(preg_match("/(images|temp)/", $info['topic']['correct_answer'])){
+                        if (preg_match("/(images|temp)/", $info['topic']['correct_answer'])) {
                             $info['topic']['correct_answer'] = get_image_url($info['topic']['correct_answer']);
                         }
 
-                        if(preg_match("/(images|temp)/", $info['topic']['answer_A'])){
+                        if (preg_match("/(images|temp)/", $info['topic']['answer_A'])) {
                             $info['topic']['answer_A'] = get_image_url($info['topic']['answer_A']);
                         }
 
-                        if(preg_match("/(images|temp)/", $info['topic']['answer_B'])){
+                        if (preg_match("/(images|temp)/", $info['topic']['answer_B'])) {
                             $info['topic']['answer_B'] = get_image_url($info['topic']['answer_B']);
                         }
 
-                        if(preg_match("/(images|temp)/", $info['topic']['answer_C'])){
+                        if (preg_match("/(images|temp)/", $info['topic']['answer_C'])) {
                             $info['topic']['answer_C'] = get_image_url($info['topic']['answer_C']);
                         }
 
-                        if(preg_match("/(images|temp)/", $info['topic']['answer_D'])){
+                        if (preg_match("/(images|temp)/", $info['topic']['answer_D'])) {
                             $info['topic']['answer_D'] = get_image_url($info['topic']['answer_D']);
                         }
 
-                        if(preg_match("/(images|temp)/", $info['topic']['answer_E'])){
+                        if (preg_match("/(images|temp)/", $info['topic']['answer_E'])) {
                             $info['topic']['answer_E'] = get_image_url($info['topic']['answer_E']);
                         }
 
-                        if(preg_match("/(images|temp)/", $info['topic']['answer_F'])){
+                        if (preg_match("/(images|temp)/", $info['topic']['answer_F'])) {
                             $info['topic']['answer_F'] = get_image_url($info['topic']['answer_F']);
                         }
                     }
                 }
 
                 // 未答题隐藏答案
-//                if(empty($info['topic']['submit_answer'])){
-//                    $info['topic']['correct_answer'] = '未答题';
-//                    $info['topic']['topic_analysis'] = '';
-//                }
+                //                if(empty($info['topic']['submit_answer'])){
+                //                    $info['topic']['correct_answer'] = '未答题';
+                //                    $info['topic']['topic_analysis'] = '';
+                //                }
 
                 // 多选题
-                $topicType = isset($info['topic']['topic_type'])? $info['topic']['topic_type'] : '';
-                if($topicType == '多选题'){
-                    $info['topic']['submit_answers'] = $info['topic']['submit_answer']?explode(',', $info['topic']['submit_answer']) : '';
-                    $info['topic']['correct_answers'] = $info['topic']['correct_answer']?explode(',', $info['topic']['correct_answer']) : [];
+                $topicType = isset($info['topic']['topic_type']) ? $info['topic']['topic_type'] : '';
+                if ($topicType == '多选题') {
+                    $info['topic']['submit_answers'] = $info['topic']['submit_answer'] ? explode(',', $info['topic']['submit_answer']) : '';
+                    $info['topic']['correct_answers'] = $info['topic']['correct_answer'] ? explode(',', $info['topic']['correct_answer']) : [];
                 }
 
                 $info['topic']['answers'] = [];
-                if($info['topic']['answer_A']){
-                    $info['topic']['answers'][] = ['code'=>'A','value'=> $info['topic']['answer_A']];
+                if ($info['topic']['answer_A']) {
+                    $info['topic']['answers'][] = ['code' => 'A', 'value' => $info['topic']['answer_A']];
                 }
 
-                if($info['topic']['answer_B']){
-                    $info['topic']['answers'][] = ['code'=>'B','value'=> $info['topic']['answer_B']];
+                if ($info['topic']['answer_B']) {
+                    $info['topic']['answers'][] = ['code' => 'B', 'value' => $info['topic']['answer_B']];
                 }
 
-                if($info['topic']['answer_C']){
-                    $info['topic']['answers'][] = ['code'=>'C','value'=> $info['topic']['answer_C']];
+                if ($info['topic']['answer_C']) {
+                    $info['topic']['answers'][] = ['code' => 'C', 'value' => $info['topic']['answer_C']];
                 }
 
-                if($info['topic']['answer_D']){
-                    $info['topic']['answers'][] = ['code'=>'D','value'=> $info['topic']['answer_D']];
+                if ($info['topic']['answer_D']) {
+                    $info['topic']['answers'][] = ['code' => 'D', 'value' => $info['topic']['answer_D']];
                 }
 
-                if($info['topic']['answer_E']){
-                    $info['topic']['answers'][] = ['code'=>'E','value'=> $info['topic']['answer_E']];
+                if ($info['topic']['answer_E']) {
+                    $info['topic']['answers'][] = ['code' => 'E', 'value' => $info['topic']['answer_E']];
                 }
 
-                if($info['topic']['answer_F']){
-                    $info['topic']['answers'][] = ['code'=>'F','value'=> $info['topic']['answer_F']];
+                if ($info['topic']['answer_F']) {
+                    $info['topic']['answers'][] = ['code' => 'F', 'value' => $info['topic']['answer_F']];
                 }
 
                 // 上一题
@@ -379,7 +380,7 @@ class PaperService extends BaseService
                     ->orderBy('sort','asc')
                     ->orderBy('id','asc')
                     ->first();
-                $info['last'] = $info['last']? $info['last']->toArray() :['id'=>0];
+                $info['last'] = $info['last'] ? $info['last']->toArray() : ['id' => 0];
 
                 // 下一题
                 $info['next'] = ExamTopicModel::where(['paper_id'=> $paperId,'status'=>1,'mark'=>1])
@@ -388,8 +389,7 @@ class PaperService extends BaseService
                     ->orderBy('sort','desc')
                     ->orderBy('id','desc')
                     ->first();
-                $info['next'] = $info['next']? $info['next']->toArray() :['id'=>0];
-
+                $info['next'] = $info['next'] ? $info['next']->toArray() : ['id' => 0];
             }
 
             RedisService::set($cacheKey, $info, rand(10, 20));
@@ -406,11 +406,11 @@ class PaperService extends BaseService
      * @param false $refresh
      * @return array|mixed
      */
-    public function getRandomPaper($userId, $type, $sceneType, $refresh=false)
+    public function getRandomPaper($userId, $type, $sceneType, $refresh = false)
     {
         $cacheKey = "caches:papers:random_{$userId}:{$type}_{$sceneType}";
         $data = RedisService::get($cacheKey);
-        if($data && !$refresh){
+        if ($data && !$refresh) {
             return $data;
         }
 
@@ -418,8 +418,8 @@ class PaperService extends BaseService
             ->where('topic_count','>',0)
             ->orderByRaw('RAND()')
             ->first();
-        $data = $data? $data->toArray() : [];
-        if($data){
+        $data = $data ? $data->toArray() : [];
+        if ($data) {
             $data['score'] = 0;
             $data['paper_id'] = $data['id'];
             $data['accurate_count'] = 0;
@@ -441,30 +441,30 @@ class PaperService extends BaseService
      */
     public function submitError($userId, $params)
     {
-        $id = isset($params['id'])? $params['id'] : 0;
-        $rid = isset($params['rid'])? $params['rid'] : 0;
-        $type = isset($params['type'])? $params['type'] : 0;
-        $moduleType = isset($params['module_type'])? $params['module_type'] : 1;
-        $description = isset($params['description'])? $params['description'] : '';
-        if($id<=0 || $moduleType<=0){
+        $id = isset($params['id']) ? $params['id'] : 0;
+        $rid = isset($params['rid']) ? $params['rid'] : 0;
+        $type = isset($params['type']) ? $params['type'] : 0;
+        $moduleType = isset($params['module_type']) ? $params['module_type'] : 1;
+        $description = isset($params['description']) ? $params['description'] : '';
+        if ($id <= 0 || $moduleType <= 0) {
             $this->error = '参数错误';
             return false;
         }
 
 
-        if($type<=0){
+        if ($type <= 0) {
             $this->error = '请选择错误类型';
             return false;
         }
 
-        if(empty($description)){
+        if (empty($description)) {
             $this->error = '请输入错误描述';
             return false;
         }
 
         $limitTime = ConfigService::make()->getConfigByCode('limit_exam_error_submit', 5);
         $cacheKey = "caches:paper:errors_{$userId}:{$id}_{$rid}";
-        if(RedisService::get($cacheKey) && $limitTime>0){
+        if (RedisService::get($cacheKey) && $limitTime > 0) {
             $this->error = '您近期已提交过该题错误,请稍后再试~';
             return false;
         }
@@ -475,32 +475,32 @@ class PaperService extends BaseService
             return false;
         }
 
-        if($limitTime>0 && $logId = ExamErrorModel::where(['user_id'=> $userId,'rid'=>$rid,'paper_id'=> $id,'mark'=>1])->where('create_time','>=', time() - $limitTime* 60)->value('id')){
+        if ($limitTime > 0 && $logId = ExamErrorModel::where(['user_id' => $userId, 'rid' => $rid, 'paper_id' => $id, 'mark' => 1])->where('create_time', '>=', time() - $limitTime * 60)->value('id')) {
             $this->error = '您近期已提交过该题错误,请稍后再试~';
-            RedisService::set($cacheKey, ['id'=> $logId,'paper_id'=>$id], $limitTime* 60);
+            RedisService::set($cacheKey, ['id' => $logId, 'paper_id' => $id], $limitTime * 60);
             return false;
         }
 
         $data = [
-            'user_id'=> $userId,
-            'paper_id'=> $id,
-            'rid'=> $rid,
-            'type'=> $type,
-            'module_type'=> $moduleType,
-            'description'=> $description,
-            'image_url'=> isset($params['image_url']) && $params['image_url']? get_image_path($params['image_url']) : '',
-            'create_time'=>time(),
-            'status'=>1,
-            'mark'=>1
+            'user_id' => $userId,
+            'paper_id' => $id,
+            'rid' => $rid,
+            'type' => $type,
+            'module_type' => $moduleType,
+            'description' => $description,
+            'image_url' => isset($params['image_url']) && $params['image_url'] ? get_image_path($params['image_url']) : '',
+            'create_time' => time(),
+            'status' => 1,
+            'mark' => 1
         ];
 
-        if(!$logId = ExamErrorModel::insertGetId($data)){
+        if (!$logId = ExamErrorModel::insertGetId($data)) {
             $this->error = '提交失败';
             return false;
         }
 
         $this->error = '提交成功';
-        RedisService::set($cacheKey, $data, $limitTime* 60);
-        return ['id'=> $logId, 'paper_id'=> $id];
+        RedisService::set($cacheKey, $data, $limitTime * 60);
+        return ['id' => $logId, 'paper_id' => $id];
     }
 }

+ 99 - 5
app/Services/Exam/PaperService.php

@@ -55,14 +55,13 @@ class PaperService extends BaseService
             });
         }
 
-        $list = $query->orderBy('sort', 'desc')->paginate($pageSize > 0 ? $pageSize : 9999999);
+        $list = $query->orderBy('sort', 'desc')->orderBy('id', 'desc')->paginate($pageSize > 0 ? $pageSize : 9999999);
         $list = $list ? $list->toArray() : [];
         return [
             'pageSize' => $pageSize,
             'total' => isset($list['total']) ? $list['total'] : 0,
             'list' => isset($list['data']) ? $list['data'] : []
         ];
-        return message("操作成功", true, $list);
     }
 
     public function createPaperWithTopics(array $formData, array $topicsData)
@@ -88,7 +87,7 @@ class PaperService extends BaseService
             $topicCount = 0;
 
             foreach ($topicsData as $index => &$topic) {
-                $score = intval($topic['score'] ?? 0);
+                $score = floatval($topic['score'] ?? 0);
                 $totalScore += $score;
                 $topicCount++;
 
@@ -105,8 +104,8 @@ class PaperService extends BaseService
             // 批量插入题目
             ExamTopicModel::insert($topicsData);
 
-            // 更新试卷总分和题目数量
-            $paper->score_total = $totalScore;
+            // 更新试卷总分和题目数量(保留两位小数)
+            $paper->score_total = round($totalScore, 2);
             $paper->topic_count = $topicCount;
             $paper->save();
 
@@ -119,6 +118,101 @@ class PaperService extends BaseService
     }
 
     /**
+     * 获取记录详情
+     * @return array
+     * @since 2020/11/11
+     * @author laravel开发员
+     */
+    public function info()
+    {
+        // 记录ID
+        $id = request()->input("id", 0);
+        $info = [];
+        if ($id) {
+            $info = $this->model->getInfo($id);
+
+            // 如果试题数量或总分为空,从数据库重新获取
+            if ($info && (empty($info['topic_count']) || empty($info['score_total']))) {
+                // 获取该试卷下所有有效题目的统计信息
+                $topicModel = new ExamTopicModel();
+                $statistics = $topicModel
+                    ->where('paper_id', $id)
+                    ->where('mark', 1)
+                    ->selectRaw('COUNT(*) as topic_count, SUM(score) as score_total')
+                    ->first();
+
+                if ($statistics) {
+                    // 如果试题数量为空,使用数据库中的值
+                    if (empty($info['topic_count'])) {
+                        $info['topic_count'] = intval($statistics->topic_count ?? 0);
+                    }
+                    // 如果总分为空,使用数据库中的值
+                    if (empty($info['score_total'])) {
+                        $info['score_total'] = floatval($statistics->score_total ?? 0);
+                    }
+                }
+            }
+        }
+        return message(MESSAGE_OK, true, $info);
+    }
+
+    /**
+     * 添加或编辑记录
+     * @return array
+     * @since 2020/11/11
+     * @author laravel开发员
+     */
+    public function edit()
+    {
+        // 获取参数
+        $argList = func_get_args();
+        // 查询条件
+        $data = isset($argList[0]) ? $argList[0] : [];
+        // 是否打印SQL
+        $is_sql = isset($argList[1]) ? $argList[1] : false;
+        if (!$data) {
+            $data = request()->all();
+        }
+
+        // 获取试卷ID
+        $paperId = isset($data['id']) ? intval($data['id']) : 0;
+
+        // 如果试题数量或总分为空,从数据库重新获取
+        // 检查字段是否为 null、未设置、空字符串或0(如果为空,从数据库重新计算)
+        $topicCountEmpty = !isset($data['topic_count']) || $data['topic_count'] === null || $data['topic_count'] === '' || $data['topic_count'] === 0;
+        $scoreTotalEmpty = !isset($data['score_total']) || $data['score_total'] === null || $data['score_total'] === '' || $data['score_total'] === 0;
+
+        if ($paperId > 0 && ($topicCountEmpty || $scoreTotalEmpty)) {
+            // 获取该试卷下所有有效题目的统计信息
+            $topicModel = new ExamTopicModel();
+            $statistics = $topicModel
+                ->where('paper_id', $paperId)
+                ->where('mark', 1)
+                ->selectRaw('COUNT(*) as topic_count, SUM(score) as score_total')
+                ->first();
+
+            if ($statistics) {
+                // 如果试题数量为空,使用数据库中的值
+                if ($topicCountEmpty) {
+                    $data['topic_count'] = intval($statistics->topic_count ?? 0);
+                }
+                // 如果总分为空,使用数据库中的值
+                if ($scoreTotalEmpty) {
+                    $data['score_total'] = floatval($statistics->score_total ?? 0);
+                }
+            }
+        }
+
+        // 调用父类的 edit 方法
+        $error = '';
+        $rowId = $this->model->edit($data, $error, $is_sql);
+        if ($rowId) {
+            return message(MESSAGE_OK, true, ['id' => $rowId]);
+        }
+        return message($error, false);
+    }
+
+    /**
      * 删除七天之前标记软删除的数据
      */
     public function delete()