riku
2024-11-13 ab70c6eb4a181b282af0eb200275cd8a4d2ab172
pages/mService/behaviors/bScheduleManager.js
@@ -12,37 +12,47 @@
  // behaviors: [bLoadingStatus],
  properties: {
    ledgerCompleted: {
      type: Boolean,
      observer(value) {
        this.checkScheduleComplete(0, value);
      },
    },
    assessmentCompleted: {
      type: Boolean,
      type: Number,
      observer(value) {
        this.checkScheduleComplete(1, value);
      },
    },
    assessmentCompleted: {
      type: Number,
      observer(value) {
        this.checkScheduleComplete(2, value);
      },
    },
  },
  data: {
    // 全部环保日程
    allSchedules: [],
    // 当前用户最临近未完成日程
    thisSchedule: {},
    // 当前用户最临近未完成日程索引
    index: 0,
    // 台账和自评本期完成情况
    scheduleComplete: new Map(),
    // 需要做日程是否完成判断的日程个数(台账和自评)
    scheduleCount: 2,
  },
  lifetimes: {
    attached: function () {
      this.getRecentSchedule();
      this.getSchedules();
      if (app.globalData.newUser) {
        this.nextSchedules();
      }
      // this.getAllSchedules()
      this.setData({
        scheduleComplete: new Map(),
      });
      this.getAllSchedules().then(() => {
        this.getRecentSchedule();
        if (app.globalData.newUser) {
          this.nextSchedules();
        }
      });
      // this.getSchedules();
    },
  },
  methods: {
    // 获取最邻近的一个环保系统日程
    getSchedules() {
      // let startTime = '2023-04-10';
      // let endTime = '2023-04-10';
      let startTime = moment().format('YYYY-MM-DD');
      let endTime = startTime;
      const that = this;
@@ -51,6 +61,9 @@
        {
          success(res) {
            if (res.data.length > 0) {
              res.data.sort(
                (a, b) => Math.abs(a.diffDays) - Math.abs(b.diffDays),
              );
              const schedule = res.data[0];
              schedule.time = moment(schedule.time.split('T')[0]).format(
                'YYYY-MM-DD',
@@ -71,7 +84,7 @@
    getRecentSchedule() {
      //1. 从接口获取用户个人、用户企业类型的相关最临近日程
      //2. 按照本地逻辑,得出最邻近日程
      const schedules = sysSchedules();
      const schedules = this.data.allSchedules;
      //2.1 将台账、评估、承诺三项APP功能定义为日程
      //2.2 选择最邻近的事项作为当日的提醒事项,
      let today = moment().hour(0).minute(0).second(0).millisecond(0);
@@ -80,8 +93,8 @@
      let index = 0;
      for (let i = 0; i < schedules.length; i++) {
        let s = schedules[i];
        let d = s.time.diff(today, 'days');
        s.diffDays = d;
        let d = s.diffDays;
        // if (d == -8) {
        if (Math.abs(d) < Math.abs(diffDays)) {
          let _index = i;
@@ -93,60 +106,52 @@
      //2.3 日程在时间维度上分为三类提醒,预告、当日、未完成的逾期提醒
      this.setData({
        allSchedules: schedules,
        // thisSchedule: {
        //   date: schedule.time,
        //   time: schedule.time.format('YYYY-MM-DD dddd'),
        //   type: schedule.type,
        //   events: schedule.events,
        //   diffDays: schedule.diffDays,
        // },
        thisSchedule: schedule,
        index,
      });
    },
    //获取所有日程
    getAllSchedules() {
      const schedules = sysSchedules();
      this.setData({
        allSchedules: schedules,
      return sysSchedules().then(res => {
        this.setData({
          allSchedules: res,
        });
      });
    },
    // 当前日程完成,顺延下一个日程
    nextSchedules() {
      let today = moment().hour(0).minute(0).second(0).millisecond(0);
      let s = this.data.allSchedules[this.data.index + 1];
      let d = s.time.diff(today, 'days');
      s.diffDays = d;
      this.setData({
        thisSchedule: {
          date: s.time,
          time: s.time.format('YYYY-MM-DD'),
          type: s.type,
          events: s.events,
          diffDays: s.diffDays,
        },
        thisSchedule: s,
        index: this.data.index + 1,
      });
    },
    // 检查当前日程是否完成
    checkScheduleComplete(type, value) {
    // 检查当前日程是否完成(2023.8.24 目前只涉及台账和自评两类日程)
    checkScheduleComplete(scId, value) {
      const { scheduleComplete, scheduleCount } = this.data;
      scheduleComplete.set(scId, value);
      // 台账和自评的结果全部传入后再做判断
      if (scheduleComplete.size < scheduleCount) return;
      // 新用户不做判断
      if (app.globalData.newUser) return;
      // 日程类型不一致无需判断
      if (this.data.thisSchedule.type != type) return;
      // 日程不在当月的无需判断
      const thisMonth = moment().month();
      const sTimeMonth = moment(this.data.thisSchedule.date).month();
      if (thisMonth != sTimeMonth) return;
      // 日程完成则顺延下个日程
      if (value) {
        this.nextSchedules();
      }
      let index = 0;
      const thisMonth = moment().month();
      do {
        const { thisSchedule } = this.data;
        // 日程不在当月的无需判断
        const sTimeMonth = moment(thisSchedule.time).month();
        if (thisMonth != sTimeMonth) return;
        // 判断当月该日程是否完成,完成则将当前日程顺延至下一个
        if (scheduleComplete.get(thisSchedule.id) == 2) {
          this.nextSchedules();
        }
        index++
      } while (index < scheduleCount);
    },
  },
});