| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- <template>
- <el-form
- ref="form"
- :model="innerValue"
- :rules="rules"
- label-width="100px"
- class="question-form"
- :disabled="isView"
- >
- <el-form-item label="序号:" prop="sort">
- <el-input v-model="innerValue.sort" placeholder="请输入序号" />
- </el-form-item>
- <el-form-item label="试题内容:" prop="content">
- <el-input
- v-model="innerValue.content"
- type="textarea"
- :rows="4"
- placeholder="请输入内容(500字以内)"
- />
- </el-form-item>
- <el-form-item label="试题类型:" prop="type">
- <el-select
- v-model="innerValue.type"
- placeholder="请选择试题类型"
- style="width: 160px"
- @change="handleTypeChange"
- >
- <el-option
- v-for="item in typeOptions"
- :key="item.value"
- :label="item.label"
- :value="item.value"
- />
- </el-select>
- <el-button
- type="primary"
- size="small"
- plain
- style="margin-left: 10px"
- @click="handleUploadImage"
- >上传图片</el-button
- >
- <span class="upload-tip">试题图片上传</span>
- </el-form-item>
- <el-form-item label="专业类别:" prop="category">
- <el-select
- v-model="innerValue.category"
- placeholder="请选择专业类别"
- style="width: 160px"
- >
- <el-option
- v-for="item in categoryOptions"
- :key="item.value"
- :label="item.label"
- :value="item.value"
- />
- </el-select>
- </el-form-item>
- <el-form-item label="试题分数:" prop="score">
- <el-input v-model="innerValue.score" placeholder="请输入试题分数" />
- </el-form-item>
- <!-- 选项 -->
- <el-form-item
- v-if="
- innerValue.type === '1' ||
- innerValue.type === '2' ||
- innerValue.type === '3'
- "
- label="选项:"
- prop="options"
- >
- <div class="option-list">
- <div
- v-for="(item, index) in innerValue.options"
- :key="index"
- class="option-item"
- >
- <el-radio
- v-if="innerValue.type === '1' || innerValue.type === '3'"
- v-model="innerValue.rightAnswer"
- :label="item.key"
- class="option-radio"
- />
- <el-checkbox
- v-else
- v-model="innerValue.rightAnswers"
- :label="item.key"
- class="option-checkbox"
- />
- <!-- <span class="option-key">{{ item.key }}</span> -->
- <el-input
- v-model="item.content"
- placeholder="请输入选项(100字以内)"
- class="option-input"
- />
- <el-button
- type="text"
- size="small"
- @click="handleUploadOptionImage(index)"
- >上传图片</el-button
- >
- <el-button
- v-if="innerValue.type !== '3' && innerValue.options.length > 2"
- type="text"
- class="danger-text"
- size="small"
- @click="handleDeleteOption(index)"
- >删除选项</el-button
- >
- </div>
- <el-button
- v-if="innerValue.type !== '3'"
- type="text"
- size="small"
- @click="handleAddOption"
- >+ 添加选项</el-button
- >
- </div>
- </el-form-item>
- <el-form-item label="题目详解:" prop="analysis">
- <el-input
- v-model="innerValue.analysis"
- type="textarea"
- :rows="4"
- placeholder="请输入内容(500字以内)"
- />
- </el-form-item>
- </el-form>
- </template>
- <script>
- const typeOptions = [
- { label: '单选题', value: '1' },
- { label: '多选题', value: '2' },
- { label: '判断题', value: '3' },
- { label: '问答题', value: '4' }
- ];
- const categoryOptions = [
- { label: '计算机类', value: '1' },
- { label: '安全生产', value: '2' },
- { label: '职业健康', value: '3' },
- { label: '环境保护', value: '4' }
- ];
- export default {
- props: {
- value: {
- type: Object,
- default: () => ({})
- },
- isView: {
- type: Boolean,
- default: false
- }
- },
- data() {
- return {
- typeOptions,
- categoryOptions,
- rules: {
- content: [
- { required: true, message: '请输入试题内容', trigger: 'blur' }
- ],
- type: [
- { required: true, message: '请选择试题类型', trigger: 'change' }
- ],
- category: [
- { required: true, message: '请选择专业类别', trigger: 'change' }
- ],
- score: [
- { required: true, message: '请输入试题分数', trigger: 'blur' }
- ],
- options: [
- {
- validator: (rule, value, callback) => {
- if (this.innerValue.type === '2') {
- if (!this.innerValue.rightAnswers.length) {
- callback(new Error('请选择正确答案'));
- return;
- }
- } else if (
- this.innerValue.type === '1' ||
- this.innerValue.type === '3'
- ) {
- if (!this.innerValue.rightAnswer) {
- callback(new Error('请选择正确答案'));
- return;
- }
- }
- callback();
- },
- trigger: 'change'
- }
- ]
- }
- };
- },
- computed: {
- innerValue: {
- get() {
- return this.value || {};
- },
- set(val) {
- this.$emit('input', val);
- }
- }
- },
- methods: {
- handleTypeChange(val) {
- if (val === '1' || val === '2') {
- this.innerValue.options = [
- { key: 'A', content: '' },
- { key: 'B', content: '' },
- { key: 'C', content: '' },
- { key: 'D', content: '' }
- ];
- this.innerValue.rightAnswer = '';
- this.innerValue.rightAnswers = [];
- } else if (val === '3') {
- this.innerValue.options = [
- { key: 'A', content: '正确' },
- { key: 'B', content: '错误' }
- ];
- this.innerValue.rightAnswer = '';
- this.innerValue.rightAnswers = [];
- } else if (val === '4') {
- this.innerValue.options = [];
- this.innerValue.rightAnswer = '';
- this.innerValue.rightAnswers = [];
- }
- },
- handleUploadImage() {
- // TODO: 试题图片上传
- this.$message.info('试题图片上传待接入');
- },
- handleUploadOptionImage(index) {
- // TODO: 选项图片上传
- this.$message.info('选项图片上传待接入:' + index);
- },
- handleAddOption() {
- if (this.innerValue.type === '3') {
- this.$message.warning('判断题不能添加选项');
- return;
- }
- const nextKey = String.fromCharCode(
- 65 + this.innerValue.options.length
- );
- this.innerValue.options.push({ key: nextKey, content: '' });
- },
- handleDeleteOption(index) {
- if (this.innerValue.type === '3') {
- this.$message.warning('判断题不能删除选项');
- return;
- }
- if (this.innerValue.options.length <= 2) {
- this.$message.warning('至少保留两个选项');
- return;
- }
- this.innerValue.options.splice(index, 1);
- this.innerValue.options.forEach((item, idx) => {
- item.key = String.fromCharCode(65 + idx);
- });
- if (this.innerValue.type === '1') {
- this.innerValue.rightAnswer = '';
- } else {
- this.innerValue.rightAnswers = [];
- }
- },
- validate(callback) {
- this.$refs.form.validate(callback);
- },
- clearValidate() {
- this.$refs.form && this.$refs.form.clearValidate();
- }
- }
- };
- </script>
- <style lang="scss" scoped>
- .question-form {
- padding-top: 10px;
- }
- .upload-tip {
- color: #909399;
- font-size: 12px;
- margin-left: 10px;
- }
- .option-list {
- .option-item {
- display: flex;
- align-items: center;
- margin-bottom: 10px;
- .option-radio,
- .option-checkbox {
- margin-right: 4px;
- }
- .option-key {
- margin-right: 8px;
- font-weight: bold;
- min-width: 20px;
- }
- .option-input {
- width: 260px;
- margin-right: 10px;
- }
- }
- }
- .danger-text {
- color: #f56c6c;
- }
- </style>
|