79 lines
1.8 KiB
TypeScript
Raw Normal View History

2023-04-02 01:01:56 +08:00
import Cookies from 'js-cookie';
import zhCN from 'element-plus/es/locale/lang/zh-cn';
import enUS from 'element-plus/es/locale/lang/en';
2023-04-02 01:01:56 +08:00
export const useAppStore = defineStore('app', () => {
2023-04-03 00:05:09 +08:00
const sidebarStatus = Cookies.get('sidebarStatus');
const sidebar = reactive({
opened: sidebarStatus ? !!+sidebarStatus : true,
withoutAnimation: false,
hide: false
});
const device = ref<string>('desktop');
const size = ref(Cookies.get('size') || 'default');
2023-04-03 00:05:09 +08:00
// 语言
const language = ref(Cookies.get('language'));
const languageObj: any = {
en_US: enUS,
zh_CN: zhCN
};
2023-04-03 00:05:09 +08:00
const locale = computed(() => {
if (!language.value) {
return zhCN;
2023-04-03 00:05:09 +08:00
}
return languageObj[language.value];
2023-04-03 00:05:09 +08:00
});
2023-04-02 01:01:56 +08:00
2023-04-03 00:05:09 +08:00
const toggleSideBar = (withoutAnimation?: boolean) => {
if (sidebar.hide) {
return false;
}
2023-04-02 01:01:56 +08:00
2023-04-03 00:05:09 +08:00
sidebar.opened = !sidebar.opened;
sidebar.withoutAnimation = withoutAnimation as boolean;
if (sidebar.opened) {
Cookies.set('sidebarStatus', '1');
} else {
Cookies.set('sidebarStatus', '0');
}
};
2023-04-02 01:01:56 +08:00
2023-04-03 00:05:09 +08:00
const closeSideBar = ({ withoutAnimation }: any): void => {
Cookies.set('sidebarStatus', '0');
sidebar.opened = false;
sidebar.withoutAnimation = withoutAnimation;
};
const toggleDevice = (d: string): void => {
device.value = d;
};
const setSize = (s: string): void => {
size.value = s;
Cookies.set('size', s);
};
const toggleSideBarHide = (status: boolean): void => {
sidebar.hide = status;
};
2023-04-02 01:01:56 +08:00
2023-04-03 00:05:09 +08:00
const changeLanguage = (val: string): void => {
language.value = val;
2023-04-18 23:36:26 +08:00
Cookies.set('language', val);
2023-04-03 00:05:09 +08:00
};
2023-04-02 01:01:56 +08:00
2023-04-03 00:05:09 +08:00
return {
device,
sidebar,
language,
locale,
size,
changeLanguage,
toggleSideBar,
closeSideBar,
toggleDevice,
setSize,
toggleSideBarHide
};
2023-04-02 01:01:56 +08:00
});
export default useAppStore;