qa-education-exam-weapp/pages/mine/survey/question.js

133 lines
3.1 KiB
JavaScript
Raw Normal View History

2026-04-17 16:22:56 +08:00
import {
getUserSurveyInfo,
surveySubmit
} from "../../../api/index";
import Dialog from '@vant/weapp/dialog/dialog'
import debounce from '../../../utils/debounce'
Page({
data: {
time: 0,
count: 0,
current: 0,
CLASS_ID: '',
SURVEY_ID: '',
CORPINFO_ID: '',
topic: [],
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
this.setData({
CLASS_ID: options.CLASS_ID,
SURVEY_ID: options.SURVEY_ID,
CORPINFO_ID: options.CORPINFO_ID,
})
this.getData()
},
onChange(e) {
const index = e.currentTarget.dataset.index
let detail = e.detail
let varList = this.data.topic
varList[index].ANSWER = detail
this.setData({
topic: [...varList]
})
},
submitPapers() {
Dialog.confirm({
title: '确定要交问卷吗?',
selector: '#nation',
})
.then(() => {
debounce(async () => {
let varList = this.data.topic
await surveySubmit({
questionList: JSON.stringify(varList),
CLASS_ID: this.data.CLASS_ID,
CORPINFO_ID: this.data.CORPINFO_ID,
})
wx.showToast({
title: '问卷提交成功',
mask: true
})
setTimeout(() => {
wx.navigateBack()
}, 1500)
})
}).catch((e) => {
})
},
async getData() {
const resData = await getUserSurveyInfo({
SURVEY_ID: this.data.SURVEY_ID
})
for (let i = 0; i < resData.questionList.length; i++) {
resData.questionList[i].ANSWER = ''
resData.questionList[i].CHECK = ''
}
this.setData({
topic: resData.questionList
})
},
chooseTopic(e) {
const {
type,
check,
itemid
} = e.currentTarget.dataset
let topic = this.data.topic
let current = this.data.current
if (type === 'radio') {
if (topic[current].CHECK === check) {
topic[current].CHECK = ''
} else {
topic[current].CHECK = check
topic[current].ANSWER = itemid
}
this.setData({
topic: [...topic]
})
}
if (type === 'multiple') {
if (topic[current].CHECK) {
let checkedArr = topic[current].CHECK.split(',')
let checkedANSWER = topic[current].ANSWER.split(',')
if (checkedArr.includes(check)) {
checkedArr.splice(checkedArr.indexOf(check), 1)
checkedANSWER.splice(checkedANSWER.indexOf(itemid), 1)
topic[current].CHECK = checkedArr.join(',')
topic[current].ANSWER = checkedANSWER.join(',')
} else {
checkedArr.push(check)
checkedANSWER.push(itemid)
checkedArr.sort()
checkedANSWER.sort()
topic[current].CHECK = checkedArr.join(',')
topic[current].ANSWER = checkedANSWER.join(',')
}
} else {
topic[current].CHECK = check
topic[current].ANSWER = itemid
}
this.setData({
topic: [...topic]
})
}
},
previousQuestion() {
this.setData({
current: --this.data.current
})
},
nextQuestion() {
this.setData({
current: ++this.data.current
})
}
})