import { message } from 'antd'; import { RcFile } from 'antd/lib/upload'; import { parse } from 'querystring'; /* eslint no-useless-escape:0 import/prefer-default-export:0 */ const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/; export const isUrl = (path: string): boolean => reg.test(path); export const isAntDesignPro = (): boolean => { if (ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') { return true; } return window.location.hostname === 'preview.pro.ant.design'; }; // 给官方演示站点用,用于关闭真实开发环境不需要使用的特性 export const isAntDesignProOrDev = (): boolean => { const { NODE_ENV } = process.env; if (NODE_ENV === 'development') { return true; } return isAntDesignPro(); }; export const getPageQuery = () => { const { href } = window.location; const qsIndex = href.indexOf('?'); const sharpIndex = href.indexOf('#'); if (qsIndex !== -1) { if (qsIndex > sharpIndex) { return parse(href.split('?')[1]); } return parse(href.slice(qsIndex + 1, sharpIndex)); } return {}; }; // 排序这是比较函数 export const compare = (field: string, order: 'descend' | 'ascend') => { // descend 降序 大到小 ascend 升序 小到大 if (order === 'ascend') { return function (m: any, n: any) { var a = m[field]; var b = n[field]; return a - b; //升序 } } else { return function (m: any, n: any) { var a = m[field]; var b = n[field]; return b - a; //降序 } } } // 返回别名 export const getChannelName = (name: string) => { let newName = name let abridgeServer: string[] = ['知定', '巨网', '广联', '太古', '云广', '傲星', '弘捷', '开域'] let asName = abridgeServer.find((item: string) => name?.indexOf(item) !== -1) if (asName) { newName = asName } else if (newName?.length > 5) { newName = newName?.slice(2, 5) + '...' } return newName } // 输入文案时判断 export const txtLength = (value?: string) => { if (value) { let length = value?.length let text = value?.replace(/[\x00-\xff]/g, '') return text?.length + Number(((length - text?.length) / 2).toFixed()) } else { return 0 } } // 返回图片宽高 export const getImgSize = (file: RcFile): Promise<{ width: number, height: number }> => { return new Promise((resolve: (value: any) => void, reject: (reason?: any) => void) => { if (file) { let img: any = new Image(); let _URL = window.URL || window.webkitURL; img.onload = function (e: any) { resolve({ width: this.width, height: this.height }) } img.src = _URL.createObjectURL(file); } else { reject() } }) } // 返回落地页组件key export const getTypeKey = (key: string): string => { switch (key) { case 'TOP_IMAGE': return 'topImageSpec' case 'TOP_SLIDER': return 'topSliderSpec' case 'TOP_VIDEO': return 'topVideoSpec' case 'IMAGE': return 'imageSpec' case 'TEXT': return 'textSpec' case 'GH': return 'ghSpec' case 'ENTERPRISE_WX': return 'enterpriseWxSpec' case 'IMAGE_TEXT': return 'imageTextSpec' case 'FLOAT_BUTTON': return 'floatButtonSpec' } return '' } // 点击复制 export const copy = (str: string) => { let element = document.createElement("textarea"); element.id = 'myTextarea' element.textContent = str document.body.append(element); (document.getElementById('myTextarea') as any).select(); document.execCommand("Copy") document.body.removeChild(element); message.success(`复制成功:${str}`) } // 数组分组 export const groupBy = (array: any[], f: (item: any) => any[]) => { const groups = {}; array.forEach(function (o) { //注意这里必须是forEach 大写 const group = JSON.stringify(f(o)); groups[group] = groups[group] || []; groups[group].push(o); }); return Object.keys(groups).map(function (group) { return groups[group]; }); } export const replaceSpecialTxt = (text: string | number | null | undefined) => { if (text) { return text.toString().replace(/[<>]/ig, '') } else { return text } } /** * 随机生成字符串 * @param flag * @param min * @param max * @returns string */ export const randomString = (flag: boolean, min: number, max: number) => { let str = "", range = min let arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']; if (flag) { range = Math.round(Math.random() * (max - min)) + min; } for (let i = 0; i < range; i++) { let pos = Math.round(Math.random() * (arr.length - 1)); str += arr[pos]; } return str; } /** * 找2个数组不同元素 * @param arr1 * @param arr2 * @returns */ export const getArrDifference = (arr1: any[], arr2: any[]) => { return arr1.concat(arr2).filter((v, i, arr) => { return arr.indexOf(v) === arr.lastIndexOf(v); }); } /** * 拼接oss参数获取视频首针图 * @param videoUrl t_0 0秒的视频截图 t_10000 10秒的视频截图帧 */ export const getVideoImgUrl = (videoUrl: string) => { if (['.mp4', '.swf', '.flv', '.rm', '.ram', '.mov', '.mpg', '.mpeg', '.wmv', '.avi'].some(item => videoUrl.includes(item))) { return videoUrl + "?x-oss-process=video/snapshot,t_0,f_jpg,w_0,h_0,m_fast,ar_auto" } return videoUrl }