import dayjs from 'dayjs'; import { openDoc } from '../../../../utils/file'; import { fetchCommitmentList } from '../../../../services/enterprise/fetchPromise'; Page({ /** * 页面的初始数据 */ data: { text1: '还未签署承诺书', text3: '去承诺', status: 0, promise: [], firstPromiseImgUrl: undefined, firstPromiseFileUrl: '/res/pdf.png', firstPromiseTime: '', deadline: '', }, promiseHistory: [], /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { if (options) { this.userId = options.userId; } this._initPlanYear(); }, /** * 生命周期函数--监听页面显示 */ onShow: function () { this.getCommitment(); }, /** * 初始化年份 */ _initPlanYear() { var now = dayjs(); 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 = dayjs(first.cmCreateTime); let leftDays = cTime.add(1, 'years').diff(dayjs(), 'days'); // this.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, deadline: '', }); this._initPlanYear(); } } }, /** * 获取历史记录 */ getCommitment() { let { userId } = this; fetchCommitmentList({ userId }).then(res => { let data = res.data; this.promiseHistory = data; let promise = []; data.forEach(d => { const time = dayjs(d.cmCreateTime); promise.push({ period: `承诺周期:${time.year()}年1月~${time.year()}年12月`, time: `${time.format('YYYY年MM月DD日')}完成`, picPath: d.cmUrl, pdfPath: d.cmPdfUrl, }); }); const p = promise[0]; if (p.picPath.length > 0) { this.setData({ firstPromiseImgUrl: p.picPath[0] }); } this.setData({ promise, firstPromiseTime: p.time, }); this.checkStatus(); }); }, /** * 跳转至去承诺 */ gotoPromise(e) { let { status } = this.data; if (status == 1) { this.gotoResult(0); } }, /** * 跳转至承诺详情 */ gotoDetail(e) { var i = e.currentTarget.dataset.index; this.gotoResult(i); }, gotoResult(i) { var p = this.data.promise[i]; if (p.picPath.length > 0) { wx.navigateTo({ url: '/pages/enterprise/promise/detail/index', success: function (res) { // 通过 eventChannel 向被打开页面传送数据 res.eventChannel.emit('acceptPromiseData', { promise: { picPath: p.picPath, pdfPath: p.pdfPath, }, }); }, }); } else { openDoc(p.pdfPath); } }, });