index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. wx.request({
  30. url: api.getShareImageSizeUrl,
  31. method: 'GET',
  32. success: res => {
  33. if(res.data.status == 'success') {
  34. console.log(res)
  35. this.setData({
  36. shareCanvasWidth: res.data.width,
  37. shareCanvasHeight: res.data.height
  38. })
  39. }
  40. }
  41. })
  42. var pt_student = wx.getStorageSync('pt_student')
  43. if (!pt_student) {
  44. wx.redirectTo({
  45. url: '/pages/login/index',
  46. })
  47. } else {
  48. app.globalData.ptStudent = pt_student;
  49. }
  50. wx.getLocation({
  51. success: (res) => {
  52. let validLocation = this.validLocation(res.latitude, res.longitude)
  53. this.setData({
  54. currentLatitude: res.latitude,
  55. currentLongitude: res.longitude,
  56. accuracy: res.accuracy,
  57. is_btn_disabled: !validLocation
  58. })
  59. }
  60. })
  61. },
  62. validLocation(latitude, longitude) {
  63. let res = api.isTest ? true : false;
  64. wx.request({
  65. url: api.checkPositionUrl,
  66. method: 'GET',
  67. data: {
  68. latitude: latitude,
  69. longitude: longitude
  70. },
  71. success: res => {
  72. if(res.data.status == 'success' && res.data.result == 'ok') {
  73. res = true;
  74. }
  75. }
  76. });
  77. return res;
  78. },
  79. handleBtnClick() {
  80. let isLearning = this.data.isLearning
  81. let is_btn_disabled = this.data.is_btn_disabled
  82. let that = this;
  83. if (is_btn_disabled) {
  84. wx.showToast({
  85. title: '请到学校之后再打卡',
  86. icon: 'none',
  87. duration: 800
  88. })
  89. return;
  90. }
  91. if (isLearning) {
  92. let check_card_id = wx.getStorageSync('check_card_id');
  93. wx.request({
  94. url: api.endCheckCardUrl,
  95. method: 'GET',
  96. data: {
  97. 'check_card_id': check_card_id
  98. },
  99. success: res => {
  100. if (res.data.status == 'success') {
  101. that.setData({
  102. isLearning: !isLearning
  103. })
  104. that.endClock()
  105. } else {
  106. wx.showToast({
  107. title: res.data.info,
  108. icon: 'none',
  109. duration: 800
  110. })
  111. }
  112. }
  113. })
  114. } else {
  115. wx.request({
  116. url: api.startCheckCardUrl,
  117. method: 'GET',
  118. data: {
  119. 'student_id': app.globalData.ptStudent.id
  120. },
  121. success: res => {
  122. if (res.data.status == 'success') {
  123. wx.setStorageSync('check_card_id', res.data.check_card_id)
  124. that.setData({
  125. isLearning: !isLearning,
  126. startTime: new Date().getTime()
  127. })
  128. that.startClock()
  129. } else {
  130. wx.showToast({
  131. title: res.data.info,
  132. icon: 'none',
  133. duration: 800
  134. })
  135. }
  136. }
  137. })
  138. }
  139. },
  140. startClock() {
  141. let interval = setInterval(() => {
  142. let now = new Date().getTime()
  143. let startTime = this.data.startTime
  144. let diff = now - startTime
  145. let hours = Math.floor(diff / HOUR)
  146. hours = (hours < 10 ? '0' + hours : hours)
  147. diff = diff % HOUR
  148. let mins = Math.floor(diff / MIN)
  149. mins = (mins < 10 ? '0' + mins : mins)
  150. diff = diff % MIN
  151. let sec = Math.ceil(diff / SEC)
  152. sec = (sec < 10 ? '0' + sec : sec)
  153. this.setData({
  154. clock: "" + hours + ":" + mins + ":" + sec
  155. })
  156. }, SEC)
  157. this.interval = interval
  158. },
  159. endClock() {
  160. clearInterval(this.interval)
  161. this.setData({
  162. isLearning: false,
  163. showPopup: true
  164. })
  165. const ctx = wx.createCanvasContext('shareCanvas')
  166. let width = this.data.shareCanvasWidth
  167. let height = this.data.shareCanvasHeight
  168. ctx.fillStyle = "#fff"
  169. ctx.fillRect(0, 0, width, height)
  170. ctx.fillStyle = "#000"
  171. ctx.setFontSize(16)
  172. ctx.textAlign = 'center'
  173. ctx.fillText('我已成功打卡14天', width / 2, height / 2 - 7)
  174. ctx.draw()
  175. },
  176. togglePopup() {
  177. this.setData({
  178. showPopup: !this.data.showPopup
  179. });
  180. },
  181. handleShare() {
  182. wx.canvasToTempFilePath({
  183. canvasId: 'shareCanvas',
  184. success: (res) => {
  185. wx.saveImageToPhotosAlbum({
  186. filePath: res.tempFilePath,
  187. success: () => {
  188. wx.showToast({
  189. title: '已保存到相册'
  190. })
  191. }
  192. })
  193. }
  194. }, this)
  195. }
  196. })