utils.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import router from "@/router";
  2. import { store } from "@/store/store";
  3. import CryptoJS from "crypto-js";
  4. import { MD5 } from "./md5";
  5. export function setUUID() {
  6. if (!localStorage.getItem("uuidForEcs")) {
  7. const uuidForEcs = "" + Date.now() + Math.random();
  8. localStorage.setItem("uuidForEcs", uuidForEcs);
  9. }
  10. }
  11. // 如果这台机器上登录过的学生人数小于5,那么这台电脑就是学生的
  12. export function isThisMachineOwnByStudent() {
  13. return JSON.parse(localStorage.getItem("userIds") || "[]").length < 5;
  14. }
  15. /** 通用的退出登录函数
  16. * @param cause - 退出登录的原因
  17. */
  18. export function showLogout(cause: string) {
  19. logger({
  20. pgu: "AUTO",
  21. cnl: ["console", "local", "server"],
  22. dtl: "用户被要求重新登录",
  23. ext: { cause },
  24. });
  25. $message.warning(cause, { duration: 5 * 60 * 1000, closable: true });
  26. void router.push({ name: "UserLogin" });
  27. }
  28. /**
  29. * 将阿拉伯数字转换成汉字数字
  30. * @param num 数值
  31. * @returns 汉字数字
  32. */
  33. export function toChineseNumber(num: number) {
  34. let ret = "";
  35. if (num < 10) {
  36. ret = num.toLocaleString("zh-u-nu-hanidec");
  37. } else if (num === 10) {
  38. ret = "十";
  39. } else if (num > 10 && num < 20) {
  40. ret = "十" + (num % 10).toLocaleString("zh-u-nu-hanidec");
  41. } else if (num >= 20 && num < 100) {
  42. const s = num
  43. .toLocaleString("zh-u-nu-hanidec", { useGrouping: false })
  44. .split("");
  45. s.splice(1, 0, "十");
  46. ret = s.join("").replace("〇", "");
  47. } else {
  48. ret = num.toLocaleString("zh-u-nu-hanidec"); // 假设没有超过100的大题
  49. }
  50. return ret;
  51. }
  52. export function preloadResource(url: string) {
  53. const preloadLink = document.createElement("link");
  54. preloadLink.href = url;
  55. preloadLink.rel = "preload";
  56. preloadLink.as = "script";
  57. document.head.appendChild(preloadLink);
  58. }
  59. // let salt: string;
  60. // export function setSalt(_salt: string) {
  61. // salt = _salt;
  62. // }
  63. export function getSalt() {
  64. return store.user.salt;
  65. }
  66. export function getKey(timestamp: number) {
  67. const { key, token } = store.user;
  68. if (!key || !token) return "";
  69. return MD5(`key=${key}&token=${token}&timestamp=${timestamp}`);
  70. }
  71. function toHex(str: string) {
  72. if (!str) {
  73. return "";
  74. }
  75. const chars = [];
  76. for (let n = 0; n < str.length; n++) {
  77. chars.push(str.charCodeAt(n).toString(16));
  78. }
  79. return chars.join("");
  80. }
  81. const ivStr = "@M#A$G%A^2&0*2(1"; // 偏移量
  82. const iv = toHex(ivStr); // 16进制偏移量
  83. export function decryptLogin(encStr: string, key: string): string {
  84. return CryptoJS.enc.Utf8.stringify(
  85. CryptoJS.AES.decrypt(
  86. CryptoJS.format.Hex.parse(encStr),
  87. CryptoJS.enc.Hex.parse(toHex(key)),
  88. {
  89. iv: CryptoJS.enc.Hex.parse(iv),
  90. mode: CryptoJS.mode.CBC,
  91. padding: CryptoJS.pad.Pkcs7,
  92. }
  93. )
  94. );
  95. }
  96. /** AES(带向量) 简写 A */
  97. export function encryptA(str: string, key: string) {
  98. return CryptoJS.AES.encrypt(str, CryptoJS.enc.Hex.parse(toHex(key)), {
  99. // iv: CryptoJS.enc.Hex.parse(iv),
  100. mode: CryptoJS.mode.ECB,
  101. padding: CryptoJS.pad.Pkcs7,
  102. }).ciphertext.toString();
  103. }
  104. export function decryptA(encStr: string, key: string): string {
  105. return CryptoJS.enc.Utf8.stringify(
  106. CryptoJS.AES.decrypt(
  107. CryptoJS.format.Hex.parse(encStr),
  108. CryptoJS.enc.Hex.parse(toHex(key)),
  109. {
  110. // iv: CryptoJS.enc.Hex.parse(iv),
  111. mode: CryptoJS.mode.ECB,
  112. padding: CryptoJS.pad.Pkcs7,
  113. }
  114. )
  115. );
  116. }
  117. /** base64加密 简写B */
  118. export function encryptB(str: string): string {
  119. // return window.btoa(str);
  120. return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(str));
  121. }
  122. /** base64加密 简写B */
  123. export function decryptB(str: string): string {
  124. return CryptoJS.enc.Utf8.stringify(CryptoJS.enc.Base64.parse(str));
  125. }
  126. /** RC4(无向量) 简写 C */
  127. export function encryptC(str: string, key: string) {
  128. return CryptoJS.RC4.encrypt(
  129. str,
  130. CryptoJS.enc.Hex.parse(toHex(key))
  131. ).toString();
  132. }
  133. export function decryptC(encStr: string, key: string): string {
  134. return CryptoJS.enc.Utf8.stringify(
  135. CryptoJS.RC4.decrypt(encStr, CryptoJS.enc.Hex.parse(toHex(key)))
  136. );
  137. }
  138. /** DES(无向量) 简写 D */
  139. export function encryptD(str: string, key: string) {
  140. return CryptoJS.DES.encrypt(str, CryptoJS.enc.Hex.parse(toHex(key)), {
  141. mode: CryptoJS.mode.ECB,
  142. padding: CryptoJS.pad.Pkcs7,
  143. }).toString();
  144. }
  145. export function decryptD(encStr: string, key: string): string {
  146. return CryptoJS.enc.Utf8.stringify(
  147. CryptoJS.DES.decrypt(encStr, CryptoJS.enc.Hex.parse(toHex(key)), {
  148. mode: CryptoJS.mode.ECB,
  149. padding: CryptoJS.pad.Pkcs7,
  150. })
  151. );
  152. }
  153. /** TripleDES(无向量) 简写 E */
  154. export function encryptE(str: string, key: string) {
  155. return CryptoJS.TripleDES.encrypt(str, CryptoJS.enc.Hex.parse(toHex(key)), {
  156. mode: CryptoJS.mode.ECB,
  157. padding: CryptoJS.pad.Pkcs7,
  158. }).toString();
  159. }
  160. export function decryptE(encStr: string, key: string): string {
  161. return CryptoJS.enc.Utf8.stringify(
  162. CryptoJS.TripleDES.decrypt(encStr, CryptoJS.enc.Hex.parse(toHex(key)), {
  163. mode: CryptoJS.mode.ECB,
  164. padding: CryptoJS.pad.Pkcs7,
  165. })
  166. );
  167. }