Skip to content

Instantly share code, notes, and snippets.

@black-black-cat
Created December 19, 2024 10:08
Show Gist options
  • Save black-black-cat/42f730beebe3bb5f70a882d6c984c1f8 to your computer and use it in GitHub Desktop.
Save black-black-cat/42f730beebe3bb5f70a882d6c984c1f8 to your computer and use it in GitHub Desktop.
import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios';
import qs from 'qs';
Vue.use(Vuex);
export default new Vuex.Store({
state: {
user: null,
// 用于标记是否正在刷新 token
refreshingToken: false,
// 存储因 token 刷新而等待的请求队列
tokenRefreshQueue: []
},
mutations: {
setUser(state, payload) {
state.user = payload;
},
setRefreshingToken(state, value) {
state.refreshingToken = value;
},
addToTokenRefreshQueue(state, callback) {
state.tokenRefreshQueue.push(callback);
},
clearTokenRefreshQueue(state) {
state.tokenRefreshQueue = [];
}
},
actions: {
// 登录获取 token 及用户信息
async login({
commit
}, data) {
try {
const response = await axios({
url: '/front/user/login',
method: 'POST',
data: qs.stringify(data)
});
const {
access_token,
expires_in,
refresh_token
} = response.data;
const user = {
access_token,
expires_in,
refresh_token
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment