Skip to content

Commit

Permalink
fix: refresh token on site created (#76)
Browse files Browse the repository at this point in the history
close #51
  • Loading branch information
lin594 committed Jan 10, 2023
1 parent a94c038 commit 5ff9bc3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
13 changes: 3 additions & 10 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
import myHeader from './components/HeaderAndFooter/Header.vue'
import myFooter from './components/HeaderAndFooter/Footer.vue'
import MusicAffix from './components/Music/MusicAffix.vue'
import { message } from 'ant-design-vue'
import store from './store'
import { refreshToken } from '@/services/login'
export default {
components: {
Expand All @@ -48,14 +47,8 @@ export default {
const width = document.documentElement.clientWidth
document.body.style.zoom = Number(width / 12).toString() + '%'
})
if (window.localStorage.getItem('login_time')) {
if (Date.now() - new Date(window.localStorage.getItem('login_time')) > 6000) {
message.error('登录已过期!请重新登录!')
window.localStorage.removeItem('id')
}
}
if (parseInt(window.localStorage.getItem('id'))) {
await store.dispatch('userLogin', window.localStorage.getItem('id'))
if (window.localStorage.getItem('token')) {
await refreshToken()
}
},
mounted () {
Expand Down
17 changes: 17 additions & 0 deletions src/services/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import request from '@/utils/request'
import { message } from 'ant-design-vue'
import store from '@/store'

/**
* LG0103 刷新 token
* @returns {Promise<void>}
*/
export async function refreshToken () {
await request.put('/login', {}).then(res => {
localStorage.setItem('token', res.token)
store.dispatch('userLogin', res.id)
}).catch(() => {
message.error('登录已过期,请重新登录')
store.commit('userLogout')
})
}

0 comments on commit 5ff9bc3

Please sign in to comment.