/* * @Description: * @Author: xuch * @FilePath: \yuan-qingdao-zld-browser\src\utils\utils.ts * @Date: 2022-08-30 16:16:39 * @LastEditTime: 2023-05-31 10:18:33 * @LastEditors: 曹文丽 */ import { getDictTypeList } from "../api/common"; /** * @description: 通用下载 (要求返回文件流) * @param {*} data 数据 * @param {*} name 文件名称 * @param {*} type 文件类型 * @author changjun */ export function downLoadFile(data, name?, type = "xlsx") { if (!data) return; const mimeType = { xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", xls: "application/vnd.ms-excel", pdf: "application/pdf", zip: "application/x-zip-compressed", }; const blob = new Blob([data], { type: mimeType[type] }); // const blob = new Blob([data], { type: data.type }) const _link = document.createElement("a"); _link.download = name; _link.style.display = "none"; _link.href = URL.createObjectURL(blob); document.body.appendChild(_link); _link.click(); URL.revokeObjectURL(_link.href); document.body.removeChild(_link); } /** * @description: 获取当前年月日 * @author tqh */ export function getNowFormatDate() { let date = new Date(), seperator1 = "-", //格式分隔符 year = date.getFullYear(), //获取完整的年份(4位) month = date.getMonth() + 1, //获取当前月份(0-11,0代表1月) strDate = date.getDate(); // 获取当前日(1-31) if (month >= 1 && month <= 9) month = "0" + month; // 如果月份是个位数,在前面补0 if (strDate >= 0 && strDate <= 9) strDate = "0" + strDate; // 如果日是个位数,在前面补0 let currentdate = year + seperator1 + month + seperator1 + strDate; return currentdate; } /** * @description: 深度拷贝 * @author tqh */ export function deepCopy(obj) { let target = null; if (typeof obj === "object") { if (Array.isArray(obj)) { //数组 target = []; obj.forEach((item) => { target.push(deepCopy(item)); }); } else if (obj) { target = {}; let objKeys = Object.keys(obj); objKeys.forEach((key) => { target[key] = deepCopy(obj[key]); }); } else { target = obj; } } else { target = obj; } return target; } /** * @description: 通用下载文件流 * @author tqh */ export function downLoadAllFile(data, fileName, type) { let blob = new Blob([data], { type: `application/${type};charset=utf-8` }); // 获取heads中的filename文件名 let downloadElement = document.createElement("a"); // 创建下载的链接 let href = window.URL.createObjectURL(blob); downloadElement.href = href; // 下载后文件名 downloadElement.download = fileName; document.body.appendChild(downloadElement); // 点击下载 downloadElement.click(); // 下载完成移除元素 document.body.removeChild(downloadElement); // 释放掉blob对象 window.URL.revokeObjectURL(href); } /** * * @param url {string} - url * @param key {string} - 需要查询的键 * @returns value {string} - 查询的键值 */ export function getUrlSingleParam(url, key) { const reg = new RegExp("(\\?|&)" + key + "=([^&]*)(&|$)", "i"); const r = url.match(reg); let value = ""; if (r != null) { value = decodeURIComponent(r[2]); } return value == null || value == "" || value == "undefined" ? null : value; } /** * 需大量计算数据缓存 * @param fn {function} - 运行函数 * @returns */ export function cached(fn) { var cache = Object.create(null); return function cachedFn(str) { var hit = cache[str]; return hit || (cache[str] = fn(str)); }; } // 主题滑轮 // let scal = 1; export function onMouseWheel(ev) { var oDiv = document.getElementById("ya-graph"); console.log(oDiv.style.transform); const reg = /(?<=\()\S+(?=\))/g; const transform = oDiv.style.transform.match(reg) || []; let scal = transform[0] || 1; /*当鼠标滚轮事件发生时,执行一些操作*/ var ev = ev || window.event; let down = true; // 定义一个标志,当滚轮向下滚时,执行一些操作 down = ev.wheelDelta ? ev.wheelDelta < 0 : ev.detail > 0; console.log(ev.wheelDelta); if (!down) { scal = (parseFloat(scal) + 0.01).toFixed(2); oDiv.style.transform = "scale(" + scal + ")"; //scale()在这里要使用拼接的方式才能生效 // oDiv.style.transformOrigin = "0 0"; } else { if (scal == 0.01) { scal = 0.01; return; } else { scal = (parseFloat(scal) - 0.01).toFixed(2); } oDiv.style.transform = "scale(" + scal + ")"; //scale()在这里要使用拼接的方式才能生效。 // oDiv.style.transformOrigin = "0 0"; } if (ev.preventDefault) { /*FF 和 Chrome*/ ev.preventDefault(); // 阻止默认事件 } return false; } export function addEvent(obj, xEvent, fn) { if (obj.attachEvent) { obj.attachEvent("on" + xEvent, fn); } else { obj.addEventListener(xEvent, fn, { passive: false }); } } /** * 大数字转化处理 * @param value * @param type * @returns */ export function bigNumberTransform(value, type?) { const newValue = ["", "", ""]; let fr = 1000; let num = 3; let text1 = ""; let text2 = ""; let fm = 1; if (value == "" || value == null || isNaN(value)) { return type ? newValue : value; } if (value < 0) { value = Math.abs(value); text2 = "-"; } while (value / fr >= 1) { fr *= 10; num += 1; // console.log('数字', value / fr, 'num:', num) } if (num <= 4) { // 千 newValue[0] = value; newValue[1] = ""; } else if (num <= 8) { // 万 // text1 = parseInt(num - 4) / 3 > 1 ? '千万' : '万' text1 = "万+"; // tslint:disable-next-line:no-shadowed-variable fm = text1 === "万+" ? 10000 : 10000000; if (value % fm === 0) { newValue[0] = parseInt(value / fm) + ""; } else { newValue[0] = toFixedFloor(parseFloat(value / fm)) + ""; } newValue[1] = text1; } else { // 亿 if (num <= 16) // text1 = (num - 8) / 3 > 1 ? '千亿' : '亿' text1 = "亿+"; text1 = (num - 8) / 4 > 1 ? "万亿+" : text1; text1 = (num - 8) / 7 > 1 ? "千万亿+" : text1; text1 = (num - 8) / 10 > 1 ? "亿亿+" : text1; // tslint:disable-next-line:no-shadowed-variable fm = 1; if (text1 === "亿+") { fm = 100000000; } else if (text1 === "千亿+") { fm = 100000000000; } else if (text1 === "万亿+") { fm = 1000000000000; } else if (text1 === "千万亿+") { fm = 1000000000000000; } else { fm = 1000000000000000000; } if (value % fm === 0) { newValue[0] = parseInt(value / fm) + ""; } else { newValue[0] = toFixedFloor(parseFloat(value / fm)) + ""; } newValue[1] = text1; } if (value < 1000) { newValue[0] = value + ""; newValue[1] = ""; } newValue[0] = text2 ? text2 + newValue[0] : newValue[0]; return type ? newValue : newValue[0] + newValue[1]; } /** * 向下取整 * @param num - 数值 * @param decimal - 小数位数 * @returns */ function toFixedFloor(num, decimal = 2) { return ( Math.floor(num * Math.pow(10, decimal)) / Math.pow(10, decimal) ).toFixed(decimal); } /** * 获取当前时间 * @returns */ export function getCurrentDate() { const date = new Date(); const month = date.getMonth() + 1; const day = date.getDate(); var s1 = date.getFullYear() + "-" + (month > 9 ? month : "0" + month) + "-" + (day > 9 ? day : "0" + day); return s1; } /** * @description: 获取字典 * @param {string} - dictType - 字典类型 * @param {boolean} - same - label和value是否相同 */ export const getDictList = (dictType, same?) => { return new Promise((resolve, reject) => { getDictTypeList({ dictType: dictType }).then((res) => { let { code, data = [] } = res || {}; if (code === 200) { data = data || []; data.forEach((item) => { item.key = item.dictName; item.value = item.dictValue; item.label = item.dictName; }); resolve(data); } else { res && reject(res.message); } }); }); };