import Vue from 'vue' /*引入vue框架*/
import CreatePersistedState from 'vuex-persistedstate' /*引入vuex持久化框架*/
import Vuex from 'vuex'

Vue.use(Vuex)
// vuex持久化
const vuexPersisted = new CreatePersistedState({
    key: 'VuexPersisted',
    storage: window.sessionStorage, // 可选择 localStorage、sessionStorage、cookie 看需求
    reducer: state => ({
        token: state.token, //需要持久化的数据
        routes: state.routes,
        userinfo: state.userinfo,
    })
})

const store = new Vuex.Store({
    // 模块化注册
    modules: {},
    // 全局属性变量 调用方法,this.$store.state.idFrontPath
    state: {
        token: '',
        password: '',  // 用户密码
        userinfo: {},
        idFrontPath: '',
        routes: [], // 从后端获取的路由菜单
        brokerEndpoint: '',
        videoReminder: '',
    },
    plugins: [vuexPersisted],
    // 全局同步方法, 调用方法,this.$store.commit("xxx",'赋值数据')
    mutations: {
        setToken(state, token) {
            state.token = token;
        },
        setPassword(state, pwd) {
            state.password = pwd;
        },
        setRoutes(state, perms) {
            state.routes = perms
        },
        setUserInfo(state, data) {
            state.userinfo = data;
        },
        setBrokerEndpoint(state, data) {
            state.brokerEndpoint = data;
        },
        setVideoReminder(state, data) {
            state.videoReminder = data;
        }

    },
    // 异步方法 调用方法,this.$store.dispatch("xxx")
    actions: {},
    // Vuex属性计算,在视图里面当变量使用
    getters: {},
});

export default store;