liuyu
22 小时以前 8534025c45b4736975730678b9ccf23570a75f95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<script lang="ts" setup>
import type { QuestionBankOption } from '#/views/x/tms/question/types';
import type { SelfTestPaper, SelfTestRecordItem } from './types';
 
import { onMounted, reactive, ref } from 'vue';
import { useRouter } from 'vue-router';
 
import { useMessage } from '@jnpf/hooks';
import { useTabbarStore } from '@vben/stores';
import dayjs from 'dayjs';
 
import { getSelfTestBanks, getSelfTestInfo, getSelfTestList } from '#/api/x/tms/selfTest';
import { useBaseStore } from '#/store';
import {
  TMS_DIC,
  TMS_DIC_FIELD_NAMES,
  labelOfDic,
  loadTmsDic,
  type TmsDicOpt,
} from '#/views/x/tms/shared/dic';
 
defineOptions({ name: 'TmsSelfTestRecords' });
 
const baseStore = useBaseStore();
const statusOpts = ref<TmsDicOpt[]>([]);
 
const router = useRouter();
const { createMessage } = useMessage();
const tabbarStore = useTabbarStore();
 
const banks = ref<QuestionBankOption[]>([]);
const records = ref<SelfTestRecordItem[]>([]);
const loading = ref(false);
const continueLoadingId = ref('');
const pagination = ref({ currentPage: 1, pageSize: 10, total: 0 });
 
const queryForm = reactive<{
  bankId?: string;
  testStatus?: string;
  /** jnpf-date-range 值为时间戳数组 */
  timeRange?: number[];
}>({
  bankId: undefined,
  testStatus: undefined,
  timeRange: undefined,
});
 
async function loadBanks() {
  try {
    banks.value = (await getSelfTestBanks()) || [];
  } catch {
    banks.value = [];
  }
}
 
async function loadRecords() {
  loading.value = true;
  try {
    const range = queryForm.timeRange;
    const begin = range?.[0] != null ? dayjs(range[0]).format('YYYY-MM-DD') : undefined;
    const end = range?.[1] != null ? dayjs(range[1]).format('YYYY-MM-DD') : undefined;
    const res = await getSelfTestList({
      currentPage: pagination.value.currentPage,
      pageSize: pagination.value.pageSize,
      bankId: queryForm.bankId || undefined,
      testStatus: queryForm.testStatus || undefined,
      startTimeBegin: begin ? `${begin} 00:00:00` : undefined,
      startTimeEnd: end ? `${end} 23:59:59` : undefined,
    });
    records.value = res?.list || [];
    const p = res?.pagination || {};
    pagination.value.total = Number(p.total || p.totalCount || records.value.length);
  } catch (e: any) {
    records.value = [];
    createMessage.error(e?.message || '加载检测记录失败');
  } finally {
    loading.value = false;
  }
}
 
function handleSearch() {
  pagination.value.currentPage = 1;
  loadRecords();
}
 
function handleReset() {
  queryForm.bankId = undefined;
  queryForm.testStatus = undefined;
  queryForm.timeRange = undefined;
  pagination.value.currentPage = 1;
  loadRecords();
}
 
onMounted(async () => {
  tabbarStore.renderRouteView = true;
  const examStatus = await loadTmsDic(baseStore, TMS_DIC.examStatus);
  statusOpts.value = examStatus.filter(
    (x) => x.enCode === 'submitted' || x.enCode === 'doing',
  );
  await loadBanks();
  loadRecords();
});
 
function statusLabel(status?: string) {
  return labelOfDic(statusOpts.value, status);
}
 
function goBack() {
  tabbarStore.renderRouteView = true;
  router.push('/tms/selfTest');
}
 
function handleView(record: SelfTestRecordItem) {
  if (record.testStatus !== 'submitted') {
    createMessage.warning('该检测尚未交卷,暂无答题结果可查看');
    return;
  }
  router.push(`/tms/selfTest/detail/${record.id}`);
}
 
/** 继续未交卷的检测 */
async function handleContinue(record: SelfTestRecordItem) {
  if (record.testStatus !== 'doing') return;
  continueLoadingId.value = record.id;
  try {
    const detail = await getSelfTestInfo(record.id);
    if (detail.testStatus === 'submitted') {
      createMessage.warning('该检测已交卷,请直接查看详情');
      loadRecords();
      return;
    }
    if (!detail.questions?.length) {
      createMessage.warning('未找到可继续的试题');
      return;
    }
    const paper: SelfTestPaper = {
      paperId: detail.paperId,
      bankId: detail.bankId,
      bankName: detail.bankName,
      testStatus: detail.testStatus,
      questions: detail.questions,
    };
    // 把已作答内容一并带入答题页
    const savedAnswers: Record<string, string | string[]> = {};
    detail.questions.forEach((q) => {
      if (!q.userAnswer) return;
      if (q.questionType === 'multi') {
        savedAnswers[q.id] = q.userAnswer.split(',').filter(Boolean);
      } else {
        savedAnswers[q.id] = q.userAnswer;
      }
    });
    sessionStorage.setItem('tms_self_test_paper', JSON.stringify(paper));
    sessionStorage.setItem('tms_self_test_answers', JSON.stringify(savedAnswers));
    router.push('/tms/selfTest/exam');
  } catch (e: any) {
    createMessage.error(e?.message || '继续考试失败');
  } finally {
    continueLoadingId.value = '';
  }
}
 
function onPageChange(page: number, pageSize: number) {
  pagination.value.currentPage = page;
  pagination.value.pageSize = pageSize;
  loadRecords();
}
 
/** 弹出层挂到 body,避免被 jnpf-content-wrapper overflow:hidden 裁切 */
function popupContainer() {
  return document.body;
}
</script>
 
<template>
  <div class="jnpf-content-wrapper tms-self-test-records-page">
    <div class="jnpf-content-wrapper-center tms-self-test-records-center">
      <div class="jnpf-content-wrapper-content tms-self-test-records">
        <div class="records-top">
          <div class="page-header">
            <div>
              <div class="text-base font-medium">我的检测记录</div>
              <div class="mt-1 text-gray-400 text-sm">查看历史自我检测成绩与错题。</div>
            </div>
            <a-button @click="goBack">返回自我检测</a-button>
          </div>
 
          <div class="search-bar">
            <a-form layout="inline" class="search-fields" :model="queryForm" @finish="handleSearch">
              <a-form-item label="题库">
                <jnpf-select
                  v-model:value="queryForm.bankId"
                  :options="banks"
                  allow-clear
                  show-search
                  placeholder="请选择题库"
                  class="!w-[200px]"
                />
              </a-form-item>
              <a-form-item label="状态">
                <jnpf-select
                  v-model:value="queryForm.testStatus"
                  :options="statusOpts"
                  :field-names="TMS_DIC_FIELD_NAMES"
                  allow-clear
                  placeholder="请选择状态"
                  class="!w-[140px]"
                />
              </a-form-item>
              <a-form-item label="时间范围">
                <jnpf-date-range
                  v-model:value="queryForm.timeRange"
                  allow-clear
                  format="YYYY-MM-DD"
                  class="!w-[260px]"
                  :placeholder="['开始日期', '结束日期']"
                  :get-popup-container="popupContainer"
                />
              </a-form-item>
            </a-form>
            <a-space class="search-actions">
              <a-button type="primary" :loading="loading" @click="handleSearch">查询</a-button>
              <a-button @click="handleReset">重置</a-button>
            </a-space>
          </div>
        </div>
 
        <div class="records-body">
          <a-table
            :data-source="records"
            :loading="loading"
            row-key="id"
            size="middle"
            :pagination="{
              current: pagination.currentPage,
              pageSize: pagination.pageSize,
              total: pagination.total,
              showSizeChanger: true,
              showTotal: (t: number) => `共 ${t} 条`,
              onChange: onPageChange,
            }"
            :columns="[
              { title: '题库', dataIndex: 'bankName', key: 'bankName', ellipsis: true },
              { title: '题量', dataIndex: 'totalCount', key: 'totalCount', width: 80 },
              { title: '正确', dataIndex: 'correctCount', key: 'correctCount', width: 80 },
              { title: '正确率', key: 'scoreRate', width: 100 },
              { title: '状态', key: 'testStatus', width: 100 },
              { title: '开始时间', dataIndex: 'startTime', key: 'startTime', width: 170 },
              { title: '交卷时间', dataIndex: 'submitTime', key: 'submitTime', width: 170 },
              { title: '操作', key: 'action', width: 120, fixed: 'right' },
            ]"
          >
            <template #bodyCell="{ column, record }">
              <template v-if="column.key === 'scoreRate'">
                {{ record.scoreRate != null ? `${record.scoreRate}%` : '-' }}
              </template>
              <template v-else-if="column.key === 'testStatus'">
                {{ statusLabel(record.testStatus) }}
              </template>
              <template v-else-if="column.key === 'correctCount'">
                {{ record.correctCount ?? '-' }}
              </template>
              <template v-else-if="column.key === 'action'">
                <a-button
                  v-if="record.testStatus === 'doing'"
                  type="link"
                  size="small"
                  :loading="continueLoadingId === record.id"
                  @click="handleContinue(record)"
                >
                  继续考试
                </a-button>
                <a-button
                  v-else
                  type="link"
                  size="small"
                  @click="handleView(record)"
                >
                  查看
                </a-button>
              </template>
            </template>
          </a-table>
        </div>
      </div>
    </div>
  </div>
</template>
 
<style scoped>
/* 全局 jnpf-content-wrapper* 为 overflow:hidden 且无 min-height:0,滚动必须落在内层 body */
.tms-self-test-records-page {
  min-height: 0;
}
 
.tms-self-test-records-center {
  min-height: 0 !important;
}
 
.tms-self-test-records {
  display: flex !important;
  flex-direction: column;
  flex: 1 1 0 !important;
  min-height: 0 !important;
  height: auto !important;
  overflow: hidden !important;
  background: #fff;
  padding: 0;
}
 
.records-top {
  flex-shrink: 0;
  padding: 20px 24px 0;
}
 
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #f0f0f0;
}
 
.search-bar {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 12px;
}
 
.search-fields {
  flex: 1;
  row-gap: 12px;
}
 
.search-actions {
  flex-shrink: 0;
  padding-top: 4px;
}
 
.records-body {
  flex: 1 1 0;
  min-height: 0;
  overflow-y: auto !important;
  overflow-x: hidden;
  padding: 0 24px 24px;
  -webkit-overflow-scrolling: touch;
}
 
.records-body :deep(.ant-table-wrapper) {
  overflow: visible !important;
}
</style>