app.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import type { ProjectConfig } from '/@/types/config';
  2. import { VuexModule, getModule, Module, Mutation, Action } from 'vuex-module-decorators';
  3. import store from '/@/store';
  4. import { PROJ_CFG_KEY, LOCK_INFO_KEY } from '/@/enums/cacheEnum';
  5. import { hotModuleUnregisterModule } from '/@/utils/helper/vuexHelper';
  6. import {
  7. setLocal,
  8. getLocal,
  9. removeLocal,
  10. clearSession,
  11. clearLocal,
  12. } from '/@/utils/helper/persistent';
  13. import { deepMerge } from '/@/utils';
  14. import { resetRouter } from '/@/router';
  15. import { permissionStore } from './permission';
  16. import { tabStore } from './tab';
  17. import { userStore } from './user';
  18. export interface LockInfo {
  19. pwd: string | undefined;
  20. isLock: boolean;
  21. }
  22. let timeId: TimeoutHandle;
  23. const NAME = 'app';
  24. hotModuleUnregisterModule(NAME);
  25. @Module({ dynamic: true, namespaced: true, store, name: NAME })
  26. class App extends VuexModule {
  27. // Page loading status
  28. private pageLoadingState = false;
  29. // project config
  30. private projectConfigState: ProjectConfig | null = getLocal(PROJ_CFG_KEY);
  31. // lock info
  32. private lockInfoState: LockInfo | null = getLocal(LOCK_INFO_KEY);
  33. // set main overflow hidden
  34. private lockMainScrollState = false;
  35. get getPageLoading() {
  36. return this.pageLoadingState;
  37. }
  38. get getLockMainScrollState() {
  39. return this.lockMainScrollState;
  40. }
  41. get getLockInfo(): LockInfo {
  42. return this.lockInfoState || ({} as LockInfo);
  43. }
  44. get getProjectConfig(): ProjectConfig {
  45. return this.projectConfigState || ({} as ProjectConfig);
  46. }
  47. @Mutation
  48. commitPageLoadingState(loading: boolean): void {
  49. this.pageLoadingState = loading;
  50. }
  51. @Mutation
  52. commitLockMainScrollState(lock: boolean): void {
  53. this.lockMainScrollState = lock;
  54. }
  55. @Mutation
  56. commitProjectConfigState(proCfg: DeepPartial<ProjectConfig>): void {
  57. this.projectConfigState = deepMerge(this.projectConfigState || {}, proCfg);
  58. setLocal(PROJ_CFG_KEY, this.projectConfigState);
  59. }
  60. @Mutation
  61. commitLockInfoState(info: LockInfo): void {
  62. this.lockInfoState = Object.assign({}, this.lockInfoState, info);
  63. setLocal(LOCK_INFO_KEY, this.lockInfoState);
  64. }
  65. @Mutation
  66. resetLockInfo(): void {
  67. removeLocal(LOCK_INFO_KEY);
  68. this.lockInfoState = null;
  69. }
  70. @Action
  71. async resumeAllState() {
  72. resetRouter();
  73. clearSession();
  74. clearLocal();
  75. permissionStore.commitResetState();
  76. tabStore.commitResetState();
  77. userStore.commitResetState();
  78. }
  79. @Action
  80. public async setPageLoadingAction(loading: boolean): Promise<void> {
  81. if (loading) {
  82. clearTimeout(timeId);
  83. // Prevent flicker
  84. timeId = setTimeout(() => {
  85. this.commitPageLoadingState(loading);
  86. }, 50);
  87. } else {
  88. this.commitPageLoadingState(loading);
  89. clearTimeout(timeId);
  90. }
  91. }
  92. /**
  93. * @description: unlock page
  94. */
  95. @Action
  96. public async unLockAction({ password, valid = true }: { password: string; valid?: boolean }) {
  97. if (!valid) {
  98. this.resetLockInfo();
  99. return true;
  100. }
  101. const tryLogin = async () => {
  102. try {
  103. const username = userStore.getUserInfoState.username;
  104. const res = await userStore.login({ username, password }, false);
  105. if (res) {
  106. this.resetLockInfo();
  107. }
  108. return res;
  109. } catch (error) {
  110. return false;
  111. }
  112. };
  113. if (this.getLockInfo) {
  114. if (this.getLockInfo.pwd === password) {
  115. this.resetLockInfo();
  116. return true;
  117. }
  118. const res = await tryLogin();
  119. return res;
  120. }
  121. const res = await tryLogin();
  122. return res;
  123. }
  124. }
  125. export const appStore = getModule<App>(App);