riku
2024-11-14 00a96d6881dd10ae7d3c4f5437bfceaabe677723
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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);
    }
  },
});