index.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { Http, ModuleRoot } from "queenjs";
  2. import { actions } from "./module/actions";
  3. import { User_Config } from "./module/config";
  4. import { Dict_Auth_Storage } from "./module/dicts/storage";
  5. import { authHelper } from "./module/helper";
  6. import { https } from "./module/https";
  7. import { stores } from "./module/stores";
  8. export class AuthModule extends ModuleRoot {
  9. config = this.setConfig(User_Config);
  10. store = this.createStore(stores);
  11. https = this.createHttps([https]);
  12. actions = this.createActions(actions);
  13. helper = this.createHelper([authHelper]);
  14. onReady() {
  15. if (!Http.config.interceptors?.checkAuth) {
  16. Http.defaultConfig({
  17. interceptors: {
  18. checkAuth: Http.interceptor({
  19. response: (res: any) => {
  20. this.actions.checkLoginExpire(res.data);
  21. return res;
  22. },
  23. }),
  24. },
  25. });
  26. }
  27. }
  28. }
  29. const setToken = Http.interceptor({
  30. request(req) {
  31. if (!req.headers) req.headers = {};
  32. req.headers["authorization"] = `Bearer ${Dict_Auth_Storage.get("token")}`;
  33. return req;
  34. },
  35. });
  36. Http.defaultConfig({
  37. interceptors: { setToken },
  38. });
  39. export const { useAuth, setAuth, initAuth } = AuthModule.hook("Auth");