index.js 6.9 KB

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