Created
December 19, 2024 10:08
-
-
Save black-black-cat/42f730beebe3bb5f70a882d6c984c1f8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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