app.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { updatePreferences } from '@vben/preferences';
  2. import { acceptHMRUpdate, defineStore } from 'pinia';
  3. import { ConfigApi } from '#/api';
  4. interface AccessState {
  5. /**
  6. * 配置信息
  7. */
  8. appInfo: ConfigApi.AppInfo | null;
  9. exitUrl: string;
  10. }
  11. /**
  12. * @zh_CN 配置信息相关
  13. */
  14. export const useAppStore = defineStore('web-info', {
  15. actions: {
  16. setAppInfo(appInfo: ConfigApi.AppInfo | null) {
  17. this.appInfo = appInfo;
  18. updatePreferences({
  19. app: {
  20. watermark: appInfo?.openWatermark,
  21. name: appInfo?.webTitle,
  22. layout: 'header-nav',
  23. },
  24. tabbar: {
  25. enable: false,
  26. },
  27. theme: {
  28. builtinType: 'custom',
  29. colorPrimary: 'hsl(300 64% 31%)',
  30. mode: 'auto',
  31. },
  32. });
  33. },
  34. async loadAppInfo() {
  35. const appInfo = await ConfigApi.getWebInfo();
  36. this.setAppInfo(appInfo);
  37. return appInfo;
  38. },
  39. setExitUrl(url: string) {
  40. this.exitUrl = url;
  41. },
  42. getExitUrl() {
  43. return this.exitUrl;
  44. },
  45. },
  46. state: (): AccessState => ({
  47. appInfo: null,
  48. exitUrl: '',
  49. }),
  50. persist: {
  51. // 持久化
  52. pick: ['appInfo.webWatermark'],
  53. },
  54. });
  55. // 解决热更新问题
  56. const hot = import.meta.hot;
  57. if (hot) {
  58. hot.accept(acceptHMRUpdate(useAppStore, hot));
  59. }