index.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * @Author: wjc
  3. * @Date: 2023-05-25 16:51:28
  4. * @LastEditors: LiZhiWei
  5. * @LastEditTime: 2026-01-09 16:48:52
  6. * @Description:
  7. */
  8. import md5 from 'crypto-js/md5'
  9. /**
  10. * 打开新标签页
  11. * @param href 链接
  12. */
  13. export function openNew(href: string) {
  14. let newWin = window.open('about:blank', '_blank')
  15. newWin!.location.href = href
  16. newWin = null
  17. }
  18. /**
  19. * @description MD5加密字符串
  20. * @param code 加密目标
  21. * @returns string
  22. */
  23. export const encryptByMd5 = (code: string) => {
  24. return md5(code).toString().toUpperCase()
  25. }
  26. /**
  27. * @description 校验手机号码
  28. * @param string value 值
  29. */
  30. export const testMobile = (value: string) => {
  31. return /^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(value)
  32. }
  33. /**
  34. * @description 校验邮箱
  35. * @param string value 值
  36. */
  37. export const checkEmail = (value: string) => {
  38. return /^[a-zA-Z0-9]+([a-zA-Z0-9-_.]*)@([a-zA-Z0-9]+[-|_|.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/g.test(
  39. value
  40. )
  41. }
  42. /**
  43. * @description 校验固定电话
  44. * @param string value 值
  45. */
  46. export const testPhone = (value: string) => {
  47. return /^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/.test(value)
  48. }
  49. /**
  50. * @description 校验身份证18位
  51. * @param string value 值
  52. */
  53. export const testIdCard = (value: string) => {
  54. return /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(
  55. value
  56. )
  57. }
  58. /**
  59. * 利用Canvas生成设备指纹
  60. */
  61. export function getCanvasFingerprint(): string {
  62. const canvas = document.createElement('canvas') as HTMLCanvasElement
  63. const ctx: CanvasRenderingContext2D = canvas.getContext('2d')!
  64. const txt = 'BrowserLeaks,com <canvas> 1.0'
  65. ctx.textBaseline = 'top'
  66. ctx.font = "14px 'Arial'"
  67. ctx.textBaseline = 'alphabetic'
  68. ctx.fillStyle = '#f60'
  69. ctx.fillRect(125, 1, 62, 20)
  70. ctx.fillStyle = '#069'
  71. ctx.fillText(txt, 2, 15)
  72. ctx.fillStyle = 'rgba(102, 204, 0, 0.7)'
  73. ctx.fillText(txt, 4, 17)
  74. const canvasImageData = canvas.toDataURL()
  75. return encryptByMd5(canvasImageData)
  76. }
  77. /**
  78. * 复制字符串到剪贴板
  79. */
  80. export async function copyToClipboard(text: string) {
  81. try {
  82. // 优先调用浏览器的复制方法
  83. return window.navigator.clipboard.writeText(text)
  84. } catch {
  85. const element = document.createElement('textarea')
  86. const previouslyFocusedElement = document.activeElement
  87. element.value = text
  88. // Prevent keyboard from showing on mobile
  89. element.setAttribute('readonly', '')
  90. element.style.contain = 'strict'
  91. element.style.position = 'absolute'
  92. element.style.left = '-9999px'
  93. element.style.fontSize = '12pt' // Prevent zooming on iOS
  94. const selection = document.getSelection()
  95. const originalRange = selection ? selection.rangeCount > 0 && selection.getRangeAt(0) : null
  96. document.body.appendChild(element)
  97. element.select()
  98. // Explicit selection workaround for iOS
  99. element.selectionStart = 0
  100. element.selectionEnd = text.length
  101. document.execCommand('copy')
  102. document.body.removeChild(element)
  103. if (originalRange) {
  104. selection!.removeAllRanges() // originalRange can't be truthy when selection is falsy
  105. selection!.addRange(originalRange)
  106. }
  107. // Get the focus back on the previously focused element, if any
  108. if (previouslyFocusedElement) {
  109. ;(previouslyFocusedElement as HTMLElement).focus()
  110. }
  111. }
  112. }
  113. // export function arrayChunk<T>(source: T[] = [], chunk = 3): ChunkList<T>[] {
  114. // if (isNaN(Number(chunk))) {
  115. // return []
  116. // }
  117. // if (Object.prototype.toString.call(source) !== '[object Array]') {
  118. // return []
  119. // }
  120. // const result = []
  121. // for (let i = 0, j = source.length; i < j; i += chunk) {
  122. // result.push(source.slice(i, i + chunk))
  123. // }
  124. // return result.map((item: any, index) => {
  125. // return {
  126. // id: item.id || index,
  127. // children: item,
  128. // }
  129. // })
  130. // }