修改token保存方式
This commit is contained in:
parent
d21f63d8f3
commit
28110bd884
@ -15,7 +15,7 @@ import { ElMessage, ElLoading } from 'element-plus'
|
|||||||
// 添加请求拦截器
|
// 添加请求拦截器
|
||||||
service.interceptors.request.use(config => {
|
service.interceptors.request.use(config => {
|
||||||
// 在发送请求之前添加token到请求头
|
// 在发送请求之前添加token到请求头
|
||||||
const token = localStorage.getItem('login_token')
|
const token = app.$store.state.loginInfo.token
|
||||||
if (token !== null && config.headers) {
|
if (token !== null && config.headers) {
|
||||||
config.headers.token = token
|
config.headers.token = token
|
||||||
}
|
}
|
||||||
@ -48,7 +48,7 @@ service.interceptors.response.use(res=> {
|
|||||||
// 全局路由导航前置守卫
|
// 全局路由导航前置守卫
|
||||||
router.beforeEach(function (to, from, next) {
|
router.beforeEach(function (to, from, next) {
|
||||||
app.$store.commit('setBreadcrumb', routePathes[to.path] || [])
|
app.$store.commit('setBreadcrumb', routePathes[to.path] || [])
|
||||||
if(filterExclude.indexOf(to.path) !== -1 || localStorage.getItem('login_token')) {
|
if(filterExclude.indexOf(to.path) !== -1 || app.$store.state.loginInfo.token) {
|
||||||
next()
|
next()
|
||||||
} else {
|
} else {
|
||||||
next('/login')
|
next('/login')
|
||||||
|
|||||||
@ -33,7 +33,7 @@ export default abstract class BaseList<T extends Page> extends Vue {
|
|||||||
/**
|
/**
|
||||||
* 重置搜索项
|
* 重置搜索项
|
||||||
*/
|
*/
|
||||||
reset() {
|
reset(): void {
|
||||||
this.search.reset()
|
this.search.reset()
|
||||||
this.loadData()
|
this.loadData()
|
||||||
}
|
}
|
||||||
@ -41,7 +41,7 @@ export default abstract class BaseList<T extends Page> extends Vue {
|
|||||||
* 页码变更
|
* 页码变更
|
||||||
* @param pageNum 页码
|
* @param pageNum 页码
|
||||||
*/
|
*/
|
||||||
pageChange(pageNum: number) {
|
pageChange(pageNum: number): void {
|
||||||
this.search.pageNum = pageNum
|
this.search.pageNum = pageNum
|
||||||
this.loadData()
|
this.loadData()
|
||||||
}
|
}
|
||||||
@ -50,7 +50,7 @@ export default abstract class BaseList<T extends Page> extends Vue {
|
|||||||
* 每页数据条数变更
|
* 每页数据条数变更
|
||||||
* @param pageSize 每页数据条数
|
* @param pageSize 每页数据条数
|
||||||
*/
|
*/
|
||||||
pageSizeChange(pageSize: number) {
|
pageSizeChange(pageSize: number): void {
|
||||||
this.search.limit = pageSize
|
this.search.limit = pageSize
|
||||||
this.loadData()
|
this.loadData()
|
||||||
}
|
}
|
||||||
@ -59,7 +59,7 @@ export default abstract class BaseList<T extends Page> extends Vue {
|
|||||||
* 日期时间格式化
|
* 日期时间格式化
|
||||||
* @param dateStr 日期时间
|
* @param dateStr 日期时间
|
||||||
*/
|
*/
|
||||||
datetimeFormat(dateStr: string) {
|
datetimeFormat(dateStr: string): string | null {
|
||||||
return moment(dateStr).format('YYYY-MM-DD HH:mm:ss')
|
return dateStr ? moment(dateStr).format('YYYY-MM-DD HH:mm:ss') : null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -8,7 +8,7 @@ class Store {
|
|||||||
state: StateType = {
|
state: StateType = {
|
||||||
loginInfo: {
|
loginInfo: {
|
||||||
userInfo: null,
|
userInfo: null,
|
||||||
token: null
|
token: localStorage.getItem('login_token')
|
||||||
},
|
},
|
||||||
breadcrumb: [],
|
breadcrumb: [],
|
||||||
pageSizeOpts: [10, 20, 50, 100],
|
pageSizeOpts: [10, 20, 50, 100],
|
||||||
|
|||||||
@ -75,14 +75,14 @@ export default class Home extends Vue{
|
|||||||
this.openMenuNames.push(result[1])
|
this.openMenuNames.push(result[1])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(!localStorage.getItem('login_token')) {
|
if(!this.$store.state.loginInfo.token) {
|
||||||
this.$router.push('/login')
|
this.$router.push('/login')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
const data = await this.$http.post<{token: null | string}, any>('/api/common/verifyToken', {token: localStorage.getItem('login_token')})
|
const data = await this.$http.post<{token: string}, any>('/api/common/verifyToken', {token: this.$store.state.loginInfo.token})
|
||||||
if(data.status) {
|
if(data.status) {
|
||||||
// 如果是已过期的token 服务端会签发新的token
|
// 如果是已过期的token 服务端会签发新的token
|
||||||
this.$store.commit('login', {token: data.newToken || localStorage.getItem('login_token'), userInfo: data.userInfo})
|
this.$store.commit('login', {token: data.newToken || this.$store.state.loginInfo.token, userInfo: data.userInfo})
|
||||||
} else {
|
} else {
|
||||||
this.$router.push('/login')
|
this.$router.push('/login')
|
||||||
}
|
}
|
||||||
|
|||||||
@ -143,7 +143,7 @@ export default class SystemConfig extends Vue {
|
|||||||
* @param dateStr 日期时间
|
* @param dateStr 日期时间
|
||||||
*/
|
*/
|
||||||
datetimeFormat(dateStr: string) {
|
datetimeFormat(dateStr: string) {
|
||||||
return moment(dateStr).format('YYYY-MM-DD HH:mm:ss')
|
return dateStr ? moment(dateStr).format('YYYY-MM-DD HH:mm:ss') : null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
Loading…
x
Reference in New Issue
Block a user