Просмотр исходного кода

feat(oauth): Oauth callback page beautification (#115)

lejianwen месяцев назад: 11
Родитель
Сommit
0dd92311b2
4 измененных файлов с 181 добавлено и 11 удалено
  1. 33 11
      http/controller/api/ouath.go
  2. 2 0
      http/router/api.go
  3. 73 0
      http/templates/oauth_fail.html
  4. 73 0
      http/templates/oauth_success.html

+ 33 - 11
http/controller/api/ouath.go

@@ -145,7 +145,9 @@ func (o *Oauth) OidcAuthQuery(c *gin.Context) {
145 145
 func (o *Oauth) OauthCallback(c *gin.Context) {
146 146
 	state := c.Query("state")
147 147
 	if state == "" {
148
-		c.String(http.StatusInternalServerError, response.TranslateParamMsg(c, "ParamIsEmpty", "state"))
148
+		c.HTML(http.StatusOK, "oauth_fail.html", gin.H{
149
+			"message": response.TranslateParamMsg(c, "ParamIsEmpty", "state"),
150
+		})
149 151
 		return
150 152
 	}
151 153
 	cacheKey := state
@@ -153,7 +155,9 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
153 155
 	//从缓存中获取
154 156
 	oauthCache := oauthService.GetOauthCache(cacheKey)
155 157
 	if oauthCache == nil {
156
-		c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthExpired"))
158
+		c.HTML(http.StatusOK, "oauth_fail.html", gin.H{
159
+			"message": response.TranslateMsg(c, "OauthExpired"),
160
+		})
157 161
 		return
158 162
 	}
159 163
 	op := oauthCache.Op
@@ -164,7 +168,9 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
164 168
 	code := c.Query("code")
165 169
 	err, oauthUser := oauthService.Callback(code, verifier, op)
166 170
 	if err != nil {
167
-		c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthFailed")+response.TranslateMsg(c, err.Error()))
171
+		c.HTML(http.StatusOK, "oauth_fail.html", gin.H{
172
+			"message": response.TranslateMsg(c, "OauthFailed") + response.TranslateMsg(c, err.Error()),
173
+		})
168 174
 		return
169 175
 	}
170 176
 	userId := oauthCache.UserId
@@ -175,28 +181,38 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
175 181
 		// 检查此openid是否已经绑定过
176 182
 		utr := oauthService.UserThirdInfo(op, openid)
177 183
 		if utr.UserId > 0 {
178
-			c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthHasBindOtherUser"))
184
+			c.HTML(http.StatusOK, "oauth_fail.html", gin.H{
185
+				"message": response.TranslateMsg(c, "OauthHasBindOtherUser"),
186
+			})
179 187
 			return
180 188
 		}
181 189
 		//绑定
182 190
 		user = service.AllService.UserService.InfoById(userId)
183 191
 		if user == nil {
184
-			c.String(http.StatusInternalServerError, response.TranslateMsg(c, "ItemNotFound"))
192
+			c.HTML(http.StatusOK, "oauth_fail.html", gin.H{
193
+				"message": response.TranslateMsg(c, "ItemNotFound"),
194
+			})
185 195
 			return
186 196
 		}
187 197
 		//绑定
188 198
 		err := oauthService.BindOauthUser(userId, oauthUser, op)
189 199
 		if err != nil {
190
-			c.String(http.StatusInternalServerError, response.TranslateMsg(c, "BindFail"))
200
+			c.HTML(http.StatusOK, "oauth_fail.html", gin.H{
201
+				"message": response.TranslateMsg(c, "BindFail"),
202
+			})
191 203
 			return
192 204
 		}
193
-		c.String(http.StatusOK, response.TranslateMsg(c, "BindSuccess"))
205
+		c.HTML(http.StatusOK, "oauth_success.html", gin.H{
206
+			"message": response.TranslateMsg(c, "BindSuccess"),
207
+		})
194 208
 		return
195 209
 
196 210
 	} else if action == service.OauthActionTypeLogin {
197 211
 		//登录
198 212
 		if userId != 0 {
199
-			c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthHasBeenSuccess"))
213
+			c.HTML(http.StatusOK, "oauth_fail.html", gin.H{
214
+				"message": response.TranslateMsg(c, "OauthHasBeenSuccess"),
215
+			})
200 216
 			return
201 217
 		}
202 218
 		user = service.AllService.UserService.InfoByOauthId(op, openid)
@@ -213,7 +229,9 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
213 229
 			//自动注册
214 230
 			err, user = service.AllService.UserService.RegisterByOauth(oauthUser, op)
215 231
 			if err != nil {
216
-				c.String(http.StatusInternalServerError, response.TranslateMsg(c, err.Error()))
232
+				c.HTML(http.StatusOK, "oauth_fail.html", gin.H{
233
+					"message": response.TranslateMsg(c, err.Error()),
234
+				})
217 235
 				return
218 236
 			}
219 237
 		}
@@ -233,10 +251,14 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
233 251
 			c.Redirect(http.StatusFound, url)
234 252
 			return
235 253
 		}
236
-		c.String(http.StatusOK, response.TranslateMsg(c, "OauthSuccess"))
254
+		c.HTML(http.StatusOK, "oauth_success.html", gin.H{
255
+			"message": response.TranslateMsg(c, "OauthSuccess"),
256
+		})
237 257
 		return
238 258
 	} else {
239
-		c.String(http.StatusInternalServerError, response.TranslateMsg(c, "ParamsError"))
259
+		c.HTML(http.StatusOK, "oauth_fail.html", gin.H{
260
+			"message": response.TranslateMsg(c, "ParamsError"),
261
+		})
240 262
 		return
241 263
 	}
242 264
 

+ 2 - 0
http/router/api.go

@@ -18,6 +18,8 @@ func ApiInit(g *gin.Engine) {
18 18
 	if global.Config.App.ShowSwagger == 1 {
19 19
 		g.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, ginSwagger.InstanceName("api")))
20 20
 	}
21
+	// 加载 HTML 模板
22
+	g.LoadHTMLGlob("http/templates/*")
21 23
 
22 24
 	frg := g.Group("/api")
23 25
 

+ 73 - 0
http/templates/oauth_fail.html

@@ -0,0 +1,73 @@
1
+<!DOCTYPE html>
2
+<html lang="zh-CN">
3
+<head>
4
+    <meta charset="UTF-8">
5
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+    <title>授权失败 - RustDesk API</title>
7
+    <style>
8
+        body {
9
+            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
10
+            background-color: #f5f5f5;
11
+            margin: 0;
12
+            display: flex;
13
+            justify-content: center;
14
+            align-items: center;
15
+            min-height: 100vh;
16
+        }
17
+
18
+        .success-container {
19
+            text-align: center;
20
+            background: white;
21
+            padding: 2rem;
22
+            border-radius: 10px;
23
+            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
24
+            max-width: 400px;
25
+            width: 90%;
26
+        }
27
+
28
+        .checkmark {
29
+            color: #ba363a;
30
+            font-size: 4rem;
31
+            margin-bottom: 1rem;
32
+        }
33
+
34
+        h1 {
35
+            color: #333;
36
+            margin-bottom: 1rem;
37
+        }
38
+
39
+        p {
40
+            color: #666;
41
+            line-height: 1.6;
42
+            margin-bottom: 1.5rem;
43
+        }
44
+
45
+        .return-link {
46
+            display: inline-block;
47
+            padding: 10px 20px;
48
+            background-color: #ba363a;
49
+            color: white;
50
+            text-decoration: none;
51
+            border-radius: 5px;
52
+            transition: background-color 0.3s;
53
+        }
54
+
55
+        .return-link:hover {
56
+            background-color: #ba363a;
57
+        }
58
+    </style>
59
+    <link rel="stylesheet" href="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/font-awesome/6.0.0/css/all.min.css">
60
+</head>
61
+<body>
62
+<div class="success-container">
63
+    <i class="fas fa-triangle-exclamation    checkmark"></i>
64
+    <h1>授权失败!</h1>
65
+    <p>{{.message}}</p>
66
+    <a href="javascript:window.close()" class="return-link">关闭页面</a>
67
+</div>
68
+
69
+<script>
70
+
71
+</script>
72
+</body>
73
+</html>

+ 73 - 0
http/templates/oauth_success.html

@@ -0,0 +1,73 @@
1
+<!DOCTYPE html>
2
+<html lang="zh-CN">
3
+<head>
4
+    <meta charset="UTF-8">
5
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+    <title>授权成功 - RustDesk API</title>
7
+    <style>
8
+        body {
9
+            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
10
+            background-color: #f5f5f5;
11
+            margin: 0;
12
+            display: flex;
13
+            justify-content: center;
14
+            align-items: center;
15
+            min-height: 100vh;
16
+        }
17
+
18
+        .success-container {
19
+            text-align: center;
20
+            background: white;
21
+            padding: 2rem;
22
+            border-radius: 10px;
23
+            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
24
+            max-width: 400px;
25
+            width: 90%;
26
+        }
27
+
28
+        .checkmark {
29
+            color: #4CAF50;
30
+            font-size: 4rem;
31
+            margin-bottom: 1rem;
32
+        }
33
+
34
+        h1 {
35
+            color: #333;
36
+            margin-bottom: 1rem;
37
+        }
38
+
39
+        p {
40
+            color: #666;
41
+            line-height: 1.6;
42
+            margin-bottom: 1.5rem;
43
+        }
44
+
45
+        .return-link {
46
+            display: inline-block;
47
+            padding: 10px 20px;
48
+            background-color: #4CAF50;
49
+            color: white;
50
+            text-decoration: none;
51
+            border-radius: 5px;
52
+            transition: background-color 0.3s;
53
+        }
54
+
55
+        .return-link:hover {
56
+            background-color: #45a049;
57
+        }
58
+    </style>
59
+</head>
60
+<body>
61
+<div class="success-container">
62
+    <i class="fas fa-check-circle checkmark"></i>
63
+    <h1>授权成功!</h1>
64
+    <p>您已成功授权访问您的账户。</p>
65
+    <p>现在可以关闭本页面或返回应用继续操作。</p>
66
+    <a href="javascript:window.close()" class="return-link">关闭页面</a>
67
+</div>
68
+
69
+<script>
70
+
71
+</script>
72
+</body>
73
+</html>