| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- import mitt from "mitt";
- import router from "@/router";
- import ArcoVue from "@arco-design/web-vue";
- import * as clipboard from "clipboard-polyfill";
- import ajax from "@/ajax";
- const $bus = mitt();
- const getToken = () => {
- if (window.localStorage.tokenMap) {
- const nowTime = new Date().getTime();
- const result = JSON.parse(window.localStorage.tokenMap);
- const timeout = result.expiresTime - nowTime;
- setTimeout(async () => {
- const refreshData = await ajax.user.refreshToken(result.refreshToken);
- if (refreshData) {
- window.localStorage.tokenMap = JSON.stringify(refreshData);
- getToken();
- } else {
- helper.goLink("login");
- helper.message("error", `请重新登录`);
- }
- }, timeout);
- }
- };
- const reset = () => {
- window.localStorage.xintongxiaoshu = "";
- };
- const write = (key, value) => {
- if (!window.localStorage.xintongxiaoshu) {
- window.localStorage.xintongxiaoshu = "{}";
- }
- const xintongxiaoshu = JSON.parse(window.localStorage.xintongxiaoshu);
- const data = typeof value === "string" ? value : JSON.stringify(value);
- xintongxiaoshu[key] = data;
- window.localStorage.xintongxiaoshu = JSON.stringify(xintongxiaoshu);
- };
- const read = (key) => {
- let result;
- let data;
- try {
- data = JSON.parse(window.localStorage.xintongxiaoshu)[key];
- result = JSON.parse(data);
- } catch (e) {
- result = data || null;
- }
- return result;
- };
- const helper = (router, ArcoVue) => {
- return {
- webSocket: null,
- read,
- write,
- reset,
- dialogConfig: {
- isQuestion: false,
- selectedKeys: [],
- type: "answer"
- },
- $bus,
- getToken,
- goLink(path, query = {}) {
- router.push({ path, query });
- },
- copyText(text, content) {
- clipboard.writeText(text).then(() => {
- ArcoVue.Message["success"]({ content, duration: 1500 });
- });
- },
- shuffleArray(array) {
- array.sort(() => Math.random() - 0.5);
- return array;
- },
- goBack() {
- router.go(-1);
- },
- message(type, content) {
- ArcoVue.Message[type]({ content, duration: 1500 });
- },
- setLoading(isLoading = false) {
- $bus.emit("set-loading", isLoading);
- },
- getAssetURL(url) {
- return new URL(url, import.meta.url).href;
- },
- setDialog(isInit = false, contentNode, insertCallback = () => {}, closeCallback = () => {}) {
- if (isInit) {
- const body = document.getRootNode().body;
- const div = document.createElement("div");
- div.id = "all-screen-dialog";
- div.style = "width:100vw;height:100vh;z-index:9999;position:absolute;top:0";
- div.append(contentNode);
- body.append(div);
- insertCallback();
- div.addEventListener(
- "click",
- (e) => {
- if (!contentNode.contains(e.target)) {
- div.remove();
- closeCallback();
- }
- },
- true
- );
- } else {
- const div = document.getElementById("all-screen-dialog");
- div.remove();
- }
- }
- };
- };
- export default helper(router, ArcoVue);
|