app.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import defaultSettings from "@/settings";
  2. // 导入 Element Plus 中英文语言包
  3. import zhCn from "element-plus/es/locale/lang/zh-cn";
  4. import en from "element-plus/es/locale/lang/en";
  5. import { store } from "@/store";
  6. import { DeviceEnum } from "@/enums/DeviceEnum";
  7. import { SidebarStatusEnum } from "@/enums/SidebarStatusEnum";
  8. // setup
  9. export const useAppStore = defineStore("app", () => {
  10. // state
  11. const device = useStorage("device", DeviceEnum.DESKTOP);
  12. const size = useStorage("size", defaultSettings.size);
  13. const language = useStorage("language", defaultSettings.language);
  14. const sidebarStatus = useStorage("sidebarStatus", SidebarStatusEnum.CLOSED);
  15. const sidebar = reactive({
  16. opened: sidebarStatus.value === SidebarStatusEnum.OPENED,
  17. withoutAnimation: false,
  18. });
  19. const activeTopMenuPath = useStorage("activeTopMenuPath", "");
  20. /**
  21. * 根据语言标识读取对应的语言包
  22. */
  23. const locale = computed(() => {
  24. if (language?.value == "en") {
  25. return en;
  26. } else {
  27. return zhCn;
  28. }
  29. });
  30. // actions
  31. function toggleSidebar() {
  32. sidebar.opened = !sidebar.opened;
  33. sidebarStatus.value = sidebar.opened
  34. ? SidebarStatusEnum.OPENED
  35. : SidebarStatusEnum.CLOSED;
  36. }
  37. function closeSideBar() {
  38. sidebar.opened = false;
  39. sidebarStatus.value = SidebarStatusEnum.CLOSED;
  40. }
  41. function openSideBar() {
  42. sidebar.opened = true;
  43. sidebarStatus.value = SidebarStatusEnum.OPENED;
  44. }
  45. function toggleDevice(val: string) {
  46. device.value = val;
  47. }
  48. function changeSize(val: string) {
  49. size.value = val;
  50. }
  51. /**
  52. * 切换语言
  53. *
  54. * @param val
  55. */
  56. function changeLanguage(val: string) {
  57. language.value = val;
  58. }
  59. /**
  60. * 混合模式顶部切换
  61. */
  62. function activeTopMenu(val: string) {
  63. activeTopMenuPath.value = val;
  64. }
  65. return {
  66. device,
  67. sidebar,
  68. language,
  69. locale,
  70. size,
  71. activeTopMenu,
  72. toggleDevice,
  73. changeSize,
  74. changeLanguage,
  75. toggleSidebar,
  76. closeSideBar,
  77. openSideBar,
  78. activeTopMenuPath,
  79. };
  80. });
  81. // 手动提供给 useStore() 函数 pinia 实例
  82. // https://pinia.vuejs.org/zh/core-concepts/outside-component-usage.html#using-a-store-outside-of-a-component
  83. export function useAppStoreHook() {
  84. return useAppStore(store);
  85. }