// 统一暴露工具,供其他地方调用 // 数组转对象 export function arrToObj(arr) { // 参数必须是一个数组 if (!Array.isArray(arr)) { throw new Error("type err: arr 必须是一个数组"); } // 存放返回结果 const obj = {}; arr.map((item) => { obj[Object.keys(item)[0]] = item[Object.keys(item)[0]]; }); return obj; } /* * 文件流转换为url * @params file 文件流 * @return url */ export function getObjectURL(file) { console.log(file) let url = null; if (window.createObjectURL != undefined) { // basic url = window.createObjectURL(file); } else if (window.webkitURL != undefined) { // webkit or chrome url = window.webkitURL.createObjectURL(file); } else if (window.URL != undefined) { // mozilla(firefox) url = window.URL.createObjectURL(file); } return url; } /** * @description 文件下载(兼容IE) * @param {Object} response options 接口响应的数据 包括文件流及保文信息 * @param {String} file_name 文件名称 * @returns {undefined} undefined */ export function downloadFile(response, file_name = "ExportFile") { const fileType = response.headers["content-type"] || "application/pdf"; const disposition = response.headers["content-disposition"] if (disposition) { file_name = disposition.split("filename=")[1] } const blob = new Blob([response.data], { type: `${fileType};charset=utf-8` }); const ie = navigator.userAgent.match(/MSIE\s([\d.]+)/), ie11 = navigator.userAgent.match(/Trident\/7.0/) && navigator.userAgent.match(/rv:11/), ieEDGE = navigator.userAgent.match(/Edge/g), ieVer = ie ? ie[1] : ie11 ? 11 : ieEDGE ? 12 : -1; if (ie && ieVer < 10) { this.message.error('No blobs on IE<10'); return; } if (ieVer > -1) { window.navigator.msSaveBlob(blob, file_name); return {} } else { const url = window.URL.createObjectURL(blob); return { url, file_name } } } //导出excel export function exportFile(res, file_name) { let blob = new Blob([res.data], { type:"application/vnd.ms-excel;charset=utf-8" }); let url=window.URL.createObjectURL(blob); let aLink=document.createElement("a"); aLink.style.display="none"; aLink.href=url; aLink.setAttribute("download", file_name); document.body.appendChild(aLink); aLink.click(); document.body.removeChild(aLink); window.URL.revokeObjectURL(url); } export const numValid = /^([1-9][0-9]*|0)([.][0-9]+)?$/ // 对象转换成query字符串 export const toQuery = (params) => { if (!params) { return ""; } let query = []; for (let i in params) { query.push(`${i}=${params[i]}`); } return query.join("&"); } // 数值计算 export const accuracy = { division(arg1, arg2) { //除法 var t1 = 0, t2 = 0, r1, r2; try { t1 = arg1.toString().split('.')[1].length; } catch(e) { console.log(e); } try { t2 = arg2.toString().split('.')[1].length; } catch(e) { console.log(e); } if(Math) { r1 = Number(arg1.toString().replace('.', '')); r2 = Number(arg2.toString().replace('.', '')); return this.multiply(r1 / r2, Math.pow(10, t2 - t1)); } }, multiply(arg1, arg2) { //乘法 var m = 0, s1 = arg1.toString(), s2 = arg2.toString(); try { m += s1.split('.')[1].length; } catch(e) { console.log(e) } try { m += s2.split('.')[1].length; } catch(e) { console.log(e) } return Number(s1.replace('.', '')) * Number(s2.replace('.', '')) / Math.pow(10, m); }, add(arg1, arg2) { //加法 var r1, r2, m; try { r1 = arg1.toString().split('.')[1].length; } catch(e) { r1 = 0; } try { r2 = arg2.toString().split('.')[1].length; } catch(e) { r2 = 0; } m = Math.pow(10, Math.max(r1, r2)); console.log(this.multiply(arg1, m), this.multiply(arg2, m)) return(this.multiply(arg1, m) + this.multiply(arg2, m)) / m; }, subtract(arg1, arg2) { //减法 var r1, r2, m, n; try { r1 = arg1.toString().split('.')[1].length; } catch(e) { r1 = 0; } try { r2 = arg2.toString().split('.')[1].length; } catch(e) { r2 = 0; } m = Math.pow(10, Math.max(r1, r2)); n = r1 >= r2 ? r1 : r2; return((arg1 * m - arg2 * m) / m).toFixed(n); }, // 计算一个数组的总和 sum(arr) { let result = 0; arr.forEach(item => { result = this.add(result, item); }) return result; }, // 计算一个数组的总和 fn是对每项数据的处理方式 sumBy(arr, fn) { let result = 0; arr.forEach(item => { let val = item; if(fn) { val = fn(item) } result = this.add(result, val); }) return result; }, toCeil(num, p = 4) { num = isNaN(num) ? 0 : num; p = isNaN(p) ? 0 : p; let precision = Math.pow(10, p); return Math.ceil((num * precision).toFixed(1)) / precision; }, toFloor(num, p = 4) { num = isNaN(num) ? 0 : num; p = isNaN(p) ? 0 : p; let precision = Math.pow(10, p); return Math.floor((num * precision).toFixed(1)) / precision; } };