util.ts 1.74 KB
/*
 * @Author: yangming yangming@didihu.com.cn
 * @Date: 2023-03-15 15:11:33
 * @LastEditors: 前端 qianduan01@didihu.com.cn
 * @LastEditTime: 2023-05-04 11:04:52
 * @FilePath: \achievements-system-front\src\utils\util.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const util = {};
const downFile = (content: any, filename: any) => {
  // 创建隐藏的可下载链接
  var eleLink = document.createElement("a");
  eleLink.download = filename;
  eleLink.style.display = "none";
  // 字符内容转变成blob地址
  var blob = new Blob([content]);
  eleLink.href = URL.createObjectURL(blob);
  document.body.appendChild(eleLink);
  // 触发点击
  eleLink.click();
  // 然后移除
  document.body.removeChild(eleLink);
};
const getObjType = (obj: any) => {
  return Object.prototype.toString.call(obj).toString();
};
const deepClone = (data: any) => {
  const type = getObjType(data);
  let obj: any;
  if (type === "[object Array]") {
    obj = [];
  } else if (type === "[object Object]") {
    obj = {};
  } else {
    // 不再具有下一层次
    return data;
  }
  if (type === "[object Array]") {
    const len = data.length;
    for (let i = 0; i < len; i++) {
      data[i] = (() => {
        if (data[i] === 0) {
          return data[i];
        }
        return data[i];
      })();
      if (data[i]) {
        delete data[i].$parent;
      }
      obj.push(deepClone(data[i]));
    }
  } else if (type === "[object Object]") {
    for (const key in data) {
      if (data) {
        delete data.$parent;
      }
      obj[key] = deepClone(data[key]);
    }
  }
  return obj;
};

export default {
  downFile,
  deepClone,
};