| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721 |
- // 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: '',
- input1Value: '',
- input2Value: '',
- input3Value: '',
- input4Value: '',
- input5Value: '',
- input6Value: '',
- },
- // 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();
- // },
- handleTouchMove: function() {
- return false; // 阻止手动滑动
- },
- 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
- })
- },
- bindInput3: function(e) {
- // 你可以通过 e.detail.value 获取到输入框的值
- // 使用 this.setData() 方法更新数据
- this.setData({
- input3Value: e.detail.value
- })
- },
- bindInput4: function(e) {
- // 你可以通过 e.detail.value 获取到输入框的值
- // 使用 this.setData() 方法更新数据
- this.setData({
- input4Value: e.detail.value
- })
- },
- bindInput5: function(e) {
- // 你可以通过 e.detail.value 获取到输入框的值
- // 使用 this.setData() 方法更新数据
- this.setData({
- input5Value: e.detail.value
- })
- },
- bindInput6: function(e) {
- // 你可以通过 e.detail.value 获取到输入框的值
- // 使用 this.setData() 方法更新数据
- this.setData({
- input6Value: e.detail.value
- })
- },
- nextq: function (e) {
- if (!this.data.checkboxes[0].checked) {
- wx.showToast({
- title: '请勾选隐私条款',
- icon: 'none'
- });
- return;
- }
- let idInput = e.currentTarget.dataset.id;
- console.log('idInput====='+idInput)
- console.log('input1Value=='+this.data.input1Value);
- console.log('input2Value=='+this.data.input2Value);
- console.log('input3Value=='+this.data.input3Value);
- console.log('input4Value=='+this.data.input4Value);
- console.log('input5Value=='+this.data.input5Value);
- console.log('input6Value=='+this.data.input6Value);
- let input1 = this.data.input1Value;
- let input2 = this.data.input2Value;
- let input3 = this.data.input3Value;
- let input4 = this.data.input4Value;
- let input5 = this.data.input5Value;
- let input6 = this.data.input6Value;
- if(idInput == 0){
- if(!input1 || !input2){
- wx.showToast({
- title: '输入框不能为空',
- icon: 'none'
- });
- return;
- }
- if(input1){
- if(!(input1>=0 && input1<=24) || !(input2>=0 && input2<=60)){
- wx.showToast({
- title: '输入框时间填写错误',
- icon: 'none'
- });
- return;
- }
- }
- }
-
- if(idInput == 2){
- if(!input3 || !input4){
- wx.showToast({
- title: '输入框不能为空',
- icon: 'none'
- });
- return;
- }
- if(input1){
- if(!(input3>=0 && input3<=24) || !(input4>=0 && input4<=60)){
- wx.showToast({
- title: '输入框时间填写错误',
- icon: 'none'
- });
- return;
- }
- }
- }
- if(idInput == 3){
- if(!input5 || !input6){
- wx.showToast({
- title: '输入框不能为空',
- icon: 'none'
- });
- return;
- }
- if(input1){
- if(!(input5>=0 && input5<=24) || !(input6>=0 && input6<=60)){
- 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;
- let input3 = this.data.input3Value;
- let input4 = this.data.input4Value;
- let input5 = this.data.input5Value;
- let input6 = this.data.input6Value;
- // if(!input1 || !input2){
- // wx.showToast({
- // title: '输入框不能为空',
- // icon: 'none'
- // });
- // return;
- // }
-
- var a = "";
- let _id = e.currentTarget.dataset.id;
- var id = this.data.id-1;
- console.log('id==+=='+id);
- if(e.detail.value.answer){
- a = e.detail.value.answer;
- }else{
- if(id == 0){
- if(input1 && input2){
- a = input1+":"+input2
- }else{
- // a = "";
- }
- }
- if(id == 2){
- if(input3 && input4){
- a = input3+":"+input4
- }else{
- // a = "";
- }
- }
- if(id == 3){
- if(input5 && input6){
- a = input5+":"+input6
- }else{
- // a = "";
- }
- }
- }
-
- 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: i
- })
- }
- })
- }, 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 () {
- }
- })
|