import { RefObject, useEffect, useLayoutEffect, useState } from 'react'; import { RGBColor } from 'react-color'; import $dict from 'src/utils/dict'; // 生成uuid function uuid(len: number, radix: number) { let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split(''); let uuid = [], i; radix = radix || chars.length; if (len) { for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)]; } else { let r; uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'; uuid[14] = '4'; for (i = 0; i < 36; i++) { if (!uuid[i]) { r = 0 | (Math.random() * 16); uuid[i] = chars[i === 19 ? (r & 0x3) | 0x8 : r]; } } } return uuid.join(''); } // 将rgba字符串对象转化为rgba对象 function rgba2Obj(rgba = '') { let reg = /rgba\((\d+),(\d+),(\d+),(\d+)\)/g; let rgbaObj: RGBColor = { r: 0, g: 0, b: 0, a: 0 }; rgba.replace(reg, (_m, r, g, b, a) => { rgbaObj = { r, g, b, a }; return rgba; }); return rgbaObj; } export { uuid, rgba2Obj }; export const isDev = process.env.NODE_ENV === 'development'; export function useGetRect() { const [rect, setRect] = useState({ width: 0, height: 0 }); useEffect(() => { setRect({ width: window.innerWidth, height: window.innerHeight, }); }, []); return rect; } export function useGetScrollBarWidth(ref: RefObject) { const [width, setWidth] = useState(0); useLayoutEffect(() => { if (ref.current) { const diff = ref.current.offsetWidth - ref.current.clientWidth; setWidth(diff); } }, [ref]); return width; } export function useAnimation(state: boolean, delay: number) { const [display, setDisplay] = useState(false); useEffect(() => { let timer: number; if (state && display === true) { setDisplay(false); } else if (!state && display === false) { timer = window.setTimeout(() => { setDisplay(true); }, delay); } return () => { window.clearTimeout(timer); }; }, [delay, display, state]); return [display, setDisplay]; } export function unParams(params = '?a=1&b=2&c=3') { let obj: any = {}; params && // eslint-disable-next-line no-useless-escape params.replace(/((\w*)=([\.a-z0-9A-Z]*)?)?/g, (m, a, b, c): any => { if (b || c) obj[b] = c; }); return obj; } export function throttle(fn: Function, delay: number) { let flag = true; return (...args: any) => { if (flag) { flag = false; fn(...args); setTimeout(() => { flag = true; }, delay); } }; } export function formatTime(fmt: string, dateObj: any) { const date = dateObj || new Date(); const o: any = { 'M+': date.getMonth() + 1, //月份 'd+': date.getDate(), //日 'h+': date.getHours(), //小时 'm+': date.getMinutes(), //分 's+': date.getSeconds(), //秒 'q+': Math.floor((date.getMonth() + 3) / 3), //季度 S: date.getMilliseconds(), //毫秒 }; if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length)); } for (var k in o) { if (new RegExp('(' + k + ')').test(fmt)) { fmt = fmt.replace( RegExp.$1, RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length), ); } } return fmt; } export function nodeToString(node, type) { //createElement()返回一个Element对象 let tmpNode = document.createElement("div"); //appendChild() 参数Node对象 返回Node对象 Element方法 //cloneNode() 参数布尔类型 返回Node对象 Element方法 tmpNode.appendChild(node.cloneNode(true)); let str = tmpNode.innerHTML; tmpNode = node = null; // prevent memory leaks in IE if (type === 'wapContainer') { str = str.replace(/(\d+)px/g, function (val, group) { // wap编辑器宽是大于750px的,生成的样式转换成rem要/100,参考 “document.documentElement.clientWidth > 750” return group / 100 + 'rem' }) } return `
${str}
`; } const getMeiQiaScript = (online_consult, type) => { var html = ''; if (online_consult != 1) { return html; } html = ''; if (type == 'wap') { html += '
' } return html; } const getScript = (client?: any) => { return client == 'wap' ? '\n' : '\n' } const getBanner = (client?: any) => { return client == 'wap' ? ' for(var i=0;i<100;i++){\n' + ' $common.banner({\n' + ' name: ".banner"+i,\n' + ' addlit: true,\n' + ' preNext: true,\n' + ' ulodd: true,\n' + ' mode: true\n' + ' });\n' + ' };\n' : ' for(var i=0;i<100;i++){\n' + ' $api.banner({\n' + ' name: ".banner"+i,\n' + ' addlit: true,\n' + ' preNext: true,\n' + ' ulodd: true,\n' + ' mode: true\n' + ' });\n' + ' };\n' } const getFormScript = (client, title) => { const system = localStorage.getItem(`SITE-ALL${SITE_VERSION}`) const _system = system ? JSON.parse(system) : {} const _category = `${_system.name}-${title}(${client == 'wap' ? "WAP" : "PC"})`; var html = '\n' + '\n' + getScript(client) + '\n' + '\n' + ''; return html; } export const createHtml = ($bodyHtml, pageConfigInfo, client) => { var html = '\n' + '\n' + '\n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + ' \n' + '\n' + '\n'; html += $bodyHtml; html += getMeiQiaScript(pageConfigInfo?.online_consult, client); html += getFormScript(client, pageConfigInfo?.title); html += ''; html += '\n'; html = html.replace(/contenteditable="true"/g, "contenteditable='false'"); return html; } export const getLink = url => { return (url.indexOf('http://') > -1 || url.indexOf('https://') > -1) ? url : `http://${url}` } export const makeAssetGroupTreeData = (data, pid = 0, allData?: any[]) => { if (!allData) allData = data return data?.filter(i => i.pid == pid).map((item) => { const children = makeAssetGroupTreeData(allData.filter(i => i.pid.toString() === item.id.toString()), item.id, allData) if (children.length === 0) { return { url:item.url, value: item.id.toString(), title: item.title, type: item.type, key:item.id, path:item.path } } return { url:item.url, value: item.id.toString(), title: item.title, key:item.id, type: item.type, path:item?.path, children } }) }