index.js 5.3 KB

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