/**
* 根据数据获取分页数据
*/export const getDataFormPageIndex = (data, pageSize: number) => { if (!(data instanceof Array) || !data.length) { return data; } const result = []; let count = 0; while (count * pageSize < data.length) { result.push(data.slice(count * pageSize, pageSize * (count + 1))); count += 1; } return result;};
/** * 获取url中所有参数 * @returns 返回数据格式 { key: value } */export function getAllUrlParams() { const query = window.location.search.substr(1).split('&'); const obj = {}; for (let i = 0; i < query.length; i++) { const item = query[i]; const arg = item.split('='); if (arg.length === 2) { Object.assign(obj, { [arg[0]]: decodeURIComponent(arg[1]) }); } } return obj;}
/** * Fn 获取url参数 * @param name 参数名 */export function getQueryString(name: string) { const reg = new RegExp(`(^|&)${name}=([^&]*)(&|$)`, 'i'); const r = window.location.search.substr(1).match(reg); if (r != null) { return unescape(r[2]); } return null;}
/**
* 事件订阅
*/export const EventTrigger = (() => { const list = {}; let [listen, trigger, remove] = [undefined, undefined, undefined]; listen = (key, fn) => { if (!list[key]) { list[key] = []; } list[key][0] = fn; }; trigger = (...rest) => { const key = Array.prototype.shift.call(rest); const fns = list[key]; if (!fns || fns.length === 0) { return false; } for (let i = 0, fn; (fn = fns[i]); i++) { fn.apply(this, rest); } }; remove = (key, fn) => { const fns = list[key]; if (!fns) { return false; } if (!fn) { if (fns) { fns.length = 0; } } else { for (let i = fns.length - 1; i >= 0; i--) { const $fn = fns[i]; if ($fn === fn) { fns.splice(i, 1); } } } }; return { listen, trigger, remove };})();
/** * Get the user IP throught the webkitRTCPeerConnection * @param onNewIP {Function} listener function to expose the IP locally * @return undefined */const getUserIP = (onNewIP) => { // onNewIp - your listener function for new IPs // compatibility for firefox and chrome const MyPeerConnection = window.RTCPeerConnection || (window as any).mozRTCPeerConnection || window.webkitRTCPeerConnection; const pc = new MyPeerConnection({ iceServers: [] }); const noop = function () {}; const localIPs = {}; const ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g; function iterateIP(ip) { if (!localIPs[ip]) onNewIP(ip); localIPs[ip] = true; } // create a bogus data channel pc.createDataChannel(''); // create offer and set local description pc.createOffer() .then((sdp) => { sdp.sdp.split('\n').forEach((line) => { if (line.indexOf('candidate') < 0) return; line.match(ipRegex).forEach(iterateIP); }); pc.setLocalDescription(sdp, noop, noop); }) .catch(() => { // An error occurred, so handle the failure to connect }); // listen for candidate events pc.onicecandidate = function (ice) { if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return; ice.candidate.candidate.match(ipRegex).forEach(iterateIP); };};/** * 获取本机IP地址 */export const getCurrentIP = () => new Promise((resolve) => { getUserIP((ip) => { resolve(ip); }); });
/** * Fn 下载excel方法 * @param url 下载地址 */export function downExcel(url: string, blobType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'): void { const ajax = new XMLHttpRequest(); ajax.responseType = 'blob'; ajax.open('GET', url); ajax.onreadystatechange = function () { if (this.readyState === 4) { if (this.status === 200) { const blob = new Blob([this.response], { type: blobType }); const link = document.createElement('a'); let fileName = ''; const arr = ajax .getAllResponseHeaders() .trim() .split(/[\r\n]+/); // 获取responseHeaders const headers = {}; arr.forEach((item) => { const parts = item.split(': '); const header = parts.shift(); const value = parts.join(': '); headers[header] = value; }); if (headers['content-type'].includes('application/json')) { const readerBlob = new FileReader(); readerBlob.readAsText(this.response, 'utf-8'); // 将blob对象转换成字符串 readerBlob.onload = () => { const errorMsg = typeof readerBlob.result === 'string' && JSON.parse(readerBlob.result as string); if (errorMsg.message) { message.error(errorMsg.message); } }; } else { headers['content-disposition'].split(';').forEach((item) => { if (item.indexOf('filename') !== -1) { fileName = item.trim(); // 去掉首尾空格 } }); link.download = decodeURI(fileName.replace('filename=', '')); // 截取文件名 link.style.display = 'none'; const href = URL.createObjectURL(blob); link.href = href; document.body.appendChild(link); link.click(); document.body.removeChild(link); URL.revokeObjectURL(href); } } } else if (this.readyState === 2) { if (this.status === 200) { this.responseType = 'blob'; } else { this.responseType = 'text'; } } }; console.log(ajax, 'ajax', url); ajax.send(null);}