Skip to content

Commit

Permalink
fix: 重新登录后跳转回原页面
Browse files Browse the repository at this point in the history
  • Loading branch information
IITII committed Mar 16, 2023
1 parent 5c93543 commit 436b6d2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
8 changes: 7 additions & 1 deletion src/boot/axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import axios from 'axios'
// for each client)
let client = null

export default boot(({app, store}) => {
export default boot(({app, router, store}) => {
if (client) return

// set defaults
Expand Down Expand Up @@ -39,6 +39,12 @@ export default boot(({app, store}) => {
// 在这里丢出的错误会被 axios catch,所以无需修改业务端的逻辑
return _.errors ? Promise.reject(new Error(_.errors)) : _
}, function (error) {
if (error.response.status === 401) {
router.push({
name: 'login',
query: {redirect: router.currentRoute.fullPath || router.currentRoute._value.fullPath || '/'}
})
}
return Promise.reject(error)
})

Expand Down
13 changes: 8 additions & 5 deletions src/layouts/AdminLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,17 @@ export default {
* 可能是因为递归调用导致数据绑定失败
*/
toolbar_title() {
const updateTitle = ['image', 'video']
const updateTitle = ['image', 'video'].map(_ => ({k: `/${this.user_type}/${_}`, v: _}))
updateTitle.push({k: `/${this.user_type}`, v: 'image'})
let showTitle = 'Pic Online'
const curRoute = computed(() => useRoute().path).value
for (let title of updateTitle) {
const curRoute = computed(() => useRoute().path).value
if (curRoute.endsWith(title)) {
return this[title] + '' || 'Pic Online'
if (curRoute.endsWith(title.k)) {
showTitle = this[title.v] + ''
break
}
}
return 'Pic Online'
return showTitle
},
leftDrawer: function () {
this.$log.debug('leftDrawer')
Expand Down
3 changes: 2 additions & 1 deletion src/pages/public/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ export default {
name: this.input.name,
password: this.input.password,
}
let redirect = this.$route.query?.redirect
return this.$axios.post('/limit/user/login', data)
.then(_ => _.user)
.then(_ => afterLogin(_, this.$store, this.$router, this.$axios))
.then(_ => afterLogin(_, this.$store, this.$router, this.$axios, true, redirect))
.catch(e => this.$q.notify({type: 'error', message: e.message}))
},
Expand Down
6 changes: 3 additions & 3 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ export function userTypeToRoute(userType) {
* @param router Vue Router
* @param axios Axios Instance
* @param redirect {Boolean} redirect after login, default: true
* @param redirectPath {String} redirect path, default: `/${user.user_type}/image`
*/
export function afterLogin(user, store, router, axios, redirect = true) {
export function afterLogin(user, store, router, axios, redirect = true, redirectPath = '') {
store.dispatch('user/user', user)
// reset after re-create Vue instance, such as page reload
axios.defaults.headers['authorization'] = ['Token', user.token].join(' ')
if (redirect) {
// router.push('/' + user.user_type)
router.push({path: router.query.redirect || `/${user.user_type}/image`})
router.push({path: redirectPath || `/${user.user_type}/image`})
}
}

Expand Down

0 comments on commit 436b6d2

Please sign in to comment.