testSAS.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. // miniprogram/pages/testSAS/testSAS.js
  2. // const db = wx.cloud.database()
  3. const qnaire = require("./sas.js")
  4. var ans = new Array(24)
  5. var ansJson = []
  6. Page({
  7. data: {
  8. qnaire: qnaire.qnaire,
  9. answer: ans,
  10. answerJson: [],
  11. id: 0,
  12. inputValue: '' ,// 初始值为空字符串
  13. BMI: null,
  14. weight: 0,
  15. height: 0,
  16. promptTextMessage: [],
  17. totalAll: 0,
  18. isPrivacyAgreed: false, // 隐私条款是否勾选
  19. privacyDetailShow: false ,// 隐私条款弹出页是否显示
  20. checkboxes: [
  21. { name: 'checkbox1', checked: false },
  22. ],
  23. changeValue:'',//
  24. comScore: 0,
  25. comScoreStatus: '',
  26. input1Value: '',
  27. input2Value: '',
  28. input3Value: '',
  29. input4Value: '',
  30. input5Value: '',
  31. input6Value: '',
  32. },
  33. // drawCircle: function() {
  34. // // 创建 canvas 上下文
  35. // const ctx = wx.createCanvasContext('myCanvas');
  36. // // 设置圆环的颜色和样式
  37. // ctx.setStrokeStyle('green'); // 圆环的颜色
  38. // ctx.setLineWidth(20); // 圆环的宽度
  39. // // 绘制外圆
  40. // ctx.beginPath();
  41. // ctx.arc(90, 80, 80, 0, 2 * Math.PI); // 圆心 (100, 100),半径 100
  42. // ctx.stroke(); // 绘制外圆
  43. // // 绘制内圆(与外圆同圆心,但半径小20rpx,即80rpx)
  44. // ctx.beginPath();
  45. // ctx.arc(90, 80, 80, 0, 2 * Math.PI);
  46. // ctx.setFillStyle('white'); // 设置内圆的填充颜色为白色(或者透明,取决于你希望的效果)
  47. // ctx.fill(); // 填充内圆,这样圆环内部就是白色或透明的了
  48. // // 结束绘制并更新 canvas
  49. // ctx.draw();
  50. // },
  51. hideModal: function() {
  52. this.setData({
  53. privacyDetailShow: false
  54. });
  55. },
  56. checkboxChange: function(e) {
  57. // e.detail.value 是一个数组,包含了所有被选中的 checkbox 的 value
  58. const checkedValues = e.detail.value;
  59. // 遍历 checkboxes 数组,根据 checkedValues 更新 checked 属性
  60. const updatedCheckboxes = this.data.checkboxes.map(checkbox => {
  61. return {
  62. ...checkbox,
  63. // 如果 checkbox 的 name 在 checkedValues 中,则设为 true,否则设为 false
  64. checked: checkedValues.includes(checkbox.name)
  65. };
  66. });
  67. // 更新 data 中的 checkboxes 数组
  68. this.setData({
  69. checkboxes: updatedCheckboxes
  70. });
  71. },
  72. showPrivacyDetail: function() {
  73. this.setData({
  74. privacyDetailShow: true
  75. });
  76. // 这里可以添加代码来显示隐私条款弹出页,比如使用 wx.showModal 或自定义组件
  77. },
  78. hidePrivacyPolicy() {
  79. this.setData({
  80. privacyDetailShow: false
  81. });
  82. },
  83. // otherButtonTap: function() {
  84. // if (!this.data.isPrivacyAgreed) {
  85. // wx.showToast({
  86. // title: '请勾选隐私条款',
  87. // icon: 'none'
  88. // });
  89. // return;
  90. // }
  91. // // 如果隐私条款已勾选,执行其他操作
  92. // },
  93. handleInputChange: function(e) {
  94. this.setData({
  95. height: e.detail.value
  96. });
  97. if(this.data.id == 1){
  98. this.setData({
  99. weight: e.detail.value
  100. });
  101. }
  102. if(this.data.id == 3){
  103. this.setData({
  104. weight: e.detail.value
  105. });
  106. if (this.data.weight >= 0 || this.data.height >= 0) {
  107. var bmi = this.data.weight / (this.data.height * this.data.height);
  108. this.setData({
  109. BMI: bmi
  110. });
  111. }
  112. }
  113. },
  114. radioChange: function (e) {
  115. console.log(e.detail.value)
  116. this.setData({
  117. changeValue: e.detail.value
  118. });
  119. },
  120. bindInput1: function(e) {
  121. // 你可以通过 e.detail.value 获取到输入框的值
  122. // 使用 this.setData() 方法更新数据
  123. this.setData({
  124. input1Value: e.detail.value
  125. })
  126. },
  127. bindInput2: function(e) {
  128. // 你可以通过 e.detail.value 获取到输入框的值
  129. // 使用 this.setData() 方法更新数据
  130. this.setData({
  131. input2Value: e.detail.value
  132. })
  133. },
  134. bindInput3: function(e) {
  135. // 你可以通过 e.detail.value 获取到输入框的值
  136. // 使用 this.setData() 方法更新数据
  137. this.setData({
  138. input3Value: e.detail.value
  139. })
  140. },
  141. bindInput4: function(e) {
  142. // 你可以通过 e.detail.value 获取到输入框的值
  143. // 使用 this.setData() 方法更新数据
  144. this.setData({
  145. input4Value: e.detail.value
  146. })
  147. },
  148. bindInput5: function(e) {
  149. // 你可以通过 e.detail.value 获取到输入框的值
  150. // 使用 this.setData() 方法更新数据
  151. this.setData({
  152. input5Value: e.detail.value
  153. })
  154. },
  155. bindInput6: function(e) {
  156. // 你可以通过 e.detail.value 获取到输入框的值
  157. // 使用 this.setData() 方法更新数据
  158. this.setData({
  159. input6Value: e.detail.value
  160. })
  161. },
  162. nextq: function (e) {
  163. if (!this.data.checkboxes[0].checked) {
  164. wx.showToast({
  165. title: '请勾选隐私条款',
  166. icon: 'none'
  167. });
  168. return;
  169. }
  170. let idInput = e.currentTarget.dataset.id;
  171. console.log('idInput====='+idInput)
  172. console.log('input1Value=='+this.data.input1Value);
  173. console.log('input2Value=='+this.data.input2Value);
  174. console.log('input3Value=='+this.data.input3Value);
  175. console.log('input4Value=='+this.data.input4Value);
  176. console.log('input5Value=='+this.data.input5Value);
  177. console.log('input6Value=='+this.data.input6Value);
  178. let input1 = this.data.input1Value;
  179. let input2 = this.data.input2Value;
  180. let input3 = this.data.input3Value;
  181. let input4 = this.data.input4Value;
  182. let input5 = this.data.input5Value;
  183. let input6 = this.data.input6Value;
  184. if(idInput == 0){
  185. if(!input1 || !input2){
  186. wx.showToast({
  187. title: '输入框不能为空',
  188. icon: 'none'
  189. });
  190. return;
  191. }
  192. if(input1){
  193. if(!(input1>=0 && input1<=24) || !(input2>=0 && input2<=60)){
  194. wx.showToast({
  195. title: '输入框时间填写错误',
  196. icon: 'none'
  197. });
  198. return;
  199. }
  200. }
  201. }
  202. if(idInput == 2){
  203. if(!input3 || !input4){
  204. wx.showToast({
  205. title: '输入框不能为空',
  206. icon: 'none'
  207. });
  208. return;
  209. }
  210. if(input1){
  211. if(!(input3>=0 && input3<=24) || !(input4>=0 && input4<=60)){
  212. wx.showToast({
  213. title: '输入框时间填写错误',
  214. icon: 'none'
  215. });
  216. return;
  217. }
  218. }
  219. }
  220. if(idInput == 3){
  221. if(!input5 || !input6){
  222. wx.showToast({
  223. title: '输入框不能为空',
  224. icon: 'none'
  225. });
  226. return;
  227. }
  228. if(input1){
  229. if(!(input5>=0 && input5<=24) || !(input6>=0 && input6<=60)){
  230. wx.showToast({
  231. title: '输入框时间填写错误',
  232. icon: 'none'
  233. });
  234. return;
  235. }
  236. }
  237. }
  238. if (this.data.id < 23) {
  239. this.setData({
  240. id: this.data.id + 1,
  241. })
  242. }
  243. },
  244. lastq: function (e) {
  245. if (!this.data.checkboxes[0].checked) {
  246. wx.showToast({
  247. title: '请勾选隐私条款',
  248. icon: 'none'
  249. });
  250. return;
  251. }
  252. if (this.data.id != 0) {
  253. this.setData({
  254. id: this.data.id - 1,
  255. })
  256. }
  257. },
  258. calculateTotalHours: function (hoursInput,minutesInput) {
  259. // 获取小时和分钟的值,如果为空或不是数字,则设置为0
  260. let hours = parseFloat(hoursInput.value) || 0;
  261. let minutes = parseFloat(minutesInput.value) || 0;
  262. // 将分钟转换为小时的小数部分
  263. // let minutesInHours = minutes / 60;
  264. // 计算总时间(小时)
  265. // let totalHours = hours + minutesInHours;
  266. // 显示结果
  267. // document.getElementById('result').textContent = `总时间: ${totalHours.toFixed(2)} 小时`;
  268. // return totalHours.toFixed(2);
  269. return hours*60 + minutes;
  270. },
  271. submit: function (e) {
  272. if (!this.data.checkboxes[0].checked) {
  273. return;
  274. }
  275. // let input1 = e.detail.value.input1;
  276. // let input2 = e.detail.value.input2;
  277. let input1 = this.data.input1Value;
  278. let input2 = this.data.input2Value;
  279. let input3 = this.data.input3Value;
  280. let input4 = this.data.input4Value;
  281. let input5 = this.data.input5Value;
  282. let input6 = this.data.input6Value;
  283. // if(!input1 || !input2){
  284. // wx.showToast({
  285. // title: '输入框不能为空',
  286. // icon: 'none'
  287. // });
  288. // return;
  289. // }
  290. var a = "";
  291. let _id = e.currentTarget.dataset.id;
  292. var id = this.data.id-1;
  293. console.log('id==+=='+id);
  294. if(e.detail.value.answer){
  295. a = e.detail.value.answer;
  296. }else{
  297. if(id == 0){
  298. if(input1 && input2){
  299. a = input1+":"+input2
  300. }else{
  301. // a = "";
  302. }
  303. }
  304. if(id == 2){
  305. if(input3 && input4){
  306. a = input3+":"+input4
  307. }else{
  308. // a = "";
  309. }
  310. }
  311. if(id == 3){
  312. if(input5 && input6){
  313. a = input5+":"+input6
  314. }else{
  315. // a = "";
  316. }
  317. }
  318. }
  319. var obj = {};
  320. obj.type = e.detail.value.answer?'answer':'input'
  321. obj.value = a
  322. ans[id] = a;
  323. ansJson[id] = obj;
  324. this.setData({
  325. answer: ans,
  326. answerJson: ansJson,
  327. })
  328. console.log("submit-answer="+this.data.answer);
  329. console.log("e.detail.value.answer="+e.detail.value.answer);
  330. console.log("e.detail.value.input="+e.detail.value.input);
  331. },
  332. formSubmit: function() {
  333. if (!this.data.checkboxes[0].checked) {
  334. return;
  335. }
  336. var a = this.data.changeValue;
  337. var id = 23;
  338. var obj = {};
  339. obj.type = 'answer'
  340. obj.value = a
  341. ans[id] = a;
  342. ansJson[id] = obj;
  343. this.setData({
  344. answer: ans,
  345. answerJson: ansJson,
  346. })
  347. var finish;
  348. var i = 0;
  349. var _this = this;
  350. while(i<24) {
  351. if(ans[i]=="") {
  352. finish='false';
  353. break;
  354. } else {
  355. finish='true';
  356. }
  357. i++;
  358. }
  359. if(finish=='false') {
  360. // if('true'=='false') {
  361. wx.showModal({
  362. title: '无法提交',
  363. content: '您还有部分题目未完成,请检查后重新提交',
  364. showCancel: false,
  365. confirmColor: '#fcbe39',
  366. confirmText: "好的",
  367. success (res) {
  368. _this.setData({
  369. id: i,
  370. })
  371. }
  372. })
  373. } else{
  374. wx.showLoading({
  375. title: '加载中',
  376. })
  377. setTimeout(function () {
  378. wx.hideLoading({
  379. success (res) {
  380. _this.answer2db(_this.data.answerJson);
  381. wx.navigateBack({
  382. delta: i
  383. })
  384. }
  385. })
  386. }, 2000)
  387. }
  388. },
  389. answer2db: function(answerJson) {
  390. // db.collection('SAS').add({
  391. // data: {
  392. // answer: this.data.answer
  393. // },
  394. // success (res) {
  395. // console.log(res._id);
  396. // },
  397. // fail (res) {
  398. // wx.showToast({
  399. // icon: 'none',
  400. // title: '新增记录失败'
  401. // })
  402. // console.error('[数据库] [新增记录] 失败:', err)
  403. // }
  404. // })
  405. console.log('answerJson='+JSON.stringify(answerJson))
  406. let totalAllTemp = 0;
  407. let value1 = answerJson[0].value;
  408. let value2 = answerJson[1].value;
  409. let value3 = answerJson[2].value;
  410. let value4 = answerJson[3].value;
  411. let value5a = answerJson[4].value;
  412. let value5b = answerJson[5].value;
  413. let value5c = answerJson[6].value;
  414. let value5d = answerJson[7].value;
  415. let value5e = answerJson[8].value;
  416. let value5f = answerJson[9].value;
  417. let value5g = answerJson[10].value;
  418. let value5h = answerJson[11].value;
  419. let value5i = answerJson[12].value;
  420. let value5j = answerJson[13].value;
  421. let value6 = answerJson[14].value;
  422. let value7 = answerJson[15].value;
  423. let value8 = answerJson[16].value;
  424. let value9 = answerJson[17].value;
  425. let value10 = answerJson[18].value;
  426. let value11 = answerJson[19].value;
  427. let value12 = answerJson[20].value;
  428. let value13 = answerJson[21].value;
  429. let value14 = answerJson[22].value;
  430. let value15 = answerJson[23].value;
  431. //计算规则 https://mp.weixin.qq.com/s/QyTzuPoNErMjc2KcA1M61g
  432. let comA = 0;//条目6
  433. let comB = 0;
  434. let comB1 = 0;
  435. let comC = 0;
  436. let comD = 0;
  437. let comE = 0;
  438. let comF = 0;
  439. let comG = 0;
  440. comA = this.resultABCD(value6);
  441. comB1 = this.resultABCD(value2) + this.resultABCD(value5a);
  442. console.log("comB1="+comB1);
  443. if(comB1 == 0){
  444. comB = 0;
  445. }else if(comB1>=1 && comB1<=2){
  446. comB = 1;
  447. } else if(comB1>=3 && comB1<=4){
  448. comB = 2;
  449. } else if(comB1>=5 && comB1<=6){
  450. comB = 3;
  451. }
  452. let value41 = Number(value4.split(':')[0]) || 0;
  453. let value42 = Number(value4.split(':')[1]) || 0;
  454. let value43 = value41*60 + value42
  455. console.log("value43="+value43);
  456. if(value43 > 420){
  457. comC = 0;
  458. }else if(value43>=360 && value43<=420){
  459. comC = 1;
  460. } else if(value43>=300 && value43<360){
  461. comC = 2;
  462. } else if(value43<300){
  463. comC = 3;
  464. }
  465. let datediff = this.calculateTimeDifference(value1,value3);
  466. console.log("datediff="+datediff);
  467. let sleepRate = datediff?((value43/datediff)*100):0;
  468. console.log("sleepRate="+sleepRate);
  469. if(sleepRate >= 85){
  470. comD = 0;
  471. }else if(sleepRate>=75 && sleepRate<=84){
  472. comD = 1;
  473. } else if(sleepRate>=65 && sleepRate<74){
  474. comD = 2;
  475. } else if(sleepRate<65){
  476. comD = 3;
  477. }
  478. let value5All = this.resultABCD(value5b)
  479. + this.resultABCD(value5c)
  480. + this.resultABCD(value5d)
  481. + this.resultABCD(value5e)
  482. + this.resultABCD(value5f)
  483. + this.resultABCD(value5g)
  484. + this.resultABCD(value5h)
  485. + this.resultABCD(value5i)
  486. + this.resultABCD(value5j);
  487. console.log("value5All="+value5All);
  488. if(value5All == 0){
  489. comE = 0;
  490. }else if(value5All>=1 && value5All<=9){
  491. comE = 1;
  492. } else if(value5All>=10 && value5All<=18){
  493. comE = 2;
  494. } else if(value5All>=19 && value5All<=27){
  495. comE = 3;
  496. }
  497. comF = this.resultABCD(value7);
  498. console.log("comF="+comF);
  499. let valueGAll = this.resultABCD(value8) + this.resultABCD(value9);
  500. console.log("valueGAll="+valueGAll);
  501. if(valueGAll == 0){
  502. comG = 0;
  503. }else if(valueGAll>=1 && valueGAll<=2){
  504. comG = 1;
  505. } else if(valueGAll>=3 && valueGAll<=4){
  506. comG = 2;
  507. } else if(valueGAll>=5 && valueGAll<=6){
  508. comG = 3;
  509. }
  510. console.log('comA='+comA);
  511. console.log('comB='+comB);
  512. console.log('comC='+comC);
  513. console.log('comD='+comD);
  514. console.log('comE='+comE);
  515. console.log('comF='+comF);
  516. console.log('comG='+comG);
  517. let comAll = comA + comB + comC + comD + comE + comF + comG;
  518. let _comScoreStatus = "";
  519. if(comAll >= 0 && comAll<=5){
  520. _comScoreStatus = "很好";
  521. }else if(comAll>=6 && comAll<=10){
  522. _comScoreStatus = "较好";
  523. } else if(comAll>=11 && comAll<=15){
  524. _comScoreStatus = "一般";
  525. } else if(comAll>=16 && comAll<=21){
  526. _comScoreStatus = "差";
  527. }
  528. let promptTextMessageTemp = []
  529. promptTextMessageTemp.push('sucess');
  530. console.log('comAll='+comAll+',comScoreStatus='+_comScoreStatus);
  531. this.setData({
  532. comScore: comAll,
  533. comScoreStatus: _comScoreStatus,
  534. promptTextMessage: promptTextMessageTemp,
  535. })
  536. },
  537. resultABCD:function(a){
  538. if(a == 'A'){
  539. return 0;
  540. }else if(a == 'B'){
  541. return 1;
  542. } else if(a == 'C'){
  543. return 2;
  544. } else if(a == 'D'){
  545. return 3;
  546. }
  547. },
  548. sleepState:function(a,b){
  549. console.log('a='+a+',b='+b);
  550. if(a=='A'){
  551. if(b>=1 && b<=9){
  552. return '不足';
  553. }else if(b>=10 && b<=12){
  554. return '适中';
  555. }else if(b>=13){
  556. return '过长';
  557. }else{
  558. return '未知';
  559. }
  560. }else if(a=='B'){
  561. if(b>=1 && b<=9){
  562. return '不足';
  563. }else if(b>=10 && b<=12){
  564. return '适中';
  565. }else if(b>=13){
  566. return '过长';
  567. }else{
  568. return '未知';
  569. }
  570. }else if(a=='C'){
  571. if(b>=1 && b<=9){
  572. return '不足';
  573. }else if(b>=10 && b<=12){
  574. return '适中';
  575. }else if(b>=13){
  576. return '过长';
  577. }else{
  578. return '未知';
  579. }
  580. }else if(a=='D'){
  581. if(b>=1 && b<=9){
  582. return '不足';
  583. }else if(b>=10 && b<=12){
  584. return '适中';
  585. }else if(b>=13){
  586. return '过长';
  587. }else{
  588. return '未知';
  589. }
  590. }else{
  591. return '未知';
  592. }
  593. },
  594. // 将时间字符串转换为分钟数
  595. timeStringToMinutes:function(timeStr) {
  596. var parts = timeStr.split(":");
  597. return parseInt(parts[0], 10) * 60 + parseInt(parts[1], 10);
  598. },
  599. // 将分钟数转换回时间字符串格式 HH:mm
  600. minutesToTimeString:function(minutes) {
  601. var hours = Math.floor(minutes / 60);
  602. var mins = minutes % 60;
  603. var minsStr = mins < 10 ? "0" + mins : mins.toString();
  604. return hours.toString() + ":" + minsStr;
  605. },
  606. // 计算时间差(分钟)
  607. calculateTimeDifference:function(sleepTimeStr, wakeUpTimeStr) {
  608. // 将时间字符串转换为分钟
  609. var sleepTimeMinutes = this.timeStringToMinutes(sleepTimeStr);
  610. var wakeUpTimeMinutes = this.timeStringToMinutes("24:00") + this.timeStringToMinutes(wakeUpTimeStr); // 加上第二天的0点到起床时间的分钟数
  611. // 计算时间差
  612. var diffInMinutes = wakeUpTimeMinutes - sleepTimeMinutes;
  613. // 将分钟数转换回时间字符串格式 HH:mm
  614. var diffInHours = Math.floor(diffInMinutes / 60);
  615. var diffInMins = diffInMinutes % 60;
  616. // 返回时间差字符串
  617. console.log("diffInMinutes=="+diffInMinutes)
  618. return diffInMinutes;
  619. },
  620. /**
  621. * 生命周期函数--监听页面加载
  622. */
  623. onLoad: function (options) {
  624. for(var i=0; i<24; i++) {
  625. ans[i] = "";
  626. }
  627. this.setData({
  628. privacyDetailShow: false
  629. });
  630. // 绘制圆环
  631. // this.drawCircle();
  632. },
  633. /**
  634. * 生命周期函数--监听页面初次渲染完成
  635. */
  636. onReady: function () {
  637. },
  638. /**
  639. * 生命周期函数--监听页面显示
  640. */
  641. onShow: function () {
  642. },
  643. /**
  644. * 生命周期函数--监听页面隐藏
  645. */
  646. onHide: function () {
  647. },
  648. /**
  649. * 生命周期函数--监听页面卸载
  650. */
  651. onUnload: function () {
  652. },
  653. /**
  654. * 页面相关事件处理函数--监听用户下拉动作
  655. */
  656. onPullDownRefresh: function () {
  657. },
  658. /**
  659. * 页面上拉触底事件的处理函数
  660. */
  661. onReachBottom: function () {
  662. },
  663. /**
  664. * 用户点击右上角分享
  665. */
  666. onShareAppMessage: function () {
  667. }
  668. })