utils.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { message } from 'antd';
  2. import { RcFile } from 'antd/lib/upload';
  3. import { parse } from 'querystring';
  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 getImgSize = (file: RcFile): Promise<{ width: number, height: number }> => {
  74. return new Promise((resolve: (value: any) => void, reject: (reason?: any) => void) => {
  75. if (file) {
  76. let img: any = new Image();
  77. let _URL = window.URL || window.webkitURL;
  78. img.onload = function (e: any) {
  79. resolve({ width: this.width, height: this.height })
  80. }
  81. img.src = _URL.createObjectURL(file);
  82. } else {
  83. reject()
  84. }
  85. })
  86. }
  87. // 返回落地页组件key
  88. export const getTypeKey = (key: string): string => {
  89. switch (key) {
  90. case 'TOP_IMAGE':
  91. return 'topImageSpec'
  92. case 'TOP_SLIDER':
  93. return 'topSliderSpec'
  94. case 'TOP_VIDEO':
  95. return 'topVideoSpec'
  96. case 'IMAGE':
  97. return 'imageSpec'
  98. case 'TEXT':
  99. return 'textSpec'
  100. case 'GH':
  101. return 'ghSpec'
  102. case 'ENTERPRISE_WX':
  103. return 'enterpriseWxSpec'
  104. case 'IMAGE_TEXT':
  105. return 'imageTextSpec'
  106. case 'FLOAT_BUTTON':
  107. return 'floatButtonSpec'
  108. }
  109. return ''
  110. }
  111. // 点击复制
  112. export const copy = (str: string) => {
  113. let element = document.createElement("textarea");
  114. element.id = 'myTextarea'
  115. element.textContent = str
  116. document.body.append(element);
  117. (document.getElementById('myTextarea') as any).select();
  118. document.execCommand("Copy")
  119. document.body.removeChild(element);
  120. message.success(`复制成功:${str}`)
  121. }
  122. // 数组分组
  123. export const groupBy = (array: any[], f: (item: any) => any[]) => {
  124. const groups = {};
  125. array.forEach(function (o) { //注意这里必须是forEach 大写
  126. const group = JSON.stringify(f(o));
  127. groups[group] = groups[group] || [];
  128. groups[group].push(o);
  129. });
  130. return Object.keys(groups).map(function (group) {
  131. return groups[group];
  132. });
  133. }
  134. export const replaceSpecialTxt = (text: string | number | null | undefined) => {
  135. if (text) {
  136. return text.toString().replace(/[<>]/ig, '')
  137. } else {
  138. return text
  139. }
  140. }
  141. /**
  142. * 随机生成字符串
  143. * @param flag
  144. * @param min
  145. * @param max
  146. * @returns string
  147. */
  148. export const randomString = (flag: boolean, min: number, max: number) => {
  149. let str = "", range = min
  150. 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'];
  151. if (flag) {
  152. range = Math.round(Math.random() * (max - min)) + min;
  153. }
  154. for (let i = 0; i < range; i++) {
  155. let pos = Math.round(Math.random() * (arr.length - 1));
  156. str += arr[pos];
  157. }
  158. return str;
  159. }
  160. /**
  161. * 找2个数组不同元素
  162. * @param arr1
  163. * @param arr2
  164. * @returns
  165. */
  166. export const getArrDifference = (arr1: any[], arr2: any[]) => {
  167. return arr1.concat(arr2).filter((v, i, arr) => {
  168. return arr.indexOf(v) === arr.lastIndexOf(v);
  169. });
  170. }
  171. /**
  172. * 拼接oss参数获取视频首针图
  173. * @param videoUrl t_0 0秒的视频截图 t_10000 10秒的视频截图帧
  174. */
  175. export const getVideoImgUrl = (videoUrl: string) => {
  176. if (['.mp4', '.swf', '.flv', '.rm', '.ram', '.mov', '.mpg', '.mpeg', '.wmv', '.avi'].some(item => videoUrl.includes(item))) {
  177. return videoUrl + "?x-oss-process=video/snapshot,t_0,f_jpg,w_0,h_0,m_fast,ar_auto"
  178. }
  179. return videoUrl
  180. }