import { BASE_IMG_URL } from "@/utils/request" export default { splitImgHost(url) { if (!url) { return '' } if (url.indexOf("http") != -1 || url.indexOf("https") != -1 || url.indexOf("base64") != -1) { return url; } return BASE_IMG_URL + url }, //秒级别 onlyForma(timeStamp, Diszing = true) { if (!timeStamp) { return } var date = new Date(timeStamp * 1000); //时间戳为10位需*1000,时间戳为13位的话不需乘1000 var Y = date.getFullYear() + '-'; var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'; var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'; var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'; var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds(); if (Diszing) { return Y + M + D + ' ' + h + m + s; //时分秒可以根据自己的需求加上 } return Y + M + D; }, //毫秒级别 onlyFormaHao(timeStamp, Diszing = true) { if (!timeStamp) { return } var date = new Date(timeStamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000 var Y = date.getFullYear() + '-'; var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'; var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(); var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'; var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'; var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds(); if (Diszing) { return Y + M + D + ' ' + h + m + s; //时分秒可以根据自己的需求加上 } return Y + M + D; }, //毫秒级别 onlyFormaWeek(timeStamp, Diszing = true) { if (!timeStamp) { return } var date = new Date(timeStamp); //时间戳为10位需*1000,时间戳为13位的话不需乘1000 var Y = date.getFullYear() + '年'; var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) + '月' : date.getMonth() + 1 + '月'); var D = date.getDate() < 10 ? '0' + date.getDate() + '日' : date.getDate() + '日'; var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'; var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'; var s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds(); let dis = '' if (Diszing) { dis = Y + M + D + ' ' + h + m + s; //时分秒可以根据自己的需求加上 } dis = Y + M + D; var str = ""; var week = new Date().getDay(); if (week == 0) { str = "星期日"; } else if (week == 1) { str = "星期一"; } else if (week == 2) { str = "星期二"; } else if (week == 3) { str = "星期三"; } else if (week == 4) { str = "星期四"; } else if (week == 5) { str = "星期五"; } else if (week == 6) { str = "星期六"; } return dis + ' ' + str }, //判断是否为数字 isNotANumber(inputData) { //isNaN(inputData)不能判断空串或一个空格 //如果是一个空串或是一个空格,而isNaN是做为数字0进行处理的,而parseInt与parseFloat是返回一个错误消息,这个isNaN检查不严密而导致的。 if (parseFloat(inputData).toString() == "NaN") { //alert("请输入数字……");注掉,放到调用时,由调用者弹出提示。 return false; } else { return true; } }, //判断是否为数组 isArrayFn(o) { return Object.prototype.toString.call(o) === '[object Array]'; }, /** * * @param {int} result * @returns {string} * @remard 单位S转小时分钟秒 */ secondToDate(result, Diszing = true) { var h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600); var m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60)); var s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60)); if (h == 0 && Diszing) { result = m + ":" + s; } else { result = h + ':' + m + ":" + s } return result; }, /** * @param {string} result * @returns int * @remard 00:00:00格式转秒 */ secondFormDate(result) { if (result) { let reTime = result.split(":").map(Number) var hs = reTime[0] * 60 * 60 var ms = reTime[1] * 60 var ss = reTime[2] return hs + ms + ss } else { return 0 } }, /** * 扁平结构转换成树形结构 ---最佳性能方法 * @param {Arrays} items 数据 * @param {Strings} Id 参数名称 例如:'id' 默认id * @param {Strings} pId 参数名称 例如:'pid' 默认 pid * @returns 返回树形结构 */ arrayToTree(items, Id = 'id', pId = 'pid') { const result = []; // 存放结果集 const itemMap = {}; // for (const item of items) { const id = item[Id]; const pid = item[pId]; if (!itemMap[id]) { itemMap[id] = { children: [], }; } itemMap[id] = { ...item, children: itemMap[id]["children"], }; const treeItem = itemMap[id]; if (pid === 0) { result.push(treeItem); } else { if (!itemMap[pid]) { itemMap[pid] = { children: [], }; } itemMap[pid].children.push(treeItem); } } return result; }, getQueryVariable(variable) { var query = window.location.search.substring(1); var vars = query.split("&"); for (var i = 0; i < vars.length; i++) { var pair = vars[i].split("="); if (pair[0] == variable) { return pair[1]; } } return (false); }, /** * * @param {Arrays} arr 数据 * @param {Strings} uniId 根据哪个参数去重 * @returns 对象数组去重 */ uniqueFunc(arr, uniId) { const res = new Map(); return arr.filter( (item) => !res.has(item[uniId]) && res.set(item[uniId], 1) ); }, exportData(msg, status = true) { if (!msg) { this.$message.warning("导出地址获取错误,请联系开发人员处理") return } var baseUrl =BASE_IMG_URL + '/' let url = baseUrl + msg; let link = document.createElement("a"); let fileName = "导入模板" + ".xlsx"; document.body.appendChild(link); link.href = url; link.download = fileName; link.click(); link.remove(); }, downloadPicBase (msg) { var baseUrl =BASE_IMG_URL let imgsrc = baseUrl + msg; var image = new Image() // 解决跨域canvas污染问题 image.setAttribute('crossOrigin', 'anonymous') image.onload = function () { let canvas = document.createElement('canvas') canvas.width = image.width canvas.height = image.height let context = canvas.getContext('2d') context.drawImage(image, 0, 0, image.width, image.height) let url = canvas.toDataURL('image/png') // 得到图片的base64编码数据 let a = document.createElement('a') a.download = 'download' a.href = url a.click() } image.src = imgsrc }, //base64转blob base64ToBlob(code) { const parts = code.split(';base64,'); const contentType = parts[0].split(':')[1]; const raw = window.atob(parts[1]); const rawLength = raw.length; const uInt8Array = new Uint8Array(rawLength); for (let i = 0; i < rawLength; ++i) { uInt8Array[i] = raw.charCodeAt(i); } return new Blob([uInt8Array], { type: contentType }); }, //下载 downloadbase64(fileName, content) { const blob = this.base64ToBlob(content); // new Blob([content]); if (window.navigator.msSaveOrOpenBlob) { navigator.msSaveBlob(blob, fileName); } else { const link = document.createElement('a'); link.href = window.URL.createObjectURL(blob); link.download = fileName; //此写法兼容可火狐浏览器 document.body.appendChild(link); const evt = document.createEvent("MouseEvents"); evt.initEvent("click", false, false); link.dispatchEvent(evt); document.body.removeChild(link); } }, downloadPic (msg) { var baseUrl =BASE_IMG_URL let imgsrc = baseUrl + msg; let x = new XMLHttpRequest() x.open('GET', imgsrc, true) x.responseType = 'blob' x.onload = function () { let url = window.URL.createObjectURL(x.response) let a = document.createElement('a') a.href = url a.download = 'download'; a.click() } x.send() } }