123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- import { message } from 'antd';
- import { parse } from 'querystring';
- import moment from 'moment';
- /* 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 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}`)
- }
- type Type = "day" | "week" | "month" | "year" | "years" | "y" | "months" | "M" | "weeks" | "w" | "days" | "d" | "hour" | "hours" | "h" | "minute"
- type Format = 'YYYY-MM-DD' | 'YYYY-MM-DD HH' | 'YYYY-MM-DD HH:mm' | 'YYYY-MM-DD HH:mm:ss'
- /**往后日期 */
- export function useAddTime(num: number, type: Type, format?: Format, date?: Date | string): string {
- return moment(date || new Date()).add(num, type).format(format || 'YYYY-MM-DD')
- }
- /**
- * 精度
- * @param num1 number
- * @param num2 number
- * @param operator '+' | '-' | '*' | '/'
- * @returns
- */
- export const getNumber = (num1: number, num2: number, operator: '+' | '-' | '*' | '/') => {
- if (!num1) {
- return '0.00'
- }
- return Number(eval(`${num1.toFixed(10)}${operator}${num2.toFixed(10)}`).toFixed(10))
- }
- /**
- * 拼接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
- }
|