utils.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { message } from 'antd';
  2. import { parse } from 'querystring';
  3. /* eslint no-useless-escape:0 import/prefer-default-export:0 */
  4. const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
  5. export const isUrl = (path: string): boolean => reg.test(path);
  6. export const isAntDesignPro = (): boolean => {
  7. if (ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') {
  8. return true;
  9. }
  10. return window.location.hostname === 'preview.pro.ant.design';
  11. };
  12. // 给官方演示站点用,用于关闭真实开发环境不需要使用的特性
  13. export const isAntDesignProOrDev = (): boolean => {
  14. const { NODE_ENV } = process.env;
  15. if (NODE_ENV === 'development') {
  16. return true;
  17. }
  18. return isAntDesignPro();
  19. };
  20. export const getPageQuery = () => {
  21. const { href } = window.location;
  22. const qsIndex = href.indexOf('?');
  23. const sharpIndex = href.indexOf('#');
  24. if (qsIndex !== -1) {
  25. if (qsIndex > sharpIndex) {
  26. return parse(href.split('?')[1]);
  27. }
  28. return parse(href.slice(qsIndex + 1, sharpIndex));
  29. }
  30. return {};
  31. };
  32. // 排序这是比较函数
  33. export const compare = (field: string, order: 'descend' | 'ascend') => {
  34. // descend 降序 大到小 ascend 升序 小到大
  35. if (order === 'ascend') {
  36. return function (m: any, n: any) {
  37. var a = m[field];
  38. var b = n[field];
  39. return a - b; //升序
  40. }
  41. } else {
  42. return function (m: any, n: any) {
  43. var a = m[field];
  44. var b = n[field];
  45. return b - a; //降序
  46. }
  47. }
  48. }
  49. // 返回别名
  50. export const getChannelName = (name: string) => {
  51. let newName = name
  52. let abridgeServer: string[] = ['知定', '巨网', '广联', '太古', '云广', '傲星', '弘捷', '开域']
  53. let asName = abridgeServer.find((item: string) => name?.indexOf(item) !== -1)
  54. if (asName) {
  55. newName = asName
  56. } else if (newName?.length > 5) {
  57. newName = newName?.slice(2, 5) + '...'
  58. }
  59. return newName
  60. }
  61. // 输入文案时判断
  62. export const txtLength = (value?: string) => {
  63. if (value) {
  64. let length = value?.length
  65. let text = value?.replace(/[\x00-\xff]/g, '')
  66. return text?.length + Number(((length - text?.length) / 2).toFixed())
  67. } else {
  68. return 0
  69. }
  70. }
  71. // 点击复制
  72. export const copy = (str: string) => {
  73. let element = document.createElement("textarea");
  74. element.id = 'myTextarea'
  75. element.textContent = str
  76. document.body.append(element);
  77. (document.getElementById('myTextarea') as any).select();
  78. document.execCommand("Copy")
  79. document.body.removeChild(element);
  80. message.success(`复制成功:${str}`)
  81. }