index.js 5.6 KB

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