Skip to content

Commit

Permalink
fix: 断点续答pinia改写
Browse files Browse the repository at this point in the history
  • Loading branch information
taoshuang committed Aug 23, 2024
1 parent 0dd4e90 commit 895a741
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 7 deletions.
13 changes: 13 additions & 0 deletions web/src/render/components/QuestionWrapper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ const handleChange = (data) => {
if (props.moduleConfig.type === NORMAL_CHOICES) {
store.dispatch('changeQuota', data)
}
// 断点续答的的数据缓存
localStorageBack()
processJumpSkip()
}
Expand Down Expand Up @@ -184,4 +186,15 @@ const processJumpSkip = () => {
questionStore.addNeedHideFields(skipKey)
}
const localStorageBack = () => {
var formData = Object.assign({}, surveyStore.formValues);
for(const key in formData){
formData[key] = encodeURIComponent(formData[key])
}
//浏览器存储
localStorage.removeItem(surveyStore.surveyPath + "_questionData")
localStorage.setItem(surveyStore.surveyPath + "_questionData", JSON.stringify(formData))
localStorage.setItem('isSubmit', JSON.stringify(false))
}
</script>
2 changes: 1 addition & 1 deletion web/src/render/pages/RenderPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const normalizationRequestBody = () => {
localStorage.removeItem(surveyPath.value + "_questionData")
localStorage.removeItem("isSubmit")
//数据加密
var formData = Object.assign({}, store.state.formValues)
var formData = Object.assign({}, surveyStore.formValues)
for(const key in formData){
formData[key] = encodeURIComponent(formData[key])
}
Expand Down
6 changes: 6 additions & 0 deletions web/src/render/stores/question.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import { QUESTION_TYPE, NORMAL_CHOICES } from '@/common/typeEnum'
const VOTE_INFO_KEY = 'voteinfo'
const QUOTA_INFO_KEY = 'limitinfo'


import useCommandComponent from '../hooks/useCommandComponent'
import BackAnswerDialog from '../components/BackAnswerDialog.vue'

const confirm = useCommandComponent(BackAnswerDialog)

// 投票进度逻辑聚合
const usevVoteMap = (questionData) => {
const voteMap = ref({})
Expand Down
140 changes: 134 additions & 6 deletions web/src/render/stores/survey.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const CODE_MAP = {
ERROR: 500,
NO_AUTH: 403
}


export const useSurveyStore = defineStore('survey', () => {
const surveyPath = ref('')
const isMobile = ref(isInMobile())
Expand Down Expand Up @@ -110,15 +112,11 @@ export const useSurveyStore = defineStore('survey', () => {

return isSuccess
}
const initSurvey = (option) => {
setEnterTime()

if (!canFillQuestionnaire(option.baseConf, option.submitConf)) {
return
}



// 加载空白页面
function clearFormData(option) {
// 根据初始的schema生成questionData, questionSeq, rules, formValues, 这四个字段
const {
questionData,
Expand Down Expand Up @@ -152,9 +150,139 @@ export const useSurveyStore = defineStore('survey', () => {
formValues.value = _formValues
whiteData.value = option.whiteData
pageConf.value = option.pageConf

// 获取已投票数据
questionStore.initVoteData()
questionStore.initQuotaMap()

}

// 加载上次填写过的数据到问卷页
function loadFormData({bannerConf, baseConf, bottomConf, dataConf, skinConf, submitConf }, formData) {
// 根据初始的schema生成questionData, questionSeq, rules, formValues, 这四个字段
const { questionData, questionSeq, rules, formValues } = adapter.generateData({
bannerConf,
baseConf,
bottomConf,
dataConf,
skinConf,
submitConf
})

for(const key in formData){
formValues[key] = formData[key]
}

// 将数据设置到state上
commit('assignState', {
questionData,
questionSeq,
rules,
bannerConf,
baseConf,
bottomConf,
dataConf,
skinConf,
submitConf,
formValues
})
// 获取已投票数据
dispatch('initVoteData')
// 获取选项上线选中数据
dispatch('initQuotaMap')

// todo: 建议通过questionStore提供setqueationdata方法修改属性,否则不好跟踪变化
questionStore.questionData = questionData
questionStore.questionSeq = questionSeq

// 将数据设置到state上
rules.value = rules
bannerConf.value = option.bannerConf
baseConf.value = option.baseConf
bottomConf.value = option.bottomConf
dataConf.value = option.dataConf
skinConf.value = option.skinConf
submitConf.value = option.submitConf
formValues.value = _formValues

whiteData.value = option.whiteData
pageConf.value = option.pageConf

// 获取已投票数据
questionStore.initVoteData()
questionStore.initQuotaMap()

}
const initSurvey = (option) => {
setEnterTime()

if (!canFillQuestionnaire(option.baseConf, option.submitConf)) {
return
}

const localData = JSON.parse(localStorage.getItem(surveyPath.value + "_questionData"))
for(const key in localData){
localData[key] = decodeURIComponent(localData[key])
}

const isSubmit = JSON.parse(localStorage.getItem('isSubmit'))
if(localData) {
if(isSubmit){
if(!option.baseConf.backAnswer) {
clearFormData(option)
} else {
confirm({
title: "您之前已提交过问卷,是否要回填?",
onConfirm: async () => {
try {
loadFormData(option, localData)
} catch (error) {
console.log(error)
} finally {
confirm.close()
}
},
onCancel: async() => {
try {
clearFormData({ commit, dispatch }, { bannerConf, baseConf, bottomConf, dataConf, skinConf, submitConf })
} catch (error) {
console.log(error)
} finally {
confirm.close()
}
}
})
}
} else {
if(!breakAnswer) {
clearFormData(option)
} else {
confirm({
title: "您之前已填写部分内容, 是否要继续填写?",
onConfirm: async () => {
try {
loadFormData(option, localData)
} catch (error) {
console.log(error)
} finally {
confirm.close()
}
},
onCancel: async() => {
try {
clearFormData(option)
} catch (error) {
console.log(error)
} finally {
confirm.close()
}
}
})
}
}
} else {
clearFormData(option)
}
}

// 用户输入或者选择后,更新表单数据
Expand Down

0 comments on commit 895a741

Please sign in to comment.