-
+
@@ -18,11 +18,11 @@ import { useStore } from 'vuex'
import HeaderSetter from '../components/HeaderSetter.vue'
import MainTitle from '../components/MainTitle.vue'
-import submit from '../components/SubmitSetter.vue'
+import Submit from '../components/SubmitSetter.vue'
import MainRenderer from '../components/MainRenderer.vue'
import AlertDialog from '../components/AlertDialog.vue'
import ConfirmDialog from '../components/ConfirmDialog.vue'
-import progressBar from '../components/ProgressBar.vue'
+import ProgressBar from '../components/ProgressBar.vue'
import LogoIcon from '../components/LogoIcon.vue'
import { submitForm } from '../api/survey'
@@ -58,12 +58,12 @@ const validate = (cbk: (v: boolean) => void) => {
const normalizationRequestBody = () => {
const enterTime = store.state.enterTime
const encryptInfo = store.state.encryptInfo
- const formModel = store.getters.formModel
+ const formValues = store.state.formValues
const surveyPath = store.state.surveyPath
const result: any = {
surveyPath,
- data: JSON.stringify(formModel),
+ data: JSON.stringify(formValues),
difTime: Date.now() - enterTime,
clientTime: Date.now()
}
diff --git a/web/src/render/store/actions.js b/web/src/render/store/actions.js
index 50dec853..3438ebea 100644
--- a/web/src/render/store/actions.js
+++ b/web/src/render/store/actions.js
@@ -5,6 +5,7 @@ import 'moment/locale/zh-cn'
moment.locale('zh-cn')
import adapter from '../adapter'
import { queryVote, getEncryptInfo } from '@/render/api/survey'
+import { RuleMatch } from '@/common/logicEngine/RulesMatch'
/**
* CODE_MAP不从management引入,在dev阶段,会导致B端 router被加载,进而导致C端路由被添加 baseUrl: /management
*/
@@ -13,6 +14,7 @@ const CODE_MAP = {
ERROR: 500,
NO_AUTH: 403
}
+const VOTE_INFO_KEY = 'voteinfo'
export default {
// 初始化
@@ -101,18 +103,63 @@ export default {
return
}
try {
+ localStorage.removeItem(VOTE_INFO_KEY)
const voteRes = await queryVote({
surveyPath,
fieldList: fieldList.join(',')
})
if (voteRes.code === 200) {
+ localStorage.setItem(
+ VOTE_INFO_KEY,
+ JSON.stringify({
+ ...voteRes.data
+ })
+ )
commit('setVoteMap', voteRes.data)
}
} catch (error) {
console.log(error)
}
},
+ updateVoteData({ state, commit }, data) {
+ const { key: questionKey, value: questionVal } = data
+ // 更新前获取接口缓存在localStorage中的数据
+ const localData = localStorage.getItem(VOTE_INFO_KEY)
+ const voteinfo = JSON.parse(localData)
+ const currentQuestion = state.questionData[questionKey]
+ const options = currentQuestion.options
+ const voteTotal = voteinfo?.[questionKey]?.total || 0
+ let totalPayload = {
+ questionKey,
+ voteKey: 'total',
+ voteValue: voteTotal
+ }
+ options.forEach((option) => {
+ const optionhash = option.hash
+ const voteCount = voteinfo?.[questionKey]?.[optionhash] || 0
+ // 如果选中值包含该选项,对应voteCount 和 voteTotal + 1
+ if (
+ Array.isArray(questionVal) ? questionVal.includes(optionhash) : questionVal === optionhash
+ ) {
+ const countPayload = {
+ questionKey,
+ voteKey: optionhash,
+ voteValue: voteCount + 1
+ }
+ totalPayload.voteValue += 1
+ commit('updateVoteMapByKey', countPayload)
+ } else {
+ const countPayload = {
+ questionKey,
+ voteKey: optionhash,
+ voteValue: voteCount
+ }
+ commit('updateVoteMapByKey', countPayload)
+ }
+ commit('updateVoteMapByKey', totalPayload)
+ })
+ },
async getEncryptInfo({ commit }) {
try {
const res = await getEncryptInfo()
@@ -122,5 +169,9 @@ export default {
} catch (error) {
console.log(error)
}
+ },
+ async initRuleEngine({ commit }, ruleConf) {
+ const ruleEngine = new RuleMatch(ruleConf)
+ commit('setRuleEgine', ruleEngine)
}
}
diff --git a/web/src/render/store/getters.js b/web/src/render/store/getters.js
index 9764b6b3..f5a67d4e 100644
--- a/web/src/render/store/getters.js
+++ b/web/src/render/store/getters.js
@@ -1,117 +1,31 @@
-import { flatten } from 'lodash-es'
-
export default {
// 题目列表
renderData: (state) => {
- const { questionSeq, questionData, formValues } = state
+ const { questionSeq, questionData } = state
+
let index = 1
return (
questionSeq &&
questionSeq.reduce((pre, item) => {
const questionArr = []
- for (const questionKey of item) {
+
+ item.forEach(questionKey => {
+ console.log('题目重新计算')
const question = { ...questionData[questionKey] }
- const { type, extraOptions, options, rangeConfig } = question
-
- const questionVal = formValues[questionKey]
-
- question.value = questionVal
- // 本题开启了
+ // 开启显示序号
if (question.showIndex) {
question.indexNumber = index++
}
-
- const allOptions = []
- if (Array.isArray(extraOptions)) {
- allOptions.push(...extraOptions)
- }
- if (Array.isArray(options)) {
- allOptions.push(...options)
- }
-
- let othersValue = {}
- let voteTotal = 0
- const voteMap = state.voteMap
- if (/vote/.test(type)) {
- voteTotal = voteMap?.[questionKey]?.total || 0
- }
- // 遍历所有的选项
- for (const optionItem of allOptions) {
- // 开启了更多输入框,生成othersValue的值
- if (optionItem.others) {
- const opKey = `${questionKey}_${optionItem.hash}`
- optionItem.othersKey = opKey
- optionItem.othersValue = formValues[opKey]
- othersValue[opKey] = formValues[opKey]
- }
-
- // 投票题,用户手动选择选项后,要实时更新展示数据和进度
- if (/vote/.test(type)) {
- const voteCount = voteMap?.[questionKey]?.[optionItem.hash] || 0
- if (
- Array.isArray(questionVal)
- ? questionVal.includes(optionItem.hash)
- : questionVal === optionItem.hash
- ) {
- optionItem.voteCount = voteCount + 1
- voteTotal = voteTotal + 1
- } else {
- optionItem.voteCount = voteCount
- }
- question.voteTotal = voteTotal
- }
- }
-
- // 开启了更多输入框,要将当前的value赋值给question
- if (rangeConfig && Object.keys(rangeConfig).length > 0 && rangeConfig[questionVal]) {
- const curRange = rangeConfig[questionVal]
- if (curRange?.isShowInput) {
- const rangeKey = `${questionKey}_${questionVal}`
- curRange.othersKey = rangeKey
- curRange.othersValue = formValues[rangeKey]
- othersValue[rangeKey] = formValues[rangeKey]
- }
- }
-
- // 将othersValue赋值给
- question.othersValue = othersValue
+
questionArr.push(question)
- }
+ })
if (questionArr && questionArr.length) {
pre.push(questionArr)
}
+
return pre
}, [])
)
- },
- // 根据渲染的题目生成的用户输入或者选择的数据
- formModel: (state, getters) => {
- const { renderData } = getters
- const formdata = flatten(renderData).reduce((pre, current) => {
- const { othersValue, type, field } = current
- if (othersValue && Object.keys(othersValue).length) {
- Object.assign(pre, othersValue)
- }
- switch (type) {
- // case 'fillin':
- // current.fillinConfig.forEach(item => {
- // item.forEach(subItem => {
- // if (subItem.blanks > 0) {
- // const resultField = `${field}_${subItem.hash}`
- // Object.assign(pre, { [resultField]: subItem.value })
- // }
- // })
- // })
- // Object.assign(pre, { [field]: formValues[field] })
- // break
- default:
- Object.assign(pre, { [field]: current.value })
- break
- }
-
- return pre
- }, {})
- return formdata
}
}
diff --git a/web/src/render/store/mutations.js b/web/src/render/store/mutations.js
index d88fb2a1..3362123e 100644
--- a/web/src/render/store/mutations.js
+++ b/web/src/render/store/mutations.js
@@ -20,8 +20,8 @@ export default {
},
changeFormData(state, data) {
let { key, value } = data
+ // console.log('formValues', key, value)
set(state, `formValues.${key}`, value)
- // set(state, `questionData.${key}.value`, value)
},
changeSelectMoreData(state, data) {
const { key, value, field } = data
@@ -36,10 +36,21 @@ export default {
setVoteMap(state, data) {
state.voteMap = data
},
+ updateVoteMapByKey(state, data) {
+ const { questionKey, voteKey, voteValue } = data
+ // 兼容为空的情况
+ if(!state.voteMap[questionKey]){
+ state.voteMap[questionKey] = {}
+ }
+ state.voteMap[questionKey][voteKey] = voteValue
+ },
setQuestionSeq(state, data) {
state.questionSeq = data
},
setEncryptInfo(state, data) {
state.encryptInfo = data
- }
+ },
+ setRuleEgine(state, ruleEngine) {
+ state.ruleEngine = ruleEngine
+ },
}
diff --git a/web/src/render/store/state.js b/web/src/render/store/state.js
index ac3f523f..233de0d3 100644
--- a/web/src/render/store/state.js
+++ b/web/src/render/store/state.js
@@ -12,5 +12,6 @@ export default {
enterTime: null,
questionSeq: [], // 题目的顺序,因为可能会有分页的情况,所以是一个二维数组[[qid1, qid2], [qid3,qid4]]
voteMap: {},
- encryptInfo: null
+ encryptInfo: null,
+ ruleEngine: null
}