import { Http, ModuleRoot } from "queenjs"; import { actions } from "./module/actions"; import { User_Config } from "./module/config"; import { Dict_Auth_Storage } from "./module/dicts/storage"; import { authHelper } from "./module/helper"; import { https } from "./module/http"; import { stores } from "./module/stores"; export class AuthModule extends ModuleRoot { config = this.setConfig(User_Config); store = this.createStore(stores); https = this.createHttps([https]); actions = this.createActions(actions); helper = this.createHelper([authHelper]); onReady() { if (!Http.config.interceptors?.checkAuth) { Http.defaultConfig({ interceptors: { checkAuth: Http.interceptor({ response: (res: any) => { this.actions.checkLoginExpire(res.data); return res; }, }), }, }); } } } const setToken = Http.interceptor({ request(req) { if (!req.headers) req.headers = {}; req.headers["authorization"] = `Bearer ${Dict_Auth_Storage.get("token")}`; return req; }, }); Http.defaultConfig({ interceptors: { setToken }, }); export const { useAuth, setAuth, initAuth } = AuthModule.hook("Auth");