index.js 5.7 KB

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