// miniprogram/pages/testSAS/testSAS.js // const db = wx.cloud.database() const qnaire = require("./sas.js") var ans = new Array(27) 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:''// }, // 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 }); }, nextq: function () { if (!this.data.checkboxes[0].checked) { wx.showToast({ title: '请勾选隐私条款', icon: 'none' }); return; } if (this.data.id < 26) { 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, }) } }, submit: function (e) { if (!this.data.checkboxes[0].checked) { return; } var a = e.detail.value.answer?e.detail.value.answer:e.detail.value.input; 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 = 26; 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<27) { 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 value20 = answerJson[19].value; let total20 = 0//因子1 if(total20 == 'A'){ total20 += 0; }else if(total20 == 'B'){ total20 += 1; }else if(total20 == 'C'){ total20 += 2; }else if(total20 == 'D'){ total20 += 3; } let total7 = 0//近1个月,从上床到入睡通常需要___________分钟 let value7 = answerJson[6].value; if(value7<15){ total7 += 0; }else if(value7>16 && value7<=30){ total7 += 1; }else if(value7>31 && value7<=60){ total7 += 2; }else if(value7>60){ total7 += 3; } let value10 = answerJson[9].value; let total10 = 0//入睡困难(30分钟内不能入睡) if(value10 == 'A'){ total10 += 0; }else if(value10 == 'B'){ total10 += 1; }else if(value10 == 'C'){ total10 += 2; }else if(value10 == 'D'){ total10 += 3; } let value9 = answerJson[8].value;//近1个月,每夜通常实际睡眠时间___________小时(不等于卧床时间) let total91 = total7 + total10 let total9_1 = 0//因子2 if(total91 == 0){ total9_1 += 0; }else if(total91 == 1 || total91 == 2){ total9_1 += 1; }else if(total91 == 3 || total91 == 4){ total9_1 += 2; }else if(total91 == 5 || total91 == 6){ total9_1 += 3; } let total9 = 0 let total9_3 = 0//因子3 if(value9 > 7){ total9_3 += 0; }else if(value9 > 6 || value9 <= 7){ total9_3 += 1; }else if(value9 > 5 || value9 <= 6){ total9_3 += 2; }else if(value9 < 5){ total9_3 += 3; } let value8 = answerJson[7].value;//起床时间 let value6 = answerJson[5].value;//入睡时间 let percentage = ((24-(value6-value8))==0)?0:((value9/(24-(value6-value8)))*100); percentage = percentage.toFixed(2); let percentage9 = 0;//因子4 if(percentage>=85){ percentage9 += 0; }else if(percentage>=75 && percentage<=84){ percentage9 += 1; }else if(percentage>=65 && percentage<=74){ percentage9 += 2; }else if(percentage<65){ percentage9 += 3; } let value11 = this.resultABCD(answerJson[10].value);//5b let value12 = this.resultABCD(answerJson[11].value);//5c let value13 = this.resultABCD(answerJson[12].value);//5d let value14 = this.resultABCD(answerJson[13].value);//5e let value15 = this.resultABCD(answerJson[14].value);//5f let value16 = this.resultABCD(answerJson[15].value);//5g let value17 = this.resultABCD(answerJson[16].value);//5h let value18 = this.resultABCD(answerJson[17].value);//5i let value_b_i = value11+value12+value13+value14+value15 +value16+value17+value18 let total_b_i = 0;//因子5 if(value_b_i<1){ total_b_i += 0; }else if(value_b_i>=1 && value_b_i<=9){ total_b_i += 1; }else if(value_b_i>=10 && value_b_i<=18){ total_b_i += 2; }else if(value_b_i>=19 && value_b_i<=27){ total_b_i += 3; } totalAllTemp = total20 + total9_1 + total9_3 + percentage9 + total_b_i let percentAll = 0 if(totalAllTemp<=5){ percentAll = '90' }else if(totalAllTemp>5 && totalAllTemp<=10){ percentAll = '75' }else if(totalAllTemp>10 && totalAllTemp<=15){ percentAll = '50' }else if(totalAllTemp>15 && totalAllTemp<=20){ percentAll = '25' }else{ percentAll = '未知' } let promptTextMessageTemp = []; let sleepState = this.sleepState(answerJson[2].value,answerJson[6].value); promptTextMessageTemp.push('在全人群中,您的个人情况决定了睡眠基础条件的分是('+totalAllTemp+')分,您已经超过了全国('+percentAll+')%的人群;(纯正态分布,5分,25%;10分,50%;15分75%;20分,位于前20%)'); promptTextMessageTemp.push('您需要改进的睡眠问题主要集中在'); promptTextMessageTemp.push('1.睡眠时长:您每天的睡眠时间'+sleepState); promptTextMessageTemp.push('2.不良因素:您的生理健康或生活习惯存在改进的空间,为了获得健康睡眠建议您(立刻进行改进//发挥优势,改进不足//继续保持良好的生活习惯)'); let text10 = ''; if(value10=='D'){ text10 += '入睡困难//' } if(sleepState == '不足'){ text10 += '深度睡眠不足//' } if(percentage9 == 3){ text10 += '睡眠效率不高' } promptTextMessageTemp.push('3.睡眠质量:您存在('+text10+')的问题,这会导致睡眠质量较低或身心难以得到放松的问题,这会是我们共同努力的方向。'); let value25 = answerJson[25].value; let value26 = answerJson[26].value; let text26 = ''; if(!value25||!value26){ text26 += ''; }else if(value25=='C'||value26=='C'){ text26 += '较差'; }else if(value25=='A'||value26=='A'){ text26 += '较好'; }else{ text26 += '适中'; } promptTextMessageTemp.push('4.睡眠环境(对应:客观环境):您的睡眠环境总体来看,('+text26+')具有安全、安静和舒适的睡眠环境可以让您的睡眠质量得到显著的改善。'); promptTextMessageTemp.push('5.改善意愿:您对改善自己睡眠质量的意愿(不足//适中//充分)。任何的改变都意味着要对以往你熟悉的环境,已经能够接受的不适进行改变甚至颠覆,不知道您愿不愿意为改善自己睡眠多一点付出,承受一点便或者痛苦呢?'); this.setData({ promptTextMessage: promptTextMessageTemp, totalAll: totalAllTemp, }) }, 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){ 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' || a=='c' || a=='d'){ if(b>=1 && b<=9){ return '不足'; }else if(b>=10 && b<=12){ return '适中'; }else if(b>=13){ return '过长'; }else{ return '未知'; } }else if(a=='e' || a=='f' || a=='g'){ if(b>=1 && b<=9){ return '不足'; }else if(b>=10 && b<=12){ return '适中'; }else if(b>=13){ return '过长'; }else{ return '未知'; } }else if(a=='h' || a=='i' || a=='j'){ if(b>=1 && b<=9){ return '不足'; }else if(b>=10 && b<=12){ return '适中'; }else if(b>=13){ return '过长'; }else{ return '未知'; } }else{ return '未知'; } }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { for(var i=0; i<27; i++) { ans[i] = ""; } this.setData({ privacyDetailShow: false }); // 绘制圆环 // this.drawCircle(); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })