testSAS.js 17 KB

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