import { updateToken } from "@/utils/auth.js";
import Cookies from "js-cookie";
import router from "@/router/index.js";


export default {
    state: {
        userInfo: null,
        token: null
    },

    mutations: {
        setUserInfo(state, userInfo) {
            state.userInfo = userInfo;
            Cookies.set("_uid", userInfo.uid);
        },
        setToken(state, token) {
            state.token = token;
            // 更新token信息
            updateToken(token);
        }
    },

    actions: {
        login({ commit}, params) {
            
        },

        logout({ commit }, autoClose) {
            commit("setUserInfo", null);
            commit("setToken", null);
            
            if (autoClose) {
                // 关闭当前窗口
                window.close();
            } else {
                 // 跳转回登录页面
                router.push({ path: "/login" });
            }
        }
    }
}