helper.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import mitt from "mitt";
  2. import router from "@/router";
  3. import ArcoVue from "@arco-design/web-vue";
  4. import * as clipboard from "clipboard-polyfill";
  5. import ajax from "@/ajax";
  6. const $bus = mitt();
  7. const getToken = () => {
  8. if (window.localStorage.tokenMap) {
  9. const nowTime = new Date().getTime();
  10. const result = JSON.parse(window.localStorage.tokenMap);
  11. const timeout = result.expiresTime - nowTime;
  12. setTimeout(async () => {
  13. const refreshData = await ajax.user.refreshToken(result.refreshToken);
  14. if (refreshData) {
  15. window.localStorage.tokenMap = JSON.stringify(refreshData);
  16. getToken();
  17. } else {
  18. helper.goLink("login");
  19. helper.message("error", `请重新登录`);
  20. }
  21. }, timeout);
  22. }
  23. };
  24. const reset = () => {
  25. window.localStorage.xintongxiaoshu = "";
  26. };
  27. const write = (key, value) => {
  28. if (!window.localStorage.xintongxiaoshu) {
  29. window.localStorage.xintongxiaoshu = "{}";
  30. }
  31. const xintongxiaoshu = JSON.parse(window.localStorage.xintongxiaoshu);
  32. const data = typeof value === "string" ? value : JSON.stringify(value);
  33. xintongxiaoshu[key] = data;
  34. window.localStorage.xintongxiaoshu = JSON.stringify(xintongxiaoshu);
  35. };
  36. const read = (key) => {
  37. let result;
  38. let data;
  39. try {
  40. data = JSON.parse(window.localStorage.xintongxiaoshu)[key];
  41. result = JSON.parse(data);
  42. } catch (e) {
  43. result = data || null;
  44. }
  45. return result;
  46. };
  47. const helper = (router, ArcoVue) => {
  48. return {
  49. webSocket: null,
  50. read,
  51. write,
  52. reset,
  53. dialogConfig: {
  54. isQuestion: false,
  55. selectedKeys: [],
  56. type: "answer"
  57. },
  58. $bus,
  59. getToken,
  60. goLink(path, query = {}) {
  61. router.push({ path, query });
  62. },
  63. copyText(text, content) {
  64. clipboard.writeText(text).then(() => {
  65. ArcoVue.Message["success"]({ content, duration: 1500 });
  66. });
  67. },
  68. shuffleArray(array) {
  69. array.sort(() => Math.random() - 0.5);
  70. return array;
  71. },
  72. goBack() {
  73. router.go(-1);
  74. },
  75. message(type, content) {
  76. ArcoVue.Message[type]({ content, duration: 1500 });
  77. },
  78. setLoading(isLoading = false) {
  79. $bus.emit("set-loading", isLoading);
  80. },
  81. getAssetURL(url) {
  82. return new URL(url, import.meta.url).href;
  83. },
  84. setDialog(isInit = false, contentNode, insertCallback = () => {}, closeCallback = () => {}) {
  85. if (isInit) {
  86. const body = document.getRootNode().body;
  87. const div = document.createElement("div");
  88. div.id = "all-screen-dialog";
  89. div.style = "width:100vw;height:100vh;z-index:9999;position:absolute;top:0";
  90. div.append(contentNode);
  91. body.append(div);
  92. insertCallback();
  93. div.addEventListener(
  94. "click",
  95. (e) => {
  96. if (!contentNode.contains(e.target)) {
  97. div.remove();
  98. closeCallback();
  99. }
  100. },
  101. true
  102. );
  103. } else {
  104. const div = document.getElementById("all-screen-dialog");
  105. div.remove();
  106. }
  107. }
  108. };
  109. };
  110. export default helper(router, ArcoVue);