utils.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { message } from 'antd';
  2. import { parse } from 'querystring';
  3. import moment from 'moment';
  4. /* eslint no-useless-escape:0 import/prefer-default-export:0 */
  5. const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
  6. export const isUrl = (path: string): boolean => reg.test(path);
  7. export const isAntDesignPro = (): boolean => {
  8. if (ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') {
  9. return true;
  10. }
  11. return window.location.hostname === 'preview.pro.ant.design';
  12. };
  13. // 给官方演示站点用,用于关闭真实开发环境不需要使用的特性
  14. export const isAntDesignProOrDev = (): boolean => {
  15. const { NODE_ENV } = process.env;
  16. if (NODE_ENV === 'development') {
  17. return true;
  18. }
  19. return isAntDesignPro();
  20. };
  21. export const getPageQuery = () => {
  22. const { href } = window.location;
  23. const qsIndex = href.indexOf('?');
  24. const sharpIndex = href.indexOf('#');
  25. if (qsIndex !== -1) {
  26. if (qsIndex > sharpIndex) {
  27. return parse(href.split('?')[1]);
  28. }
  29. return parse(href.slice(qsIndex + 1, sharpIndex));
  30. }
  31. return {};
  32. };
  33. // 排序这是比较函数
  34. export const compare = (field: string, order: 'descend' | 'ascend') => {
  35. // descend 降序 大到小 ascend 升序 小到大
  36. if (order === 'ascend') {
  37. return function (m: any, n: any) {
  38. var a = m[field];
  39. var b = n[field];
  40. return a - b; //升序
  41. }
  42. } else {
  43. return function (m: any, n: any) {
  44. var a = m[field];
  45. var b = n[field];
  46. return b - a; //降序
  47. }
  48. }
  49. }
  50. // 返回别名
  51. export const getChannelName = (name: string) => {
  52. let newName = name
  53. let abridgeServer: string[] = ['知定', '巨网', '广联', '太古', '云广', '傲星', '弘捷', '开域']
  54. let asName = abridgeServer.find((item: string) => name?.indexOf(item) !== -1)
  55. if (asName) {
  56. newName = asName
  57. } else if (newName?.length > 5) {
  58. newName = newName?.slice(2, 5) + '...'
  59. }
  60. return newName
  61. }
  62. // 输入文案时判断
  63. export const txtLength = (value?: string) => {
  64. if (value) {
  65. let length = value?.length
  66. let text = value?.replace(/[\x00-\xff]/g, '')
  67. return text?.length + Number(((length - text?.length) / 2).toFixed())
  68. } else {
  69. return 0
  70. }
  71. }
  72. // 点击复制
  73. export const copy = (str: string) => {
  74. let element = document.createElement("textarea");
  75. element.id = 'myTextarea'
  76. element.textContent = str
  77. document.body.append(element);
  78. (document.getElementById('myTextarea') as any).select();
  79. document.execCommand("Copy")
  80. document.body.removeChild(element);
  81. message.success(`复制成功:${str}`)
  82. }
  83. type Type = "day" | "week" | "month" | "year" | "years" | "y" | "months" | "M" | "weeks" | "w" | "days" | "d" | "hour" | "hours" | "h" | "minute"
  84. type Format = 'YYYY-MM-DD' | 'YYYY-MM-DD HH' | 'YYYY-MM-DD HH:mm' | 'YYYY-MM-DD HH:mm:ss'
  85. /**往后日期 */
  86. export function useAddTime(num: number, type: Type, format?: Format, date?: Date | string): string {
  87. return moment(date || new Date()).add(num, type).format(format || 'YYYY-MM-DD')
  88. }
  89. /**
  90. * 精度
  91. * @param num1 number
  92. * @param num2 number
  93. * @param operator '+' | '-' | '*' | '/'
  94. * @returns
  95. */
  96. export const getNumber = (num1: number, num2: number, operator: '+' | '-' | '*' | '/') => {
  97. if (!num1) {
  98. return '0.00'
  99. }
  100. return Number(eval(`${num1.toFixed(10)}${operator}${num2.toFixed(10)}`).toFixed(10))
  101. }
  102. /**
  103. * 拼接oss参数获取视频首针图
  104. * @param videoUrl t_0 0秒的视频截图 t_10000 10秒的视频截图帧
  105. */
  106. export const getVideoImgUrl = (videoUrl: string) => {
  107. if (['.mp4', '.swf', '.flv', '.rm', '.ram', '.mov', '.mpg', '.mpeg', '.wmv', '.avi'].some(item => videoUrl.includes(item))) {
  108. return videoUrl + "?x-oss-process=video/snapshot,t_0,f_jpg,w_0,h_0,m_fast,ar_auto"
  109. }
  110. return videoUrl
  111. }