JS 经常使用util

image.png

在接口拦截 options.isExport,下载接口返回文件,经过a标签
image.pnggit

/**数组

  • 根据枚举组装options函数

*/dom

export function getSelectedOptions(enumList) {
 const array = [];
 enumList.forEach(item => {
   array.push(
     <Select.Option value={item.id} key={item.id}>
       {item.name}
     </Select.Option>
   );
 });
 return array;
}

/**函数

  • 根据id得到枚举的name

*/ui

export function getEnumNameById(enumList, id) {
 if (id === '' || !enumList) return null;
 const result = enumList.find(item => String(item.id) === String(id));
 return result ? result.name : null;
}

/**url

  • 根据name得到枚举的id

*/spa

export function getEnumIdByName(enumList, name) {
 if (!name || !enumList) return null;
 const result = enumList.find(item => String(item.name) === String(name));
 return result ? result.id : null;
}

/**prototype

  • 获取数组对象中某个属性

*/3d

export function getArrayProps(array, key) {
 const keys = key || 'id';
 const res = [];
 if (array) {
   array.forEach(t => {
     res.push(t[keys]);
   });
 }
 return res;
}

/**code

  • 去除参数 先后空格

*/

export function paramsTrim(params) {
 const values = {};
 for (const key in params) {
   if ({}.hasOwnProperty.call(params, key)) {
     if (typeof params[key] === 'string') {
       values[key] = params[key].trim();
     } else {
       values[key] = params[key];
     }
   }
 }
 return values;
}

/**

  • 获取地址栏参数

*/

export function getUrlPrmt(url) {
  const tempUrl = url || window.location.href || '';
  const pa = tempUrl.substring(tempUrl.indexOf('?') + 1);
  const arrS = pa.split('&');
  const rs = {};
  for (let i = 0, len = arrS.length; i < len; i += 1) {
    const pos = arrS[i].indexOf('=');
    if (pos !== -1) {
      const name = arrS[i].substring(0, pos);
      const value = window.decodeURIComponent(arrS[i].substring(pos + 1));
      rs[name] = value;
    }
  }
  return rs;
}

/**

  • 判断对象是否含有该属性

*/

export function isHas(obj, key) {
   const isObj =
     Object.prototype.toString.call(obj) === '[object Object]' && obj.constructor === Object;
   if (key === null || key === undefined || isObj === false) {
     return isObj;
   }
   let isHaskey = false;
   if (isObj && Object.prototype.hasOwnProperty.call(obj, key) && obj !== undefined) {
     isHaskey = obj[key] !== null && obj[key] !== undefined;
   } else {
     isHaskey = false;
   }
   return isHaskey;
 }

/**

  • 判断是否为数组

*/

export function isArrayFn(value) {
   if (typeof Array.isArray === 'function') {
     return Array.isArray(value) && value.length > 0;
   }
   return Object.prototype.toString.call(value) === '[object Array]' && value.length > 0;
 }
 
 export default isArrayFn;

/**

  • 生成uuid

*/

export function uuidGenerate() {
 const s = [];
 const hexDigits = '0123456789abcdef';
 for (let i = 0; i < 36; i += 1) {
   s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
   if ([8, 13, 18, 25].includes(i)) {
     s[i] = '-';
   }
 }
 s[14] = '4';
 s[19] = hexDigits.substr((s[19] && 0x3) || 0x8, 1);
 const uuid = s.join('');
 return uuid;
}