riku
2022-08-10 51600113c20d84ee54660de23324400604704dc2
pages/promise/promise.js
@@ -1,5 +1,6 @@
// pages/promise/promise.js
const moment = require('../../utils/moment.min')
const promiseservice = require("../../service/promiseservice")
const app = getApp()
Page({
@@ -10,48 +11,32 @@
  data: {
    text1: "已开启!请及时完成承诺",
    text3: "去承诺",
    status: 0,
    promise: [{
      period: '承诺周期:2021年1月~2021年12月',
      time: '2021年1月6日完成'
    }, {
      period: '承诺周期:2020年1月~2020年12月',
      time: '2020年1月6日完成'
      time: '2021年1月6日完成',
      picPath: '',
      pdfPath: ''
    }],
    newPromise: [{
      period: '承诺周期:2022年1月~2022年12月',
      time: '2022年5月10日完成'
    },{
      period: '承诺周期:2021年1月~2021年12月',
      time: '2021年1月6日完成'
    }, {
      period: '承诺周期:2020年1月~2020年12月',
      time: '2020年1月6日完成'
    }],
    deadline: "----年--月--日",
    deadline: "承诺截止时间:----年--月--日",
  },
  promiseHistory: [],
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._initPlanYear()
  },
    /**
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    wx.getStorage({
      key: "promise",
      success(res) {
        if(res.data){
          that.setData({
            promise: that.data.newPromise
          })
        }
      }
    })
    this.getCommitment()
  },
  /**
@@ -63,45 +48,106 @@
    var period = `${now.year()}/${now.month()+1}-${now.month()+1}`
    var deadline = now.endOf('year').format("YYYY年MM月DD日")
    this.setData({
      planYear: `${year}年`,
      maxYear: year,
      thisPeiod: period,
      deadline: deadline
      year: year,
      deadline: `承诺截止时间:${deadline}`
    })
  },
  /**
   * 页面跳转
   * 判断当前周期内的承诺是否完成
   */
  goto: function (e) {
    var url = ""
    var index = e.currentTarget.dataset.index
    switch (index) {
      case "0":
        //去承诺
        url = `/pages/promiseinfo/promiseinfo`
        break;
      case "1":
        //查看承诺结果
        url = `/pages/promiseresult/promiseresult`
        break;
      case "2":
        //
        url = ""
        return;
      case "3":
        //
        url = ""
        break;
      case "4":
        break;
      case "5":
        break;
    }
    if (url != "") {
      wx.navigateTo({
        url: url
      })
  checkStatus() {
    if (this.promiseHistory.length > 0) {
      var first = this.promiseHistory[0]
      var firstYear = moment(first.cmCreateTime).year()
      if (firstYear >= this.data.year) {
        var deadline = moment().add(1, 'years').endOf('year').format("YYYY年MM月DD日")
        this.setData({
          text1: "很棒!本次承诺已完成",
          text3: "查看承诺",
          status: 1,
          deadline: `下次承诺时间:${deadline}`
        })
      } 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/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/promiseresult/promiseresult',
      success: function (res) {
        // 通过 eventChannel 向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', {
          promise: {
            picPath: p.picPath,
            pdfPath: p.pdfPath
          }
        })
      }
    })
  },
  /**
   * 跳转至历史记录
   */
  gotoHistory() {
  }
})