刘光辉
6 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
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
<script lang="ts" setup>
import type { ActionItem, BasicColumn } from '@jnpf/ui/vxeTable';
 
import type { MyPaperListItem } from './types';
 
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';
 
import { useMessage } from '@jnpf/hooks';
import { BasicVxeTable, TableAction, useVxeTable } from '@jnpf/ui/vxeTable';
 
import { getMyPaperList, startOnlineExam } from '#/api/x/tms/onlineExam';
import { TMS_DIC_FIELD_NAMES } from '#/views/x/tms/shared/dic';
import { TMS_BTN } from '#/views/x/tms/shared/ui';
 
import {
  ATTEMPT_TYPE_OPTIONS,
  CAN_RETAKE_OPTIONS,
  MY_PAPER_STATUS_OPTIONS,
  PASS_FLAG_QUERY_OPTIONS,
  colorOfMyPaperStatus,
  colorOfPassFlag,
  labelOfMyPaperStatus,
  labelOfPassFlag,
  loadOnlineExamDics,
} from './constants';
 
import '#/views/x/tms/shared/page.css';
 
defineOptions({ name: 'TmsOnlineExam' });
 
const router = useRouter();
const { createMessage } = useMessage();
const starting = ref(false);
 
const columns: BasicColumn[] = [
  { title: '试卷名称', dataIndex: 'paperName', minWidth: 220 },
  {
    title: '考试类型',
    dataIndex: 'attemptLabel',
    width: 130,
    align: 'center',
    customRender: ({ record }) => {
      const row = record as MyPaperListItem;
      return row.attemptLabel
        || (row.attemptNo && row.attemptNo > 1 ? `补考(第${row.attemptNo - 1}次)` : '首考');
    },
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 100,
    align: 'center',
    slots: { default: 'status' },
  },
  {
    title: '考试时间',
    dataIndex: 'examTimeText',
    minWidth: 260,
    customRender: ({ record }) => (record as MyPaperListItem).examTimeText || '-',
  },
  {
    title: '卷面总分',
    dataIndex: 'totalScore',
    width: 100,
    align: 'center',
  },
  {
    title: '是否合格',
    dataIndex: 'passFlag',
    width: 100,
    align: 'center',
    slots: { default: 'passFlag' },
  },
  {
    title: '剩余补考',
    dataIndex: 'remainingRetakes',
    width: 100,
    align: 'center',
    customRender: ({ record }) => {
      const row = record as MyPaperListItem;
      if (row.retakeLimit == null) return '-';
      return `${row.remainingRetakes ?? 0} / ${row.retakeLimit}`;
    },
  },
];
 
const [registerTable, { reload, getForm }] = useVxeTable({
  api: fetchList,
  columns,
  immediate: false,
  rowKey: 'id',
  useSearchForm: true,
  formConfig: {
    baseColProps: { span: 6 },
    compact: true,
    schemas: [
      {
        field: 'keyword',
        label: '试卷名称',
        component: 'Input',
        componentProps: { placeholder: '请输入试卷名称', submitOnPressEnter: true },
      },
      {
        field: 'status',
        label: '状态',
        component: 'Select',
        componentProps: {
          allowClear: true,
          placeholder: '请选择状态',
          options: MY_PAPER_STATUS_OPTIONS.value,
          fieldNames: TMS_DIC_FIELD_NAMES,
        },
      },
      {
        field: 'passFlag',
        label: '是否合格',
        component: 'Select',
        componentProps: {
          allowClear: true,
          placeholder: '请选择',
          options: PASS_FLAG_QUERY_OPTIONS.value,
          fieldNames: TMS_DIC_FIELD_NAMES,
        },
      },
      {
        field: 'attemptType',
        label: '考试类型',
        component: 'Select',
        componentProps: {
          allowClear: true,
          placeholder: '首考/补考',
          options: ATTEMPT_TYPE_OPTIONS,
          fieldNames: TMS_DIC_FIELD_NAMES,
        },
      },
      {
        field: 'canRetake',
        label: '可补考',
        component: 'Select',
        componentProps: {
          allowClear: true,
          placeholder: '请选择',
          options: CAN_RETAKE_OPTIONS,
          fieldNames: TMS_DIC_FIELD_NAMES,
        },
      },
      {
        field: 'examTimeRange',
        label: '考试时间',
        component: 'DateRange',
        componentProps: {
          format: 'YYYY-MM-DD',
          placeholder: ['开始日期', '结束日期'],
        },
      },
    ],
  },
  actionColumn: {
    width: 200,
    title: '操作',
    dataIndex: 'action',
    fixed: 'right',
  },
});
 
onMounted(async () => {
  await loadOnlineExamDics();
  getForm()?.updateSchema?.([
    {
      field: 'status',
      componentProps: {
        allowClear: true,
        placeholder: '请选择状态',
        options: MY_PAPER_STATUS_OPTIONS.value,
        fieldNames: TMS_DIC_FIELD_NAMES,
      },
    },
    {
      field: 'passFlag',
      componentProps: {
        allowClear: true,
        placeholder: '请选择',
        options: PASS_FLAG_QUERY_OPTIONS.value,
        fieldNames: TMS_DIC_FIELD_NAMES,
      },
    },
  ]);
  reload();
});
 
async function fetchList(params: Record<string, any>) {
  const query = { ...params };
  const range = query.examTimeRange;
  if (Array.isArray(range) && range.length === 2) {
    query.examTimeFrom = formatDay(range[0]);
    query.examTimeTo = formatDay(range[1]);
  }
  delete query.examTimeRange;
  // 清空空串,避免把 '' / 'null' 传到后端
  for (const key of Object.keys(query)) {
    const v = query[key];
    if (v === '' || v === 'null' || v === undefined) {
      delete query[key];
    }
  }
  const page = await getMyPaperList(query);
  return {
    data: {
      list: Array.isArray(page?.list) ? page.list : [],
      pagination: page?.pagination || { total: 0 },
    },
  };
}
 
function formatDay(v: unknown) {
  if (v == null || v === '') return undefined;
  if (typeof v === 'number') {
    const d = new Date(v);
    if (Number.isNaN(d.getTime())) return undefined;
    const m = `${d.getMonth() + 1}`.padStart(2, '0');
    const day = `${d.getDate()}`.padStart(2, '0');
    return `${d.getFullYear()}-${m}-${day}`;
  }
  const s = String(v);
  return s.length >= 10 ? s.slice(0, 10) : s;
}
 
function handleDetail(record: MyPaperListItem) {
  router.push(`/tms/onlineExam/detail/${record.id}`);
}
 
async function handleStart(record: MyPaperListItem) {
  if (starting.value) return;
  starting.value = true;
  try {
    const paper = await startOnlineExam(record.id);
    if (paper?.autoSubmitted) {
      createMessage.warning('考试时间已到,已自动交卷');
      reload();
      return;
    }
    sessionStorage.setItem('tms_online_exam_paper', JSON.stringify(paper));
    sessionStorage.setItem('tms_online_exam_myPaperId', record.id);
    router.push('/tms/onlineExam/exam');
  } catch (e: any) {
    const msg = e?.message || '开始考试失败';
    if (String(msg).includes('自动交卷')) {
      createMessage.warning(msg);
      reload();
    } else {
      createMessage.error(msg);
    }
  } finally {
    starting.value = false;
  }
}
 
function getTableActions(record: MyPaperListItem): ActionItem[] {
  const actions: ActionItem[] = [];
  if (record.status === 'notStarted') {
    actions.push({ label: TMS_BTN.startExam, onClick: handleStart.bind(null, record) });
  } else if (record.status === 'doing') {
    actions.push({ label: TMS_BTN.continueExam, onClick: handleStart.bind(null, record) });
  } else if (record.status === 'submitted' && record.canRetake) {
    actions.push({ label: TMS_BTN.retake, onClick: handleStart.bind(null, record) });
  }
  actions.push({ label: TMS_BTN.detail, onClick: handleDetail.bind(null, record) });
  return actions;
}
</script>
 
<template>
  <div class="jnpf-content-wrapper">
    <div class="jnpf-content-wrapper-center">
      <div class="jnpf-content-wrapper-content">
        <BasicVxeTable @register="registerTable">
          <template #tableTitle>
            <div>
              <div class="tms-page-header__title">我的试卷</div>
              <div class="tms-page-header__sub">参加考试或查看成绩;可考时操作列直接开考。</div>
            </div>
          </template>
          <template #status="{ record }">
            <span :style="{ color: colorOfMyPaperStatus(record.status) }">
              {{ labelOfMyPaperStatus(record.status) }}
            </span>
          </template>
          <template #passFlag="{ record }">
            <span :style="{ color: colorOfPassFlag(record.passFlag) }">
              {{ labelOfPassFlag(record.passFlag) }}
            </span>
          </template>
          <template #action="{ record }">
            <TableAction :actions="getTableActions(record)" />
          </template>
        </BasicVxeTable>
      </div>
    </div>
  </div>
</template>