feat(Survey): 添加模板校验功能
- 新增模板校验功能,检查问卷中是否包含移动端不支持的题型 - 在保存模板前进行校验,如果存在不支持的题型则弹出提示窗口 - 优化删除问卷和保存模板的逻辑
This commit is contained in:
@@ -2,6 +2,8 @@ import { getSurveysPage, deleteSurveys, saveTemplates } from '@/api/home';
|
||||
import { ref } from 'vue';
|
||||
import { showDialog, showConfirmDialog, showFailToast, showToast } from 'vant';
|
||||
import { getSurveysDetail } from '@/api/design';
|
||||
import { getQuestionList } from '@/api/survey';
|
||||
import { questionTypeMap } from '@/utils/question/typeMapping';
|
||||
|
||||
const searchValue = ref('');
|
||||
const survey = ref<SurveyItem[]>([]);
|
||||
@@ -55,7 +57,7 @@ function deleteItem(item: SurveyItem, form: any) {
|
||||
showCancelButton: true,
|
||||
confirmButtonColor: '#03B03C'
|
||||
})
|
||||
.then(async() => {
|
||||
.then(async () => {
|
||||
const res = await deleteSurveys(item.sn);
|
||||
if (res.data.message) {
|
||||
showToast(res.data.message);
|
||||
@@ -74,6 +76,15 @@ function deleteItem(item: SurveyItem, form: any) {
|
||||
// 保存为模板
|
||||
async function saveTemplate(item: SurveyItem) {
|
||||
const data = JSON.parse(JSON.stringify(item));
|
||||
|
||||
// 如果没有通过校验, 弹出提示窗不进行下一步
|
||||
if (!(await validateSurvey(data))) {
|
||||
showDialog({
|
||||
title: '模板校验失败',
|
||||
message: '内部存在移动端不支持的题型'
|
||||
});
|
||||
return;
|
||||
}
|
||||
const res = await saveTemplates(item.sn, data);
|
||||
if (res.data.code === 200 || res.data.code === 201) {
|
||||
showConfirmDialog({
|
||||
@@ -84,10 +95,24 @@ async function saveTemplate(item: SurveyItem) {
|
||||
showFailToast(res.data);
|
||||
}
|
||||
}
|
||||
|
||||
function clearSurveys() {
|
||||
survey.value = [];
|
||||
}
|
||||
/**
|
||||
* 校验问卷是否可以保存为模板
|
||||
* @param data
|
||||
*/
|
||||
async function validateSurvey(survey: SurveyItem): Promise<boolean> {
|
||||
const { data } = await getQuestionList(survey.sn);
|
||||
const questions = data.data.questions;
|
||||
|
||||
return questions.every((question: any) => {
|
||||
if (!questionTypeMap.has(question.question_type)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
export {
|
||||
fetchSurveys,
|
||||
|
||||
Reference in New Issue
Block a user