public.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import store from '@/store'
  2. import {stringIsBlank} from "@/utils/blank-utils";
  3. import {xcMessage} from "@/utils/xiaochan-element-plus";
  4. export function needRule(...val) {
  5. const userRoles = store.state.user.info.roles
  6. if (userRoles.includes(1)) {
  7. return true;
  8. }
  9. for (let i = 0; i < val.length; i++) {
  10. if (userRoles.includes(val[i])) {
  11. return true;
  12. }
  13. }
  14. return false;
  15. }
  16. export function noNeedRule(...val) {
  17. return !needRule(val)
  18. }
  19. export let isDev = import.meta.env.VITE_BASE_URL !== 'http://172.16.32.160:8077'
  20. export const nullToEmpty = (val) => {
  21. if (stringIsBlank(val)) {
  22. return ''
  23. } else {
  24. return val
  25. }
  26. }
  27. export const elementAddBody = (val) => {
  28. const body = document.querySelector("body");
  29. if (body.append) {
  30. body.append(val);
  31. } else {
  32. body.appendChild(val);
  33. }
  34. }
  35. /**
  36. * 对比版本号
  37. * @param version1 版本1
  38. * @param version2 版本 2
  39. * @returns {number} 1 大于 -1小于 0等于
  40. */
  41. export function compareVersion(version1, version2) {
  42. const arr1 = version1.split('.')
  43. const arr2 = version2.split('.')
  44. const length1 = arr1.length
  45. const length2 = arr2.length
  46. const minlength = Math.min(length1, length2)
  47. let i = 0
  48. for (i; i < minlength; i++) {
  49. let a = parseInt(arr1[i])
  50. let b = parseInt(arr2[i])
  51. if (a > b) {
  52. return 1
  53. } else if (a < b) {
  54. return -1
  55. }
  56. }
  57. if (length1 > length2) {
  58. for (let j = i; j < length1; j++) {
  59. if (parseInt(arr1[j]) !== 0) {
  60. return 1
  61. }
  62. }
  63. return 0
  64. } else if (length1 < length2) {
  65. for (let j = i; j < length2; j++) {
  66. if (parseInt(arr2[j]) !== 0) {
  67. return -1
  68. }
  69. }
  70. return 0
  71. }
  72. return 0
  73. }
  74. export const chineseEncrypt = {
  75. encrypt: (val) => {
  76. if (stringIsBlank(val)) {
  77. return "";
  78. }
  79. return encodeURI(JSON.stringify(val))
  80. },
  81. decrypt: (val) => {
  82. if (stringIsBlank(val)) {
  83. return "";
  84. }
  85. return JSON.parse(decodeURI(val))
  86. }
  87. }
  88. export const copyStrFunc = (val) => {
  89. let input = document.createElement('input')
  90. document.body.appendChild(input);
  91. input.value = val;
  92. input.select();
  93. document.execCommand("copy");
  94. document.body.removeChild(input);
  95. xcMessage.success("复制成功。")
  96. }
  97. // 判断字符串是否包含中文
  98. export const isChinese = (str) => {
  99. return /[\u4e00-\u9fa5]/.test(str);
  100. }
  101. // 判断字符串是否为纯英文
  102. export const isEnglish = (str) => {
  103. return /^[a-zA-Z]+$/.test(str);
  104. }