You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.5 KiB
64 lines
1.5 KiB
/*
|
|
* @Author: ch
|
|
* @Date: 2022-05-04 20:35:20
|
|
* @LastEditors: ch
|
|
* @LastEditTime: 2022-05-07 22:33:28
|
|
* @Description: file content
|
|
*/
|
|
import { TOKEN_KEY } from "@/constants";
|
|
import { ApiGetCurrentUser, ApiPostLogout } from "@/plugins/api/account";
|
|
const ONE_DAY = 86400000; // 一天的毫秒数 24 * 60 * 60 * 1000;
|
|
|
|
const state = () => ({
|
|
token: "",
|
|
userInfo: {},
|
|
loginVisible: false, // 是否展示登录弹窗
|
|
seckillTabVisible: false, // 公共头是否展示秒杀tab
|
|
});
|
|
const mutations = {
|
|
setUserInfo(state, info) {
|
|
state.userInfo = info;
|
|
},
|
|
setToken(state, token) {
|
|
state.token = token;
|
|
this.$cookies.set(TOKEN_KEY, token, {
|
|
path: "/",
|
|
maxAge: ONE_DAY,
|
|
});
|
|
},
|
|
setLoginOut(state) {
|
|
state.token = "";
|
|
state.userInfo = {};
|
|
this.$cookies.remove(TOKEN_KEY);
|
|
},
|
|
setLoginVisible(state, visible) {
|
|
state.loginVisible = visible;
|
|
},
|
|
setSeckillTabVisible(state, visible) {
|
|
state.seckillTabVisible = visible;
|
|
},
|
|
};
|
|
const actions = {
|
|
async nuxtServerInit({ state, commit, dispatch }) {
|
|
const token = this.$cookies.get(TOKEN_KEY);
|
|
if (!state.token && token) {
|
|
commit("setToken", token);
|
|
await dispatch("getUserInfo");
|
|
}
|
|
},
|
|
async getUserInfo({ commit }) {
|
|
const { result } = await ApiGetCurrentUser();
|
|
if (result) {
|
|
commit("setUserInfo", result);
|
|
}
|
|
},
|
|
|
|
// 退出登录
|
|
async logout({ commit }) {
|
|
await ApiPostLogout();
|
|
commit("setLoginOut");
|
|
},
|
|
};
|
|
|
|
export { state, mutations, actions };
|