feat: survey vuex to pinia (#331)

* feat: survey vuex to pinia

* feat: fix ts type-check

* feat: fix lint error
This commit is contained in:
yoruponder 2024-07-10 14:41:36 +08:00 committed by GitHub
parent 1a15faad42
commit 2ad6a77740
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 96 additions and 40 deletions

View File

@ -8,11 +8,13 @@ import { useRoute } from 'vue-router'
import { getPublishedSurveyInfo, getPreviewSchema } from '../api/survey' import { getPublishedSurveyInfo, getPreviewSchema } from '../api/survey'
import useCommandComponent from '../hooks/useCommandComponent' import useCommandComponent from '../hooks/useCommandComponent'
import { useSurveyStore } from '../stores/survey'
import AlertDialog from '../components/AlertDialog.vue' import AlertDialog from '../components/AlertDialog.vue'
import { initRuleEngine } from '@/render/hooks/useRuleEngine.js' import { initRuleEngine } from '@/render/hooks/useRuleEngine.js'
const store = useStore() const store = useStore()
const route = useRoute() const route = useRoute()
const surveyStore = useSurveyStore()
const loadData = (res: any, surveyPath: string) => { const loadData = (res: any, surveyPath: string) => {
if (res.code === 200) { if (res.code === 200) {
const data = res.data const data = res.data
@ -36,7 +38,7 @@ const loadData = (res: any, surveyPath: string) => {
document.title = data.title document.title = data.title
store.commit('setSurveyPath', surveyPath) surveyStore.setSurveyPath(surveyPath)
store.dispatch('init', questionData) store.dispatch('init', questionData)
initRuleEngine(logicConf?.showLogicConf) initRuleEngine(logicConf?.showLogicConf)
} else { } else {
@ -46,7 +48,7 @@ const loadData = (res: any, surveyPath: string) => {
onMounted(() => { onMounted(() => {
const surveyId = route.params.surveyId const surveyId = route.params.surveyId
console.log({ surveyId }) console.log({ surveyId })
store.commit('setSurveyPath', surveyId) surveyStore.setSurveyPath(surveyId)
getDetail(surveyId as string) getDetail(surveyId as string)
}) })
@ -60,7 +62,7 @@ const getDetail = async (surveyPath: string) => {
} else { } else {
const res: any = await getPublishedSurveyInfo({ surveyPath }) const res: any = await getPublishedSurveyInfo({ surveyPath })
loadData(res, surveyPath) loadData(res, surveyPath)
store.dispatch('getEncryptInfo') surveyStore.getEncryptInfo();
} }
} catch (error: any) { } catch (error: any) {
console.log(error) console.log(error)

View File

@ -29,6 +29,7 @@ import AlertDialog from '../components/AlertDialog.vue'
import ConfirmDialog from '../components/ConfirmDialog.vue' import ConfirmDialog from '../components/ConfirmDialog.vue'
import ProgressBar from '../components/ProgressBar.vue' import ProgressBar from '../components/ProgressBar.vue'
import { useSurveyStore } from '../stores/survey'
import { submitForm } from '../api/survey' import { submitForm } from '../api/survey'
import encrypt from '../utils/encrypt' import encrypt from '../utils/encrypt'
@ -57,12 +58,13 @@ const confirm = useCommandComponent(ConfirmDialog)
const store = useStore() const store = useStore()
const router = useRouter() const router = useRouter()
const surveyStore = useSurveyStore()
const bannerConf = computed(() => store.state?.bannerConf || {}) const bannerConf = computed(() => store.state?.bannerConf || {})
const renderData = computed(() => store.getters.renderData) const renderData = computed(() => store.getters.renderData)
const submitConf = computed(() => store.state?.submitConf || {}) const submitConf = computed(() => store.state?.submitConf || {})
const logoConf = computed(() => store.state?.bottomConf || {}) const logoConf = computed(() => store.state?.bottomConf || {})
const surveyPath = computed(() => store.state?.surveyPath || '') const surveyPath = computed(() => surveyStore.surveyPath || '')
const validate = (cbk: (v: boolean) => void) => { const validate = (cbk: (v: boolean) => void) => {
const index = 0 const index = 0
@ -70,8 +72,8 @@ const validate = (cbk: (v: boolean) => void) => {
} }
const normalizationRequestBody = () => { const normalizationRequestBody = () => {
const enterTime = store.state.enterTime const enterTime = surveyStore.enterTime
const encryptInfo = store.state.encryptInfo const encryptInfo = surveyStore.encryptInfo as any;
const formValues = store.state.formValues const formValues = store.state.formValues
const result: any = { const result: any = {
@ -82,7 +84,7 @@ const normalizationRequestBody = () => {
} }
if (encryptInfo?.encryptType) { if (encryptInfo?.encryptType) {
result.encryptType = encryptInfo?.encryptType result.encryptType = encryptInfo.encryptType
result.data = encrypt[result.encryptType as 'rsa']({ result.data = encrypt[result.encryptType as 'rsa']({
data: result.data, data: result.data,
secretKey: encryptInfo?.data?.secretKey secretKey: encryptInfo?.data?.secretKey

View File

@ -4,16 +4,17 @@ import 'moment/locale/zh-cn'
// 设置中文 // 设置中文
moment.locale('zh-cn') moment.locale('zh-cn')
import adapter from '../adapter' import adapter from '../adapter'
import { queryVote, getEncryptInfo } from '@/render/api/survey' import { queryVote } from '@/render/api/survey'
import { RuleMatch } from '@/common/logicEngine/RulesMatch' import { RuleMatch } from '@/common/logicEngine/RulesMatch'
import { useSurveyStore } from '@/render/stores/survey'
/** /**
* CODE_MAP不从management引入在dev阶段会导致B端 router被加载进而导致C端路由被添加 baseUrl: /management * CODE_MAP不从management引入在dev阶段会导致B端 router被加载进而导致C端路由被添加 baseUrl: /management
*/ */
const CODE_MAP = { // const CODE_MAP = {
SUCCESS: 200, // SUCCESS: 200,
ERROR: 500, // ERROR: 500,
NO_AUTH: 403 // NO_AUTH: 403
} // }
const VOTE_INFO_KEY = 'voteinfo' const VOTE_INFO_KEY = 'voteinfo'
import router from '../router' import router from '../router'
export default { export default {
@ -22,7 +23,8 @@ export default {
{ commit, dispatch }, { commit, dispatch },
{ bannerConf, baseConf, bottomConf, dataConf, skinConf, submitConf } { bannerConf, baseConf, bottomConf, dataConf, skinConf, submitConf }
) { ) {
commit('setEnterTime') const surveyStore = useSurveyStore()
surveyStore.setEnterTime();
const { begTime, endTime, answerBegTime, answerEndTime } = baseConf const { begTime, endTime, answerBegTime, answerEndTime } = baseConf
const { msgContent } = submitConf const { msgContent } = submitConf
const now = Date.now() const now = Date.now()
@ -90,8 +92,9 @@ export default {
}, },
// 初始化投票题的数据 // 初始化投票题的数据
async initVoteData({ state, commit }) { async initVoteData({ state, commit }) {
const surveyStore = useSurveyStore()
const questionData = state.questionData const questionData = state.questionData
const surveyPath = state.surveyPath const surveyPath = surveyStore.surveyPath
const fieldList = [] const fieldList = []
@ -163,16 +166,16 @@ export default {
commit('updateVoteMapByKey', totalPayload) commit('updateVoteMapByKey', totalPayload)
}) })
}, },
async getEncryptInfo({ commit }) { // async getEncryptInfo({ commit }) {
try { // try {
const res = await getEncryptInfo() // const res = await getEncryptInfo()
if (res.code === CODE_MAP.SUCCESS) { // if (res.code === CODE_MAP.SUCCESS) {
commit('setEncryptInfo', res.data) // commit('setEncryptInfo', res.data)
} // }
} catch (error) { // } catch (error) {
console.log(error) // console.log(error)
} // }
}, // },
async initRuleEngine({ commit }, ruleConf) { async initRuleEngine({ commit }, ruleConf) {
const ruleEngine = new RuleMatch(ruleConf) const ruleEngine = new RuleMatch(ruleConf)
commit('setRuleEgine', ruleEngine) commit('setRuleEgine', ruleEngine)

View File

@ -24,12 +24,12 @@ export default {
const { key, value, field } = data const { key, value, field } = data
set(state, `questionData.${field}.othersValue.${key}`, value) set(state, `questionData.${field}.othersValue.${key}`, value)
}, },
setEnterTime(state) { // setEnterTime(state) {
state.enterTime = Date.now() // state.enterTime = Date.now()
}, // },
setSurveyPath(state, data) { // setSurveyPath(state, data) {
state.surveyPath = data // state.surveyPath = data
}, // },
setVoteMap(state, data) { setVoteMap(state, data) {
state.voteMap = data state.voteMap = data
}, },
@ -44,9 +44,9 @@ export default {
setQuestionSeq(state, data) { setQuestionSeq(state, data) {
state.questionSeq = data state.questionSeq = data
}, },
setEncryptInfo(state, data) { // setEncryptInfo(state, data) {
state.encryptInfo = data // state.encryptInfo = data
}, // },
setRuleEgine(state, ruleEngine) { setRuleEgine(state, ruleEngine) {
state.ruleEngine = ruleEngine state.ruleEngine = ruleEngine
} }

View File

@ -1,16 +1,16 @@
import { isMobile } from '../utils/index' // import { isMobile } from '../utils/index'
export default { export default {
surveyPath: '', // surveyPath: '',
questionData: null, questionData: null,
isMobile: isMobile(), // isMobile: isMobile(),
errorInfo: { errorInfo: {
errorType: '', errorType: '',
errorMsg: '' errorMsg: ''
}, },
enterTime: null, // enterTime: null,
questionSeq: [], // 题目的顺序,因为可能会有分页的情况,所以是一个二维数组[[qid1, qid2], [qid3,qid4]] questionSeq: [], // 题目的顺序,因为可能会有分页的情况,所以是一个二维数组[[qid1, qid2], [qid3,qid4]]
voteMap: {}, voteMap: {},
encryptInfo: null, // encryptInfo: null,
ruleEngine: null ruleEngine: null
} }

View File

@ -1 +1,50 @@
// 问卷相关的Pinia Store // 问卷相关的Pinia Store
import { defineStore } from 'pinia'
import { ref } from 'vue'
import { isMobile as isInMobile } from '@/render/utils/index'
import { getEncryptInfo as getEncryptInfoApi } from '@/render/api/survey'
/**
* CODE_MAP不从management引入在dev阶段会导致B端 router被加载进而导致C端路由被添加 baseUrl: /management
*/
const CODE_MAP = {
SUCCESS: 200,
ERROR: 500,
NO_AUTH: 403
}
export const useSurveyStore = defineStore('survey', () => {
const surveyPath = ref('');
const isMobile = ref(isInMobile())
const enterTime = ref(0)
const encryptInfo = ref(null)
const setSurveyPath = ( data) => {
surveyPath.value = data
}
const setEnterTime = () => {
enterTime.value = Date.now()
}
const getEncryptInfo = async() => {
try {
const res = await getEncryptInfoApi()
if (res.code === CODE_MAP.SUCCESS) {
encryptInfo.value = res.data
}
} catch (error) {
console.log(error)
}
}
return {
surveyPath,
isMobile,
enterTime,
encryptInfo,
setSurveyPath,
setEnterTime,
getEncryptInfo
}
})