app.js 2.26 KB
import Cookies from 'js-cookie'

import zhCn from 'element-plus/es/locale/lang/zh-cn';
import en from 'element-plus/es/locale/lang/en';
import ru from 'element-plus/es/locale/lang/ru';
import router from '@/router'

const useAppStore = defineStore(
  'app',
  {
    state: () => ({
      definition: [
        { label: '中文', value: 'zh_CN', correspondence: zhCn},
        { label: '英文', value: 'en_US', correspondence: en},
        { label: '俄文', value: 'ru_RU', correspondence: ru},
      ],
      sidebar: {
        opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
        withoutAnimation: false,
        hide: false,
        status: true
      },
      device: 'desktop',
      size: Cookies.get('size') || 'default',
      language: 'zh_CN',
      locale: zhCn,
    }),
    actions: {
      toggleSideBar(withoutAnimation, opened = true) {
        if (this.sidebar.hide) {
          return false;
        }
        // this.sidebar.opened = !this.sidebar.opened
        this.sidebar.opened = opened
        this.sidebar.withoutAnimation = withoutAnimation
        if (this.sidebar.opened) {
          Cookies.set('sidebarStatus', 1)
        } else {
          Cookies.set('sidebarStatus', 0)
        }
      },
      closeSideBar({ withoutAnimation }) {
        Cookies.set('sidebarStatus', 0)
        this.sidebar.opened = false
        this.sidebar.withoutAnimation = withoutAnimation
      },
      toggleDevice(device) {
        this.device = device
      },
      setSize(size) {
        this.size = size;
        Cookies.set('size', size)
      },
      toggleSideBarHide(status) {
        this.sidebar.hide = status
      },
      // 设置侧边栏是否需要显示
      toggleSidebarStatus(status){
        this.sidebar.status = status
      },
      // 国际化改变
      changeLanguage(state) {
        this.language = state
        // location.reload()
        const { path, query } = router.currentRoute.value;
        router.replace({
          path: '/redirect' + path,
          query: query
        })
        console.log(router);
        // this.locale = this.changeLocal()
      },
      changeLocal() {
        return this.definition.find(i => i.value == this.language)?.correspondence || zhCn
      }
    }
  })

export default useAppStore