// pages/promise/promise.js const moment = require('../../../utils/moment.min') const promiseservice = require("../../../service/promiseservice") const app = getApp() Page({ /** * 页面的初始数据 */ data: { text1: "已开启!请及时完成承诺", text3: "去承诺", status: 0, promise: [{ period: '承诺周期:2021年1月~2021年12月', time: '2021年1月6日完成', picPath: [], pdfPath: '' }], deadline: "承诺截止时间:----年--月--日", }, promiseHistory: [], /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this._initPlanYear() // this.getCommitment() }, /** * 生命周期函数--监听页面显示 */ onShow: function () { console.log("promise: onshow"); this.getCommitment() }, /** * 初始化年份 */ _initPlanYear() { var now = moment() var year = now.year() var period = `${now.year()}/${now.month()+1}-${now.month()+1}` var deadline = now.endOf('year').format("YYYY年MM月DD日") this.setData({ year: year, // deadline: `承诺截止时间:${deadline}` deadline: '' }) }, /** * 判断当前周期内的承诺是否完成 */ checkStatus() { if (this.promiseHistory.length > 0) { var first = this.promiseHistory[0] var cTime = moment(first.cmCreateTime) let leftDays = cTime.add(1, 'years').diff(moment(), 'days') // that.setData({ // promsieInfo: { // leftDays: leftDays // } // }) if (leftDays > 0) { var deadline = cTime.format("YYYY-MM-DD") this.setData({ text1: "本次承诺已完成", text3: "查看承诺", status: 1, deadline: `承诺有效时间:剩余${leftDays}天` }) } else { this.setData({ text1: "请及时完成承诺", text3: "去承诺", status: 0, }) this._initPlanYear() } } }, /** * 获取历史记录 */ getCommitment() { var that = this promiseservice.getCommitment(app.globalData.accessToken.userId, { success(data) { that.promiseHistory = data let promise = [] data.forEach(d => { const time = moment(d.cmCreateTime) promise.push({ period: `承诺周期:${time.year()}年1月~${time.year()}年12月`, time: `${time.format('YYYY年MM月DD日')}完成`, picPath: d.cmUrl, pdfPath: d.cmPdfUrl }) }); that.setData({ promise }) that.checkStatus() } }) }, /** * 跳转至去承诺 */ gotoPromise(e) { var status = e.currentTarget.dataset.status if (status == 0) { wx.navigateTo({ url: '/pages/mPromise/promiseinfo/promiseinfo' }) } else { this.gotoResult(0) } }, /** * 跳转至承诺详情 */ gotoDetail(e) { var i = e.currentTarget.dataset.index this.gotoResult(i) }, gotoResult(i) { var p = this.data.promise[i] wx.navigateTo({ url: '/pages/mPromise/promiseresult/promiseresult', success: function (res) { // 通过 eventChannel 向被打开页面传送数据 res.eventChannel.emit('acceptDataFromOpenerPage', { promise: { picPath: p.picPath, pdfPath: p.pdfPath } }) } }) }, /** * 跳转至历史记录 */ gotoHistory() { } })