index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. let that = this;
  70. if (is_btn_disabled) {
  71. wx.showToast({
  72. title: '请到学校之后再打卡',
  73. icon: 'none',
  74. duration: 800
  75. })
  76. return;
  77. }
  78. if (isLearning) {
  79. this.setData({
  80. isLearning: !isLearning
  81. })
  82. this.endClock()
  83. } else {
  84. wx.request({
  85. url: api.startCheckCardUrl,
  86. method: 'GET',
  87. data: {
  88. 'student_id': app.globalData.ptStudent.id
  89. },
  90. success: res => {
  91. if (res.data.status == 'success') {
  92. that.setData({
  93. isLearning: !isLearning,
  94. startTime: new Date().getTime()
  95. })
  96. that.startClock()
  97. } else {
  98. wx.showToast({
  99. title: res.data.info,
  100. icon: 'none',
  101. duration: 800
  102. })
  103. }
  104. }
  105. })
  106. }
  107. },
  108. startClock() {
  109. let interval = setInterval(() => {
  110. let now = new Date().getTime()
  111. let startTime = this.data.startTime
  112. let diff = now - startTime
  113. let hours = Math.floor(diff / HOUR)
  114. hours = (hours < 10 ? '0' + hours : hours)
  115. diff = diff % HOUR
  116. let mins = Math.floor(diff / MIN)
  117. mins = (mins < 10 ? '0' + mins : mins)
  118. diff = diff % MIN
  119. let sec = Math.ceil(diff / SEC)
  120. sec = (sec < 10 ? '0' + sec : sec)
  121. this.setData({
  122. clock: "" + hours + ":" + mins + ":" + sec
  123. })
  124. }, SEC)
  125. this.interval = interval
  126. },
  127. endClock() {
  128. clearInterval(this.interval)
  129. this.setData({
  130. isLearning: false,
  131. showPopup: true
  132. })
  133. const ctx = wx.createCanvasContext('shareCanvas')
  134. let width = this.data.shareCanvasWidth
  135. let height = this.data.shareCanvasHeight
  136. ctx.fillStyle = "#fff"
  137. ctx.fillRect(0, 0, width, height)
  138. ctx.fillStyle = "#000"
  139. ctx.setFontSize(16)
  140. ctx.textAlign = 'center'
  141. ctx.fillText('我已成功打卡14天', width / 2, height / 2 - 7)
  142. ctx.draw()
  143. },
  144. togglePopup() {
  145. this.setData({
  146. showPopup: !this.data.showPopup
  147. });
  148. },
  149. handleShare() {
  150. wx.canvasToTempFilePath({
  151. canvasId: 'shareCanvas',
  152. success: (res) => {
  153. wx.saveImageToPhotosAlbum({
  154. filePath: res.tempFilePath,
  155. success: () => {
  156. wx.showToast({
  157. title: '已保存到相册'
  158. })
  159. }
  160. })
  161. }
  162. }, this)
  163. }
  164. })