index.js 3.4 KB

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