// packageBookService/pages/psychologyAnswer/psychologyAnswer.js
|
import {
|
getPublicImage
|
} from "../../../assets/js/middleGround/tool";
|
import {
|
loginInfo
|
} from '../../../assets/js/login';
|
const app = getApp();
|
var pastData = require('./testdata/index')
|
Page({
|
/**
|
* 页面的初始数据
|
*/
|
data: {
|
dataList: "",
|
barHeight: "",
|
navBarHeight: "",
|
loading: false,
|
answerTitle: "", // 导航栏标题
|
submitStatus: false, // 提交状态
|
currentIndex: 0, // 当前显示的题号
|
total: 0, // 题目总数
|
totalScore: 0, //题目总分
|
scoreDataList: [], // 特殊分数
|
cardList: [], // 提交项,
|
questionDataList: [], // 显示题目列表
|
questionDataTitle: "", //显示题目的提示信息
|
jumpQuestionList: [], //存储被删除的题目
|
noData: false,
|
isNight: false,
|
sliderValue: 0, // 字体滑块
|
showDialog: false, // 未提交退出拦截弹窗
|
showId: '',
|
isShowDialog: false, // 测试报告弹窗是否显示
|
},
|
|
/**
|
* 生命周期函数--监听页面加载
|
*/
|
onLoad(options) {
|
const indexof = options.listIndex;
|
const systInfo = wx.getSystemInfoSync();
|
const menu = wx.getMenuButtonBoundingClientRect(); // 胶囊信息
|
const navBarHeight =
|
(menu.top - systInfo.statusBarHeight) * 2 + menu.height; // 导航栏高度
|
this.setData({
|
dataList: pastData,
|
questionDataList: pastData.postData.list[indexof].list,
|
questionDataTitle: pastData.postData.list[indexof].title,
|
questionDataOptions: pastData.postData.list[indexof].optionList,
|
scoreDataList: pastData.postData.list[indexof].scoreData
|
});
|
wx.setNavigationBarTitle({
|
title: pastData.postData.list[indexof].name,
|
})
|
const token = wx.getStorageSync(app.config.tokenKey)
|
if (!token) {
|
loginInfo(app, (data) => {
|
if (data) {
|
this.init();
|
} else {
|
this.init();
|
}
|
})
|
} else {
|
this.init()
|
}
|
},
|
|
/**
|
* 生命周期函数--监听页面初次渲染完成
|
*/
|
onReady() {},
|
|
/**
|
* 生命周期函数--监听页面显示
|
*/
|
onShow() {
|
if (this.data.answerType != "mock") {
|
if (wx.timer) {
|
clearInterval(wx.timer);
|
}
|
}
|
if (this.data.answerType == "mock") {
|
if (wx.timer) {
|
clearInterval(wx.timer);
|
}
|
wx.timer = setInterval(() => {
|
this.setData({
|
saveTime: this.data.saveTime - 1,
|
});
|
if (this.data.saveTime == 0) {
|
this.saveMockData();
|
}
|
}, 1000);
|
}
|
this.setData({
|
startTime: Date.now(),
|
});
|
},
|
|
/**
|
* 生命周期函数--监听页面隐藏
|
*/
|
onHide() {
|
if (wx.timer) {
|
clearInterval(wx.timer);
|
}
|
this.setData({
|
pauseTime: Date.now(),
|
});
|
if (wx.getStorageSync(app.config.tokenKey)) {
|
let duration = this.data.pauseTime - this.data.startTime;
|
this.count(duration);
|
}
|
},
|
|
/**
|
* 生命周期函数--监听页面卸载
|
*/
|
onUnload(e) {
|
if (wx.timer) {
|
clearInterval(wx.timer);
|
}
|
if (this.data.countdownInterval !== null) {
|
clearInterval(this.data.countdownInterval);
|
}
|
this.setData({
|
pauseTime: Date.now(),
|
});
|
if (wx.getStorageSync(app.config.tokenKey)) {
|
let duration = this.data.pauseTime - this.data.startTime;
|
this.count(duration);
|
}
|
},
|
|
count(timeStr) {
|
const data = {
|
appRefCode: app.config.appRefCode,
|
type: "LearningTime", //统计类型--阅读时长
|
data: timeStr + "", //统计内容--时长毫秒
|
event: "LearningTime",
|
sysType: "App",
|
};
|
//阅读商品的id
|
if (this.data.bookId) {
|
data.productId = this.data.bookId;
|
}
|
//阅读资源的id
|
// if (product.cmsItemId) {
|
// data.cmsItemId = product.cmsItemId
|
// }
|
app.MG.job.newJobWithApiNewEvent(data).then((res) => {});
|
},
|
|
/**
|
* 页面相关事件处理函数--监听用户下拉动作
|
*/
|
onPullDownRefresh() {},
|
|
/**
|
* 页面上拉触底事件的处理函数
|
*/
|
onReachBottom() {},
|
|
/**
|
* 用户点击右上角分享
|
*/
|
onShareAppMessage() {},
|
onShareTimeline() {},
|
|
// 监听watch
|
watch(context, variableName, callback) {
|
let value = context.data[variableName]; // 获取被监听属性的当前值
|
// 使用 Object.defineProperty 方法在数据对象上定义属性的 getter 和 setter
|
Object.defineProperty(context.data, variableName, {
|
configurable: true, // 可配置
|
enumerable: true, // 可枚举
|
get: function () {
|
return value; // 返回属性的当前值
|
},
|
set: function (newVal) {
|
const oldVal = value; // 记录属性的旧值
|
value = newVal; // 更新属性的值
|
callback.call(context, newVal, oldVal); // 调用回调函数,传递新值和旧值
|
},
|
});
|
},
|
// 修改nav颜色
|
changeNavBarColor(e) {
|
if (type == 'night') {
|
wx.setNavigationBarColor({
|
backgroundColor: '#000000',
|
frontColor: '#ffffff',
|
})
|
} else {
|
wx.setNavigationBarColor({
|
backgroundColor: '#ffffff',
|
frontColor: '#000000',
|
})
|
}
|
},
|
// 正则找出听力src
|
extractSourceSrc(htmlString) {
|
// 正则表达式匹配<source>标签中的src属性值
|
var srcRegex = /<source\s+src="([^"]+)"/i;
|
var srcTwo = /<audio\s+src="([^"]+)"/i;
|
|
// 执行正则匹配
|
var match = srcRegex.exec(htmlString);
|
const local = srcTwo.exec(htmlString)
|
// 如果匹配成功,返回第一个捕获组的内容(src属性的值)
|
if (match && match[1]) {
|
return match[1].replace('../file', app.config.requestCtx + '/file');
|
} else if (local && local[1]) {
|
return local[1].replace('../file', app.config.requestCtx + '/file')
|
} else {
|
// 如果没有匹配到,返回null
|
return null;
|
}
|
},
|
|
// 拿到听力题除audio标签外其他内容
|
removeVideoAndAudioTags(htmlString) {
|
// 使用正则表达式匹配并移除所有的 <video> 和 <audio> 标签
|
var cleanedHtml = htmlString.replace(/<video[^>]*>[\s\S]*?<\/video>|<audio[^>]*>[\s\S]*?<\/audio>/gi, '');
|
|
return cleanedHtml;
|
},
|
|
// 改变loading状态
|
changeLoadingState() {
|
this.setData({
|
loading: true
|
})
|
},
|
// 返回拦截
|
beforeleave() {
|
if (!this.data.submitStatus) {
|
wx.showModal({
|
title: "提示",
|
content: "未提交,是否退出答题",
|
confirmColor: "#ff6c00",
|
cancelColor: "#949494",
|
complete: (res) => {
|
if (res.cancel) {
|
this.setData({
|
showDialog: true
|
})
|
}
|
if (res.confirm) {
|
this.setData({
|
submitStatus: true,
|
showDialog: false
|
});
|
wx.navigateBack();
|
}
|
},
|
});
|
}
|
},
|
onChangeSlider(e) {
|
this.setData({
|
sliderValue: e.detail.value,
|
});
|
console.log(e.detail.value);
|
},
|
// 返回
|
goBack() {
|
console.log(this.data.submitStatus);
|
wx.navigateBack();
|
},
|
//设置背景色
|
changeBGColor(e) {
|
const flag = e.detail.value
|
this.setData({
|
isNight: flag,
|
});
|
if (flag) {
|
wx.setNavigationBarColor({
|
backgroundColor: '#000000',
|
frontColor: '#ffffff',
|
})
|
} else {
|
wx.setNavigationBarColor({
|
backgroundColor: '#ffffff',
|
frontColor: '#000000',
|
})
|
}
|
},
|
// 切换题目
|
changeSwiper(e) {
|
let index = e.detail.index - 1 >= 0 ? e.detail.index - 1 : 0;
|
const item = this.data.questionDataList[index];
|
const lastItem = this.data.questionDataList[index + 1];
|
this.setData({
|
currentIndex: e.detail.index,
|
showId: lastItem.id
|
});
|
if (
|
(this.data.answerType == "collectQuestion" ||
|
this.data.answerType == "errorQuestion") &&
|
!item.isComplete
|
) {
|
let flag = this.isHaveAnswer(item.userAnswer);
|
if (flag) this.handleQuestion(index);
|
}
|
},
|
// 我的错题,收藏查看解析按钮
|
viewParsing() {
|
if (
|
this.data.answerType == "collectQuestion" ||
|
this.data.answerType == "errorQuestion"
|
) {
|
const item = this.data.questionDataList[this.data.currentIndex];
|
if (!item.isComplete) {
|
this.handleQuestion(this.data.currentIndex);
|
}
|
}
|
},
|
// 点击答题卡跳转题目
|
goQuestion(e) {
|
const id = e.detail.id;
|
this.data.questionDataList.forEach((item, index) => {
|
if (item.id == id) {
|
this.setData({
|
currentIndex: index,
|
});
|
}
|
});
|
},
|
handeleOption(e) {
|
const id = e.detail.value.currentTarget.dataset.id;
|
const radioChecked = e.detail.value.detail.value;
|
const questionList = this.data.questionDataList;
|
// 我的错题和我的收藏模式下,单选题选择了直接批改
|
for (let index = 0; index < questionList.length; index++) {
|
const item = questionList[index];
|
if (item.id == id && !item.isComplete) {
|
if (item.questionType == "linkage") {
|
item.questionTab = radioChecked;
|
item.userAnswer = "";
|
}
|
}
|
}
|
this.setData({
|
questionDataList: questionList,
|
});
|
},
|
// 单选 多选 触发
|
onChangeRadio(e) {
|
const radioData = e.detail.value.currentTarget.dataset.value;
|
const id = e.detail.value.currentTarget.dataset.id;
|
const radioChecked = e.detail.value.detail.value;
|
const questionList = this.data.questionDataList;
|
const jumpQuestion = this.data.jumpQuestionList
|
const radioChange = (item) => {
|
if (!item.jumpData) return false
|
if (item.userAnswer == item.jumpData.value) {
|
// 选中跳过答题选项 记录并删除跳过的题
|
for (let cindex = 0; cindex < item.jumpData.list.length; cindex++) {
|
const citem = item.jumpData.list[cindex];
|
jumpQuestion.push({
|
questionIndex: questionList.findIndex((ditem) => ditem.id == citem),
|
data: questionList.find((ditem) => ditem.id == citem),
|
})
|
}
|
this.setData({
|
questionDataList: questionList.filter(
|
(citem) => !item.jumpData.list.includes(citem.id)
|
),
|
jumpQuestionList: jumpQuestion,
|
});
|
console.log(this.data.questionDataList, "删除后的questionDataList");
|
} else {
|
// 选中另外的选项,要判断题目列表是否已经删除了题,删除了则加回去
|
for (let cindex = 0; cindex < item.jumpData.list.length; cindex++) {
|
const citem = item.jumpData.list[cindex]
|
if (!questionList.some((ditem) => ditem.id == citem)) {
|
const delData = jumpQuestion.find((eitem) => eitem.data.id == citem)
|
questionList.splice(delData.questionIndex, 0, delData.data)
|
}
|
}
|
this.setData({
|
questionDataList: questionList,
|
});
|
console.log(this.data.questionDataList, "恢复后的questionDataList");
|
}
|
}
|
for (let index = 0; index < questionList.length; index++) {
|
const item = questionList[index];;
|
if (item.id == id && !item.isComplete) {
|
item.userAnswer = radioChecked;
|
item.isUserAnswer = this.isHaveAnswer(radioChecked);
|
}
|
}
|
this.setData({
|
questionDataList: questionList,
|
});
|
// 调用处理方法
|
for (let cindex = 0; cindex < questionList.length; cindex++) {
|
const citem = questionList[cindex];;
|
if (citem.id == id && !citem.isComplete) {
|
radioChange(citem)
|
}
|
}
|
},
|
// 输入框触发
|
onChangeInput(e) {
|
const inputData = e.detail.value.detail.value;
|
const id = e.detail.value.currentTarget.dataset.id;
|
const index = Number(e.detail.value.currentTarget.dataset.index);
|
const questionList = this.data.questionDataList;
|
questionList.forEach((item) => {
|
if (item.id == id) {
|
item.userAnswer[index] = inputData;
|
return (item.isUserAnswer = this.isHaveAnswer(inputData));
|
}
|
});
|
const cardListUpdata = this.data.cardList;
|
cardListUpdata.forEach((item) => {
|
item.infoList.forEach((citem) => {
|
if (citem.id == id) {
|
citem.userAnswer[index] = inputData;
|
return (citem.isUserAnswer = this.isHaveAnswer(inputData));
|
}
|
});
|
});
|
this.setData({
|
questionDataList: questionList,
|
cardList: cardListUpdata,
|
});
|
if (this.data.answerType == "mock") {
|
this.setData({
|
saveTime: 20,
|
});
|
}
|
},
|
// 数组转为字符串方法
|
arrayToString(data) {
|
// 检查是否为数组
|
if (Array.isArray(data)) {
|
// 使用 join 方法将数组转换为字符串,默认使用逗号分隔
|
return data.join(",").replace(/<[^>]*>/g, "");
|
} else {
|
// 如果不是数组,直接返回原始值
|
return data.replace(/<[^>]*>/g, "");
|
}
|
},
|
// 判断是否有用户答案
|
isHaveAnswer(data) {
|
if (typeof data == "string") {
|
data = data
|
.replace(/<[^>]*>/g, "")
|
.replace(/ /g, "")
|
.trim();
|
if (data.length) {
|
return true;
|
} else {
|
return false;
|
}
|
} else if (typeof data == "object") {
|
const answer = data.find((item) => item.length > 0);
|
if (answer) {
|
return true;
|
} else {
|
return false;
|
}
|
} else {
|
return false;
|
}
|
},
|
// 提交逻辑
|
submitPaper() {
|
this.setData({
|
submitStatus: true,
|
loading: true,
|
});
|
// 先遍历所有题目,将未批改的题目批改
|
const qustionList = this.data.questionDataList;
|
if (qustionList.length) this.handleQuestion();
|
this.setData({
|
isShowDialog: true,
|
submitStatus: true,
|
})
|
// 记录用户答题数据
|
let saveData = [];
|
for (let index = 0; index < this.data.questionDataList.length; index++) {
|
const item = this.data.questionDataList[index];
|
saveData.push({
|
id: item.id,
|
answer: item.userAnswer,
|
isRight: item.isRight,
|
});
|
}
|
this.setData({
|
loading: false,
|
showDialog: false,
|
});
|
},
|
// 初始化函数
|
async init() {
|
this.setData({
|
loading: false,
|
});
|
},
|
|
// 获取题库题目
|
async getQuestionList(oldData) {
|
// 清空正确题数记录
|
this.setData({
|
noData: false,
|
cardList: [],
|
correctNum: 0,
|
});
|
},
|
|
// 批改题目 (练习,我的错题,我的收藏,,组卷)
|
handleQuestion() {
|
const questionList = this.data.questionDataList;
|
const optionList = this.data.questionDataOptions;
|
const scoreData = this.data.scoreDataList;
|
for (let index = 0; index < questionList.length; index++) {
|
const item = questionList[index];
|
if (item.questionType == 'judge' && item.userAnswer) {
|
if (optionList && optionList.length) {
|
const optionData = optionList[item.optionIndex].find(
|
(citem) => citem.value == item.userAnswer
|
)
|
item.score = optionData.score
|
}
|
}
|
if (item.questionType == 'singleChoice' && item.userAnswer) {
|
if (optionList && optionList.length) {
|
const optionData = optionList[item.optionIndex].find(
|
(citem) => citem.value == item.userAnswer
|
)
|
item.score = optionData.score
|
} else {
|
item.score = item.option.find((citem) => citem.value == item.userAnswer).score
|
}
|
}
|
if (item.questionType == 'linkage' && item.userAnswer) {
|
if (item.optionData) {
|
const optionScore = item.optionData[item.questionTab].find(
|
(citem) => citem.value == item.userAnswer
|
)
|
item.score = optionScore.score
|
}
|
}
|
if (!item.userAnswer) item.score = 0
|
}
|
//计算各项的总分
|
for (let cindex = 0; cindex < scoreData.length; cindex++) {
|
const scoreItem = scoreData[cindex];
|
if (scoreItem.range == 'all') {
|
questionList.forEach((item) => {
|
scoreItem.score += item.score
|
})
|
} else {
|
questionList.forEach((item, index) => {
|
if (scoreItem.range.includes(index)) scoreItem.score += item.score
|
})
|
}
|
if (scoreItem.isAverage) {
|
scoreItem.score = Math.floor(scoreItem.score / scoreItem.range.length)
|
}
|
if (scoreItem.multiply) {
|
scoreItem.score = Math.floor(scoreItem.score * scoreItem.multiply)
|
}
|
}
|
//根据总分给出测试结果(文字)
|
for (let dindex = 0; dindex < scoreData.length; dindex++) {
|
const scoreItem = scoreData[dindex];
|
scoreItem.dispatch = scoreItem.result.find((item) => item.score[0] <= scoreItem.score && item.score[1] >= scoreItem.score).desc
|
}
|
this.setData({
|
scoreDataList: scoreData,
|
questionDataList: questionList,
|
submitStatus: true,
|
dialogState: true,
|
});
|
},
|
|
// 处理答题数据
|
recordAnswerData() {
|
this.data.cardList.push({
|
name: "客观题得分",
|
score: this.data.subjectiveNum,
|
path: this.data.productLinkPath,
|
// infoList: [],
|
// catalogName: ''
|
});
|
let setInfoData = {
|
currentIndex: this.data.currentIndex,
|
dataList: JSON.parse(JSON.stringify(this.data.cardList)),
|
};
|
for (let i = 0; i < setInfoData.dataList.length; i++) {
|
const item = setInfoData.dataList[i];
|
if (!item.name && !item.name == "客观题得分") {
|
for (let j = 0; j < item.infoList.length; j++) {
|
let obj = {
|
id: item.infoList[j].id,
|
userAnswer: item.infoList[j].userAnswer,
|
isComplete: item.infoList[j].isComplete,
|
isRight: item.infoList[j].isRight,
|
isCollect: item.infoList[j].isCollect,
|
};
|
item.infoList[j] = obj;
|
}
|
}
|
}
|
this.setAnswerInfo(setInfoData);
|
},
|
// 提交答题数据
|
setAnswerInfo(data) {
|
app.MG.identity
|
.setUserKey({
|
setKeyRequests: [{
|
domain: "answerData",
|
key: this.data.productLinkPath,
|
value: JSON.stringify(data),
|
}, ],
|
})
|
.then((res) => {});
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 记录模考答题数据
|
saveMockData() {
|
const data = this.data.mockData.datas.find(
|
(itme) => itme.id == this.data.uuid
|
);
|
if (data.state == "3") return true;
|
const saveData = this.data.mockData.answer;
|
for (let i = 0; i < this.data.questionDataList.length; i++) {
|
const item = this.data.questionDataList[i];
|
if (this.isHaveAnswer(item.userAnswer)) {
|
const index = saveData.findIndex((saveitem) => saveitem.id == item.id);
|
if (index + "" != "-1") {
|
saveData[index].answer = item.userAnswer;
|
} else {
|
saveData.push({
|
id: item.id,
|
answer: item.userAnswer,
|
});
|
}
|
}
|
}
|
//
|
if (this.data.mockData.state == "1") {
|
const datas = this.data.mockData.datas;
|
datas.forEach((item) => {
|
if (item.id == this.data.uuid) {
|
item.state = "2";
|
}
|
});
|
this.setMockInfo(datas);
|
}
|
//
|
app.MG.identity
|
.setUserKey({
|
setKeyRequests: [{
|
domain: "mockAnswerData",
|
key: this.data.uuid,
|
value: JSON.stringify({
|
currentIndex: this.data.currentIndex,
|
time: this.data.countdownTime,
|
answerData: saveData,
|
}),
|
}, ],
|
})
|
.then((res) => {
|
this.setData({
|
saveTime: 20,
|
});
|
console.log("模考答题数据已记录");
|
});
|
},
|
|
// 富文本处理
|
formatRichText(html) {
|
console.log(html);
|
let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
|
match = match
|
.replace(/style="[^"]+"/gi, "")
|
.replace(/style='[^']+'/gi, "");
|
match = match
|
.replace(/width="[^"]+"/gi, "")
|
.replace(/width='[^']+'/gi, "");
|
match = match
|
.replace(/height="[^"]+"/gi, "")
|
.replace(/height='[^']+'/gi, "");
|
return match;
|
});
|
newContent = newContent.replace(
|
/style="[^"]+"/gi,
|
function (match, capture) {
|
match = match
|
.replace(/<p>/gi, '<p class="p_class">')
|
.replace(/width:[^;]+;/gi, "max-width:100%;")
|
.replace(/width:[^;]+;/gi, "max-width:100%;");
|
return match;
|
}
|
);
|
newContent = newContent.replace(/<br[^>]*\/>/gi, "");
|
newContent = newContent.replace(/<a>/gi, '<a class="p_class "');
|
newContent = newContent.replace(/<li>/gi, '<li class="p_class "');
|
newContent = newContent.replace(/\<p/gi, '<p class="p_class "');
|
newContent = newContent.replace(/\<span/gi, '<span class="p_class "');
|
newContent = newContent.replace(
|
/\<img/gi,
|
'<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"'
|
);
|
return newContent;
|
},
|
});
|