lejianwen месяцев назад: 9
Родитель
Сommit
fe7b8b53a6

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

@@ -8,6 +8,8 @@ import (
8 8
 	apiResp "github.com/lejianwen/rustdesk-api/v2/http/response/api"
9 9
 	"github.com/lejianwen/rustdesk-api/v2/model"
10 10
 	"github.com/lejianwen/rustdesk-api/v2/service"
11
+	"github.com/lejianwen/rustdesk-api/v2/utils"
12
+	"github.com/nicksnyder/go-i18n/v2/i18n"
11 13
 	"net/http"
12 14
 )
13 15
 
@@ -145,7 +147,8 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
145 147
 	state := c.Query("state")
146 148
 	if state == "" {
147 149
 		c.HTML(http.StatusOK, "oauth_fail.html", gin.H{
148
-			"message": response.TranslateParamMsg(c, "ParamIsEmpty", "state"),
150
+			"message":     "ParamIsEmpty",
151
+			"sub_message": "state",
149 152
 		})
150 153
 		return
151 154
 	}
@@ -155,7 +158,7 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
155 158
 	oauthCache := oauthService.GetOauthCache(cacheKey)
156 159
 	if oauthCache == nil {
157 160
 		c.HTML(http.StatusOK, "oauth_fail.html", gin.H{
158
-			"message": response.TranslateMsg(c, "OauthExpired"),
161
+			"message": "OauthExpired",
159 162
 		})
160 163
 		return
161 164
 	}
@@ -169,7 +172,8 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
169 172
 	err, oauthUser := oauthService.Callback(code, verifier, op, nonce)
170 173
 	if err != nil {
171 174
 		c.HTML(http.StatusOK, "oauth_fail.html", gin.H{
172
-			"message": response.TranslateMsg(c, "OauthFailed") + response.TranslateMsg(c, err.Error()),
175
+			"message":     "OauthFailed",
176
+			"sub_message": err.Error(),
173 177
 		})
174 178
 		return
175 179
 	}
@@ -182,7 +186,7 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
182 186
 		utr := oauthService.UserThirdInfo(op, openid)
183 187
 		if utr.UserId > 0 {
184 188
 			c.HTML(http.StatusOK, "oauth_fail.html", gin.H{
185
-				"message": response.TranslateMsg(c, "OauthHasBindOtherUser"),
189
+				"message": "OauthHasBindOtherUser",
186 190
 			})
187 191
 			return
188 192
 		}
@@ -190,7 +194,7 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
190 194
 		user = service.AllService.UserService.InfoById(userId)
191 195
 		if user == nil {
192 196
 			c.HTML(http.StatusOK, "oauth_fail.html", gin.H{
193
-				"message": response.TranslateMsg(c, "ItemNotFound"),
197
+				"message": "ItemNotFound",
194 198
 			})
195 199
 			return
196 200
 		}
@@ -198,12 +202,12 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
198 202
 		err := oauthService.BindOauthUser(userId, oauthUser, op)
199 203
 		if err != nil {
200 204
 			c.HTML(http.StatusOK, "oauth_fail.html", gin.H{
201
-				"message": response.TranslateMsg(c, "BindFail"),
205
+				"message": "BindFail",
202 206
 			})
203 207
 			return
204 208
 		}
205 209
 		c.HTML(http.StatusOK, "oauth_success.html", gin.H{
206
-			"message": response.TranslateMsg(c, "BindSuccess"),
210
+			"message": "BindSuccess",
207 211
 		})
208 212
 		return
209 213
 
@@ -211,7 +215,7 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
211 215
 		//登录
212 216
 		if userId != 0 {
213 217
 			c.HTML(http.StatusOK, "oauth_fail.html", gin.H{
214
-				"message": response.TranslateMsg(c, "OauthHasBeenSuccess"),
218
+				"message": "OauthHasBeenSuccess",
215 219
 			})
216 220
 			return
217 221
 		}
@@ -230,7 +234,7 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
230 234
 			err, user = service.AllService.UserService.RegisterByOauth(oauthUser, op)
231 235
 			if err != nil {
232 236
 				c.HTML(http.StatusOK, "oauth_fail.html", gin.H{
233
-					"message": response.TranslateMsg(c, err.Error()),
237
+					"message": err.Error(),
234 238
 				})
235 239
 				return
236 240
 			}
@@ -252,14 +256,50 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
252 256
 			return
253 257
 		}
254 258
 		c.HTML(http.StatusOK, "oauth_success.html", gin.H{
255
-			"message": response.TranslateMsg(c, "OauthSuccess"),
259
+			"message": "OauthSuccess",
256 260
 		})
257 261
 		return
258 262
 	} else {
259 263
 		c.HTML(http.StatusOK, "oauth_fail.html", gin.H{
260
-			"message": response.TranslateMsg(c, "ParamsError"),
264
+			"message": "ParamsError",
261 265
 		})
262 266
 		return
263 267
 	}
264 268
 
265 269
 }
270
+
271
+type MessageParams struct {
272
+	Lang  string `json:"lang" form:"lang"`
273
+	Title string `json:"title" form:"title"`
274
+	Msg   string `json:"msg" form:"msg"`
275
+}
276
+
277
+func (o *Oauth) Message(c *gin.Context) {
278
+	mp := &MessageParams{}
279
+	if err := c.ShouldBindQuery(mp); err != nil {
280
+		return
281
+	}
282
+	localizer := global.Localizer(mp.Lang)
283
+	res := ""
284
+	if mp.Title != "" {
285
+		title, err := localizer.LocalizeMessage(&i18n.Message{
286
+			ID: mp.Title,
287
+		})
288
+		if err == nil {
289
+			res = utils.StringConcat(";title='", title, "';")
290
+		}
291
+
292
+	}
293
+	if mp.Msg != "" {
294
+		msg, err := localizer.LocalizeMessage(&i18n.Message{
295
+			ID: mp.Msg,
296
+		})
297
+		if err == nil {
298
+			res = utils.StringConcat(res, "msg = '", msg, "';")
299
+		}
300
+	}
301
+
302
+	//返回js内容
303
+	c.Header("Content-Type", "application/javascript")
304
+	c.String(http.StatusOK, res)
305
+}

+ 1 - 0
http/router/api.go

@@ -48,6 +48,7 @@ func ApiInit(g *gin.Engine) {
48 48
 		//api/oauth/callback
49 49
 		frg.GET("/oauth/callback", o.OauthCallback)
50 50
 		frg.GET("/oauth/login", o.OauthCallback)
51
+		frg.GET("/oauth/msg", o.Message)
51 52
 	}
52 53
 	{
53 54
 		pe := &api.Peer{}

+ 16 - 8
resources/templates/oauth_fail.html

@@ -1,9 +1,9 @@
1 1
 <!DOCTYPE html>
2
-<html lang="zh-CN">
2
+<html>
3 3
 <head>
4 4
     <meta charset="UTF-8">
5 5
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
-    <title>授权失败 - RustDesk API</title>
6
+    <title>OauthFailed - RustDesk API</title>
7 7
     <style>
8 8
         body {
9 9
             font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
@@ -57,17 +57,25 @@
57 57
         }
58 58
     </style>
59 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
+    <script>
61
+        var lang = navigator.language || navigator.userLanguage || 'zh-CN';
62
+        var title = 'OauthFailed'
63
+        var msg = '{{.message}}'
64
+        var btn = 'Close'
65
+        document.writeln('<script src="/api/oauth/msg?lang=' + lang + '&msg=' + msg + '&title=OauthFailed"><\/script>');
66
+    </script>
60 67
 </head>
61 68
 <body>
62 69
 <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>
70
+    <i class="fas fa-triangle-exclamation checkmark"></i>
71
+    <h1 id="h1"></h1>
72
+    <p id="msg"></p>
73
+    <a href="javascript:window.close()" class="return-link" id="btn">Close</a>
67 74
 </div>
68
-
69 75
 <script>
70
-
76
+    document.title = title + ' - RustDesk API';
77
+    document.getElementById('h1').innerText = title;
78
+    document.getElementById('msg').innerText = msg;
71 79
 </script>
72 80
 </body>
73 81
 </html>

+ 15 - 6
resources/templates/oauth_success.html

@@ -3,7 +3,7 @@
3 3
 <head>
4 4
     <meta charset="UTF-8">
5 5
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
-    <title>授权成功 - RustDesk API</title>
6
+    <title>OauthSuccess - RustDesk API</title>
7 7
     <style>
8 8
         body {
9 9
             font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
@@ -56,18 +56,27 @@
56 56
             background-color: #45a049;
57 57
         }
58 58
     </style>
59
+    <script>
60
+        var lang = navigator.language || navigator.userLanguage || 'zh-CN';
61
+        var title = 'OauthSuccess'
62
+        var msg = '{{.message}}'
63
+        var btn = 'Close'
64
+        document.writeln('<script src="/api/oauth/msg?lang=' + lang + '&msg=' + msg + '&title=OauthSuccess"><\/script>');
65
+    </script>
59 66
 </head>
60 67
 <body>
61 68
 <div class="success-container">
62 69
     <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>
70
+    <h1 id="h1"></h1>
71
+<!--    <p>您已成功授权访问您的账户。</p>-->
72
+<!--    <p>现在可以关闭本页面或返回应用继续操作。</p>-->
73
+    <a href="javascript:window.close()" class="return-link">Close</a>
67 74
 </div>
68 75
 
69 76
 <script>
70
-
77
+    document.title = title + ' - RustDesk API';
78
+    document.getElementById('h1').innerText = title;
79
+    document.getElementById('msg').innerText = msg;
71 80
 </script>
72 81
 </body>
73 82
 </html>

+ 9 - 0
utils/tools.go

@@ -7,6 +7,7 @@ import (
7 7
 	"math/rand"
8 8
 	"reflect"
9 9
 	"runtime/debug"
10
+	"strings"
10 11
 )
11 12
 
12 13
 func Md5(str string) string {
@@ -100,3 +101,11 @@ func InArray(k string, arr []string) bool {
100 101
 	}
101 102
 	return false
102 103
 }
104
+
105
+func StringConcat(strs ...string) string {
106
+	var builder strings.Builder
107
+	for _, str := range strs {
108
+		builder.WriteString(str)
109
+	}
110
+	return builder.String()
111
+}