forked from drone/command-center-dashboard

罗广辉
2025-04-02 e1699c4851f6ca397cd0ad1ff63f32c737654836
src/utils/func.js
@@ -8,7 +8,7 @@
   * @returns {boolean}
   */
  static notEmpty(val) {
    return !this.isEmpty(val);
      return !this.isEmpty(val)
  }
  /**
@@ -17,7 +17,7 @@
   * @returns {boolean}
   */
  static isUndefined(val) {
    return val === null || typeof val === 'undefined';
      return val === null || typeof val === 'undefined'
  }
  /**
@@ -31,9 +31,9 @@
      typeof val === 'undefined' ||
      (typeof val === 'string' && val === '' && val !== 'undefined')
    ) {
      return true;
         return true
    }
    return false;
      return false
  }
  /**
@@ -44,10 +44,10 @@
   */
  static toInt(val, defaultValue) {
    if (this.isEmpty(val)) {
      return defaultValue === undefined ? -1 : defaultValue;
         return defaultValue === undefined ? -1 : defaultValue
    }
    const num = parseInt(val, 0);
    return Number.isNaN(num) ? (defaultValue === undefined ? -1 : defaultValue) : num;
      const num = parseInt(val, 0)
      return Number.isNaN(num) ? (defaultValue === undefined ? -1 : defaultValue) : num
  }
  /**
@@ -56,10 +56,10 @@
   */
  static toNumber(val) {
    if (this.isEmpty(val)) {
      return '';
         return ''
    }
    const num = parseFloat(val);
    return Number.isNaN(num) ? val : num;
      const num = parseFloat(val)
      return Number.isNaN(num) ? val : num
  }
  /**
@@ -68,11 +68,11 @@
   * @returns {FormData}
   */
  static toFormData(obj) {
    const data = new FormData();
      const data = new FormData()
    Object.keys(obj).forEach(key => {
      data.append(key, Array.isArray(obj[key]) ? obj[key].join(',') : obj[key]);
    });
    return data;
         data.append(key, Array.isArray(obj[key]) ? obj[key].join(',') : obj[key])
      })
      return data
  }
  /**
@@ -82,7 +82,7 @@
   * @returns {null}
   */
  static format(date, format = 'YYYY-MM-DD HH:mm:ss') {
    return date ? date.format(format) : null;
      return date ? date.format(format) : null
  }
  /**
@@ -91,7 +91,7 @@
   * @returns {string}
   */
  static formatDateTime(timestamp) {
    return this.formatDate(new Date(timestamp));
      return this.formatDate(new Date(timestamp))
  }
  /**
@@ -100,16 +100,16 @@
   * @returns {string}
   */
  static formatDate(date) {
    const pad = num => (num < 10 ? '0' + num : num);
      const pad = num => (num < 10 ? '0' + num : num)
    const year = date.getFullYear();
    const month = pad(date.getMonth() + 1); // 月份从0开始,所以+1
    const day = pad(date.getDate());
    const hour = pad(date.getHours());
    const minute = pad(date.getMinutes());
    const second = pad(date.getSeconds());
      const year = date.getFullYear()
      const month = pad(date.getMonth() + 1) // 月份从0开始,所以+1
      const day = pad(date.getDate())
      const hour = pad(date.getHours())
      const minute = pad(date.getMinutes())
      const second = pad(date.getSeconds())
    return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
      return `${year}-${month}-${day} ${hour}:${minute}:${second}`
  }
  /**
@@ -118,9 +118,9 @@
   * @returns {string}
   */
  static toLocalISOString(datetime) {
    let timezoneOffset = datetime.getTimezoneOffset() * 60000; // 获取当前时区与UTC的时间差(以毫秒为单位)
    let localDatetime = new Date(datetime - timezoneOffset); // 调整时间,得到当前时区时间
    return localDatetime.toISOString();
      let timezoneOffset = datetime.getTimezoneOffset() * 60000 // 获取当前时区与UTC的时间差(以毫秒为单位)
      let localDatetime = new Date(datetime - timezoneOffset) // 调整时间,得到当前时区时间
      return localDatetime.toISOString()
  }
  /**
@@ -129,7 +129,7 @@
   * @returns {string}
   */
  static join(arr) {
    return Array.isArray(arr) ? arr.join(',') : arr;
      return Array.isArray(arr) ? arr.join(',') : arr
  }
  /**
@@ -138,7 +138,7 @@
   * @returns {string}
   */
  static split(str) {
    return str ? String(str).split(',') : '';
      return str ? String(str).split(',') : ''
  }
  /**
@@ -148,9 +148,9 @@
   */
  static toStr(str) {
    if (typeof str === 'undefined' || str === null) {
      return '';
         return ''
    }
    return str;
      return str
  }
  /**
@@ -159,7 +159,7 @@
   * @returns {boolean}
   */
  static isArrayAndNotEmpty(param) {
    return Array.isArray(param) && param.length > 0;
      return Array.isArray(param) && param.length > 0
  }
  /**
@@ -168,11 +168,11 @@
   * @returns {*|string}
   */
  static formatUrl(url) {
    if (!url) return url;
      if (!url) return url
    if (url.startsWith('http://') || url.startsWith('https://')) {
      return url;
         return url
    } else {
      return `http://${url}`;
         return `http://${url}`
    }
  }
@@ -182,8 +182,8 @@
   * @returns {string}
   */
  static bytesToKB(bytes) {
    const kb = bytes / 1024;
    return kb.toFixed(2);
      const kb = bytes / 1024
      return kb.toFixed(2)
  }
  /**
@@ -193,9 +193,9 @@
   */
  static jsonArrayToKeyValue(jsonArray) {
    if (this.isEmpty(jsonArray)) {
      return '';
         return ''
    }
    return jsonArray.map(item => `${item.enumKey}:${item.enumValue}`).join(';');
      return jsonArray.map(item => `${item.enumKey}:${item.enumValue}`).join(';')
  }
  /**
@@ -205,16 +205,16 @@
   */
  static keyValueToJsonArray(keyValue) {
    if (this.isEmpty(keyValue)) {
      return [];
         return []
    }
    return keyValue.split(';').map((kv, index) => {
      const [key, value] = kv.split(':');
         const [key, value] = kv.split(':')
      return {
        id: index,
        enumKey: key,
        enumValue: value,
      };
    });
         }
      })
  }
  /**
@@ -226,9 +226,9 @@
  static contains(str, val) {
    // 检查str是否为字符串且不为空
    if (typeof str === 'string' && str.length > 0) {
      return str.includes(val);
         return str.includes(val)
    }
    return false;
      return false
  }
  /**
@@ -239,9 +239,9 @@
   */
  static truncateString(str, len = 20) {
    if (str.length > len) {
      return str.slice(0, len) + '...';
         return str.slice(0, len) + '...'
    }
    return str;
      return str
  }
  /**
@@ -250,7 +250,7 @@
   * @returns {*}
   */
  static camelCaseString(str) {
    return str.replace(/_([a-z])/g, g => g[1].toUpperCase());
      return str.replace(/_([a-z])/g, g => g[1].toUpperCase())
  }
  /**
@@ -259,15 +259,15 @@
   * @returns {string}
   */
  static strGenerate(length) {
    const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    const maxLength = 256;
      const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
      const maxLength = 256
    if (length > maxLength) {
      throw new Error(`长度最大值不能超过 ${maxLength}`);
         throw new Error(`长度最大值不能超过 ${maxLength}`)
    }
    return Array.from({ length }, () =>
      characters.charAt(Math.floor(Math.random() * characters.length))
    ).join('');
      ).join('')
  }
  /**
@@ -277,9 +277,9 @@
  static generateUUID() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
      const r = (Math.random() * 16) | 0,
        v = c === 'x' ? r : (r & 0x3) | 0x8;
      return v.toString(16);
    });
            v = c === 'x' ? r : (r & 0x3) | 0x8
         return v.toString(16)
      })
  }
  /**
@@ -292,7 +292,7 @@
      Object.entries(obj).filter(
        ([, value]) => value !== '' && value !== null && value !== undefined
      )
    );
      )
  }
  /**
@@ -302,9 +302,9 @@
   */
  static getUserTenantId(userInfo) {
    if (userInfo && userInfo.tenant_id) {
      return userInfo.tenant_id;
         return userInfo.tenant_id
    } else if (userInfo && userInfo.tenantId) {
      return userInfo.tenantId;
         return userInfo.tenantId
    }
  }
}