refactor(Survey): 优化问卷创建和保存逻辑
- 在 Design/Index.vue 中添加对 sn 参数的判断,确保只在有有效 sn 时执行保存操作 - 在 Survey/views/Create/Index.vue 中,将 sn 存储到 sessionStorage,以便在不同组件间共享 - 修改保存逻辑,使用 sessionStorage 中的 sn 替代 route.query.sn - 优化获取问卷详情和设置的代码结构,提高可读性和性能 - 修改模板保存功能的中文描述,使其更加准确
This commit is contained in:
@@ -418,27 +418,29 @@ const actionFun = {
|
||||
|
||||
// emit 事件
|
||||
const saveQueItem = (logics, questions, survey) => {
|
||||
if (route.query.sn) {
|
||||
saveQuestion({
|
||||
sn: route.query.sn,
|
||||
data: {
|
||||
logics: logics || [],
|
||||
questions: questions || [],
|
||||
survey: {
|
||||
local_pages: [],
|
||||
...survey,
|
||||
pages: getPages(
|
||||
questionInfo.value.questions,
|
||||
questionInfo.value.survey.is_one_page_one_question
|
||||
),
|
||||
version: Base64.encode(`${new Date().getTime()}`)
|
||||
}
|
||||
}
|
||||
}).then(() => {
|
||||
sync({ sn: route.query.sn });
|
||||
});
|
||||
}
|
||||
// questions.map((item, index) => {
|
||||
// item.title = index + 1;
|
||||
// });
|
||||
saveQuestion({
|
||||
sn: route.query.sn,
|
||||
data: {
|
||||
logics: logics || [],
|
||||
questions: questions || [],
|
||||
survey: {
|
||||
local_pages: [],
|
||||
...survey,
|
||||
pages: getPages(
|
||||
questionInfo.value.questions,
|
||||
questionInfo.value.survey.is_one_page_one_question
|
||||
),
|
||||
version: Base64.encode(`${new Date().getTime()}`)
|
||||
}
|
||||
}
|
||||
}).then(() => {
|
||||
sync({ sn: route.query.sn });
|
||||
});
|
||||
};
|
||||
|
||||
const emitFun = {
|
||||
|
||||
@@ -98,7 +98,7 @@
|
||||
>
|
||||
<el-dropdown-item @click="copyItem(item)">复制</el-dropdown-item>
|
||||
<el-dropdown-item @click="deleteItem(item)">删除</el-dropdown-item>
|
||||
<el-dropdown-item @click="saveTemplate(item)">存为模板</el-dropdown-item>
|
||||
<el-dropdown-item @click="saveTemplate(item)">保存为模板</el-dropdown-item>
|
||||
</el-dropdown-menu>
|
||||
</template>
|
||||
</el-dropdown>
|
||||
|
||||
@@ -442,6 +442,7 @@ const onConfirmDate = (e) => {
|
||||
|
||||
// 获取选中的题目
|
||||
const getActiveQuestion = (activeQues) => {
|
||||
activeQuestionIndex.value = questionInfo.value.questions.length - 1;
|
||||
chooseQuestionId.value = activeQues.id;
|
||||
activeId.value = activeQues.id;
|
||||
// 在questions 里 查找index 给 activeQuestionIndex
|
||||
@@ -453,20 +454,22 @@ const getActiveQuestion = (activeQues) => {
|
||||
};
|
||||
|
||||
const saveTitle = () => {
|
||||
let sn = sessionStorage.setItem('sn');
|
||||
|
||||
if (!questionInfo.value.survey.title || questionInfo.value.survey.title === '<br/>') {
|
||||
showToast('问卷标题不能为空');
|
||||
return false;
|
||||
}
|
||||
titleActive.value = false;
|
||||
saveQuestions({
|
||||
sn: route.query.sn,
|
||||
sn: sn,
|
||||
title: questionInfo.value.survey.title,
|
||||
introduction: questionInfo.value.survey.introduction
|
||||
}).then((res) => {
|
||||
if (res.data) {
|
||||
// questionInfo.value.survey.title 富文本转为html获取innertext
|
||||
let title = questionInfo.value.survey.title.replace(/<[^>]*>/g, '');
|
||||
saveProjectName(route.query.sn, { project_name: title });
|
||||
saveProjectName(sn, { project_name: title });
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -521,8 +524,9 @@ const questionEvent = (item) => {
|
||||
|
||||
// 新增保存增加的一个
|
||||
const saveQuestionItem = (questionJson) => {
|
||||
let sn = sessionStorage.setItem('sn');
|
||||
const query = {
|
||||
sn: route.query.sn,
|
||||
sn: sn,
|
||||
|
||||
data: {
|
||||
logics: questionInfo.value.logics,
|
||||
@@ -601,16 +605,18 @@ const saveSetting = (parentKey, childKeys, type) => {
|
||||
// delete query['is_publish_number'];
|
||||
delete query['publish_number'];
|
||||
}
|
||||
let sn = sessionStorage.setItem('sn');
|
||||
saveSettings({
|
||||
sn: route.query.sn,
|
||||
sn: sn,
|
||||
...query
|
||||
});
|
||||
};
|
||||
|
||||
// 保存是否每页一题
|
||||
const saveIsOnePage = () => {
|
||||
let sn = sessionStorage.setItem('sn');
|
||||
saveQuestions({
|
||||
sn: route.query.sn,
|
||||
sn: sn,
|
||||
is_one_page_one_question: questionInfo.value.survey.is_one_page_one_question
|
||||
});
|
||||
};
|
||||
@@ -626,30 +632,35 @@ let settingList = [];
|
||||
|
||||
// 获取题目相亲
|
||||
const getQuestionDetail = () => {
|
||||
return snQuestions({ sn: route.query.sn }).then((res) => {
|
||||
if (res.data) {
|
||||
questionInfo.value.survey = Object.assign({}, res.data.data.survey);
|
||||
questionInfo.value.questions = res.data.data.questions.map((item, index) => {
|
||||
return {
|
||||
...item,
|
||||
title: index + 1
|
||||
};
|
||||
});
|
||||
questionInfo.value.logics = res.data.data.logics.map((item) => {
|
||||
return {
|
||||
...item
|
||||
};
|
||||
});
|
||||
let sn = sessionStorage.getItem('sn');
|
||||
if (sn) {
|
||||
console.log(sn, 213123);
|
||||
|
||||
getSetting({ sn: route.query.sn }).then((setting) => {
|
||||
if (setting.data) {
|
||||
// 获取所有的key
|
||||
settingList = Object.keys(setting.data.data);
|
||||
questionInfo.value.survey = Object.assign(questionInfo.value.survey, setting.data.data);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
snQuestions({ sn: sn }).then((res) => {
|
||||
if (res.data) {
|
||||
questionInfo.value.survey = Object.assign({}, res.data.data.survey);
|
||||
questionInfo.value.questions = res.data.data.questions.map((item, index) => {
|
||||
return {
|
||||
...item,
|
||||
title: index + 1
|
||||
};
|
||||
});
|
||||
questionInfo.value.logics = res.data.data.logics.map((item) => {
|
||||
return {
|
||||
...item
|
||||
};
|
||||
});
|
||||
|
||||
getSetting({ sn: sn }).then((setting) => {
|
||||
if (setting.data) {
|
||||
// 获取所有的key
|
||||
settingList = Object.keys(setting.data.data);
|
||||
questionInfo.value.survey = Object.assign(questionInfo.value.survey, setting.data.data);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
const questionInfo = computed(() => store.questionsInfo.value);
|
||||
|
||||
@@ -746,7 +757,9 @@ watch(
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (route.query.sn) {
|
||||
sessionStorage.setItem('sn', route.query.sn);
|
||||
}
|
||||
// 监听ip数量
|
||||
watch(
|
||||
() => questionInfo.value.survey.ip_number,
|
||||
@@ -774,6 +787,7 @@ watch(
|
||||
|
||||
// 保存 目前没有任何逻辑可以执行所有保存
|
||||
const saveAs = (back) => {
|
||||
let sn = sessionStorage.getItem('sn');
|
||||
let questions = JSON.parse(JSON.stringify(questionInfo.value.questions));
|
||||
questions = questions.map((item, index) => {
|
||||
return {
|
||||
@@ -783,7 +797,7 @@ const saveAs = (back) => {
|
||||
});
|
||||
|
||||
let query = {
|
||||
sn: route.query.sn,
|
||||
sn: sn,
|
||||
|
||||
data: {
|
||||
logics: questionInfo.value.logics,
|
||||
@@ -799,7 +813,7 @@ const saveAs = (back) => {
|
||||
}
|
||||
};
|
||||
saveQuestion(query).then(() => {
|
||||
sync({ sn: route.query.sn }).then(() => {
|
||||
sync({ sn: sn }).then(() => {
|
||||
if (back) {
|
||||
back();
|
||||
} else {
|
||||
@@ -827,18 +841,19 @@ const previewQuestion = () => {
|
||||
};
|
||||
|
||||
const surveyDetailFun = (back) => {
|
||||
getSurveysDetail(route.query.sn).then((res) => {
|
||||
let sn = sessionStorage.getItem('sn');
|
||||
getSurveysDetail(sn).then((res) => {
|
||||
if (res.data) {
|
||||
// 投放中
|
||||
if (res.data.data.status === 1) {
|
||||
changeStatus({ sn: route.query.sn, status: 0 });
|
||||
changeStatus({ sn: sn, status: 0 });
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
onMounted(async () => {
|
||||
await getQuestionDetail();
|
||||
onMounted(() => {
|
||||
getQuestionDetail();
|
||||
|
||||
surveyDetailFun();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user