123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- 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) {
- let res = api.isTest ? true : false;
- wx.request({
- url: api.checkPositionUrl,
- method: 'GET',
- data: {
- latitude: latitude,
- longitude: longitude
- },
- success: res => {
- if(res.data.status == 'success' && res.data.result == 'ok') {
- res = true;
- }
- }
- });
- return res;
- },
- handleBtnClick() {
- let isLearning = this.data.isLearning
- let is_btn_disabled = this.data.is_btn_disabled
- let that = this;
- if (is_btn_disabled) {
- wx.showToast({
- title: '请到学校之后再打卡',
- icon: 'none',
- duration: 800
- })
- return;
- }
- if (isLearning) {
- this.setData({
- isLearning: !isLearning
- })
- this.endClock()
- } else {
- wx.request({
- url: api.startCheckCardUrl,
- method: 'GET',
- data: {
- 'student_id': app.globalData.ptStudent.id
- },
- success: res => {
- if (res.data.status == 'success') {
- that.setData({
- isLearning: !isLearning,
- startTime: new Date().getTime()
- })
- that.startClock()
- } else {
- wx.showToast({
- title: res.data.info,
- icon: 'none',
- duration: 800
- })
- }
- }
- })
- }
- },
- 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)
- }
- })
|