index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. var app = getApp()
  2. var api = require('../../utils/api.js');
  3. const SEC = 1000
  4. const MIN = 60 * SEC
  5. const HOUR = 60 * MIN
  6. Page({
  7. data: {
  8. currentLatitude: 0, //用户当前纬度
  9. currentLongitude: 0, //用户当前经度
  10. is_btn_disabled: true, //开始按钮状态
  11. locationAccuracy: 0, //gps 误差
  12. isLearning: false, //是否在学习中?
  13. startTime: null, //开始时间
  14. clock: '00:00:00', //计时
  15. shareCanvasWidth: 200,
  16. shareCanvasHeight: 280,
  17. },
  18. onLoad: function () {
  19. wx.getSystemInfo({
  20. success: (res) => {
  21. this.setData({
  22. screenWidth: res.screenWidth,
  23. screenHeight: res.screenHeight,
  24. bannerWidth: res.screenWidth,
  25. bannerHeight: res.screenWidth * 363 / 543
  26. })
  27. }
  28. })
  29. var pt_student = wx.getStorageSync('pt_student')
  30. if (!pt_student) {
  31. wx.redirectTo({
  32. url: '/pages/login/index',
  33. })
  34. } else {
  35. app.globalData.ptStudent = pt_student;
  36. }
  37. wx.getLocation({
  38. success: (res) => {
  39. let validLocation = this.validLocation(res.latitude, res.longitude)
  40. this.setData({
  41. currentLatitude: res.latitude,
  42. currentLongitude: res.longitude,
  43. accuracy: res.accuracy,
  44. is_btn_disabled: !validLocation
  45. })
  46. }
  47. })
  48. },
  49. validLocation(latitude, longitude) {
  50. let res = api.isTest ? true : false;
  51. wx.request({
  52. url: api.checkPositionUrl,
  53. method: 'GET',
  54. data: {
  55. latitude: latitude,
  56. longitude: longitude
  57. },
  58. success: res => {
  59. if(res.data.status == 'success' && res.data.result == 'ok') {
  60. res = true;
  61. }
  62. }
  63. });
  64. return res;
  65. },
  66. handleBtnClick() {
  67. let isLearning = this.data.isLearning
  68. let is_btn_disabled = this.data.is_btn_disabled
  69. if (is_btn_disabled) {
  70. wx.showToast({
  71. title: '请到学校之后再打卡',
  72. icon: 'none',
  73. duration: 800
  74. })
  75. return;
  76. }
  77. if (isLearning) {
  78. this.setData({
  79. isLearning: !isLearning
  80. })
  81. this.endClock()
  82. } else {
  83. this.setData({
  84. isLearning: !isLearning,
  85. startTime: new Date().getTime()
  86. })
  87. this.startClock()
  88. }
  89. },
  90. startClock() {
  91. let interval = setInterval(() => {
  92. let now = new Date().getTime()
  93. let startTime = this.data.startTime
  94. let diff = now - startTime
  95. let hours = Math.floor(diff / HOUR)
  96. hours = (hours < 10 ? '0' + hours : hours)
  97. diff = diff % HOUR
  98. let mins = Math.floor(diff / MIN)
  99. mins = (mins < 10 ? '0' + mins : mins)
  100. diff = diff % MIN
  101. let sec = Math.ceil(diff / SEC)
  102. sec = (sec < 10 ? '0' + sec : sec)
  103. this.setData({
  104. clock: "" + hours + ":" + mins + ":" + sec
  105. })
  106. }, SEC)
  107. wx.request({
  108. url: api.startCheckCardUrl,
  109. method: 'GET',
  110. data: {
  111. 'student_id': app.globalData.ptStudent.id
  112. },
  113. success: res => {
  114. }
  115. })
  116. this.interval = interval
  117. },
  118. endClock() {
  119. clearInterval(this.interval)
  120. this.setData({
  121. isLearning: false,
  122. showPopup: true
  123. })
  124. const ctx = wx.createCanvasContext('shareCanvas')
  125. let width = this.data.shareCanvasWidth
  126. let height = this.data.shareCanvasHeight
  127. ctx.fillStyle = "#fff"
  128. ctx.fillRect(0, 0, width, height)
  129. ctx.fillStyle = "#000"
  130. ctx.setFontSize(16)
  131. ctx.textAlign = 'center'
  132. ctx.fillText('我已成功打卡14天', width / 2, height / 2 - 7)
  133. ctx.draw()
  134. },
  135. togglePopup() {
  136. this.setData({
  137. showPopup: !this.data.showPopup
  138. });
  139. },
  140. handleShare() {
  141. wx.canvasToTempFilePath({
  142. canvasId: 'shareCanvas',
  143. success: (res) => {
  144. wx.saveImageToPhotosAlbum({
  145. filePath: res.tempFilePath,
  146. success: () => {
  147. wx.showToast({
  148. title: '已保存到相册'
  149. })
  150. }
  151. })
  152. }
  153. }, this)
  154. }
  155. })