// miniprogram/pages/testSAS/testSAS.js // const db = wx.cloud.database() const qnaire = require("./sas.js") var ans = new Array(24) var ansJson = [] Page({ data: { qnaire: qnaire.qnaire, answer: ans, answerJson: [], id: 0, inputValue: '' ,// 初始值为空字符串 BMI: null, weight: 0, height: 0, promptTextMessage: [], totalAll: 0, isPrivacyAgreed: false, // 隐私条款是否勾选 privacyDetailShow: false ,// 隐私条款弹出页是否显示 checkboxes: [ { name: 'checkbox1', checked: false }, ], changeValue:'',// comScore: 0, comScoreStatus: '', }, // drawCircle: function() { // // 创建 canvas 上下文 // const ctx = wx.createCanvasContext('myCanvas'); // // 设置圆环的颜色和样式 // ctx.setStrokeStyle('green'); // 圆环的颜色 // ctx.setLineWidth(20); // 圆环的宽度 // // 绘制外圆 // ctx.beginPath(); // ctx.arc(90, 80, 80, 0, 2 * Math.PI); // 圆心 (100, 100),半径 100 // ctx.stroke(); // 绘制外圆 // // 绘制内圆(与外圆同圆心,但半径小20rpx,即80rpx) // ctx.beginPath(); // ctx.arc(90, 80, 80, 0, 2 * Math.PI); // ctx.setFillStyle('white'); // 设置内圆的填充颜色为白色(或者透明,取决于你希望的效果) // ctx.fill(); // 填充内圆,这样圆环内部就是白色或透明的了 // // 结束绘制并更新 canvas // ctx.draw(); // }, hideModal: function() { this.setData({ privacyDetailShow: false }); }, checkboxChange: function(e) { // e.detail.value 是一个数组,包含了所有被选中的 checkbox 的 value const checkedValues = e.detail.value; // 遍历 checkboxes 数组,根据 checkedValues 更新 checked 属性 const updatedCheckboxes = this.data.checkboxes.map(checkbox => { return { ...checkbox, // 如果 checkbox 的 name 在 checkedValues 中,则设为 true,否则设为 false checked: checkedValues.includes(checkbox.name) }; }); // 更新 data 中的 checkboxes 数组 this.setData({ checkboxes: updatedCheckboxes }); }, showPrivacyDetail: function() { this.setData({ privacyDetailShow: true }); // 这里可以添加代码来显示隐私条款弹出页,比如使用 wx.showModal 或自定义组件 }, hidePrivacyPolicy() { this.setData({ privacyDetailShow: false }); }, // otherButtonTap: function() { // if (!this.data.isPrivacyAgreed) { // wx.showToast({ // title: '请勾选隐私条款', // icon: 'none' // }); // return; // } // // 如果隐私条款已勾选,执行其他操作 // }, handleInputChange: function(e) { this.setData({ height: e.detail.value }); if(this.data.id == 1){ this.setData({ weight: e.detail.value }); } if(this.data.id == 3){ this.setData({ weight: e.detail.value }); if (this.data.weight >= 0 || this.data.height >= 0) { var bmi = this.data.weight / (this.data.height * this.data.height); this.setData({ BMI: bmi }); } } }, radioChange: function (e) { console.log(e.detail.value) this.setData({ changeValue: e.detail.value }); }, bindInput1: function(e) { // 你可以通过 e.detail.value 获取到输入框的值 // 使用 this.setData() 方法更新数据 this.setData({ input1Value: e.detail.value }) }, bindInput2: function(e) { // 你可以通过 e.detail.value 获取到输入框的值 // 使用 this.setData() 方法更新数据 this.setData({ input2Value: e.detail.value }) }, nextq: function (e) { if (!this.data.checkboxes[0].checked) { wx.showToast({ title: '请勾选隐私条款', icon: 'none' }); return; } console.log('input1Value=='+this.data.input1Value); console.log('input2Value=='+this.data.input2Value); // let input1 = e.detail.value.input1; // let input2 = e.detail.value.input2; let input1 = this.data.input1Value; let input2 = this.data.input2Value; if(!input1 || !input2){ wx.showToast({ title: '输入框不能为空', icon: 'none' }); return; } if (this.data.id < 23) { this.setData({ id: this.data.id + 1, }) } }, lastq: function (e) { if (!this.data.checkboxes[0].checked) { wx.showToast({ title: '请勾选隐私条款', icon: 'none' }); return; } if (this.data.id != 0) { this.setData({ id: this.data.id - 1, }) } }, calculateTotalHours: function (hoursInput,minutesInput) { // 获取小时和分钟的值,如果为空或不是数字,则设置为0 let hours = parseFloat(hoursInput.value) || 0; let minutes = parseFloat(minutesInput.value) || 0; // 将分钟转换为小时的小数部分 // let minutesInHours = minutes / 60; // 计算总时间(小时) // let totalHours = hours + minutesInHours; // 显示结果 // document.getElementById('result').textContent = `总时间: ${totalHours.toFixed(2)} 小时`; // return totalHours.toFixed(2); return hours*60 + minutes; }, submit: function (e) { if (!this.data.checkboxes[0].checked) { return; } // let input1 = e.detail.value.input1; // let input2 = e.detail.value.input2; let input1 = this.data.input1Value; let input2 = this.data.input2Value; if(!input1 || !input2){ wx.showToast({ title: '输入框不能为空', icon: 'none' }); return; } if(!(input1>=0 && input1<=24) || !(input2>=0 && input2<=60)){ wx.showToast({ title: '输入框时间填写错误', icon: 'none' }); return; } var a = ""; let _id = e.currentTarget.dataset.id; if(e.detail.value.answer){ a = e.detail.value.answer; }else{ a = input1+":"+input2 } var id = this.data.id-1; var obj = {}; obj.type = e.detail.value.answer?'answer':'input' obj.value = a ans[id] = a; ansJson[id] = obj; this.setData({ answer: ans, answerJson: ansJson, }) console.log("submit-answer="+this.data.answer); console.log("e.detail.value.answer="+e.detail.value.answer); console.log("e.detail.value.input="+e.detail.value.input); }, formSubmit: function() { if (!this.data.checkboxes[0].checked) { return; } var a = this.data.changeValue; var id = 23; var obj = {}; obj.type = 'answer' obj.value = a ans[id] = a; ansJson[id] = obj; this.setData({ answer: ans, answerJson: ansJson, }) var finish; var i = 0; var _this = this; while(i<24) { if(ans[i]=="") { finish='false'; break; } else { finish='true'; } i++; } if(finish=='false') { // if('true'=='false') { wx.showModal({ title: '无法提交', content: '您还有部分题目未完成,请检查后重新提交', showCancel: false, confirmColor: '#fcbe39', confirmText: "好的", success (res) { _this.setData({ id: i, }) } }) } else{ wx.showLoading({ title: '加载中', }) setTimeout(function () { wx.hideLoading({ success (res) { _this.answer2db(_this.data.answerJson); wx.navigateBack({ delta: 1 }) } }) }, 2000) } }, answer2db: function(answerJson) { // db.collection('SAS').add({ // data: { // answer: this.data.answer // }, // success (res) { // console.log(res._id); // }, // fail (res) { // wx.showToast({ // icon: 'none', // title: '新增记录失败' // }) // console.error('[数据库] [新增记录] 失败:', err) // } // }) console.log('answerJson='+JSON.stringify(answerJson)) let totalAllTemp = 0; let value1 = answerJson[0].value; let value2 = answerJson[1].value; let value3 = answerJson[2].value; let value4 = answerJson[3].value; let value5a = answerJson[4].value; let value5b = answerJson[5].value; let value5c = answerJson[6].value; let value5d = answerJson[7].value; let value5e = answerJson[8].value; let value5f = answerJson[9].value; let value5g = answerJson[10].value; let value5h = answerJson[11].value; let value5i = answerJson[12].value; let value5j = answerJson[13].value; let value6 = answerJson[14].value; let value7 = answerJson[15].value; let value8 = answerJson[16].value; let value9 = answerJson[17].value; let value10 = answerJson[18].value; let value11 = answerJson[19].value; let value12 = answerJson[20].value; let value13 = answerJson[21].value; let value14 = answerJson[22].value; let value15 = answerJson[23].value; //计算规则 https://mp.weixin.qq.com/s/QyTzuPoNErMjc2KcA1M61g let comA = 0;//条目6 let comB = 0; let comB1 = 0; let comC = 0; let comD = 0; let comE = 0; let comF = 0; let comG = 0; comA = this.resultABCD(value6); comB1 = this.resultABCD(value2) + this.resultABCD(value5a); console.log("comB1="+comB1); if(comB1 == 0){ comB = 0; }else if(comB1>=1 && comB1<=2){ comB = 1; } else if(comB1>=3 && comB1<=4){ comB = 2; } else if(comB1>=5 && comB1<=6){ comB = 3; } let value41 = Number(value4.split(':')[0]) || 0; let value42 = Number(value4.split(':')[1]) || 0; let value43 = value41*60 + value42 console.log("value43="+value43); if(value43 > 420){ comC = 0; }else if(value43>=360 && value43<=420){ comC = 1; } else if(value43>=300 && value43<360){ comC = 2; } else if(value43<300){ comC = 3; } let datediff = this.calculateTimeDifference(value1,value3); console.log("datediff="+datediff); let sleepRate = datediff?((value43/datediff)*100):0; console.log("sleepRate="+sleepRate); if(sleepRate >= 85){ comD = 0; }else if(sleepRate>=75 && sleepRate<=84){ comD = 1; } else if(sleepRate>=65 && sleepRate<74){ comD = 2; } else if(sleepRate<65){ comD = 3; } let value5All = this.resultABCD(value5b) + this.resultABCD(value5c) + this.resultABCD(value5d) + this.resultABCD(value5e) + this.resultABCD(value5f) + this.resultABCD(value5g) + this.resultABCD(value5h) + this.resultABCD(value5i) + this.resultABCD(value5j); console.log("value5All="+value5All); if(value5All == 0){ comE = 0; }else if(value5All>=1 && value5All<=9){ comE = 1; } else if(value5All>=10 && value5All<=18){ comE = 2; } else if(value5All>=19 && value5All<=27){ comE = 3; } comF = this.resultABCD(value7); console.log("comF="+comF); let valueGAll = this.resultABCD(value8) + this.resultABCD(value9); console.log("valueGAll="+valueGAll); if(valueGAll == 0){ comG = 0; }else if(valueGAll>=1 && valueGAll<=2){ comG = 1; } else if(valueGAll>=3 && valueGAll<=4){ comG = 2; } else if(valueGAll>=5 && valueGAll<=6){ comG = 3; } console.log('comA='+comA); console.log('comB='+comB); console.log('comC='+comC); console.log('comD='+comD); console.log('comE='+comE); console.log('comF='+comF); console.log('comG='+comG); let comAll = comA + comB + comC + comD + comE + comF + comG; let _comScoreStatus = ""; if(comAll >= 0 && comAll<=5){ _comScoreStatus = "很好"; }else if(comAll>=6 && comAll<=10){ _comScoreStatus = "较好"; } else if(comAll>=11 && comAll<=15){ _comScoreStatus = "一般"; } else if(comAll>=16 && comAll<=21){ _comScoreStatus = "差"; } let promptTextMessageTemp = [] promptTextMessageTemp.push('sucess'); console.log('comAll='+comAll+',comScoreStatus='+_comScoreStatus); this.setData({ comScore: comAll, comScoreStatus: _comScoreStatus, promptTextMessage: promptTextMessageTemp, }) }, resultABCD:function(a){ if(a == 'A'){ return 0; }else if(a == 'B'){ return 1; } else if(a == 'C'){ return 2; } else if(a == 'D'){ return 3; } }, sleepState:function(a,b){ console.log('a='+a+',b='+b); if(a=='A'){ if(b>=1 && b<=9){ return '不足'; }else if(b>=10 && b<=12){ return '适中'; }else if(b>=13){ return '过长'; }else{ return '未知'; } }else if(a=='B'){ if(b>=1 && b<=9){ return '不足'; }else if(b>=10 && b<=12){ return '适中'; }else if(b>=13){ return '过长'; }else{ return '未知'; } }else if(a=='C'){ if(b>=1 && b<=9){ return '不足'; }else if(b>=10 && b<=12){ return '适中'; }else if(b>=13){ return '过长'; }else{ return '未知'; } }else if(a=='D'){ if(b>=1 && b<=9){ return '不足'; }else if(b>=10 && b<=12){ return '适中'; }else if(b>=13){ return '过长'; }else{ return '未知'; } }else{ return '未知'; } }, // 将时间字符串转换为分钟数 timeStringToMinutes:function(timeStr) { var parts = timeStr.split(":"); return parseInt(parts[0], 10) * 60 + parseInt(parts[1], 10); }, // 将分钟数转换回时间字符串格式 HH:mm minutesToTimeString:function(minutes) { var hours = Math.floor(minutes / 60); var mins = minutes % 60; var minsStr = mins < 10 ? "0" + mins : mins.toString(); return hours.toString() + ":" + minsStr; }, // 计算时间差(分钟) calculateTimeDifference:function(sleepTimeStr, wakeUpTimeStr) { // 将时间字符串转换为分钟 var sleepTimeMinutes = this.timeStringToMinutes(sleepTimeStr); var wakeUpTimeMinutes = this.timeStringToMinutes("24:00") + this.timeStringToMinutes(wakeUpTimeStr); // 加上第二天的0点到起床时间的分钟数 // 计算时间差 var diffInMinutes = wakeUpTimeMinutes - sleepTimeMinutes; // 将分钟数转换回时间字符串格式 HH:mm var diffInHours = Math.floor(diffInMinutes / 60); var diffInMins = diffInMinutes % 60; // 返回时间差字符串 console.log("diffInMinutes=="+diffInMinutes) return diffInMinutes; }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { for(var i=0; i<24; i++) { ans[i] = ""; } this.setData({ privacyDetailShow: false }); // 绘制圆环 // this.drawCircle(); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })