result.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // pages/result/result.js
  2. const { throttle } = require('../../utils/util.js')
  3. const wxCharts = require("../../utils/wxcharts-min.js");
  4. const app = getApp();
  5. var numCount = 6;
  6. var numSlot = 10;
  7. var sys = wx.getSystemInfoSync();
  8. var windowWidth = sys.windowWidth;
  9. var mW = windowWidth;
  10. var mH = 520;
  11. var mCenter = mW/ 2.33; //中心点
  12. var mAngle = Math.PI * 2 / numCount; //角度
  13. var mRadius = mCenter - 60; //半径(减去的值用于给绘制的文本留空间)
  14. //获取Canvas
  15. var radCtx = wx.createCanvasContext("result")
  16. Page({
  17. /**
  18. * 页面的初始数据
  19. */
  20. data: {
  21. number:0,
  22. isRefer:0,
  23. isTest:1,
  24. statusBar: app.globalData.statusBar,
  25. isHistory:0,
  26. list:[
  27. {
  28. text: '图形推理',
  29. value:36,
  30. appraisal: '图形推理这么强,你上辈⼦⼀定是裁缝!'
  31. },
  32. {
  33. text: '心理旋转',
  34. value: 48,
  35. appraisal: '你对物体旋转的观察⼒都超过我了!'
  36. },
  37. {
  38. text: '空间想象',
  39. value: 64,
  40. appraisal: '你这空间想象⼒,不当建筑师⽩瞎了!'
  41. },
  42. {
  43. text: '视觉搜索',
  44. value: 85,
  45. appraisal: '搜索能⼒这么强,你的脑袋上⼀定装了雷达!'
  46. },
  47. {
  48. text: '工作记忆',
  49. value:60,
  50. appraisal: '好强的观察能⼒,⼀点点的变化都逃不过你的法眼!'
  51. },
  52. {
  53. text: '执行功能',
  54. value: 100,
  55. appraisal: '反应能⼒这么快,你⼀定练过武!'
  56. }
  57. ]
  58. },
  59. /**
  60. * 生命周期函数--监听页面加载
  61. */
  62. onLoad: function (options) {
  63. console.log(options)
  64. this.setData(options);
  65. app.getWxloginCode(code => {
  66. this.setData({ code })
  67. })
  68. const { isRefer,list}=this.data
  69. const userInfo = wx.getStorageSync('userInfo')
  70. if (userInfo) {
  71. this.setData({ userInfo})
  72. }
  73. if (isRefer == 1) {
  74. const { graphicReasoning,spaceImagination,executionFunctionality,visualSearch,workingMemory,mentalRotation,list } = this.data
  75. var score = Number(spaceImagination) + Number(executionFunctionality) + Number(visualSearch) + Number(workingMemory) + Number(mentalRotation) + Number(graphicReasoning);
  76. list[0].value=graphicReasoning
  77. list[1].value=mentalRotation
  78. list[2].value=spaceImagination
  79. list[3].value=visualSearch
  80. list[4].value=workingMemory
  81. list[5].value=executionFunctionality
  82. this.setData({ score:score,list,isTest:0})
  83. const arr=[]
  84. for(var i in list){
  85. if(list[i].value>90||list[i].value==90){
  86. arr.push(list[i].appraisal)
  87. }
  88. }
  89. // if(arr.length>0){
  90. // this.setData({ appraisal: arr[Math.floor(Math.random()*(arr.length))] })
  91. // if(arr.length>5){
  92. // this.setData({ appraisal: '我的天,你的⼤脑⼀定被爱因斯坦摸过!' })
  93. // }
  94. // }else if(score<361){
  95. this.setData({ appraisal: '原来你和我⼀样都是普通⼈!' })
  96. // }
  97. this.drawRadar();
  98. this.addScore();
  99. }else{
  100. this.findResult()
  101. }
  102. },
  103. /**
  104. * 生命周期函数--监听页面初次渲染完成
  105. */
  106. onReady: function () {
  107. },
  108. /**
  109. * 生命周期函数--监听页面显示
  110. */
  111. onShow: function () {
  112. },
  113. /**
  114. * 生命周期函数--监听页面隐藏
  115. */
  116. onHide: function () {
  117. },
  118. /**
  119. * 生命周期函数--监听页面卸载
  120. */
  121. onUnload: function () {
  122. },
  123. /**
  124. * 页面相关事件处理函数--监听用户下拉动作
  125. */
  126. onPullDownRefresh: function () {
  127. },
  128. /**
  129. * 页面上拉触底事件的处理函数
  130. */
  131. onReachBottom: function () {
  132. },
  133. /**
  134. * 用户点击右上角分享
  135. */
  136. onShareAppMessage: function () {
  137. if (app.sharePageDefaultCtl) {
  138. return app.sharePageDefaultCtl()
  139. }
  140. },
  141. /**
  142. * 路由跳转
  143. */
  144. navigateCtl: app.navigateCtl,
  145. /**
  146. * 底部导航跳转
  147. */
  148. redirectCtl: app.redirectCtl,
  149. /**
  150. * 返回上一级页面
  151. */
  152. blockCtl: function () {
  153. const {isHistory}=this.data
  154. if(isHistory==1){
  155. this.redirectCtl({ url: 'history', method: {} }, true)
  156. }else{
  157. this.redirectCtl({ url: 'me', method: {} }, true)
  158. }
  159. },
  160. /**
  161. * 微信授权
  162. */
  163. bindGetUserInfo: function (e) {
  164. const { code } = this.data;
  165. if (e.detail.userInfo) {
  166. app.saveUserInfo(e.detail, code)
  167. } else {
  168. wx.showToast({
  169. title: '微信授权失败',
  170. icon: 'none'
  171. })
  172. }
  173. },
  174. // 知识报告雷达
  175. knowledgeCanvas: function () {
  176. var sys = wx.getSystemInfoSync();
  177. var windowWidth = sys.windowWidth;
  178. const { graphicReasoning,spaceImagination,executionFunctionality,visualSearch,workingMemory,mentalRotation } = this.data;
  179. // console.log(graphicReasoning,spaceImagination,executionFunctionality,visualSearch,workingMemory,mentalRotation);
  180. new wxCharts({
  181. animation: true,
  182. canvasId: 'result',
  183. type: 'radar',
  184. categories: ['空间想象', '执行功能', '视觉搜索', '工作记忆','心理旋转','图形推理'],
  185. series: [{
  186. name: '',
  187. data: [spaceImagination, executionFunctionality, visualSearch, workingMemory, mentalRotation, graphicReasoning],
  188. }],
  189. width: windowWidth,
  190. height: 250,
  191. dataPointShape: true,
  192. legend: false,
  193. extra: {
  194. radar: {
  195. max: 100,//雷达数值的最大值
  196. gridColor: '#000',
  197. labelColor: '#000'
  198. }
  199. }
  200. });
  201. },
  202. /**
  203. * 保存游戏结果
  204. */
  205. addScore: function () {
  206. const postData = { fn: this.addScore, param: { ...arguments } }
  207. var that=this;
  208. const { gameRespondentId,appraisal, score,level,graphicReasoning,spaceImagination,executionFunctionality,visualSearch,workingMemory,mentalRotation} = that.data;
  209. app.post('/api/game/v2/saveV2', { gameRespondentId,appraisal, score, level,graphicReasoning,spaceImagination,executionFunctionality,visualSearch,workingMemory,mentalRotation}, 0, postData).then(res => {
  210. // wx.showToast({
  211. // title: '保存成功',
  212. // icon: 'none'
  213. // })
  214. })
  215. },
  216. /**
  217. * 查询测试结果
  218. */
  219. findResult:function(){
  220. const postData = { fn: this.findResult, param: { ...arguments } }
  221. var that = this;
  222. const {list,gameRecordId=''}=this.data;
  223. app.post('/api/game/v2/findResult', {gameRecordId}, 0, postData).then(res => {
  224. const { appraisal, score, level,totalScore,graphicReasoning,spaceImagination,executionFunctionality,visualSearch,workingMemory,mentalRotation}=res.data;
  225. list[0].value=graphicReasoning
  226. list[1].value=mentalRotation
  227. list[2].value=spaceImagination
  228. list[3].value=visualSearch
  229. list[4].value=workingMemory
  230. list[5].value=executionFunctionality
  231. that.setData({ appraisal, score, level,totalScore,graphicReasoning,spaceImagination,executionFunctionality,visualSearch,workingMemory,mentalRotation,list,isTest:0});
  232. that.drawRadar();
  233. })
  234. },
  235. /**
  236. * 去测试
  237. */
  238. getTest:function(){
  239. const { level, ageGroup} = this.data;
  240. this.redirectCtl({ url: 'set_info', method: {} }, true)
  241. // if (ageGroup < 0) {
  242. // this.redirectCtl({ url: 'set_info', method: { level: 0, ageGroup } }, true)
  243. // } else {
  244. // this.redirectCtl({ url: 'tu_xing_tui_li', method: { level: 0, ageGroup } }, true)
  245. // }
  246. },
  247. /**
  248. * 挑战下一关
  249. */
  250. nextCard:function(){
  251. const { level, ageGroup } = this.data;
  252. console.log(level, ageGroup)
  253. this.redirectCtl({ url: 'tu_xing_tui_li', method: { level: level+1, ageGroup } }, true)
  254. },
  255. // 雷达图
  256. drawRadar:function(){
  257. // var sourceData1 = this.data.chanelArray1
  258. const { graphicReasoning,spaceImagination,executionFunctionality,visualSearch,workingMemory,mentalRotation } = this.data;
  259. var sourceData1 = [['空间想象',spaceImagination], ['执行功能',executionFunctionality], ['视觉搜索',visualSearch], ['工作记忆',workingMemory],['心理旋转',mentalRotation],['图形推理',graphicReasoning]]
  260. //调用
  261. this.drawEdge() //画六边形
  262. //this.drawArcEdge() //画圆
  263. this.drawLinePoint()
  264. //设置数据
  265. this.drawRegion(sourceData1,'rgba(118,205,255,0.7)') //第一个人的
  266. //设置文本数据
  267. this.drawTextCans(sourceData1)
  268. //设置节点
  269. this.drawCircle(sourceData1,'#76CDFF')
  270. // this.drawCircle(sourceData2,'yellow')
  271. //开始绘制
  272. radCtx.draw()
  273. },
  274. // 绘制6条边
  275. drawEdge: function(){
  276. radCtx.setStrokeStyle("#000000")
  277. radCtx.setLineWidth(1) //设置线宽
  278. for (var i = 0; i < numSlot; i++) {
  279. //计算半径
  280. radCtx.beginPath()
  281. var rdius = mRadius / numSlot * (i + 1)
  282. //画6条线段
  283. for (var j = 0; j < numCount; j++) {
  284. //坐标
  285. var x = mCenter + rdius * Math.cos(mAngle * j);
  286. var y = mCenter + rdius * Math.sin(mAngle * j);
  287. radCtx.lineTo(x, y);
  288. }
  289. radCtx.closePath()
  290. radCtx.stroke()
  291. }
  292. },
  293. // 绘制连接点
  294. drawLinePoint:function(){
  295. radCtx.beginPath();
  296. for (var k = 0; k < numCount; k++) {
  297. var x = mCenter + mRadius * Math.cos(mAngle * k);
  298. var y = mCenter + mRadius * Math.sin(mAngle * k);
  299. radCtx.moveTo(mCenter, mCenter);
  300. radCtx.lineTo(x, y);
  301. }
  302. radCtx.stroke();
  303. },
  304. //绘制数据区域(数据和填充颜色)
  305. drawRegion: function (mData,color){
  306. radCtx.beginPath();
  307. for (var m = 0; m < numCount; m++){
  308. var x = mCenter + mRadius * Math.cos(mAngle * m) * mData[m][1] / 100;
  309. var y = mCenter + mRadius * Math.sin(mAngle * m) * mData[m][1] / 100;
  310. radCtx.lineTo(x, y);
  311. }
  312. radCtx.closePath();
  313. radCtx.setFillStyle(color)
  314. radCtx.fill();
  315. },
  316. //绘制文字
  317. drawTextCans: function (mData){
  318. radCtx.setFillStyle("#000000")
  319. radCtx.font = 'bold 13px cursive' //设置字体
  320. for (var n = 0; n < numCount; n++) {
  321. var x = mCenter + mRadius * Math.cos(mAngle * n);
  322. var y = mCenter + mRadius * Math.sin(mAngle * n);
  323. // radCtx.fillText(mData[n][0], x, y);
  324. //通过不同的位置,调整文本的显示位置
  325. if (mAngle * n >= 0 && mAngle * n <= Math.PI / 2) {
  326. radCtx.fillText(mData[n][0], x+5, y+5);
  327. } else if (mAngle * n > Math.PI / 2 && mAngle * n <= Math.PI) {
  328. radCtx.fillText(mData[n][0], x - radCtx.measureText(mData[n][0]).width-7, y+5);
  329. } else if (mAngle * n > Math.PI && mAngle * n <= Math.PI * 3 / 2) {
  330. radCtx.fillText(mData[n][0], x - radCtx.measureText(mData[n][0]).width-5, y);
  331. } else {
  332. radCtx.fillText(mData[n][0], x+7, y+2);
  333. }
  334. }
  335. },
  336. //画点
  337. drawCircle: function(mData,color){
  338. var r = 3; //设置节点小圆点的半径
  339. for(var i = 0; i<numCount; i ++){
  340. var x = mCenter + mRadius * Math.cos(mAngle * i) * mData[i][1] / 100;
  341. var y = mCenter + mRadius * Math.sin(mAngle * i) * mData[i][1] / 100;
  342. radCtx.beginPath();
  343. radCtx.arc(x, y, r, 0, Math.PI * 2);
  344. radCtx.fillStyle = color;
  345. radCtx.fill();
  346. }
  347. }
  348. })