index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. wx.request({
  51. url: api.checkPosition,
  52. method: 'GET',
  53. data: {
  54. latitude: latitude,
  55. longitude: longitude
  56. },
  57. success: res => {
  58. console.log(res);
  59. }
  60. })
  61. return true;
  62. },
  63. handleBtnClick() {
  64. let isLearning = this.data.isLearning
  65. let is_btn_disabled = this.data.is_btn_disabled
  66. if (is_btn_disabled) {
  67. wx.showToast({
  68. title: '请到学校之后再打卡',
  69. icon: 'none',
  70. duration: 800
  71. })
  72. return;
  73. }
  74. if (isLearning) {
  75. this.setData({
  76. isLearning: !isLearning
  77. })
  78. this.endClock()
  79. } else {
  80. this.setData({
  81. isLearning: !isLearning,
  82. startTime: new Date().getTime()
  83. })
  84. this.startClock()
  85. }
  86. },
  87. startClock() {
  88. let interval = setInterval(() => {
  89. let now = new Date().getTime()
  90. let startTime = this.data.startTime
  91. let diff = now - startTime
  92. let hours = Math.floor(diff / HOUR)
  93. hours = (hours < 10 ? '0' + hours : hours)
  94. diff = diff % HOUR
  95. let mins = Math.floor(diff / MIN)
  96. mins = (mins < 10 ? '0' + mins : mins)
  97. diff = diff % MIN
  98. let sec = Math.ceil(diff / SEC)
  99. sec = (sec < 10 ? '0' + sec : sec)
  100. this.setData({
  101. clock: "" + hours + ":" + mins + ":" + sec
  102. })
  103. }, SEC)
  104. this.interval = interval
  105. },
  106. endClock() {
  107. clearInterval(this.interval)
  108. this.setData({
  109. isLearning: false,
  110. showPopup: true
  111. })
  112. const ctx = wx.createCanvasContext('shareCanvas')
  113. let width = this.data.shareCanvasWidth
  114. let height = this.data.shareCanvasHeight
  115. ctx.fillStyle = "#fff"
  116. ctx.fillRect(0, 0, width, height)
  117. ctx.fillStyle = "#000"
  118. ctx.setFontSize(16)
  119. ctx.textAlign = 'center'
  120. ctx.fillText('我已成功打卡14天', width / 2, height / 2 - 7)
  121. ctx.draw()
  122. },
  123. togglePopup() {
  124. this.setData({
  125. showPopup: !this.data.showPopup
  126. });
  127. },
  128. handleShare() {
  129. wx.canvasToTempFilePath({
  130. canvasId: 'shareCanvas',
  131. success: (res) => {
  132. wx.saveImageToPhotosAlbum({
  133. filePath: res.tempFilePath,
  134. success: () => {
  135. wx.showToast({
  136. title: '已保存到相册'
  137. })
  138. }
  139. })
  140. }
  141. }, this)
  142. }
  143. })