
import { message } from 'antd';

export function copy(text) {
  let oInput = document.createElement('input')
  oInput.value = text;
  document.body.appendChild(oInput)
  oInput.select() // 选择对象
  document.execCommand("Copy") // 执行浏览器复制命令
  message.success('复制成功');
  oInput.remove()
}

export function exportData(_res, name) {
  console.log(_res);
  // 获取文件的二进制数据
  const blob = new Blob([_res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
  // 创建下载链接
  const link = document.createElement('a');
  link.href = URL.createObjectURL(blob);
  link.download = name;
  // 触发点击事件以下载文件
  document.body.appendChild(link);
  link.click();
  // 清理
  document.body.removeChild(link);
  URL.revokeObjectURL(link.href);
};



//下载连接
export function exportLink(searchParams, api) {
  searchParams.token = localStorage.getItem('token');
  const params = Object.entries(searchParams).length ? `?${new URLSearchParams(searchParams)}` : '';
  return `${process.env.API_URL}${api}${params}`;
};


/*
 * @Note:
 * @Author: 2058827620@qq.com
 * @Date: 2022-07-31 19:24:14
 */
export function deepCopy(data) {
  if (data.constructor.name === 'Array') {
    // 判断为数组类型
    const arrCopy = []
    for (let i = 0, len = data.length; i < len; i++) {
      //遍历数组
      if (data[i] instanceof Object) {
        arrCopy.push(deepCopy(data[i]))
      } else {
        // 基本类型
        arrCopy.push(data[i])
      }
    }
    return arrCopy;

  } else { // 为对象
    const objCopy = {};
    for (let x in data) {
      if (data[x] instanceof Object) {
        objCopy[x] = deepCopy(data[x])
      } else { // 基本类型
        objCopy[x] = data[x];
      }
    }
    return objCopy;
  }
}
