修改token保存方式

This commit is contained in:
灌糖包子 2021-10-06 13:11:11 +08:00
parent d21f63d8f3
commit 28110bd884
5 changed files with 12 additions and 12 deletions

View File

@ -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')

View File

@ -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
} }
} }

View File

@ -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],

View File

@ -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')
} }

View File

@ -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>