Merge remote-tracking branch 'origin/feature/feature-20250331-h5' into feature/feature-20250331-h5
This commit is contained in:
@@ -1,19 +1,14 @@
|
|||||||
<template>
|
<template>
|
||||||
<choice
|
<choice v-model:answer="choiceValue" :element="question" :index="answerIndex" :is-preview="true"
|
||||||
v-model:answer="choiceValue"
|
:errorMessage="question.error" />
|
||||||
:element="question"
|
|
||||||
:index="answerIndex"
|
|
||||||
:is-preview="true"
|
|
||||||
:errorMessage="question.error"
|
|
||||||
/>
|
|
||||||
</template>
|
</template>
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { watch, ref, computed } from 'vue';
|
import { watch, ref, computed } from 'vue';
|
||||||
import Choice from '@/views/Design/components/Questions/Choice.vue';
|
import Choice from '@/views/Design/components/Questions/Choice.vue';
|
||||||
|
|
||||||
type answerType = {
|
type answerType = {
|
||||||
options: any[];
|
options?: any[];
|
||||||
value: number;
|
value: { [key: number]: number };
|
||||||
};
|
};
|
||||||
// // 预览新增 v-model
|
// // 预览新增 v-model
|
||||||
const answer = defineModel<answerType>('answer', { default: undefined });
|
const answer = defineModel<answerType>('answer', { default: undefined });
|
||||||
@@ -22,24 +17,45 @@ const answerIndex = computed(() => question.value.title);
|
|||||||
const question = defineModel<question>('question', { default: { config: { is_required: false } } });
|
const question = defineModel<question>('question', { default: { config: { is_required: false } } });
|
||||||
const list = defineModel<questionsList[]>('list', { default: [[]] });
|
const list = defineModel<questionsList[]>('list', { default: [[]] });
|
||||||
|
|
||||||
const choiceValue = ref<string>((answer.value?.value - 1).toString() ?? '0');
|
const choiceValue = ref<string>('0');
|
||||||
|
|
||||||
// 初始化数据,因为 preview 的数据源和 element 的数据源不相同, 所以需要配置一遍数据
|
// 初始化数据,因为 preview 的数据源和 element 的数据源不相同, 所以需要配置一遍数据
|
||||||
initData();
|
initData();
|
||||||
function initData() {
|
function initData () {
|
||||||
question.value.options = list.value;
|
question.value.options = list.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// // 预览新增 emit ['changeAnswer', 'previous', 'next']
|
// // 预览新增 emit ['changeAnswer', 'previous', 'next']
|
||||||
const emit = defineEmits(['update:element', 'changeAnswer', 'previous', 'next', 'update:element']);
|
const emit = defineEmits(['update:element', 'changeAnswer', 'previous', 'next', 'update:element']);
|
||||||
|
|
||||||
|
if (answer.value) {
|
||||||
|
parseAnswer();
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* 如果再次加载组件, 组件内已经有 answer. 那么开始解析答案
|
||||||
|
*/
|
||||||
|
function parseAnswer () {
|
||||||
|
for (const key in answer.value.value) {
|
||||||
|
choiceValue.value = (key);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 自动生成答案,伴随 answer 的变化
|
||||||
|
*/
|
||||||
watch(
|
watch(
|
||||||
() => choiceValue.value,
|
() => choiceValue.value,
|
||||||
() => {
|
() => {
|
||||||
|
console.log(`choiceValue`, choiceValue.value);
|
||||||
const res = {
|
const res = {
|
||||||
options: list.value.flatMap((group) => group.options || []),
|
// options: list.value.flatMap((group) => group.options || []),
|
||||||
value: Number(choiceValue.value) + 1
|
value: {
|
||||||
|
[choiceValue.value]: 1
|
||||||
|
}
|
||||||
|
// value: choiceValue.value
|
||||||
};
|
};
|
||||||
answer.value = res;
|
answer.value = res;
|
||||||
|
console.log(`answer`, res);
|
||||||
emit('changeAnswer', res);
|
emit('changeAnswer', res);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ function optionHandle(questionIndex) {
|
|||||||
classOptAndrelateList.push(...l.options);
|
classOptAndrelateList.push(...l.options);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
// 选项列表
|
||||||
const optList = optAndrelateList.map((opt, oIndex) => {
|
const optList = optAndrelateList.map((opt, oIndex) => {
|
||||||
opt.option_index = oIndex + 1;
|
opt.option_index = oIndex + 1;
|
||||||
return opt;
|
return opt;
|
||||||
@@ -93,14 +94,43 @@ function quesHandle(answer, logChild) {
|
|||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
const { optAndrelateList } = optionHandle(logChild.question_index);
|
const { optAndrelateList } = optionHandle(logChild.question_index);
|
||||||
|
// 旧的错误代码,处理 answer 有问题. pc 端夹带私货
|
||||||
|
// const postion = optAndrelateList.find((opt) => opt.option_key === key).option_index;
|
||||||
// 拿到答题时选中的选项位置
|
// 拿到答题时选中的选项位置
|
||||||
const postion = optAndrelateList.find((opt) => opt.option_key === key).option_index;
|
const postion = optAndrelateList.find((opt) => {
|
||||||
|
/**
|
||||||
|
* todo: 旧的答案配置
|
||||||
|
* key : value。 value: 答案的索引 + 1.
|
||||||
|
* 答案格式 {
|
||||||
|
* answer: {
|
||||||
|
* value: 1,
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
// return opt.option_key === answer[key];
|
||||||
|
/**
|
||||||
|
* todo: 新的答案配置
|
||||||
|
* key : 答案的索引 + 1. value: 1
|
||||||
|
* 答案格式 {
|
||||||
|
* answer: {
|
||||||
|
* '1': 1
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
// 遍历答案的key值
|
||||||
|
for (const _key in answer[key]) {
|
||||||
|
// _key 选中的答案
|
||||||
|
if (Number(_key) === Number(opt.option_key)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).option_index;
|
||||||
// 拿到配置的逻辑里选项的位置
|
// 拿到配置的逻辑里选项的位置
|
||||||
const logPostion = optAndrelateList.find(
|
const logPostion = optAndrelateList.find(
|
||||||
(opt) => opt.option_key === logChild.option_index
|
(opt) => opt.option_key === logChild.option_index
|
||||||
).option_index;
|
).option_index;
|
||||||
// eslint-disable-next-line no-eval
|
// eslint-disable-next-line no-eval
|
||||||
status = eval(`${postion}${operatorHandle(logChild.operator)}${logPostion}`);
|
status = eval(`'${postion}'${operatorHandle(logChild.operator)}'${logPostion}'`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return status;
|
return status;
|
||||||
|
|||||||
Reference in New Issue
Block a user