refactor(Survey): 优化问卷创建和保存逻辑

- 在 Design/Index.vue 中添加对 sn 参数的判断,确保只在有有效 sn 时执行保存操作
- 在 Survey/views/Create/Index.vue 中,将 sn 存储到 sessionStorage,以便在不同组件间共享
- 修改保存逻辑,使用 sessionStorage 中的 sn 替代 route.query.sn
- 优化获取问卷详情和设置的代码结构,提高可读性和性能
- 修改模板保存功能的中文描述,使其更加准确
This commit is contained in:
陈昱达
2025-03-27 16:17:12 +08:00
parent 4a5ead6f19
commit c2b254a2f9
3 changed files with 71 additions and 54 deletions

View File

@@ -418,27 +418,29 @@ const actionFun = {
// emit 事件 // emit 事件
const saveQueItem = (logics, questions, survey) => { 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) => { // questions.map((item, index) => {
// item.title = index + 1; // 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 = { const emitFun = {

View File

@@ -98,7 +98,7 @@
> >
<el-dropdown-item @click="copyItem(item)">复制</el-dropdown-item> <el-dropdown-item @click="copyItem(item)">复制</el-dropdown-item>
<el-dropdown-item @click="deleteItem(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> </el-dropdown-menu>
</template> </template>
</el-dropdown> </el-dropdown>

View File

@@ -442,6 +442,7 @@ const onConfirmDate = (e) => {
// 获取选中的题目 // 获取选中的题目
const getActiveQuestion = (activeQues) => { const getActiveQuestion = (activeQues) => {
activeQuestionIndex.value = questionInfo.value.questions.length - 1;
chooseQuestionId.value = activeQues.id; chooseQuestionId.value = activeQues.id;
activeId.value = activeQues.id; activeId.value = activeQues.id;
// 在questions 里 查找index 给 activeQuestionIndex // 在questions 里 查找index 给 activeQuestionIndex
@@ -453,20 +454,22 @@ const getActiveQuestion = (activeQues) => {
}; };
const saveTitle = () => { const saveTitle = () => {
let sn = sessionStorage.setItem('sn');
if (!questionInfo.value.survey.title || questionInfo.value.survey.title === '<br/>') { if (!questionInfo.value.survey.title || questionInfo.value.survey.title === '<br/>') {
showToast('问卷标题不能为空'); showToast('问卷标题不能为空');
return false; return false;
} }
titleActive.value = false; titleActive.value = false;
saveQuestions({ saveQuestions({
sn: route.query.sn, sn: sn,
title: questionInfo.value.survey.title, title: questionInfo.value.survey.title,
introduction: questionInfo.value.survey.introduction introduction: questionInfo.value.survey.introduction
}).then((res) => { }).then((res) => {
if (res.data) { if (res.data) {
// questionInfo.value.survey.title 富文本转为html获取innertext // questionInfo.value.survey.title 富文本转为html获取innertext
let title = questionInfo.value.survey.title.replace(/<[^>]*>/g, ''); 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) => { const saveQuestionItem = (questionJson) => {
let sn = sessionStorage.setItem('sn');
const query = { const query = {
sn: route.query.sn, sn: sn,
data: { data: {
logics: questionInfo.value.logics, logics: questionInfo.value.logics,
@@ -601,16 +605,18 @@ const saveSetting = (parentKey, childKeys, type) => {
// delete query['is_publish_number']; // delete query['is_publish_number'];
delete query['publish_number']; delete query['publish_number'];
} }
let sn = sessionStorage.setItem('sn');
saveSettings({ saveSettings({
sn: route.query.sn, sn: sn,
...query ...query
}); });
}; };
// 保存是否每页一题 // 保存是否每页一题
const saveIsOnePage = () => { const saveIsOnePage = () => {
let sn = sessionStorage.setItem('sn');
saveQuestions({ saveQuestions({
sn: route.query.sn, sn: sn,
is_one_page_one_question: questionInfo.value.survey.is_one_page_one_question is_one_page_one_question: questionInfo.value.survey.is_one_page_one_question
}); });
}; };
@@ -626,30 +632,35 @@ let settingList = [];
// 获取题目相亲 // 获取题目相亲
const getQuestionDetail = () => { const getQuestionDetail = () => {
return snQuestions({ sn: route.query.sn }).then((res) => { let sn = sessionStorage.getItem('sn');
if (res.data) { if (sn) {
questionInfo.value.survey = Object.assign({}, res.data.data.survey); console.log(sn, 213123);
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: route.query.sn }).then((setting) => { snQuestions({ sn: sn }).then((res) => {
if (setting.data) { if (res.data) {
// 获取所有的key questionInfo.value.survey = Object.assign({}, res.data.data.survey);
settingList = Object.keys(setting.data.data); questionInfo.value.questions = res.data.data.questions.map((item, index) => {
questionInfo.value.survey = Object.assign(questionInfo.value.survey, setting.data.data); 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); const questionInfo = computed(() => store.questionsInfo.value);
@@ -746,7 +757,9 @@ watch(
} }
} }
); );
if (route.query.sn) {
sessionStorage.setItem('sn', route.query.sn);
}
// 监听ip数量 // 监听ip数量
watch( watch(
() => questionInfo.value.survey.ip_number, () => questionInfo.value.survey.ip_number,
@@ -774,6 +787,7 @@ watch(
// 保存 目前没有任何逻辑可以执行所有保存 // 保存 目前没有任何逻辑可以执行所有保存
const saveAs = (back) => { const saveAs = (back) => {
let sn = sessionStorage.getItem('sn');
let questions = JSON.parse(JSON.stringify(questionInfo.value.questions)); let questions = JSON.parse(JSON.stringify(questionInfo.value.questions));
questions = questions.map((item, index) => { questions = questions.map((item, index) => {
return { return {
@@ -783,7 +797,7 @@ const saveAs = (back) => {
}); });
let query = { let query = {
sn: route.query.sn, sn: sn,
data: { data: {
logics: questionInfo.value.logics, logics: questionInfo.value.logics,
@@ -799,7 +813,7 @@ const saveAs = (back) => {
} }
}; };
saveQuestion(query).then(() => { saveQuestion(query).then(() => {
sync({ sn: route.query.sn }).then(() => { sync({ sn: sn }).then(() => {
if (back) { if (back) {
back(); back();
} else { } else {
@@ -827,18 +841,19 @@ const previewQuestion = () => {
}; };
const surveyDetailFun = (back) => { const surveyDetailFun = (back) => {
getSurveysDetail(route.query.sn).then((res) => { let sn = sessionStorage.getItem('sn');
getSurveysDetail(sn).then((res) => {
if (res.data) { if (res.data) {
// 投放中 // 投放中
if (res.data.data.status === 1) { if (res.data.data.status === 1) {
changeStatus({ sn: route.query.sn, status: 0 }); changeStatus({ sn: sn, status: 0 });
} }
} }
}); });
}; };
onMounted(async () => { onMounted(() => {
await getQuestionDetail(); getQuestionDetail();
surveyDetailFun(); surveyDetailFun();
}); });