File tree Expand file tree Collapse file tree 3 files changed +28
-9
lines changed Expand file tree Collapse file tree 3 files changed +28
-9
lines changed Original file line number Diff line number Diff line change @@ -23,12 +23,12 @@ func JWTAuth() gin.HandlerFunc {
23
23
// 我们这里jwt鉴权取头部信息 x-token 登录时回返回token信息 这里前端需要把token存储到cookie或者本地localStorage中 不过需要跟后端协商过期时间 可以约定刷新令牌或者重新登录
24
24
token := utils .GetToken (c )
25
25
if token == "" {
26
- response .FailWithDetailed (gin. H { "reload" : true }, "未登录或非法访问" , c )
26
+ response .NoAuth ( "未登录或非法访问" , c )
27
27
c .Abort ()
28
28
return
29
29
}
30
30
if jwtService .IsBlacklist (token ) {
31
- response .FailWithDetailed (gin. H { "reload" : true }, "您的帐户异地登陆或令牌失效" , c )
31
+ response .NoAuth ( "您的帐户异地登陆或令牌失效" , c )
32
32
utils .ClearToken (c )
33
33
c .Abort ()
34
34
return
@@ -38,12 +38,12 @@ func JWTAuth() gin.HandlerFunc {
38
38
claims , err := j .ParseToken (token )
39
39
if err != nil {
40
40
if errors .Is (err , utils .TokenExpired ) {
41
- response .FailWithDetailed (gin. H { "reload" : true }, "授权已过期" , c )
41
+ response .NoAuth ( "授权已过期" , c )
42
42
utils .ClearToken (c )
43
43
c .Abort ()
44
44
return
45
45
}
46
- response .FailWithDetailed (gin. H { "reload" : true }, err .Error (), c )
46
+ response .NoAuth ( err .Error (), c )
47
47
utils .ClearToken (c )
48
48
c .Abort ()
49
49
return
Original file line number Diff line number Diff line change @@ -50,6 +50,14 @@ func FailWithMessage(message string, c *gin.Context) {
50
50
Result (ERROR , map [string ]interface {}{}, message , c )
51
51
}
52
52
53
+ func NoAuth (message string , c * gin.Context ) {
54
+ c .JSON (http .StatusUnauthorized , Response {
55
+ 7 ,
56
+ nil ,
57
+ message ,
58
+ })
59
+ }
60
+
53
61
func FailWithDetailed (data interface {}, message string , c * gin.Context ) {
54
62
Result (ERROR , data , message , c )
55
63
}
Original file line number Diff line number Diff line change @@ -83,11 +83,6 @@ service.interceptors.response.use(
83
83
message : response . data . msg || decodeURI ( response . headers . msg ) ,
84
84
type : 'error'
85
85
} )
86
- if ( response . data . data && response . data . data . reload ) {
87
- userStore . token = ''
88
- window . localStorage . removeItem ( 'token' )
89
- router . push ( { name : 'Login' , replace : true } )
90
- }
91
86
return response . data . msg ? response . data : response
92
87
}
93
88
} ,
@@ -137,6 +132,22 @@ service.interceptors.response.use(
137
132
cancelButtonText : '取消'
138
133
} )
139
134
break
135
+ case 401 :
136
+ ElMessageBox . confirm ( `
137
+ <p>无效的令牌</p>
138
+ <p>错误码:<span style="color:red"> 401 </span>错误信息:${ error } </p>
139
+ ` , '身份信息' , {
140
+ dangerouslyUseHTMLString : true ,
141
+ distinguishCancelAndClose : true ,
142
+ confirmButtonText : '重新登录' ,
143
+ cancelButtonText : '取消'
144
+ } )
145
+ . then ( ( ) => {
146
+ const userStore = useUserStore ( )
147
+ userStore . ClearStorage ( )
148
+ router . push ( { name : 'Login' , replace : true } )
149
+ } )
150
+ break
140
151
}
141
152
142
153
return error
You can’t perform that action at this time.
0 commit comments