question.vue 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. <template>
  2. <div class="detail_main">
  3. <div class="question_header">
  4. <div class="item_title">
  5. <div class="item_step">1</div>
  6. 扫描标准答案:<el-button type="primary" @click="GotoScan()" :loading="scanLoading" style="width: 88px;">{{scanText}}</el-button>
  7. <span class="preview_btn" @click="OpenImage()">预览</span>
  8. <!-- <el-button style="margin-left: 10px;width: 116px;">引用试题结构</el-button> -->
  9. </div>
  10. <div class="choose_template">
  11. 当前模板:{{currentTemplateName}}
  12. <!-- <el-select v-model="queryParams.start" placeholder="请选择" class="select_width" >
  13. <el-option :label="number" :value="number" v-for="number in templateQuestionCount"></el-option>
  14. </el-select> -->
  15. <span class="choose_btn" @click="OpenChooseTemplate()">更换</span>
  16. </div>
  17. </div>
  18. <div class="page_jg_20"></div>
  19. <div class="page_module">
  20. <div class="item_title">
  21. <div class="item_step">2</div>
  22. 设置试题结构:
  23. <div class="item_search">
  24. <div class="search_name">题目从</div>
  25. <el-select v-model="queryParams.start" placeholder="请选择" class="select_width" >
  26. <el-option :label="number" :value="number" v-for="number in questionRange"></el-option>
  27. </el-select>
  28. <div class="search_name">至</div>
  29. <el-select v-model="queryParams.end" placeholder="请选择" class="select_width" >
  30. <el-option :label="number" :value="number" v-for="number in questionRange"></el-option>
  31. </el-select>
  32. </div>
  33. <div class="item_search">
  34. <div class="search_name">题目类型:</div>
  35. <el-select v-model="queryParams.questionType" placeholder="请选择" class="select_width" >
  36. <el-option label="单选题" :value="1"></el-option>
  37. <el-option label="多选题" :value="2"></el-option>
  38. </el-select>
  39. </div>
  40. <div class="item_search">
  41. <div class="search_name">每题满分:</div>
  42. <el-input v-model="queryParams.fullMark" placeholder="请输入" maxlength="3" @input="(val: any) => onScoreInput(queryParams, 'fullMark', val)" class="select_width" ></el-input>
  43. </div>
  44. <div class="item_search">
  45. <div class="search_name">分组:</div>
  46. <el-input v-model="queryParams.groupName" placeholder="请输入" class="select_width" ></el-input>
  47. </div>
  48. <div class="item_search" v-if="queryParams.questionType == 2" >
  49. (给分规则:少选得<el-input v-model="shaoxuan" style="width: 50px;" maxlength="4" @change="ScoreYanzheng()"></el-input>分
  50. )
  51. </div>
  52. <div class="item_search">
  53. <el-button type="primary" style="width: 68px;" @click="EnterSetQuestion()">确定</el-button>
  54. </div>
  55. <div class="delete_all">
  56. <div class="delete_btn" @click="DeleteAll()">全部删除</div>
  57. </div>
  58. </div>
  59. <div class="page_jg_16"></div>
  60. <div class="table_42">
  61. <el-table :data="tableData" v-loading="tableLoading" element-loading-text="加载中……" element-loading-spinner="el-icon-loading" element-loading-background="#ffffff" style="width: 100%" :height="tableHeight">
  62. <el-table-column prop="questionName" label="题目名称" width="100">
  63. </el-table-column>
  64. <!-- 题目类型 questionType 1 单校 2多选 -->
  65. <el-table-column prop="questionType" label="题目类型" width="120">
  66. <template v-slot="scope">
  67. <el-select v-model="scope.row.questionType" style="width: calc(100% - 32px);" @change="TypeChange(scope.row)">
  68. <el-option :value="1" label="单选题"></el-option>
  69. <el-option :value="2" label="多选题"></el-option>
  70. </el-select>
  71. </template>
  72. </el-table-column>
  73. <el-table-column prop="date" label="标注答案" width="242">
  74. <template v-slot="scope">
  75. <div class="answer_input">
  76. <!-- 单选题 -->
  77. <div class="single_selected" v-if="scope.row.questionType == 1">
  78. <el-button v-for="(item,index) in scope.row.optionsSize" :type="scope.row.standardAnswer === answerList[index] ? 'primary': ''" class="selected_button" @click="SingleClick(scope.row,answerList[index])">{{answerList[index]}}</el-button>
  79. </div>
  80. <!-- 多选 -->
  81. <div class="single_selected" v-if="scope.row.questionType == 2">
  82. <el-button v-for="(item,index) in scope.row.optionsSize" :type="scope.row.standardAnswer?.includes(answerList[index]) ? 'primary': ''" class="selected_button" @click="MultiClick(answerList[index],scope.row)">{{answerList[index]}}</el-button>
  83. </div>
  84. <!-- 判断题 -->
  85. <div class="single_selected" v-if="scope.row.questionType == 3">
  86. <el-button v-for="(item,index) in 2" :type="scope.row.standardAnswer === tflist[index] ? 'primary': ''" class="selected_button" @click="SingleClick(tflist[index],scope.row)">{{tflist[index]}}</el-button>
  87. </div>
  88. </div>
  89. </template>
  90. </el-table-column>
  91. <el-table-column prop="fullScore" label="满分" width="120">
  92. <template v-slot="scope">
  93. <div class="full_mark_input">
  94. <el-input v-model="scope.row.fullScore" maxlength="3" @input="(val: any) => onScoreInput(scope.row, 'fullScore', val)"></el-input>
  95. </div>
  96. </template>
  97. </el-table-column>
  98. <el-table-column prop="name" label="多选题给分分布">
  99. <template v-slot="scope">
  100. <div v-if="scope.row.questionType == '1' || scope.row.questionType == '3'">
  101. {{ '-' }}
  102. </div>
  103. <div v-if="scope.row.questionType == '2'">
  104. <div v-if="scope.row.multipleRule == '1'">
  105. 全对得满分
  106. <span>,少选得{{ scope.row.multipleRuleDesc || 0 }}分,选错不得分</span>
  107. </div>
  108. <div v-if="scope.row.multipleRule == '2'">
  109. 全对得满分,选对1个得{{ scope.row.multipleRuleDesc?.split(',')[0] }}分
  110. <span v-if="scope.row.multipleRuleDesc && scope.row.standardAnswer.split(',').length > 2">,选对2个得{{ scope.row.multipleRuleDesc?.split(',')[1] }}分</span>
  111. <span v-if="scope.row.multipleRuleDesc && scope.row.standardAnswer.split(',').length == 4">,选对3个得{{ scope.row.multipleRuleDesc?.split(',')[2] }}分</span>
  112. </div>
  113. <div v-if="scope.row.multipleRule == '3'">
  114. {{scope.row.multipleRuleDesc}}
  115. </div>
  116. </div>
  117. </template>
  118. </el-table-column>
  119. <el-table-column prop="groupName" label="分组">
  120. <template v-slot="scope">
  121. <div class="input_width">
  122. <el-input v-model="scope.row.groupName" maxlength="3" ></el-input>
  123. </div>
  124. </template>
  125. </el-table-column>
  126. <el-table-column prop="name" label="操作" width="150">
  127. <template v-slot="scope">
  128. <div class="ele_button table_row_button">
  129. <span class="btn_editor" @click="EditorSingle(scope.row)">编辑</span>
  130. <span class="btn_delete" @click="DeleteSingle(scope.row)">删除</span>
  131. </div>
  132. </template>
  133. </el-table-column>
  134. </el-table>
  135. <div class="page_info">
  136. 共{{tableData.length}}题 总分:{{totalScore}}分
  137. </div>
  138. </div>
  139. <div class="page_button">
  140. <el-button type="primary" style="width: 200px;" @click="FinishQuestion()">完成</el-button>
  141. </div>
  142. </div>
  143. <ChooseTemplate v-model="showChooseTemplate" :currentTemplateId="currentTemplateId" @success="onTemplateChanged"></ChooseTemplate>
  144. <EditorQuestion ref="enterStandardEdit" @AnswerEnter="EditorAnswerEnter"></EditorQuestion>
  145. <ImagePreview v-model="showPaperDialog" :imageList="previewImageList" ></ImagePreview>
  146. </div>
  147. </template>
  148. <script lang="ts" setup>
  149. import { useExamStore } from '@/store/exam'
  150. import { useRouter } from 'vue-router'
  151. import { onMounted,computed ,onUnmounted,ref,nextTick } from 'vue';
  152. import ChooseTemplate from './components/chooseTemplate.vue';
  153. import EditorQuestion from './components/EditorQuestion.vue';
  154. import ImagePreview from './components/ImagePreview.vue'
  155. import { ElMessageBox,ElMessage } from 'element-plus'
  156. import { getScanSetCertificate,getScanAnswerPreview,getScanAnswerStates,getSmartQuestionList,getTemplateInfo,setQuestion,saveQuestion,deleteAllQuestion,deleteSingleQuestion,getExamProcess} from '@/api/exam'
  157. import scanCommon from '@/utils/scanCommon.ts'
  158. import { useThrottleFn } from '@vueuse/core';
  159. import { useUserStore } from '@/store/user'
  160. // 实例化 Store
  161. const examStore = useExamStore()
  162. const router = useRouter()
  163. const userStore = useUserStore()
  164. // 修改 schoolId 计算属性,直接从 store 获取
  165. const selectSchoolId = computed(() => userStore.schoolId)
  166. // 定义题目数据的接口类型
  167. interface QuestionItem {
  168. id?: string;
  169. questionName?: string;
  170. questionType?: number; // 1:单选, 2:多选, 3:判断
  171. standardAnswer?: string;
  172. fullScore?: string | number;
  173. groupName?: string;
  174. optionsSize?: number;
  175. multipleRule?: string;
  176. multipleRuleDesc?: string;
  177. [key: string]: any; // 允许其他动态属性
  178. }
  179. const queryParams= ref({
  180. start:'',
  181. end:'',
  182. questionType:null,
  183. fullMark:'',
  184. groupName:'',//分组名称
  185. })
  186. //多选题「少选得分」(questionType==2 时使用),与模板 v-model="shaoxuan" 绑定
  187. const shaoxuan = ref('')//批量设置多选题给分分布的功能
  188. const scanText=ref('开始扫描');
  189. const templateQuestionCount=ref(0);//模板题目总数量
  190. const templateQuestionStart=ref(1);//模板题目起始编号
  191. const templateQuestionEnd=ref(0);//模板题目结束编号
  192. const currentTemplateId=ref(0);//当前模板ID
  193. const currentTemplateName=ref('');//当前模板名称
  194. const showChooseTemplate=ref(false);// 弹出选择模板的弹窗
  195. const tableData = ref<QuestionItem[]>([])
  196. const tableLoading = ref(false)//表格loading状态
  197. const enterStandardEdit=ref<HTMLElement>();//标准答案编辑组件实例
  198. const showPaperDialog=ref(false);//答案预览弹窗
  199. const previewImageList=ref<any[]>([]);//答案预览图片列表
  200. const previewAnswerData=ref<any>({});//答案预览数据
  201. const tableHeight = ref(400)// 表格高度
  202. const answerList=['A','B','C','D','E','F','G','H','I','J','K']//答案列表
  203. const tflist=['T','F']//判断题列表
  204. let uploadUrl=`https://www.k12100.com/teaching/api/v1/ai_exam_scan/upload_multi_img`;//图片上传地址
  205. let updateCountUrl=`https://www.k12100.com/teaching/api/v1/ai_exam_scan/record_batch_scan_number`;
  206. //扫描答案
  207. const scanLoading=ref(false);//开始扫描答案
  208. let skipCertificateNextScan = false;//未检测到纸张后,下次点击开始扫描时跳过后端证书接口,直接下发扫描仪指令
  209. const examSubjectCode=ref('');//扫描答案的科目编号
  210. const showScanAnswer=ref(false);//扫描答案预览
  211. const imageFilesList=ref<any[]>([]);//扫描答案的图片列表
  212. const scanAnswerLoading=ref(false);//确定扫描答案loading
  213. const scanClientConnected=ref(false);//扫描客户端的连接状态
  214. // 考试科目 ID
  215. const examSubjectId = computed(() => {
  216. return examStore.currentExam?.id
  217. })//计算属性
  218. const templateId=computed(() => {
  219. return examStore.currentExam?.templateId
  220. })//计算属性
  221. // 题目编号可选范围(从接口返回的 start 到 end)
  222. const questionRange=computed(() => {
  223. const start = Number(templateQuestionStart.value) || 1
  224. const end = Number(templateQuestionEnd.value) || 0
  225. if (end < start) return []
  226. const list: number[] = []
  227. for (let i = start; i <= end; i++) {
  228. list.push(i)
  229. }
  230. return list
  231. })
  232. const totalScore=computed(() => {
  233. if (!tableData.value || tableData.value.length === 0) {
  234. return 0;
  235. }
  236. // 累加所有题目的 fullScore,防止值为空或非数字时出错
  237. const sum = tableData.value.reduce((total, item) => {
  238. // 确保 fullScore 是有效数字,如果是空字符串或 null 则视为 0
  239. const score = Number(item.fullScore);
  240. return total + (isNaN(score) ? 0 : score);
  241. }, 0);
  242. return sum;
  243. })
  244. // 通用分数输入处理函数
  245. // target: 需要更新的目标对象 (如 scope.row 或 queryParams)
  246. // key: 目标对象中的属性名 (如 'fullScore' 或 'fullMark')
  247. // // val: 当前输入的值
  248. const onScoreInput = (target: any, key: string, val: any) => {
  249. // 1. 确保 val 是字符串
  250. let strVal = String(val || '');
  251. // 2. 正则替换:只保留数字和小数点
  252. strVal = strVal.replace(/[^\d.]/g, '');
  253. // 3. 逻辑:只允许一个小数点
  254. const parts = strVal.split('.');
  255. if (parts.length > 2) {
  256. strVal = parts[0] + '.' + parts.slice(1).join('');
  257. }
  258. // 4. 逻辑:限制总长度不超过3
  259. if (strVal.length > 3) {
  260. strVal = strVal.slice(0, 3);
  261. }
  262. // 5. 逻辑:如果不以数字开头且不为空,防止单独输入 '.'
  263. // 注意:如果允许 "0." 这种输入,则不需要此步,但通常 "0." 后面还得跟数字,这里暂时保留防止非法状态
  264. if (strVal === '.') {
  265. strVal = '';
  266. }
  267. // 6. 关键步骤:直接修改目标对象的属性
  268. if (target && key) {
  269. target[key] = strVal;
  270. }
  271. };
  272. // 组件顶层分数输入 ref 映射:当 obj 传 null 时,按 prop 名写入对应的 ref
  273. const scoreRefMap: Record<string, any> = { shaoxuan }
  274. // 少选得分校验(@change 触发):不能超过每题满分
  275. const ScoreYanzheng = () => {
  276. const fullMark = parseFloat(String(queryParams.value.fullMark ?? ''))
  277. const score = parseFloat(String(shaoxuan.value ?? ''))
  278. if (!isNaN(score) && !isNaN(fullMark) && score >= fullMark) {
  279. ElMessage.error('少选得分必须小于每题满分')
  280. shaoxuan.value = ''
  281. }
  282. }
  283. //打开选择模板弹窗
  284. const OpenChooseTemplate=()=>{
  285. showChooseTemplate.value=true;
  286. }
  287. //模板更换成功回调
  288. const onTemplateChanged=(templateData: any)=>{
  289. console.log('模板更换成功', templateData)
  290. currentTemplateId.value = templateData.id
  291. currentTemplateName.value = templateData.templateName
  292. // 更新 store 中的 templateId 并持久化到 localStorage
  293. examStore.setExamInfo({ ...examStore.currentExam, templateId: templateData.id })
  294. // 重新获取模板信息(含题目起止范围)并刷新试题列表
  295. GetTemplateInfo()
  296. GetSmartQuestionList()
  297. }
  298. //切换题目类型
  299. const TypeChange=(item:any)=>{
  300. console.log("打印题目类型",item)
  301. }
  302. //确定扫描答案
  303. const EnterSubmit=()=>{
  304. }
  305. //单选点击事件
  306. const SingleClick=(item:any,answer:any)=>{
  307. console.log("打印单选点击",item,answer)
  308. item.standardAnswer = answer;
  309. console.log(item);//打印当前标准客户题题列表
  310. }
  311. // 多选点击事件
  312. const MultiClick=(answer:any,item:any)=>{
  313. console.log("打印多选点击",answer,item)
  314. // console.log("打印当前questionTable",this.questionTable);
  315. if(item.standardAnswer==undefined)
  316. {
  317. item.standardAnswer='';
  318. }
  319. // let answerlist=item.standardAnswer==''?[]:item.standardAnswer?.split(",");
  320. let answerlist = item.standardAnswer === '' ? [] : (item.standardAnswer || '').replace(/\s+/g, '').split(',').flatMap(item => item === '' ? [] : item.split(''));
  321. if (!answerlist?.includes(answer))
  322. {
  323. answerlist?.push(answer)
  324. }
  325. else
  326. {
  327. answerlist?.forEach((i,index) => {
  328. if (i === answer) {
  329. answerlist?.splice(index,1)
  330. }
  331. })
  332. }
  333. item.standardAnswer = answerlist?.join(",")
  334. // // 给当前题选择的答案赋值
  335. // for(var i=0;i<this.questionTable.length;i++)
  336. // {
  337. // if(item.id==this.questionTable[i].id)
  338. // {
  339. // this.questionTable[i].standardAnswer=item.standardAnswer;
  340. // }
  341. // }
  342. // console.log("打印当前questionTable",this.questionTable);
  343. }
  344. //删除全部 需要二次弹窗确认提示
  345. const DeleteAll=()=>{
  346. if(tableData.value.length==0)
  347. {
  348. ElMessage.error("您还没有添加题目哦!")
  349. return
  350. }
  351. console.log("打印删除全部")
  352. ElMessageBox.confirm('确认删除所有题目吗?', '提示', {
  353. confirmButtonText: '确定',
  354. cancelButtonText: '取消',
  355. type: 'warning',
  356. customClass:'page_dialog',
  357. }).then(async () => {
  358. console.log('删除考试')
  359. const param={
  360. id:examSubjectId.value,
  361. };
  362. const res = await deleteAllQuestion(param)
  363. if(res.code == 200)
  364. {
  365. ElMessage.success("删除成功!")
  366. GetExamStatus();//获取考试状态
  367. // GetSmartQuestionList();
  368. }
  369. else
  370. {
  371. ElMessage.error(res.msg)
  372. }
  373. })
  374. }
  375. //确定完成
  376. const FinishQuestion=async ()=>{
  377. console.log("打印完成")
  378. if(tableData.value.length==0)
  379. {
  380. ElMessage.error("请添加题目!")
  381. return
  382. }
  383. ElMessageBox.confirm('确认完成吗?', '提示', {
  384. confirmButtonText: '确定',
  385. cancelButtonText: '取消',
  386. type: 'warning',
  387. customClass:'page_dialog',
  388. }).then(async () => {
  389. console.log('完成',tableData.value)
  390. const res = await saveQuestion(tableData.value)
  391. if(res.code == 200)
  392. {
  393. ElMessage.success("保存成功!")
  394. GetExamStatus();//获取考试状态
  395. }
  396. else
  397. {
  398. ElMessage.error(res.msg)
  399. }
  400. })
  401. }
  402. //获取考试状态
  403. const GetExamStatus=async ()=>{
  404. const param = {
  405. examSubjectId:examSubjectId.value
  406. }
  407. getExamProcess(param).then((res: any) => {
  408. console.log('打印考试流程状态返回', res)
  409. if (res.code === 200) {
  410. const newStateStr = res.data;
  411. console.log('打印考试流程状态', newStateStr)
  412. // 替换为 Store Commit/Action
  413. examStore.setExamState(newStateStr);//设置状态数据
  414. if(newStateStr.includes(1))
  415. {
  416. router.push('/exam/scanList')
  417. }
  418. else
  419. {
  420. //未完成 不跳转
  421. GetSmartQuestionList();
  422. }
  423. }
  424. })
  425. }
  426. //确定编辑
  427. const EditorAnswerEnter=async (item:any)=>{
  428. console.log("打印确定编辑",item)
  429. let params=[];
  430. params.push(item);
  431. const res = await saveQuestion(params)
  432. if(res.code == 200)
  433. {
  434. ElMessage.success("保存成功!")
  435. GetSmartQuestionList();
  436. }
  437. else
  438. {
  439. ElMessage.error(res.msg)
  440. }
  441. }
  442. //删除单个题目 需要二次弹窗确认提示
  443. const DeleteSingle=(item:any)=>{
  444. console.log("打印删除单个",item)
  445. ElMessageBox.confirm('确认删除该题目吗?', '提示', {
  446. confirmButtonText: '确定',
  447. cancelButtonText: '取消',
  448. customClass:'page_dialog',
  449. type: 'warning'
  450. }).then(async () => {
  451. console.log('删除考试')
  452. const param={
  453. id:item.id,
  454. examSubjectId:examSubjectId.value,
  455. };
  456. const res = await deleteSingleQuestion(param)
  457. if(res.code == 200)
  458. {
  459. ElMessage.success("删除成功!")
  460. GetSmartQuestionList();
  461. }
  462. else
  463. {
  464. ElMessage.error(res.msg);
  465. }
  466. })
  467. }
  468. const ConfigRequestUrlByEnv= () =>{
  469. console.log("打印环境", import.meta.env.MODE);
  470. if (import.meta.env.MODE === 'development')
  471. {
  472. //本地开发环境:写死测试环境地址
  473. uploadUrl = `https://index.k12100.net/teaching/api/v1/ai_exam_scan/upload_multi_img`;//图片上传地址
  474. updateCountUrl = `https://index.k12100.net/teaching/api/v1/ai_exam_scan/record_batch_scan_number`;//更新扫描数量地址
  475. }
  476. else
  477. {
  478. //线上环境(测试/生产):读取配置的环境变量
  479. uploadUrl = `${import.meta.env.VITE_API_BASE_URL_API}/api/v1/ai_exam_scan/upload_multi_img`;//图片上传地址
  480. updateCountUrl = `${import.meta.env.VITE_API_BASE_URL_API}/api/v1/ai_exam_scan/record_batch_scan_number`;//更新扫描数量地址
  481. }
  482. console.log("打印上传图片地址", uploadUrl);
  483. console.log("打印更新扫描数量地址", updateCountUrl);
  484. }
  485. //编辑单个题目
  486. const EditorSingle=(item:any)=>{
  487. console.log("打印编辑单个题目",item)
  488. enterStandardEdit.value.InitData(item);
  489. }
  490. // 打开预览图片
  491. const OpenImage=()=>{
  492. let params={
  493. examSubjectId:examSubjectId.value,
  494. };
  495. getScanAnswerPreview(params).then((res:any)=>{
  496. console.log("打印预览图片返回结果",res);
  497. if(res.code==200)
  498. {
  499. previewImageList.value=[];//初始清空
  500. res.data.forEach((image:any)=>{
  501. console.log("image",image);
  502. let obj={
  503. url:image,
  504. pageNo:1,//页面页码
  505. };
  506. previewImageList.value.push(obj);
  507. })
  508. if(previewImageList.value.length>0)
  509. {
  510. showPaperDialog.value=true;
  511. }
  512. else
  513. {
  514. ElMessage.warning('您还未扫描答案哦!');
  515. }
  516. }
  517. })
  518. }
  519. // 打开扫描仪器
  520. const GotoScan = useThrottleFn(() => {
  521. // showSelectType.value = 'scan';
  522. console.log("打印打开扫描仪器",scanCommon.isClientConnected())
  523. if (!scanClientConnected.value) {
  524. ElMessage.warning('请先打开扫描客户端');
  525. return;
  526. }
  527. scanLoading.value = true;
  528. scanText.value='扫描中...';
  529. //未检测到纸张后的重试:跳过后端证书接口,直接下发扫描仪指令并恢复轮询
  530. if (skipCertificateNextScan) {
  531. skipCertificateNextScan = false;
  532. StartScan();
  533. ScheduleScanStatePolling();
  534. return;
  535. }
  536. // showScanAnswer.value=true;
  537. //先调用后端的接口
  538. const param={
  539. examSubjectId:examSubjectId.value,
  540. };
  541. getScanSetCertificate(param).then((res:any)=>{
  542. console.log("打印返回结果",res);
  543. if(res.code==200)
  544. {
  545. StartScan();
  546. }
  547. else
  548. {
  549. //后端证书接口失败:复位按钮,允许用户重新点击开始扫描
  550. scanLoading.value = false;
  551. scanText.value='开始扫描';
  552. ElMessage.error(res.msg);
  553. }
  554. })
  555. }, 5000);
  556. // 启动扫描
  557. const StartScan = () => {
  558. scanCommon.init(HandleScanResult); // 跟扫描仪插件 websocket 连接
  559. console.log("打印 VUE_APP_BASE_API", import.meta.env.VITE_APP_BASE_API); // Vue3/Vite 环境变量获取方式
  560. console.log("打印上传图片地址", uploadUrl);
  561. //开始扫描
  562. let jsonParam={
  563. examSubjectId:examSubjectId.value,
  564. batchNumber:'9999',//批次号 固定9999 代表扫描答案
  565. // sensitive: 35,//灵敏度参数 固定35
  566. settingFlag:1,//设置参数 (0-正常图片 1- 设置题目 以及答案)
  567. schoolId:selectSchoolId.value//学校id 联校单校都需要学校id
  568. };
  569. let json = {
  570. "action":"startScan",//交互指令参数
  571. "batchNumber":'9999',//批次号 这里传给客户端的批次号需要进行处理 截取批次id后5位拼接batchNumber
  572. "subjectCode":examSubjectCode.value,//科目编号
  573. "paperSchema":1,//this.paperSchema,// 单双面//页面类型 1 单面 2 双面
  574. "token":localStorage.getItem('token'),//token信息
  575. "uploadUrl":uploadUrl, // 图片上传地址
  576. "updateCountUrl":updateCountUrl,//更新扫描纸张数量接口 接口参数 批次id post 请求 id
  577. "batchId":'10000',//批次号id
  578. "dpi":150,//dpi参数 设置图片清晰度质量的
  579. "isColor":1,//是否彩色图片 手阅卡扫描彩色图片 0 黑白 1 彩色
  580. "useDriveUI":0,//startScan和scanTemplate增加useDriveUI参数,=1时会弹框,其他值或不写则不弹
  581. "jsonParam":JSON.stringify(jsonParam),// 后端使用的参数 json字符串 后端接口固定三个参数 1:jsonParam 2:seqNumber(这个批次的图片序号:从1开始) 3:file 图片文件
  582. };//正式版参数
  583. console.log("打印发送的数据",JSON.stringify(json));
  584. setTimeout(() => {
  585. scanCommon.send(JSON.stringify(json))
  586. },100)
  587. };
  588. //扫描状态轮询定时器
  589. let scanStateTimer: ReturnType<typeof setTimeout> | null = null;
  590. //停止扫描状态轮询
  591. const StopScanStatePolling=()=>{
  592. if(scanStateTimer){
  593. clearTimeout(scanStateTimer);
  594. scanStateTimer = null;
  595. }
  596. };
  597. //继续轮询:2秒后再次调用获取扫描答案状态
  598. const ScheduleScanStatePolling=()=>{
  599. StopScanStatePolling();//先清除已有定时器,避免重复轮询
  600. scanStateTimer = setTimeout(()=>{
  601. GetScanAnswerStates();
  602. },2000);
  603. };
  604. //获取扫描答案状态
  605. // 轮询查询识别是否完成,每隔2S调用一次,0-表示识别完成,1-上传中 2-识别中 3-失败,其中1和2继续保持loading 0和3可以加载题目
  606. const GetScanAnswerStates=()=>{
  607. const param={
  608. examSubjectId:examSubjectId.value,
  609. };
  610. getScanAnswerStates(param).then((res:any)=>{
  611. console.log("打印获取扫描答案状态",res);
  612. if(res.code==200)
  613. {
  614. //0-表示识别完成,1-上传中 2-识别中 3-失败,其中1和2继续保持loading 0和3可以加载题目
  615. if(res.data==0)
  616. {
  617. //识别完成:结束轮询,加载试题列表
  618. StopScanStatePolling();
  619. scanLoading.value = false;
  620. scanText.value='开始扫描';
  621. GetSmartQuestionList();//获取试题列表
  622. // showScanAnswer.value = true;
  623. }
  624. else if(res.data==1)
  625. {
  626. //上传中:保持loading,2秒后继续轮询
  627. scanLoading.value = true;
  628. scanText.value='上传中...';
  629. ScheduleScanStatePolling();
  630. }
  631. else if(res.data==2)
  632. {
  633. //识别中:保持loading,2秒后继续轮询
  634. scanLoading.value = true;
  635. scanText.value='识别中...';
  636. ScheduleScanStatePolling();
  637. }
  638. else if(res.data==3)
  639. {
  640. //扫描失败:结束轮询,加载试题列表
  641. StopScanStatePolling();
  642. scanLoading.value = false;
  643. scanText.value='扫描失败';
  644. ElMessage.error('扫描失败');
  645. GetSmartQuestionList();//获取试题列表
  646. }
  647. }
  648. else
  649. {
  650. //接口异常:结束轮询,避免无限请求
  651. StopScanStatePolling();
  652. scanLoading.value = false;
  653. ElMessage.error(res.msg);
  654. }
  655. })
  656. .catch(()=>{
  657. //请求异常:结束轮询,避免无限请求
  658. StopScanStatePolling();
  659. scanLoading.value = false;
  660. })
  661. };
  662. //关闭扫描答案预览
  663. const CloseScanAnswer=()=>{
  664. showScanAnswer.value = false;
  665. }
  666. // 4. 计算高度的函数
  667. const CalculateTableHeight = () => {
  668. // 获取父容器的高度
  669. const containerHeight = window.innerHeight
  670. // 减去其他固定元素的高度(需要根据实际布局调整这些数值)
  671. // 例如:header(64px) + 间距(20px) + 标题区域(约50px) + 底部按钮区域(约60px) + 分页信息(56px)
  672. // 建议通过浏览器控制台检查实际占用高度
  673. const offset = 320 // 这是一个预估值,请根据实际 CSS 调整
  674. tableHeight.value = containerHeight - offset
  675. }
  676. //获取模板信息
  677. const GetTemplateInfo=()=>{
  678. const params={
  679. templateId:currentTemplateId.value,
  680. };
  681. getTemplateInfo(params).then((res) => {
  682. console.log("打印获取模板信息结果",res)
  683. if(res.code==200)
  684. {
  685. templateQuestionCount.value=res.data.questionEnd;//题目数量
  686. templateQuestionStart.value=res.data.questionStart;//题目起始编号
  687. templateQuestionEnd.value=res.data.questionEnd;//题目结束编号
  688. queryParams.value.end=res.data.questionEnd;//题目结束
  689. queryParams.value.start=res.data.questionStart;//题目开始
  690. currentTemplateName.value=res.data.templateName;//当前模板名称
  691. }
  692. })
  693. }
  694. //获取试题列表
  695. const GetSmartQuestionList=()=>{
  696. const params={
  697. examSubjectId: examSubjectId.value,
  698. };
  699. tableLoading.value = true;//开启表格loading
  700. getSmartQuestionList(params).then((res) => {
  701. console.log("打印获取试题结构列表结果",res)
  702. if(res.code==200)
  703. {
  704. // tableData.value=res.data;
  705. // 增加空值保护,确保 tableData 始终是数组
  706. tableData.value = Array.isArray(res.data) ? res.data : [];
  707. let result={};
  708. for (const item of tableData.value) {
  709. // name转数字作为key
  710. const key = (item.questionName);
  711. result[key] = item.standardAnswer;
  712. }
  713. previewAnswerData.value=result;
  714. console.log("打印转换后的数据",result)
  715. }
  716. }).finally(()=>{
  717. tableLoading.value = false;//关闭表格loading
  718. })
  719. }
  720. //确定设置题目
  721. const EnterSetQuestion=()=>{
  722. console.log("打印提交参数",queryParams.value)
  723. // 添加判断:end 不能小于 start
  724. if (queryParams.value.end < queryParams.value.start) {
  725. ElMessage.error('结束题号不能小于起始题号');
  726. return;
  727. }
  728. const params={
  729. examSubjectId: examSubjectId.value,//考试科目id
  730. questionMin: queryParams.value.start,//起始题号
  731. questionMax: queryParams.value.end,//结束题号
  732. questionType: queryParams.value.questionType,
  733. fullScore: queryParams.value.fullMark,
  734. groupName: queryParams.value.groupName,
  735. multipleRule: queryParams.value.questionType==2?'1':null,
  736. multipleRuleDesc: shaoxuan.value,
  737. };
  738. setQuestion(params).then((res) => {
  739. console.log("打印设置题目结果",res)
  740. if(res.code==200)
  741. {
  742. //设置成功 更新列表
  743. GetSmartQuestionList();
  744. }
  745. })
  746. }
  747. //扫描结果回调处理函数
  748. const HandleScanResult=(res:any)=>{
  749. console.log("打印扫描返回的res", res);
  750. //未检测到纸张或卡纸(code 502):停止轮询,复位按钮,允许用户重新点击开始扫描(下次直接下发扫描仪指令,不再调后端接口)
  751. if (res.code === 502) {
  752. StopScanStatePolling();
  753. scanLoading.value = false;
  754. scanText.value='开始扫描';
  755. skipCertificateNextScan = true;
  756. ElMessage.error(res.msg || '未检测到纸张或卡纸');
  757. return;
  758. }
  759. if (res.action === 'uploadFinish')
  760. {
  761. //扫描仪返回上传结束 开始启动轮询调用状态接口
  762. scanText.value='上传中...';
  763. ScheduleScanStatePolling();
  764. }
  765. }
  766. onMounted(() => {
  767. if (!examStore.currentExam) {
  768. console.warn('当前没有选中的考试信息')
  769. // 可选:如果没有数据,可以重定向回列表页或提示用户
  770. }
  771. scanCommon.init(HandleScanResult);
  772. scanCommon.watchConnection((isOnline, clientVersion) => {
  773. scanClientConnected.value = isOnline;
  774. // console.log('扫描客户端连接状态:', isOnline, '版本号:', clientVersion);
  775. });
  776. currentTemplateId.value=examStore.currentExam.templateId;
  777. ConfigRequestUrlByEnv();//配置请求地址
  778. GetScanAnswerStates();//获取扫描答案状态
  779. // GetSmartQuestionList();
  780. GetTemplateInfo();
  781. // 初始化计算
  782. nextTick(() => {
  783. CalculateTableHeight()
  784. })
  785. window.addEventListener('resize', CalculateTableHeight)
  786. })
  787. onUnmounted(() => {
  788. // 移除监听,防止内存泄漏
  789. window.removeEventListener('resize', CalculateTableHeight)
  790. // 停止扫描状态轮询,防止组件销毁后继续请求
  791. StopScanStatePolling();
  792. })
  793. </script>
  794. <style lang="scss" scoped>
  795. .detail_main
  796. {
  797. height: 100vh; /* 或 100% */
  798. display: flex;
  799. flex-direction: column;
  800. }
  801. .question_header
  802. {
  803. width: 100%;
  804. height: 64px;
  805. line-height: 64px;
  806. background: #FFFFFF;
  807. border-radius: 10px 10px 10px 10px;
  808. padding: 0 20px;
  809. box-sizing: border-box;
  810. display: flex;
  811. justify-content: space-between;
  812. .choose_template
  813. {
  814. font-weight: 500;
  815. font-size: 16px;
  816. color: #333333;
  817. .choose_btn
  818. {
  819. font-weight: 500;
  820. font-size: 16px;
  821. color: #2E64FA;
  822. margin-left: 10px;
  823. cursor: pointer;
  824. }
  825. }
  826. }
  827. .item_step
  828. {
  829. width: 20px;
  830. height: 20px;
  831. background: #2E64FA;
  832. border-radius: 50%;
  833. font-weight: 500;
  834. font-size: 14px;
  835. color: #FFFFFF;
  836. text-align: center;
  837. line-height: 20px;
  838. }
  839. .item_search
  840. {
  841. display: flex;
  842. align-items: center;
  843. margin-right: 10px;
  844. .search_name
  845. {
  846. width: auto;
  847. font-weight: 400;
  848. font-size: 16px;
  849. color: #666666;
  850. }
  851. .select_width
  852. {
  853. margin-left: 5px;
  854. margin-right: 5px;
  855. width: 90px;
  856. }
  857. }
  858. .item_title
  859. {
  860. font-weight: 500;
  861. font-size: 16px;
  862. color: #333333;
  863. display: flex;
  864. justify-content: flex-start;
  865. align-items: center;
  866. gap: 4px;
  867. position: relative;
  868. .preview_btn
  869. {
  870. font-weight: 500;
  871. font-size: 16px;
  872. color: #2E64FA;
  873. cursor: pointer;
  874. margin-left: 10px;
  875. }
  876. .delete_all
  877. {
  878. position: absolute;
  879. right: 0;
  880. top: 0;
  881. width: 100px;
  882. height: 36px;
  883. display: flex;
  884. justify-content: flex-end;
  885. align-items: center;
  886. .delete_btn
  887. {
  888. width: 88px;
  889. height: 34px;
  890. line-height: 34px;
  891. text-align: center;
  892. border-radius: 4px 4px 4px 4px;
  893. border: 1px solid #F56C6C;
  894. font-weight: 500;
  895. font-size: 14px;
  896. color: #F56C6C;
  897. cursor: pointer;
  898. }
  899. }
  900. }
  901. .page_info
  902. {
  903. font-weight: 500;
  904. font-size: 16px;
  905. color: #333333;
  906. text-align: right;
  907. height: 56px;
  908. line-height: 56px;
  909. }
  910. .page_button
  911. {
  912. text-align: center;
  913. }
  914. //标准答案录入
  915. .answer_input {
  916. width: 100%;
  917. padding-top: 8px;
  918. padding-bottom: 8px;
  919. margin: auto;
  920. // background-color: red;
  921. display: flex;
  922. justify-content: flex-start;
  923. flex-wrap: wrap;
  924. height: auto;
  925. .single_selected {
  926. width: auto;
  927. margin: auto;
  928. display: flex;
  929. justify-content: flex-start;
  930. flex-wrap: wrap;
  931. gap: 8px;
  932. .selected_button {
  933. width: 32px;
  934. height: 32px;
  935. display: flex;
  936. justify-content: center;
  937. align-items: center;
  938. margin-bottom: 5px;
  939. padding: 0 !important;
  940. border-radius: 50%;
  941. margin: 0;
  942. }
  943. }
  944. }
  945. </style>