InventoryInfo.js 2.76 KB
// components/InventoryInfo/InventoryInfo.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    // 是否显示弹窗
    show: {
      type: Boolean,
      value: false,
      observer: 'onShowChange'
    },
    
    // 库存信息数据
    inventoryData: {
      type: Object,
      value: {},
      observer: 'onDataChange'
    },
    
    // 自定义标题
    title: {
      type: String,
      value: '库存信息'
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    // 弹窗显示状态
    isShowing: false,
    // 缓存的数据
	dataList:[]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 显示弹窗
     */
    showPopup() {
      this.setData({
        isShowing: true
      });
    },

    /**
     * 隐藏弹窗
     */
    hidePopup() {
      this.setData({
        isShowing: false
      });
    },

    /**
     * 关闭弹窗事件处理
     */
    onClose() {
      this.hidePopup();
      // 触发关闭事件
      this.triggerEvent('close');
    },

    /**
     * 复制文本到剪贴板
     */
    copyText(e) {
      const { text } = e.currentTarget.dataset;
      wx.setClipboardData({
        data: text,
        success: () => {
          wx.showToast({
            title: '复制成功',
            icon: 'success',
            duration: 1500
          });
        },
        fail: () => {
          wx.showToast({
            title: '复制失败',
            icon: 'none',
            duration: 1500
          });
        }
      });
    },

    /**
     * 监听show属性变化
     */
    onShowChange(newVal, oldVal) {
      if (newVal !== oldVal) {
        this.setData({
          isShowing: newVal
        });
      }
    },

    /**
     * 监听数据变化
     */
	onDataChange(newVal, oldVal) {
		if (newVal && typeof newVal === 'object') {
		  const dataList = Object.keys(newVal).map(key => ({
			key: key,
			value: newVal[key],
			label: this.getFieldLabel(key)
		  }));
		  
		  this.setData({
			dataList: dataList
		  },()=>{
			  console.log(this.data.dataList)
		  });
		}
	  },

    /**
     * 获取字段值(支持格式化)
     */
    getFieldLabel(field) {
      const fieldMap = {
		upgradeGoodsCommodityCode: 'UID',
		upgradeGoodsCustomCode: '商品编码',
		specification: '规格值',
		stock: '库存总数'
      };
      return fieldMap[field] || field;
    },

    /**
     * 获取字段值
     */
    getFieldValue(field) {
      const data = this.data.cachedData;
      return data ? data[field] : '';
    }
  },

  /**
   * 组件生命周期函数
   */
  lifetimes: {
    attached() {
      // 组件创建完成后执行
    },

    detached() {
      // 组件销毁时执行
    }
  },

  /**
   * 外部样式类
   */
  externalClasses: [
    'custom-class'
  ]
});