index.js 4.8 KB

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