utils工具封装

  /**
 *去除字符串的空格
 * @param {Object} obj 去除空格的对象
 */
 export const removeSpaces = async (obj:Object) => {
    return new Promise((resolve, reject) => {
      let copyObj = {}
     Object.keys(obj).forEach((key) => {
        if (typeof obj[key]=='number' || typeof obj[key]=='string') {
          copyObj[key] = (obj[key] + '').trim()
        }else{
          copyObj[key] = obj[key]
        }
      })
      resolve(copyObj)
    })
  }
  /***
 * @description 封装返回结果
 */
export const handleResponse = ({
    res,
    success,
    fail,
    complete,
  }: {
    res: {
      msgCode?: string;
      message?: string;
      data?: any;
    };
     success?: (data?: any) => void;
    fail?: (data?: any) => void;
    complete?: (data?: any) => void;
  }) => {
    complete?.();
    if (res.msgCode === '000000') {
      message.success('操作成功');
      success?.();
      return true;
    }
    fail?.();
    return false;
    };
  /**
 * @description: 对象数组根据某个key去重
 * @param: arr: 数组, u_key_arr: key值得集合
 * @return {*}
 */
export const unique = (arr: any[], u_key_arr: string[]) => {
  const getValue = (obj: Record<string, any>, selectors: string) =>
    selectors
      .replace(/\[([^\[\]]*)\]/g, '.$1.')
      .split('.')
      .filter((t: string) => t !== '')
      .reduce((prev: { [x: string]: any }, cur: string | number) => prev && prev[cur], obj);
 let map = new Map();
  arr.forEach((item, index) => {
    u_key_arr.forEach((key) => {
      if (!map.has(getValue(item, key))) {
       map.set(getValue(item, key), item);
      }
    });
  });
  return [...map.values()];
};
/**
 * @description tree数组扁平化
 */
export const flattenArray = (list: any[], childrenFiled: string = 'children') => {
  const newList: any[] = [];
 list.forEach((element: any) => {
    newList.push(element);
    if (element[childrenFiled]) {
      newList.push(...flattenArray(element[childrenFiled], childrenFiled));
    }
  });
  return newList;
};
/**
 * @description 解析地址栏search参数
 */
export function getSearch(name: string | number) {
  const url = window.location.search;
  const p = url.split('?')[1];
  const keyValue = p ? p.split('&') : [];
  const obj = {};
  for (let i = 0; i < keyValue.length; i++) {
    const item = keyValue[i].split('=');
    const key = item[0];
    const value = item[1];
    obj[key] = value;
   }
  return obj[name];
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值