index.js 6.5 KB

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