1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- 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/https";
- 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");
|