WeeklyConversionRate.js 3.42 KB
// packageStudyCenter/pages/myTask/components/WeeklyConversionRate/WeeklyConversionRate.js
Component({
  properties: {
    isFlag: {
      type: Boolean,
      value: false
    },
    dataInfo: {
      type: Object,
      value: {},
      observer: function(newVal) {
        this.updateAllData(true)
      }
    },
    isHmzx: {
      type: Boolean,
      value: false
    },
    isYb: {
      type: Boolean,
      value: false
    },
    isBeisen: {
      type: Boolean,
      value: false
    }
  },

  data: {
    barList: [
      {
        label: '我的',
        value: '74.3',
        key: 'myConversionRatio',
        class: 'my'
      },
      {
        label: 'Top1',
        value: '94.5',
        key: 'topOneConversionRatio',
        class: 'top1'
      }
    ],
    tabIndex: 0,
    tabList: [],
    currentConversionData: {},
    barStyles: []
  },

  observers: {
    'tabIndex': function(tabIndex) {
      this.updateAllData();
    }
  },

  lifetimes: {
    attached() {
      // 初始化时更新
      this.updateAllData(true);
    }
  },

  methods: {
    updateAllData(updateTabFlag = false) {
      const { dataInfo, tabIndex, barList } = this.data;
      const weekConversionRatio = dataInfo && dataInfo.weekConversionRatio
      // 更新tabList
      if (updateTabFlag) this.updateTabList()
      
      // 计算当前数据
      let currentConversionData = {};
      if (weekConversionRatio && this.data.tabList.length > 0 && this.data.tabList[tabIndex]) {
        const key = this.data.tabList[tabIndex].value;
        currentConversionData = weekConversionRatio[key] || {};
      }
      
      // 计算进度条样式
      const barStyles = barList.map(item => {
        const value = this.getNumberValue(currentConversionData[item.key]);
        const width = Math.min(100, Math.max(2, value));
        const left = value > 100 ? 100 : value;
        const displayValue = value.toFixed(1);
        
        return {
          width: `${width}%`,
          left: `calc(${left}%)`,
          text: `${displayValue}%`
        };
      });
      
      this.setData({
        currentConversionData,
        barStyles
      });
    },

    updateTabList() {
      const weekConversionRatio = this.properties.dataInfo && this.properties.dataInfo.weekConversionRatio
      if (!weekConversionRatio) {
        this.setData({ tabList: [] });
        return;
      }

      const order = [0, 1, 2];
      const tabArr = order
        .filter(key => weekConversionRatio.hasOwnProperty(key))
        .map(key => {
          let title = '';
          if (key == 0) title = '轻改';
          else if (key == 1) title = '权益';
          else if (key == 2) title = '延保';
          else title = `类型${key}`;
          return {
            title,
            value: key
          };
        });
      this.setData({ tabList: tabArr }, () => {
        // tabList更新后,检查是否需要调整tabIndex
        if (tabArr.length > 0) {
          const yanbaoIndex = tabArr.findIndex(item => item.value == 2);
          let newTabIndex = 0;
          if (yanbaoIndex !== -1) {
            newTabIndex = yanbaoIndex;
          }
          if (this.data.tabIndex !== newTabIndex) {
            this.setData({ tabIndex: newTabIndex });
          }
        }
      });
    },

    getNumberValue(value) {
      return Number(value) || 0
    },

    handleTabClick(e) {
      const index = e.currentTarget.dataset.index
      this.setData({ tabIndex: index })
    }
  }
})