🎨 oauth 与 userInfo 分离

This commit is contained in:
ronger 2020-08-06 15:44:29 +08:00
parent d1bc9eda54
commit 96b556a3ab
4 changed files with 58 additions and 9 deletions

View File

@ -72,6 +72,8 @@
</template> </template>
<script> <script>
import { isBrowser } from '~/environment'
const Cookie = process.client ? require('js-cookie') : undefined const Cookie = process.client ? require('js-cookie') : undefined
export default { export default {
name: "MobileHeader", name: "MobileHeader",
@ -83,10 +85,28 @@
return this.$store.state.oauth; return this.$store.state.oauth;
}, },
avatarURL() { avatarURL() {
return this.$store.state.oauth?.avatarURL; let _ts = this;
if (isBrowser) {
if (!_ts.$store.state.userInfo) {
let user = localStorage.getItem('user');
if (user) {
_ts.$store.commit('setUser', JSON.parse(user))
}
}
}
return _ts.$store.state.userInfo?.avatarURL;
}, },
nickname() { nickname() {
return this.$store.state.oauth?.nickname; let _ts = this;
if (isBrowser) {
if (!_ts.$store.state.userInfo) {
let user = localStorage.getItem('user');
if (user) {
_ts.$store.commit('setUser', JSON.parse(user))
}
}
}
return _ts.$store.state.userInfo?.nickname;
}, },
hasPermissions() { hasPermissions() {
return this.$store.getters.hasPermissions('blog_admin'); return this.$store.getters.hasPermissions('blog_admin');

View File

@ -87,6 +87,8 @@
</template> </template>
<script> <script>
import { isBrowser } from '~/environment'
const Cookie = process.client ? require('js-cookie') : undefined const Cookie = process.client ? require('js-cookie') : undefined
export default { export default {
name: "PcHeader", name: "PcHeader",
@ -98,10 +100,28 @@
return this.$store.state.oauth; return this.$store.state.oauth;
}, },
avatarURL() { avatarURL() {
return this.$store.state.oauth?.avatarURL; let _ts = this;
if (isBrowser) {
if (!_ts.$store.state.userInfo) {
let user = localStorage.getItem('user');
if (user) {
_ts.$store.commit('setUser', JSON.parse(user))
}
}
}
return _ts.$store.state.userInfo?.avatarURL;
}, },
nickname() { nickname() {
return this.$store.state.oauth?.nickname; let _ts = this;
if (isBrowser) {
if (!_ts.$store.state.userInfo) {
let user = localStorage.getItem('user');
if (user) {
_ts.$store.commit('setUser', JSON.parse(user))
}
}
}
return _ts.$store.state.userInfo?.nickname;
}, },
hasPermissions() { hasPermissions() {
return this.$store.getters.hasPermissions('blog_admin'); return this.$store.getters.hasPermissions('blog_admin');

View File

@ -94,12 +94,17 @@
} }
let auth = { let auth = {
accessToken: res.user.token, accessToken: res.user.token,
nickname: res.user.nickname,
avatarURL: res.user.avatarUrl,
idUser: res.user.idUser, idUser: res.user.idUser,
role: res.user.weights role: res.user.weights
} }
let user = {
nickname: res.user.nickname,
avatarURL: res.user.avatarUrl
}
_ts.$store.commit('setAuth', auth) // mutating to store for client rendering _ts.$store.commit('setAuth', auth) // mutating to store for client rendering
localStorage.setItem('user', JSON.stringify(user))
_ts.$store.commit('setUser', user) // mutating to store for client rendering
Cookie.set('auth', auth) Cookie.set('auth', auth)
_ts.$router.push({ _ts.$router.push({
name: 'index' name: 'index'
@ -134,7 +139,7 @@
let data = { let data = {
email: email email: email
}; };
_ts.axios.post('/console/get-forget-password-email', data).then(function (res) { _ts.$axios.$post('/api/console/get-forget-password-email', data).then(function (res) {
_ts.loading = false; _ts.loading = false;
_ts.forget = false; _ts.forget = false;
if (res) { if (res) {

View File

@ -6,6 +6,7 @@ export const state = () => {
return { return {
activeMenu: 'index', activeMenu: 'index',
oauth: null, oauth: null,
userInfo: null,
locale: 'zh_CN', locale: 'zh_CN',
uploadHeaders: '' uploadHeaders: ''
} }
@ -15,6 +16,9 @@ export const mutations = {
setAuth (state, auth) { setAuth (state, auth) {
state.oauth = auth state.oauth = auth
}, },
setUser (state, data) {
state.userInfo = data
},
setActiveMenu (state, activeMenu) { setActiveMenu (state, activeMenu) {
state.activeMenu = activeMenu state.activeMenu = activeMenu
}, },
@ -22,8 +26,8 @@ export const mutations = {
state.uploadHeaders = data state.uploadHeaders = data
}, },
setUserInfo(state, data) { setUserInfo(state, data) {
state.oauth.avatarURL = data.avatarUrl; state.userInfo.avatarURL = data.avatarUrl;
state.oauth.nickname = data.nickname; state.userInfo.nickname = data.nickname;
} }
} }