liuyu
4 小时以前 82a74ba0402ab546ba524d23ce62198c4d00d2f7
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
<script lang="ts" setup>
import type { ActionItem, BasicColumn } from '@jnpf/ui/vxeTable';
 
import type { QuestionBankOption, QuestionEntity } 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 { Modal } from 'ant-design-vue';
 
import { deleteQuestion, getQuestionBanks, getQuestionList } from '#/api/x/tms/question';
 
import {
  QUESTION_TYPE_OPTIONS,
  STATUS_OPTIONS,
  labelOfStatus,
  labelOfType,
} from './constants';
 
defineOptions({ name: 'TmsQuestionList' });
 
const router = useRouter();
const { createMessage } = useMessage();
 
const banks = ref<QuestionBankOption[]>([]);
 
const columns: BasicColumn[] = [
  { title: '编号', dataIndex: 'questionNo', width: 90 },
  { title: '题库', dataIndex: 'bankName', minWidth: 220 },
  {
    title: '类型',
    dataIndex: 'questionType',
    width: 90,
    customRender: ({ record }) => labelOfType((record as QuestionEntity).questionType),
  },
  {
    title: '题干',
    dataIndex: 'stem',
    minWidth: 280,
    customRender: ({ record }) => stripHtml((record as QuestionEntity).stem),
  },
  { title: '创建时间', dataIndex: 'creatorTime', width: 170 },
  { title: '管理员', dataIndex: 'adminUserName', width: 100 },
  {
    title: '状态',
    dataIndex: 'bizStatus',
    width: 90,
    align: 'center',
    slots: { default: 'bizStatus' },
  },
];
 
const [registerTable, { reload, getForm }] = useVxeTable({
  api: fetchList,
  columns,
  immediate: true,
  rowKey: 'id',
  useSearchForm: true,
  formConfig: {
    baseColProps: { span: 6 },
    compact: true,
    showAdvancedButton: true,
    autoAdvancedLine: 1,
    schemas: [
      {
        field: 'bankId',
        label: '题库',
        component: 'Select',
        componentProps: {
          allowClear: true,
          showSearch: true,
          placeholder: '请选择题库',
          options: [],
        },
      },
      {
        field: 'questionType',
        label: '题型',
        component: 'Select',
        componentProps: {
          allowClear: true,
          showSearch: true,
          placeholder: '请选择题型',
          options: QUESTION_TYPE_OPTIONS,
        },
      },
      {
        field: 'bizStatus',
        label: '状态',
        component: 'Select',
        componentProps: {
          allowClear: true,
          showSearch: true,
          placeholder: '请选择状态',
          options: STATUS_OPTIONS.filter((x) => x.id !== 'invalid'),
        },
      },
      {
        field: 'adminUserId',
        label: '管理员',
        component: 'UserSelect',
        componentProps: {
          placeholder: '请选择管理员',
        },
      },
      {
        field: 'keyword',
        label: '关键词',
        component: 'Input',
        componentProps: { placeholder: '请输入关键词', submitOnPressEnter: true },
      },
    ],
  },
  actionColumn: {
    width: 100,
    title: '操作',
    dataIndex: 'action',
    fixed: 'right',
  },
});
 
onMounted(async () => {
  banks.value = (await getQuestionBanks()) || [];
  getForm()?.updateSchema?.({
    field: 'bankId',
    componentProps: {
      allowClear: true,
      showSearch: true,
      placeholder: '请选择题库',
      options: banks.value,
    },
  });
});
 
async function fetchList(params: Record<string, any>) {
  return { data: await getQuestionList(params) };
}
 
function stripHtml(html?: string) {
  if (!html) return '';
  const text = html.replace(/<[^>]+>/g, '').replace(/&nbsp;/g, ' ').trim();
  return text.length > 80 ? `${text.slice(0, 80)}…` : text;
}
 
function statusColor(status?: string) {
  if (status === 'open') return '#52c41a';
  if (status === 'closed') return '#ff4d4f';
  return undefined;
}
 
function handleCreate() {
  router.push('/tms/question/create');
}
 
function handleEdit(record: QuestionEntity) {
  router.push(`/tms/question/edit/${record.id}`);
}
 
function handleDelete(record: QuestionEntity) {
  Modal.confirm({
    title: '确认删除',
    content: `确定删除试题「${stripHtml(record.stem) || record.questionNo}」吗?`,
    onOk: async () => {
      await deleteQuestion(record.id!);
      createMessage.success('删除成功');
      reload();
    },
  });
}
 
function getTableActions(record: QuestionEntity): ActionItem[] {
  return [
    { icon: 'icon-ym icon-ym-btn-edit', tooltip: '编辑', onClick: handleEdit.bind(null, record) },
    {
      icon: 'icon-ym icon-ym-btn-clearn',
      tooltip: '删除',
      color: 'error',
      onClick: handleDelete.bind(null, record),
    },
  ];
}
</script>
 
<template>
  <div class="jnpf-content-wrapper">
    <div class="jnpf-content-wrapper-center">
      <div class="jnpf-content-wrapper-content">
        <BasicVxeTable @register="registerTable">
          <template #tableTitle>
            <a-space>
              <a-button type="primary" pre-icon="icon-ym icon-ym-btn-add" @click="handleCreate">创建试题</a-button>
              <a-button disabled>管理试题</a-button>
            </a-space>
          </template>
          <template #bizStatus="{ record }">
            <span :style="{ color: statusColor(record.bizStatus) }">{{ labelOfStatus(record.bizStatus) }}</span>
          </template>
          <template #action="{ record }">
            <TableAction :actions="getTableActions(record)" />
          </template>
        </BasicVxeTable>
      </div>
    </div>
  </div>
</template>