common.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import useMemberStores from '@/stores/member'
  2. import { ElMessage } from 'element-plus'
  3. /**
  4. * 获取token
  5. * @returns
  6. */
  7. export function getToken(): null | string {
  8. return useMemberStores().token
  9. }
  10. /**
  11. * 防抖函数
  12. * @param fn
  13. * @param delay
  14. * @returns
  15. */
  16. export function debounce(fn: (args?: any) => any, delay: number = 300) {
  17. let timer: null | number = null
  18. return function (...args: any) {
  19. if (timer != null) {
  20. clearTimeout(timer)
  21. timer = null
  22. }
  23. timer = setTimeout(() => {
  24. fn.call(this, ...args)
  25. }, delay);
  26. }
  27. }
  28. /**
  29. * 判断是否是url
  30. * @param str
  31. * @returns
  32. */
  33. export function isUrl(str: string): boolean {
  34. return str.indexOf('http://') != -1 || str.indexOf('https://') != -1
  35. }
  36. /**
  37. * 图片输出
  38. * @param path
  39. * @returns
  40. */
  41. export function img(path: string): string {
  42. const runtimeConfig = useRuntimeConfig()
  43. return isUrl(path) ? path : `${runtimeConfig.public.VITE_IMG_DOMAIN || location.origin}/${path}`
  44. }
  45. /**
  46. * 金额格式化
  47. */
  48. export function moneyFormat(money: string): string {
  49. return isNaN(parseFloat(money)) ? money : parseFloat(money).toFixed(2)
  50. }
  51. /**
  52. * @description 深度克隆
  53. * @param {object} obj 需要深度克隆的对象
  54. * @returns {*} 克隆后的对象或者原值(不是对象)
  55. */
  56. export function deepClone(obj: object) {
  57. // 对常见的“非”值,直接返回原来值
  58. if ([null, undefined, NaN, false].includes(obj)) return obj
  59. if (typeof obj !== 'object' && typeof obj !== 'function') {
  60. // 原始类型直接返回
  61. return obj
  62. }
  63. const o = isArray(obj) ? [] : {}
  64. for (const i in obj) {
  65. if (obj.hasOwnProperty(i)) {
  66. o[i] = typeof obj[i] === 'object' ? deepClone(obj[i]) : obj[i]
  67. }
  68. }
  69. return o
  70. }
  71. const isArray = (value: any) => {
  72. if (typeof Array.isArray === 'function') {
  73. return Array.isArray(value)
  74. }
  75. return Object.prototype.toString.call(value) === '[object Array]'
  76. }
  77. /**
  78. * 复制
  79. * @param {Object} message
  80. * @param {Object} callback
  81. */
  82. export function copy(value, callback) {
  83. var oInput = document.createElement('input'); //创建一个隐藏input(重要!)
  84. oInput.value = value; //赋值
  85. oInput.setAttribute("readonly", "readonly");
  86. document.body.appendChild(oInput);
  87. oInput.select(); // 选择对象
  88. document.execCommand("Copy"); // 执行浏览器复制命令
  89. oInput.className = 'oInput';
  90. oInput.style.display = 'none';
  91. document.body.removeChild(oInput); // 删除创建的对象
  92. ElMessage({
  93. message: '复制成功',
  94. type: 'success',
  95. })
  96. typeof callback == 'function' && callback();
  97. }
  98. /**
  99. * 过滤特殊字符
  100. * @param event
  101. */
  102. export function filterSpecial(event:any){
  103. event.target.value = event.target.value.replace(/[^\u4e00-\u9fa5a-zA-Z0-9]/g, '')
  104. event.target.value = event.target.value.replace(/[`~!@#$%^&*()_\-+=<>?:"{}|,.\/;'\\[\]·~!@#¥%……&*()——\-+={}|《》?:“”【】、;‘’,。、]/g,'')
  105. }