app.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import zhCN from 'element-plus/es/locale/lang/zh-cn';
  2. import enUS from 'element-plus/es/locale/lang/en';
  3. import { defineStore } from 'pinia';
  4. import { useStorage } from '@vueuse/core';
  5. import { ref, reactive, computed } from 'vue';
  6. export const useAppStore = defineStore('app', () => {
  7. const sidebarStatus = useStorage('sidebarStatus', '1');
  8. const sidebar = reactive({
  9. opened: sidebarStatus.value ? !!+sidebarStatus.value : true,
  10. withoutAnimation: false,
  11. hide: false
  12. });
  13. const device = ref<string>('desktop');
  14. const size = useStorage<'large' | 'default' | 'small'>('size', 'default');
  15. // 语言
  16. const language = useStorage('language', 'zh_CN');
  17. const languageObj: any = {
  18. en_US: enUS,
  19. zh_CN: zhCN
  20. };
  21. const locale = computed(() => {
  22. return languageObj[language.value];
  23. });
  24. const toggleSideBar = (withoutAnimation: boolean) => {
  25. if (sidebar.hide) {
  26. return false;
  27. }
  28. sidebar.opened = !sidebar.opened;
  29. sidebar.withoutAnimation = withoutAnimation;
  30. if (sidebar.opened) {
  31. sidebarStatus.value = '1';
  32. } else {
  33. sidebarStatus.value = '0';
  34. }
  35. };
  36. const closeSideBar = ({ withoutAnimation }: any): void => {
  37. sidebarStatus.value = '0';
  38. sidebar.opened = false;
  39. sidebar.withoutAnimation = withoutAnimation;
  40. };
  41. const toggleDevice = (d: string): void => {
  42. device.value = d;
  43. };
  44. const setSize = (s: 'large' | 'default' | 'small'): void => {
  45. size.value = s;
  46. };
  47. const toggleSideBarHide = (status: boolean): void => {
  48. sidebar.hide = status;
  49. };
  50. const changeLanguage = (val: string): void => {
  51. language.value = val;
  52. };
  53. return {
  54. device,
  55. sidebar,
  56. language,
  57. locale,
  58. size,
  59. changeLanguage,
  60. toggleSideBar,
  61. closeSideBar,
  62. toggleDevice,
  63. setSize,
  64. toggleSideBarHide
  65. };
  66. });