var app = getApp() var api = require('utils/api.js'); const SEC = 1000 const MIN = 60 * SEC const HOUR = 60 * MIN Page({ data: { currentLatitude: 0, //用户当前纬度 currentLongitude: 0, //用户当前经度 is_btn_disabled: true, //开始按钮状态 locationAccuracy: 0, //gps 误差 isLearning: false, //是否在学习中? startTime: null, //开始时间 clock: '00:00:00', //计时 shareCanvasWidth: 200, shareCanvasHeight: 280, }, onLoad: function () { wx.getSystemInfo({ success: (res) => { this.setData({ screenWidth: res.screenWidth, screenHeight: res.screenHeight, bannerWidth: res.screenWidth, bannerHeight: res.screenWidth * 363 / 543 }) } }) var pt_student = wx.getStorageSync('pt_student') if (!pt_student) { wx.redirectTo({ url: '/pages/login/index', }) } else { app.globalData.ptStudent = pt_student; } wx.getLocation({ success: (res) => { let validLocation = this.validLocation(res.latitude, res.longitude) this.setData({ currentLatitude: res.latitude, currentLongitude: res.longitude, accuracy: res.accuracy, is_btn_disabled: !validLocation }) } }) }, validLocation(latitude, longitude) { wx.request({ url: api.checkPosition, method: 'GET', data: { latitude: latitude, longitude: longitude }, success: res => { console.log(res); } }) return true; }, handleBtnClick() { let isLearning = this.data.isLearning let is_btn_disabled = this.data.is_btn_disabled if (is_btn_disabled) { wx.showToast({ title: '请到学校之后再打卡', icon: 'none', duration: 800 }) return; } if (isLearning) { this.setData({ isLearning: !isLearning }) this.endClock() } else { this.setData({ isLearning: !isLearning, startTime: new Date().getTime() }) this.startClock() } }, startClock() { let interval = setInterval(() => { let now = new Date().getTime() let startTime = this.data.startTime let diff = now - startTime let hours = Math.floor(diff / HOUR) hours = (hours < 10 ? '0' + hours : hours) diff = diff % HOUR let mins = Math.floor(diff / MIN) mins = (mins < 10 ? '0' + mins : mins) diff = diff % MIN let sec = Math.ceil(diff / SEC) sec = (sec < 10 ? '0' + sec : sec) this.setData({ clock: "" + hours + ":" + mins + ":" + sec }) }, SEC) this.interval = interval }, endClock() { clearInterval(this.interval) this.setData({ isLearning: false, showPopup: true }) const ctx = wx.createCanvasContext('shareCanvas') let width = this.data.shareCanvasWidth let height = this.data.shareCanvasHeight ctx.fillStyle = "#fff" ctx.fillRect(0, 0, width, height) ctx.fillStyle = "#000" ctx.setFontSize(16) ctx.textAlign = 'center' ctx.fillText('我已成功打卡14天', width / 2, height / 2 - 7) ctx.draw() }, togglePopup() { this.setData({ showPopup: !this.data.showPopup }); }, handleShare() { wx.canvasToTempFilePath({ canvasId: 'shareCanvas', success: (res) => { wx.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: () => { wx.showToast({ title: '已保存到相册' }) } }) } }, this) } })