refactor(Survey): 优化问卷数据验证逻辑
- 修改逻辑验证条件,提高代码可读性- 增加对随机题组和循环题组的判断- 优化验证流程,提高整体问卷数据质量
This commit is contained in:
@@ -1,139 +1,140 @@
|
|||||||
import { getSurveysPage, deleteSurveys, saveTemplates } from '@/api/home';
|
import { getSurveysPage, deleteSurveys, saveTemplates } from '@/api/home';
|
||||||
import { ref } from 'vue';
|
import { ref } from 'vue';
|
||||||
import { showDialog, showConfirmDialog, showFailToast, showToast } from 'vant';
|
import { showDialog, showConfirmDialog, showFailToast, showToast } from 'vant';
|
||||||
import { getSurveysDetail } from '@/api/design';
|
import { getSurveysDetail } from '@/api/design';
|
||||||
import { getQuestionList } from '@/api/survey';
|
import { getQuestionList } from '@/api/survey';
|
||||||
import { questionTypeMap } from '@/utils/question/typeMapping';
|
import { questionTypeMap } from '@/utils/question/typeMapping';
|
||||||
|
|
||||||
const searchValue = ref('');
|
const searchValue = ref('');
|
||||||
const survey = ref<SurveyItem[]>([]);
|
const survey = ref<SurveyItem[]>([]);
|
||||||
const total = ref(0);
|
const total = ref(0);
|
||||||
const loading = ref(false);
|
const loading = ref(false);
|
||||||
const requestLoading = ref(false);
|
const requestLoading = ref(false);
|
||||||
const finished = ref(false);
|
const finished = ref(false);
|
||||||
const currentSurvey = ref<SurveyItem>();
|
const currentSurvey = ref<SurveyItem>();
|
||||||
|
|
||||||
const requestSingle = ref(true);
|
const requestSingle = ref(true);
|
||||||
|
|
||||||
async function fetchSingleSurvey(sn: string) {
|
async function fetchSingleSurvey(sn: string) {
|
||||||
const res = await getSurveysDetail(sn);
|
const res = await getSurveysDetail(sn);
|
||||||
// const res = await getSetting({sn})
|
// const res = await getSetting({sn})
|
||||||
// console.log(res);
|
// console.log(res);
|
||||||
if (res.data.code === 0) {
|
if (res.data.code === 0) {
|
||||||
currentSurvey.value = res.data.data;
|
currentSurvey.value = res.data.data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchSurveys(form: any) {
|
async function fetchSurveys(form: any) {
|
||||||
if (!requestSingle.value) return;
|
if (!requestSingle.value) return;
|
||||||
requestSingle.value = false;
|
requestSingle.value = false;
|
||||||
requestLoading.value = true;
|
requestLoading.value = true;
|
||||||
const params = {
|
const params = {
|
||||||
page: form.page,
|
page: form.page,
|
||||||
per_page: form.pageSize,
|
per_page: form.pageSize,
|
||||||
group_id: 0,
|
group_id: 0,
|
||||||
// project_name: searchValue.value
|
// project_name: searchValue.value
|
||||||
key_word: form.key_word
|
key_word: form.key_word
|
||||||
};
|
};
|
||||||
const res = await getSurveysPage(params);
|
const res = await getSurveysPage(params);
|
||||||
if (res.data.code === 0) {
|
if (res.data.code === 0) {
|
||||||
survey.value = survey.value.concat(res.data.data);
|
survey.value = survey.value.concat(res.data.data);
|
||||||
total.value = res.data.meta.total;
|
total.value = res.data.meta.total;
|
||||||
loading.value = false;
|
loading.value = false;
|
||||||
// 数据全部加载完成
|
// 数据全部加载完成
|
||||||
if (survey.value.length >= total.value) {
|
if (survey.value.length >= total.value) {
|
||||||
finished.value = true;
|
finished.value = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Toast()
|
// Toast()
|
||||||
}
|
}
|
||||||
requestLoading.value = false;
|
requestLoading.value = false;
|
||||||
requestSingle.value = true;
|
requestSingle.value = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function deleteItem(item: SurveyItem, form: any) {
|
function deleteItem(item: SurveyItem, form: any) {
|
||||||
showDialog({
|
showDialog({
|
||||||
title: `确认删除问卷 "${item.project_name}" ?`,
|
title: `确认删除问卷 "${item.project_name}" ?`,
|
||||||
showCancelButton: true,
|
showCancelButton: true,
|
||||||
confirmButtonColor: '#03B03C'
|
confirmButtonColor: '#03B03C'
|
||||||
})
|
})
|
||||||
.then(async () => {
|
.then(async () => {
|
||||||
const res = await deleteSurveys(item.sn);
|
const res = await deleteSurveys(item.sn);
|
||||||
if (res.data.message) {
|
if (res.data.message) {
|
||||||
showToast(res.data.message);
|
showToast(res.data.message);
|
||||||
} else {
|
} else {
|
||||||
showToast('删除成功!');
|
showToast('删除成功!');
|
||||||
}
|
}
|
||||||
form.page = 1;
|
form.page = 1;
|
||||||
clearSurveys();
|
clearSurveys();
|
||||||
await fetchSurveys(form);
|
await fetchSurveys(form);
|
||||||
})
|
})
|
||||||
.catch(() => {
|
.catch(() => {
|
||||||
// on cancel
|
// on cancel
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 保存为模板
|
// 保存为模板
|
||||||
async function saveTemplate(item: SurveyItem) {
|
async function saveTemplate(item: SurveyItem) {
|
||||||
const data = JSON.parse(JSON.stringify(item));
|
const data = JSON.parse(JSON.stringify(item));
|
||||||
|
|
||||||
// 如果没有通过校验, 弹出提示窗不进行下一步
|
// 如果没有通过校验, 弹出提示窗不进行下一步
|
||||||
if (!(await validateSurvey(data))) {
|
if (!(await validateSurvey(data))) {
|
||||||
showDialog({
|
showDialog({
|
||||||
title: '无法保存模板',
|
title: '无法保存模板',
|
||||||
message: '问卷内包含移动端暂未兼容题型/逻辑设置,请至PC端编辑后重新保存。'
|
message: '问卷内包含移动端暂未兼容题型/逻辑设置,请至PC端编辑后重新保存。'
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const res = await saveTemplates(item.sn, data);
|
const res = await saveTemplates(item.sn, data);
|
||||||
if (res.data.code === 200 || res.data.code === 201) {
|
if (res.data.code === 200 || res.data.code === 201) {
|
||||||
showConfirmDialog({
|
showConfirmDialog({
|
||||||
message: '模板保存成功,请前往更多模板页面查看',
|
message: '模板保存成功,请前往更多模板页面查看',
|
||||||
showCancelButton: false
|
showCancelButton: false
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
showFailToast(res.data);
|
showFailToast(res.data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function clearSurveys() {
|
function clearSurveys() {
|
||||||
survey.value = [];
|
survey.value = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 校验问卷是否可以保存为模板
|
* 校验问卷是否可以保存为模板
|
||||||
* @param data
|
* @param data
|
||||||
*/
|
*/
|
||||||
async function validateSurvey(survey: SurveyItem): Promise<boolean> {
|
async function validateSurvey(survey: SurveyItem): Promise<boolean> {
|
||||||
const { data } = await getQuestionList(survey.sn);
|
const { data } = await getQuestionList(survey.sn);
|
||||||
const { questions, logics } = data.data;
|
const { questions, logics } = data.data;
|
||||||
const questionValid = questions.every((question: any) => {
|
const questionValid = questions.every((question: any) => {
|
||||||
if (!questionTypeMap.has(question.question_type)) {
|
if (!questionTypeMap.has(question.question_type)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
// 2 自动填写, 3 是逻辑配额
|
// 2 自动填写, 3 是逻辑配额
|
||||||
const logicValid = logics.every((logic: any) => {
|
const logicValid = logics.every((logic: any) => {
|
||||||
if (![2].includes(logic.skip_type)) {
|
if ([0, 1].includes(logic.skip_type)) {
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
console.log(questionValid, logicValid);
|
|
||||||
|
// 判断是否是随机题组/循环题组
|
||||||
return questionValid && logicValid;
|
const surveyValid = !data.data.survey.group_pages?.length > 0;
|
||||||
}
|
return questionValid && logicValid && surveyValid;
|
||||||
|
}
|
||||||
export {
|
|
||||||
fetchSurveys,
|
export {
|
||||||
loading,
|
fetchSurveys,
|
||||||
finished,
|
loading,
|
||||||
survey,
|
finished,
|
||||||
total,
|
survey,
|
||||||
searchValue,
|
total,
|
||||||
deleteItem,
|
searchValue,
|
||||||
saveTemplate,
|
deleteItem,
|
||||||
currentSurvey,
|
saveTemplate,
|
||||||
requestLoading,
|
currentSurvey,
|
||||||
fetchSingleSurvey,
|
requestLoading,
|
||||||
clearSurveys
|
fetchSingleSurvey,
|
||||||
};
|
clearSurveys
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user