ljw 1 год назад
Родитель
Сommit
c99261c12d

+ 1 - 0
.gitignore

@@ -5,5 +5,6 @@ go.sum
5 5
 resources/*
6 6
 !resources/public/upload/.gitignore
7 7
 !resources/web
8
+!resources/i18n
8 9
 release
9 10
 data

+ 8 - 2
README.md

@@ -21,6 +21,7 @@
21 21
     - 地址簿
22 22
     - 群组
23 23
     - 授权登录,支持`github`和`google`登录,支持`web后台`授权登录
24
+    - i18n
24 25
 - Web Admin
25 26
     - 用户管理
26 27
     - 设备管理
@@ -29,6 +30,7 @@
29 30
     - 群组管理
30 31
     - Oauth 管理
31 32
     - 快速使用web client
33
+    - i18n
32 34
 - Web Client
33 35
     - 自动获取API server
34 36
     - 自动获取ID服务器和KEY
@@ -72,9 +74,11 @@
72 74
 
73 75
 ![pc_gr](docs/pc_gr.png)
74 76
 
75
-### Web Admin: 
77
+### Web Admin:
76 78
 
77
-***使用前后端分离,提供用户友好的管理界面,主要用来管理和展示。前端代码在[rustdesk-api-web](https://github.com/lejianwen/rustdesk-api-web)***
79
+**
80
+*使用前后端分离,提供用户友好的管理界面,主要用来管理和展示。前端代码在[rustdesk-api-web](https://github.com/lejianwen/rustdesk-api-web)
81
+***
78 82
 
79 83
 ***后台访问地址是`http://<your server>[:port]/_admin/`初次安装管理员为用户名密码为`admin` `admin`,请即时更改密码***
80 84
 
@@ -117,6 +121,7 @@
117 121
 * 参考`conf/config.yaml`配置文件,修改相关配置。如果`gorm.type`是`sqlite`,则不需要配置mysql相关配置。
118 122
 
119 123
 ```yaml
124
+lang: "en"
120 125
 gin:
121 126
   api-addr: "0.0.0.0:21114"
122 127
   mode: "release"
@@ -144,6 +149,7 @@ rustdesk:
144 149
 | 变量名                                 | 说明                                   | 示例                          |
145 150
 |-------------------------------------|--------------------------------------|-----------------------------|
146 151
 | TZ                                  | 时区                                   | Asia/Shanghai               |
152
+| RUSTDESK_API_LANG                   | 语言                                   | `en`,`zh-CN`                |
147 153
 | -----GIN配置-----                     | ----------                           | ----------                  |
148 154
 | RUSTDESK_API_GIN_TRUST_PROXY        | 信任的代理IP列表,以`,`分割,默认信任所有              | 192.168.1.2,192.168.1.3     |
149 155
 | -----------GORM配置------------------ | ------------------------------------ | --------------------------- |

+ 8 - 2
README_EN.md

@@ -20,6 +20,7 @@ desktop software that provides self-hosted solutions.
20 20
     - Address Book
21 21
     - Groups
22 22
     - Authorized login, supports `GitHub` and `Google` login, supports `web admin` authorized login
23
+    - i18n
23 24
 - Web Admin
24 25
     - User Management
25 26
     - Device Management
@@ -28,6 +29,7 @@ desktop software that provides self-hosted solutions.
28 29
     - Group Management
29 30
     - OAuth Management
30 31
     - Quick access to web client
32
+    - i18n
31 33
 - Web Client
32 34
     - Automatically obtain API server
33 35
     - Automatically obtain ID server and KEY
@@ -59,7 +61,8 @@ desktop software that provides self-hosted solutions.
59 61
 
60 62
 #### Login
61 63
 
62
-- Added `GitHub` and `Google` login, which can be used after configuration in the admin panel. See the OAuth configuration section for details.
64
+- Added `GitHub` and `Google` login, which can be used after configuration in the admin panel. See the OAuth
65
+  configuration section for details.
63 66
 - Added authorization login for the web admin panel.
64 67
 
65 68
 ![pc_login](docs/pc_login.png)
@@ -122,6 +125,7 @@ installation are `admin` `admin`, please change the password immediately.***
122 125
   not required.
123 126
 
124 127
 ```yaml
128
+lang: "en"
125 129
 gin:
126 130
   api-addr: "0.0.0.0:21114"
127 131
   mode: "release"
@@ -144,12 +148,14 @@ rustdesk:
144 148
   personal: 1
145 149
 ```
146 150
 
147
-* Environment variables, with the prefix `RUSTDESK_API_RUSTDESK_PERSONAL`, will override the settings in the configuration file if
151
+* Environment variables, with the prefix `RUSTDESK_API_RUSTDESK_PERSONAL`, will override the settings in the
152
+  configuration file if
148 153
   present.
149 154
 
150 155
 | Variable Name                      | Description                                               | Example                        |
151 156
 |------------------------------------|-----------------------------------------------------------|--------------------------------|
152 157
 | TZ                                 | timezone                                                  | Asia/Shanghai                  |
158
+| RUSTDESK_API_LANG                  | Language                                                  | `en`,`zh-CN`                   |
153 159
 | ----- GIN Configuration -----      | ---------------------------------------                   | ------------------------------ |
154 160
 | RUSTDESK_API_GIN_TRUST_PROXY       | Trusted proxy IPs, separated by commas.                   | 192.168.1.2,192.168.1.3        |
155 161
 | ----- GORM Configuration -----     | ---------------------------------------                   | ------------------------------ |

+ 85 - 10
cmd/apimain.go

@@ -12,12 +12,17 @@ import (
12 12
 	"Gwen/model"
13 13
 	"Gwen/service"
14 14
 	"fmt"
15
+	"github.com/BurntSushi/toml"
16
+	"github.com/gin-gonic/gin"
15 17
 	"github.com/go-playground/locales/en"
16 18
 	"github.com/go-playground/locales/zh_Hans_CN"
17 19
 	ut "github.com/go-playground/universal-translator"
18 20
 	"github.com/go-playground/validator/v10"
21
+	en_translations "github.com/go-playground/validator/v10/translations/en"
19 22
 	zh_translations "github.com/go-playground/validator/v10/translations/zh"
20 23
 	"github.com/go-redis/redis/v8"
24
+	"github.com/nicksnyder/go-i18n/v2/i18n"
25
+	"golang.org/x/text/language"
21 26
 	"reflect"
22 27
 )
23 28
 
@@ -98,6 +103,7 @@ func main() {
98 103
 	//locker
99 104
 	global.Lock = lock.NewLocal()
100 105
 
106
+	InitI18n()
101 107
 	//gin
102 108
 	http.ApiInit()
103 109
 
@@ -105,15 +111,25 @@ func main() {
105 111
 
106 112
 func ApiInitValidator() {
107 113
 	validate := validator.New()
114
+
115
+	// 定义不同的语言翻译
108 116
 	enT := en.New()
109 117
 	cn := zh_Hans_CN.New()
118
+
110 119
 	uni := ut.New(enT, cn)
111
-	trans, _ := uni.GetTranslator("cn")
112
-	err := zh_translations.RegisterDefaultTranslations(validate, trans)
120
+
121
+	enTrans, _ := uni.GetTranslator("en")
122
+	zhTrans, _ := uni.GetTranslator("zh_Hans_CN")
123
+
124
+	err := zh_translations.RegisterDefaultTranslations(validate, zhTrans)
125
+	if err != nil {
126
+		panic(err)
127
+	}
128
+	err = en_translations.RegisterDefaultTranslations(validate, enTrans)
113 129
 	if err != nil {
114
-		//退出
115 130
 		panic(err)
116 131
 	}
132
+
117 133
 	validate.RegisterTagNameFunc(func(field reflect.StructField) string {
118 134
 		label := field.Tag.Get("label")
119 135
 		if label == "" {
@@ -122,10 +138,16 @@ func ApiInitValidator() {
122 138
 		return label
123 139
 	})
124 140
 	global.Validator.Validate = validate
125
-	global.Validator.VTrans = trans
141
+	global.Validator.UT = uni // 存储 Universal Translator
142
+	global.Validator.VTrans = zhTrans
126 143
 
127
-	global.Validator.ValidStruct = func(i interface{}) []string {
144
+	global.Validator.ValidStruct = func(ctx *gin.Context, i interface{}) []string {
128 145
 		err := global.Validator.Validate.Struct(i)
146
+		lang := ctx.GetHeader("Accept-Language")
147
+		if lang == "" {
148
+			lang = global.Config.Lang
149
+		}
150
+		trans := getTranslatorForLang(lang)
129 151
 		errList := make([]string, 0, 10)
130 152
 		if err != nil {
131 153
 			if _, ok := err.(*validator.InvalidValidationError); ok {
@@ -133,14 +155,18 @@ func ApiInitValidator() {
133 155
 				return errList
134 156
 			}
135 157
 			for _, err2 := range err.(validator.ValidationErrors) {
136
-				errList = append(errList, err2.Translate(global.Validator.VTrans))
158
+				errList = append(errList, err2.Translate(trans))
137 159
 			}
138 160
 		}
139 161
 		return errList
140 162
 	}
141
-	global.Validator.ValidVar = func(field interface{}, tag string) []string {
163
+	global.Validator.ValidVar = func(ctx *gin.Context, field interface{}, tag string) []string {
142 164
 		err := global.Validator.Validate.Var(field, tag)
143
-		fmt.Println(err)
165
+		lang := ctx.GetHeader("Accept-Language")
166
+		if lang == "" {
167
+			lang = global.Config.Lang
168
+		}
169
+		trans := getTranslatorForLang(lang)
144 170
 		errList := make([]string, 0, 10)
145 171
 		if err != nil {
146 172
 			if _, ok := err.(*validator.InvalidValidationError); ok {
@@ -148,14 +174,29 @@ func ApiInitValidator() {
148 174
 				return errList
149 175
 			}
150 176
 			for _, err2 := range err.(validator.ValidationErrors) {
151
-				errList = append(errList, err2.Translate(global.Validator.VTrans))
177
+				errList = append(errList, err2.Translate(trans))
152 178
 			}
153 179
 		}
154 180
 		return errList
155 181
 	}
156 182
 
157 183
 }
158
-
184
+func getTranslatorForLang(lang string) ut.Translator {
185
+	switch lang {
186
+	case "zh_CN":
187
+		fallthrough
188
+	case "zh-CN":
189
+		fallthrough
190
+	case "zh":
191
+		trans, _ := global.Validator.UT.GetTranslator("zh_Hans_CN")
192
+		return trans
193
+	case "en":
194
+		fallthrough
195
+	default:
196
+		trans, _ := global.Validator.UT.GetTranslator("en")
197
+		return trans
198
+	}
199
+}
159 200
 func DatabaseAutoUpdate() {
160 201
 	version := 126
161 202
 
@@ -251,3 +292,37 @@ func Migrate(version uint) {
251 292
 	}
252 293
 
253 294
 }
295
+
296
+func InitI18n() {
297
+	bundle := i18n.NewBundle(language.English)
298
+	bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
299
+	bundle.LoadMessageFile(global.Config.Gin.ResourcesPath + "/i18n/en.toml")
300
+	bundle.LoadMessageFile(global.Config.Gin.ResourcesPath + "/i18n/zh_CN.toml")
301
+	global.Localizer = func(ctx *gin.Context) *i18n.Localizer {
302
+		lang := ctx.GetHeader("Accept-Language")
303
+		if lang == "" {
304
+			lang = global.Config.Lang
305
+		}
306
+		if lang == "en" {
307
+			return i18n.NewLocalizer(bundle, "en")
308
+		} else {
309
+			return i18n.NewLocalizer(bundle, lang, "en")
310
+		}
311
+	}
312
+
313
+	//personUnreadEmails := localizer.MustLocalize(&i18n.LocalizeConfig{
314
+	//	DefaultMessage: &i18n.Message{
315
+	//		ID: "PersonUnreadEmails",
316
+	//	},
317
+	//	PluralCount: 6,
318
+	//	TemplateData: map[string]interface{}{
319
+	//		"Name":        "LE",
320
+	//		"PluralCount": 6,
321
+	//	},
322
+	//})
323
+	//personUnreadEmails, err := global.Localizer.LocalizeMessage(&i18n.Message{
324
+	//	ID: "ParamsError",
325
+	//})
326
+	//fmt.Println(err, personUnreadEmails)
327
+
328
+}

+ 1 - 0
conf/config.yaml

@@ -1,3 +1,4 @@
1
+lang: "en"
1 2
 gin:
2 3
   api-addr: "0.0.0.0:21114"
3 4
   mode: "release" #release,debug,test

+ 1 - 0
config/config.go

@@ -15,6 +15,7 @@ const (
15 15
 )
16 16
 
17 17
 type Config struct {
18
+	Lang     string `mapstructure:"lang"`
18 19
 	Gorm     Gorm
19 20
 	Mysql    Mysql
20 21
 	Gin      Gin

+ 0 - 1
docs/admin/admin_docs.go

@@ -2455,7 +2455,6 @@ const docTemplateadmin = `{
2455 2455
             "type": "object",
2456 2456
             "required": [
2457 2457
                 "group_id",
2458
-                "nickname",
2459 2458
                 "status",
2460 2459
                 "username"
2461 2460
             ],

+ 0 - 1
docs/admin/admin_swagger.json

@@ -2448,7 +2448,6 @@
2448 2448
             "type": "object",
2449 2449
             "required": [
2450 2450
                 "group_id",
2451
-                "nickname",
2452 2451
                 "status",
2453 2452
                 "username"
2454 2453
             ],

+ 0 - 1
docs/admin/admin_swagger.yaml

@@ -165,7 +165,6 @@ definitions:
165 165
         type: string
166 166
     required:
167 167
     - group_id
168
-    - nickname
169 168
     - status
170 169
     - username
171 170
     type: object

+ 9 - 5
global/global.go

@@ -6,9 +6,11 @@ import (
6 6
 	"Gwen/lib/jwt"
7 7
 	"Gwen/lib/lock"
8 8
 	"Gwen/lib/upload"
9
+	"github.com/gin-gonic/gin"
9 10
 	ut "github.com/go-playground/universal-translator"
10 11
 	"github.com/go-playground/validator/v10"
11 12
 	"github.com/go-redis/redis/v8"
13
+	"github.com/nicksnyder/go-i18n/v2/i18n"
12 14
 	"github.com/sirupsen/logrus"
13 15
 	"github.com/spf13/viper"
14 16
 	"gorm.io/gorm"
@@ -23,11 +25,13 @@ var (
23 25
 	Cache     cache.Handler
24 26
 	Validator struct {
25 27
 		Validate    *validator.Validate
28
+		UT          *ut.UniversalTranslator
26 29
 		VTrans      ut.Translator
27
-		ValidStruct func(interface{}) []string
28
-		ValidVar    func(field interface{}, tag string) []string
30
+		ValidStruct func(*gin.Context, interface{}) []string
31
+		ValidVar    func(ctx *gin.Context, field interface{}, tag string) []string
29 32
 	}
30
-	Oss  *upload.Oss
31
-	Jwt  *jwt.Jwt
32
-	Lock lock.Locker
33
+	Oss       *upload.Oss
34
+	Jwt       *jwt.Jwt
35
+	Lock      lock.Locker
36
+	Localizer func(ctx *gin.Context) *i18n.Localizer
33 37
 )

+ 3 - 3
go.mod

@@ -20,7 +20,7 @@ require (
20 20
 	github.com/swaggo/gin-swagger v1.6.0
21 21
 	github.com/swaggo/swag v1.16.3
22 22
 	golang.org/x/oauth2 v0.23.0
23
-	golang.org/x/text v0.15.0
23
+	golang.org/x/text v0.18.0
24 24
 	gorm.io/driver/mysql v1.5.7
25 25
 	gorm.io/driver/sqlite v1.5.6
26 26
 	gorm.io/gorm v1.25.7
@@ -67,9 +67,9 @@ require (
67 67
 	github.com/ugorji/go/codec v1.2.9 // indirect
68 68
 	golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
69 69
 	golang.org/x/crypto v0.23.0 // indirect
70
-	golang.org/x/net v0.21.0 // indirect
70
+	golang.org/x/net v0.25.0 // indirect
71 71
 	golang.org/x/sys v0.25.0 // indirect
72
-	golang.org/x/tools v0.7.0 // indirect
72
+	golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
73 73
 	google.golang.org/protobuf v1.33.0 // indirect
74 74
 	gopkg.in/ini.v1 v1.63.2 // indirect
75 75
 	gopkg.in/yaml.v2 v2.4.0 // indirect

+ 16 - 16
http/controller/admin/addressBook.go

@@ -31,14 +31,14 @@ func (ct *AddressBook) Detail(c *gin.Context) {
31 31
 	t := service.AllService.AddressBookService.InfoByRowId(uint(iid))
32 32
 	u := service.AllService.UserService.CurUser(c)
33 33
 	if !service.AllService.UserService.IsAdmin(u) && t.UserId != u.Id {
34
-		response.Fail(c, 101, "无权限")
34
+		response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
35 35
 		return
36 36
 	}
37 37
 	if t.RowId > 0 {
38 38
 		response.Success(c, t)
39 39
 		return
40 40
 	}
41
-	response.Fail(c, 101, "信息不存在")
41
+	response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
42 42
 	return
43 43
 }
44 44
 
@@ -56,10 +56,10 @@ func (ct *AddressBook) Detail(c *gin.Context) {
56 56
 func (ct *AddressBook) Create(c *gin.Context) {
57 57
 	f := &admin.AddressBookForm{}
58 58
 	if err := c.ShouldBindJSON(f); err != nil {
59
-		response.Fail(c, 101, "参数错误")
59
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
60 60
 		return
61 61
 	}
62
-	errList := global.Validator.ValidStruct(f)
62
+	errList := global.Validator.ValidStruct(c, f)
63 63
 	if len(errList) > 0 {
64 64
 		response.Fail(c, 101, errList[0])
65 65
 		return
@@ -71,7 +71,7 @@ func (ct *AddressBook) Create(c *gin.Context) {
71 71
 	}
72 72
 	err := service.AllService.AddressBookService.Create(t)
73 73
 	if err != nil {
74
-		response.Fail(c, 101, "创建失败")
74
+		response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
75 75
 		return
76 76
 	}
77 77
 	response.Success(c, u)
@@ -94,7 +94,7 @@ func (ct *AddressBook) Create(c *gin.Context) {
94 94
 func (ct *AddressBook) List(c *gin.Context) {
95 95
 	query := &admin.AddressBookQuery{}
96 96
 	if err := c.ShouldBindQuery(query); err != nil {
97
-		response.Fail(c, 101, "参数错误")
97
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
98 98
 		return
99 99
 	}
100 100
 	u := service.AllService.UserService.CurUser(c)
@@ -123,27 +123,27 @@ func (ct *AddressBook) List(c *gin.Context) {
123 123
 func (ct *AddressBook) Update(c *gin.Context) {
124 124
 	f := &admin.AddressBookForm{}
125 125
 	if err := c.ShouldBindJSON(f); err != nil {
126
-		response.Fail(c, 101, "参数错误")
126
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
127 127
 		return
128 128
 	}
129
-	errList := global.Validator.ValidStruct(f)
129
+	errList := global.Validator.ValidStruct(c, f)
130 130
 	if len(errList) > 0 {
131 131
 		response.Fail(c, 101, errList[0])
132 132
 		return
133 133
 	}
134 134
 	if f.RowId == 0 {
135
-		response.Fail(c, 101, "参数错误")
135
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
136 136
 		return
137 137
 	}
138 138
 	t := f.ToAddressBook()
139 139
 	u := service.AllService.UserService.CurUser(c)
140 140
 	if !service.AllService.UserService.IsAdmin(u) && t.UserId != u.Id {
141
-		response.Fail(c, 101, "无权限")
141
+		response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
142 142
 		return
143 143
 	}
144 144
 	err := service.AllService.AddressBookService.Update(t)
145 145
 	if err != nil {
146
-		response.Fail(c, 101, "更新失败")
146
+		response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
147 147
 		return
148 148
 	}
149 149
 	response.Success(c, nil)
@@ -163,11 +163,11 @@ func (ct *AddressBook) Update(c *gin.Context) {
163 163
 func (ct *AddressBook) Delete(c *gin.Context) {
164 164
 	f := &admin.AddressBookForm{}
165 165
 	if err := c.ShouldBindJSON(f); err != nil {
166
-		response.Fail(c, 101, "系统错误")
166
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
167 167
 		return
168 168
 	}
169 169
 	id := f.RowId
170
-	errList := global.Validator.ValidVar(id, "required,gt=0")
170
+	errList := global.Validator.ValidVar(c, id, "required,gt=0")
171 171
 	if len(errList) > 0 {
172 172
 		response.Fail(c, 101, errList[0])
173 173
 		return
@@ -175,7 +175,7 @@ func (ct *AddressBook) Delete(c *gin.Context) {
175 175
 	t := service.AllService.AddressBookService.InfoByRowId(f.RowId)
176 176
 	u := service.AllService.UserService.CurUser(c)
177 177
 	if !service.AllService.UserService.IsAdmin(u) && t.UserId != u.Id {
178
-		response.Fail(c, 101, "无权限")
178
+		response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
179 179
 		return
180 180
 	}
181 181
 	if u.Id > 0 {
@@ -184,8 +184,8 @@ func (ct *AddressBook) Delete(c *gin.Context) {
184 184
 			response.Success(c, nil)
185 185
 			return
186 186
 		}
187
-		response.Fail(c, 101, err.Error())
187
+		response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
188 188
 		return
189 189
 	}
190
-	response.Fail(c, 101, "信息不存在")
190
+	response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
191 191
 }

+ 13 - 13
http/controller/admin/group.go

@@ -31,7 +31,7 @@ func (ct *Group) Detail(c *gin.Context) {
31 31
 		response.Success(c, u)
32 32
 		return
33 33
 	}
34
-	response.Fail(c, 101, "信息不存在")
34
+	response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
35 35
 	return
36 36
 }
37 37
 
@@ -49,10 +49,10 @@ func (ct *Group) Detail(c *gin.Context) {
49 49
 func (ct *Group) Create(c *gin.Context) {
50 50
 	f := &admin.GroupForm{}
51 51
 	if err := c.ShouldBindJSON(f); err != nil {
52
-		response.Fail(c, 101, "参数错误")
52
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
53 53
 		return
54 54
 	}
55
-	errList := global.Validator.ValidStruct(f)
55
+	errList := global.Validator.ValidStruct(c, f)
56 56
 	if len(errList) > 0 {
57 57
 		response.Fail(c, 101, errList[0])
58 58
 		return
@@ -60,7 +60,7 @@ func (ct *Group) Create(c *gin.Context) {
60 60
 	u := f.ToGroup()
61 61
 	err := service.AllService.GroupService.Create(u)
62 62
 	if err != nil {
63
-		response.Fail(c, 101, "创建失败")
63
+		response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
64 64
 		return
65 65
 	}
66 66
 	response.Success(c, u)
@@ -81,7 +81,7 @@ func (ct *Group) Create(c *gin.Context) {
81 81
 func (ct *Group) List(c *gin.Context) {
82 82
 	query := &admin.PageQuery{}
83 83
 	if err := c.ShouldBindQuery(query); err != nil {
84
-		response.Fail(c, 101, "参数错误")
84
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
85 85
 		return
86 86
 	}
87 87
 	res := service.AllService.GroupService.List(query.Page, query.PageSize, nil)
@@ -102,14 +102,14 @@ func (ct *Group) List(c *gin.Context) {
102 102
 func (ct *Group) Update(c *gin.Context) {
103 103
 	f := &admin.GroupForm{}
104 104
 	if err := c.ShouldBindJSON(f); err != nil {
105
-		response.Fail(c, 101, "参数错误")
105
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
106 106
 		return
107 107
 	}
108 108
 	if f.Id == 0 {
109
-		response.Fail(c, 101, "参数错误")
109
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
110 110
 		return
111 111
 	}
112
-	errList := global.Validator.ValidStruct(f)
112
+	errList := global.Validator.ValidStruct(c, f)
113 113
 	if len(errList) > 0 {
114 114
 		response.Fail(c, 101, errList[0])
115 115
 		return
@@ -117,7 +117,7 @@ func (ct *Group) Update(c *gin.Context) {
117 117
 	u := f.ToGroup()
118 118
 	err := service.AllService.GroupService.Update(u)
119 119
 	if err != nil {
120
-		response.Fail(c, 101, "更新失败")
120
+		response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
121 121
 		return
122 122
 	}
123 123
 	response.Success(c, nil)
@@ -137,11 +137,11 @@ func (ct *Group) Update(c *gin.Context) {
137 137
 func (ct *Group) Delete(c *gin.Context) {
138 138
 	f := &admin.GroupForm{}
139 139
 	if err := c.ShouldBindJSON(f); err != nil {
140
-		response.Fail(c, 101, "系统错误")
140
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
141 141
 		return
142 142
 	}
143 143
 	id := f.Id
144
-	errList := global.Validator.ValidVar(id, "required,gt=0")
144
+	errList := global.Validator.ValidVar(c, id, "required,gt=0")
145 145
 	if len(errList) > 0 {
146 146
 		response.Fail(c, 101, errList[0])
147 147
 		return
@@ -153,8 +153,8 @@ func (ct *Group) Delete(c *gin.Context) {
153 153
 			response.Success(c, nil)
154 154
 			return
155 155
 		}
156
-		response.Fail(c, 101, err.Error())
156
+		response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
157 157
 		return
158 158
 	}
159
-	response.Fail(c, 101, "信息不存在")
159
+	response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
160 160
 }

+ 3 - 3
http/controller/admin/login.go

@@ -28,11 +28,11 @@ func (ct *Login) Login(c *gin.Context) {
28 28
 	f := &admin.Login{}
29 29
 	err := c.ShouldBindJSON(f)
30 30
 	if err != nil {
31
-		response.Fail(c, 101, "参数错误")
31
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
32 32
 		return
33 33
 	}
34 34
 
35
-	errList := global.Validator.ValidStruct(f)
35
+	errList := global.Validator.ValidStruct(c, f)
36 36
 	if len(errList) > 0 {
37 37
 		response.Fail(c, 101, errList[0])
38 38
 		return
@@ -40,7 +40,7 @@ func (ct *Login) Login(c *gin.Context) {
40 40
 	u := service.AllService.UserService.InfoByUsernamePassword(f.Username, f.Password)
41 41
 
42 42
 	if u.Id == 0 {
43
-		response.Fail(c, 101, "用户名或密码错误")
43
+		response.Fail(c, 101, response.TranslateMsg(c, "UsernameOrPasswordError"))
44 44
 		return
45 45
 	}
46 46
 

+ 6 - 6
http/controller/admin/loginLog.go

@@ -33,7 +33,7 @@ func (ct *LoginLog) Detail(c *gin.Context) {
33 33
 		response.Success(c, u)
34 34
 		return
35 35
 	}
36
-	response.Fail(c, 101, "信息不存在")
36
+	response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
37 37
 	return
38 38
 }
39 39
 
@@ -53,7 +53,7 @@ func (ct *LoginLog) Detail(c *gin.Context) {
53 53
 func (ct *LoginLog) List(c *gin.Context) {
54 54
 	query := &admin.LoginLogQuery{}
55 55
 	if err := c.ShouldBindQuery(query); err != nil {
56
-		response.Fail(c, 101, "参数错误")
56
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
57 57
 		return
58 58
 	}
59 59
 	u := service.AllService.UserService.CurUser(c)
@@ -82,11 +82,11 @@ func (ct *LoginLog) List(c *gin.Context) {
82 82
 func (ct *LoginLog) Delete(c *gin.Context) {
83 83
 	f := &model.LoginLog{}
84 84
 	if err := c.ShouldBindJSON(f); err != nil {
85
-		response.Fail(c, 101, "系统错误")
85
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
86 86
 		return
87 87
 	}
88 88
 	id := f.Id
89
-	errList := global.Validator.ValidVar(id, "required,gt=0")
89
+	errList := global.Validator.ValidVar(c, id, "required,gt=0")
90 90
 	if len(errList) > 0 {
91 91
 		response.Fail(c, 101, errList[0])
92 92
 		return
@@ -94,7 +94,7 @@ func (ct *LoginLog) Delete(c *gin.Context) {
94 94
 	l := service.AllService.LoginLogService.InfoById(f.Id)
95 95
 	u := service.AllService.UserService.CurUser(c)
96 96
 	if !service.AllService.UserService.IsAdmin(u) && l.UserId != u.Id {
97
-		response.Fail(c, 101, "无权限")
97
+		response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
98 98
 		return
99 99
 	}
100 100
 	if l.Id > 0 {
@@ -106,5 +106,5 @@ func (ct *LoginLog) Delete(c *gin.Context) {
106 106
 		response.Fail(c, 101, err.Error())
107 107
 		return
108 108
 	}
109
-	response.Fail(c, 101, "信息不存在")
109
+	response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
110 110
 }

+ 33 - 25
http/controller/admin/oauth.go

@@ -18,12 +18,12 @@ type Oauth struct {
18 18
 func (o *Oauth) Info(c *gin.Context) {
19 19
 	code := c.Query("code")
20 20
 	if code == "" {
21
-		response.Fail(c, 101, "参数错误")
21
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
22 22
 		return
23 23
 	}
24 24
 	v := service.AllService.OauthService.GetOauthCache(code)
25 25
 	if v == nil {
26
-		response.Fail(c, 101, "信息不存在")
26
+		response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
27 27
 		return
28 28
 	}
29 29
 	response.Success(c, v)
@@ -33,20 +33,20 @@ func (o *Oauth) ToBind(c *gin.Context) {
33 33
 	f := &adminReq.BindOauthForm{}
34 34
 	err := c.ShouldBindJSON(f)
35 35
 	if err != nil {
36
-		response.Fail(c, 101, "参数错误")
36
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
37 37
 		return
38 38
 	}
39 39
 	u := service.AllService.UserService.CurUser(c)
40 40
 
41 41
 	utr := service.AllService.UserService.UserThirdInfo(u.Id, f.Op)
42 42
 	if utr.Id > 0 {
43
-		response.Fail(c, 101, "已绑定过了")
43
+		response.Fail(c, 101, response.TranslateMsg(c, "OauthHasBindOtherUser"))
44 44
 		return
45 45
 	}
46 46
 
47 47
 	err, code, url := service.AllService.OauthService.BeginAuth(f.Op)
48 48
 	if err != nil {
49
-		response.Error(c, err.Error())
49
+		response.Error(c, response.TranslateMsg(c, err.Error()))
50 50
 		return
51 51
 	}
52 52
 
@@ -89,22 +89,22 @@ func (o *Oauth) BindConfirm(c *gin.Context) {
89 89
 	j := &adminReq.OauthConfirmForm{}
90 90
 	err := c.ShouldBindJSON(j)
91 91
 	if err != nil {
92
-		response.Fail(c, 101, "参数错误"+err.Error())
92
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
93 93
 		return
94 94
 	}
95 95
 	if j.Code == "" {
96
-		response.Fail(c, 101, "参数错误: code 不存在")
96
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
97 97
 		return
98 98
 	}
99 99
 	v := service.AllService.OauthService.GetOauthCache(j.Code)
100 100
 	if v == nil {
101
-		response.Fail(c, 101, "授权已过期")
101
+		response.Fail(c, 101, response.TranslateMsg(c, "OauthExpired"))
102 102
 		return
103 103
 	}
104 104
 	u := service.AllService.UserService.CurUser(c)
105 105
 	err = service.AllService.OauthService.BindGithubUser(v.ThirdOpenId, v.ThirdOpenId, u.Id)
106 106
 	if err != nil {
107
-		response.Fail(c, 101, "绑定失败,请重试")
107
+		response.Fail(c, 101, response.TranslateMsg(c, "BindFail"))
108 108
 		return
109 109
 	}
110 110
 
@@ -117,22 +117,30 @@ func (o *Oauth) Unbind(c *gin.Context) {
117 117
 	f := &adminReq.UnBindOauthForm{}
118 118
 	err := c.ShouldBindJSON(f)
119 119
 	if err != nil {
120
-		response.Fail(c, 101, "参数错误")
120
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
121 121
 		return
122 122
 	}
123 123
 	u := service.AllService.UserService.CurUser(c)
124 124
 	utr := service.AllService.UserService.UserThirdInfo(u.Id, f.Op)
125 125
 	if utr.Id == 0 {
126
-		response.Fail(c, 101, "未绑定")
126
+		response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
127 127
 		return
128 128
 	}
129 129
 	if f.Op == model.OauthTypeGithub {
130 130
 		err = service.AllService.OauthService.UnBindGithubUser(u.Id)
131 131
 		if err != nil {
132
-			response.Fail(c, 101, "解绑失败")
132
+			response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
133 133
 			return
134 134
 		}
135 135
 	}
136
+	if f.Op == model.OauthTypeGoogle {
137
+		err = service.AllService.OauthService.UnBindGoogleUser(u.Id)
138
+		if err != nil {
139
+			response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
140
+			return
141
+		}
142
+	}
143
+
136 144
 	response.Success(c, nil)
137 145
 }
138 146
 
@@ -155,7 +163,7 @@ func (o *Oauth) Detail(c *gin.Context) {
155 163
 		response.Success(c, u)
156 164
 		return
157 165
 	}
158
-	response.Fail(c, 101, "信息不存在")
166
+	response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
159 167
 	return
160 168
 }
161 169
 
@@ -173,10 +181,10 @@ func (o *Oauth) Detail(c *gin.Context) {
173 181
 func (o *Oauth) Create(c *gin.Context) {
174 182
 	f := &admin.OauthForm{}
175 183
 	if err := c.ShouldBindJSON(f); err != nil {
176
-		response.Fail(c, 101, "参数错误"+err.Error())
184
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
177 185
 		return
178 186
 	}
179
-	errList := global.Validator.ValidStruct(f)
187
+	errList := global.Validator.ValidStruct(c, f)
180 188
 	if len(errList) > 0 {
181 189
 		response.Fail(c, 101, errList[0])
182 190
 		return
@@ -184,14 +192,14 @@ func (o *Oauth) Create(c *gin.Context) {
184 192
 
185 193
 	ex := service.AllService.OauthService.InfoByOp(f.Op)
186 194
 	if ex.Id > 0 {
187
-		response.Fail(c, 101, "已存在"+f.Op)
195
+		response.Fail(c, 101, response.TranslateMsg(c, "ItemExists"))
188 196
 		return
189 197
 	}
190 198
 
191 199
 	u := f.ToOauth()
192 200
 	err := service.AllService.OauthService.Create(u)
193 201
 	if err != nil {
194
-		response.Fail(c, 101, "创建失败")
202
+		response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
195 203
 		return
196 204
 	}
197 205
 	response.Success(c, u)
@@ -212,7 +220,7 @@ func (o *Oauth) Create(c *gin.Context) {
212 220
 func (o *Oauth) List(c *gin.Context) {
213 221
 	query := &admin.PageQuery{}
214 222
 	if err := c.ShouldBindQuery(query); err != nil {
215
-		response.Fail(c, 101, "参数错误")
223
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
216 224
 		return
217 225
 	}
218 226
 	res := service.AllService.OauthService.List(query.Page, query.PageSize, nil)
@@ -233,14 +241,14 @@ func (o *Oauth) List(c *gin.Context) {
233 241
 func (o *Oauth) Update(c *gin.Context) {
234 242
 	f := &admin.OauthForm{}
235 243
 	if err := c.ShouldBindJSON(f); err != nil {
236
-		response.Fail(c, 101, "参数错误")
244
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
237 245
 		return
238 246
 	}
239 247
 	if f.Id == 0 {
240
-		response.Fail(c, 101, "参数错误")
248
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
241 249
 		return
242 250
 	}
243
-	errList := global.Validator.ValidStruct(f)
251
+	errList := global.Validator.ValidStruct(c, f)
244 252
 	if len(errList) > 0 {
245 253
 		response.Fail(c, 101, errList[0])
246 254
 		return
@@ -248,7 +256,7 @@ func (o *Oauth) Update(c *gin.Context) {
248 256
 	u := f.ToOauth()
249 257
 	err := service.AllService.OauthService.Update(u)
250 258
 	if err != nil {
251
-		response.Fail(c, 101, "更新失败")
259
+		response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
252 260
 		return
253 261
 	}
254 262
 	response.Success(c, nil)
@@ -268,11 +276,11 @@ func (o *Oauth) Update(c *gin.Context) {
268 276
 func (o *Oauth) Delete(c *gin.Context) {
269 277
 	f := &admin.OauthForm{}
270 278
 	if err := c.ShouldBindJSON(f); err != nil {
271
-		response.Fail(c, 101, "系统错误")
279
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
272 280
 		return
273 281
 	}
274 282
 	id := f.Id
275
-	errList := global.Validator.ValidVar(id, "required,gt=0")
283
+	errList := global.Validator.ValidVar(c, id, "required,gt=0")
276 284
 	if len(errList) > 0 {
277 285
 		response.Fail(c, 101, errList[0])
278 286
 		return
@@ -287,5 +295,5 @@ func (o *Oauth) Delete(c *gin.Context) {
287 295
 		response.Fail(c, 101, err.Error())
288 296
 		return
289 297
 	}
290
-	response.Fail(c, 101, "信息不存在")
298
+	response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
291 299
 }

+ 13 - 13
http/controller/admin/peer.go

@@ -31,7 +31,7 @@ func (ct *Peer) Detail(c *gin.Context) {
31 31
 		response.Success(c, u)
32 32
 		return
33 33
 	}
34
-	response.Fail(c, 101, "信息不存在")
34
+	response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
35 35
 	return
36 36
 }
37 37
 
@@ -49,10 +49,10 @@ func (ct *Peer) Detail(c *gin.Context) {
49 49
 func (ct *Peer) Create(c *gin.Context) {
50 50
 	f := &admin.PeerForm{}
51 51
 	if err := c.ShouldBindJSON(f); err != nil {
52
-		response.Fail(c, 101, "参数错误")
52
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
53 53
 		return
54 54
 	}
55
-	errList := global.Validator.ValidStruct(f)
55
+	errList := global.Validator.ValidStruct(c, f)
56 56
 	if len(errList) > 0 {
57 57
 		response.Fail(c, 101, errList[0])
58 58
 		return
@@ -60,7 +60,7 @@ func (ct *Peer) Create(c *gin.Context) {
60 60
 	u := f.ToPeer()
61 61
 	err := service.AllService.PeerService.Create(u)
62 62
 	if err != nil {
63
-		response.Fail(c, 101, "创建失败")
63
+		response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
64 64
 		return
65 65
 	}
66 66
 	response.Success(c, u)
@@ -81,7 +81,7 @@ func (ct *Peer) Create(c *gin.Context) {
81 81
 func (ct *Peer) List(c *gin.Context) {
82 82
 	query := &admin.PageQuery{}
83 83
 	if err := c.ShouldBindQuery(query); err != nil {
84
-		response.Fail(c, 101, "参数错误")
84
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
85 85
 		return
86 86
 	}
87 87
 	res := service.AllService.PeerService.List(query.Page, query.PageSize, nil)
@@ -102,14 +102,14 @@ func (ct *Peer) List(c *gin.Context) {
102 102
 func (ct *Peer) Update(c *gin.Context) {
103 103
 	f := &admin.PeerForm{}
104 104
 	if err := c.ShouldBindJSON(f); err != nil {
105
-		response.Fail(c, 101, "参数错误")
105
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
106 106
 		return
107 107
 	}
108 108
 	if f.RowId == 0 {
109
-		response.Fail(c, 101, "参数错误")
109
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
110 110
 		return
111 111
 	}
112
-	errList := global.Validator.ValidStruct(f)
112
+	errList := global.Validator.ValidStruct(c, f)
113 113
 	if len(errList) > 0 {
114 114
 		response.Fail(c, 101, errList[0])
115 115
 		return
@@ -117,7 +117,7 @@ func (ct *Peer) Update(c *gin.Context) {
117 117
 	u := f.ToPeer()
118 118
 	err := service.AllService.PeerService.Update(u)
119 119
 	if err != nil {
120
-		response.Fail(c, 101, "更新失败")
120
+		response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
121 121
 		return
122 122
 	}
123 123
 	response.Success(c, nil)
@@ -137,11 +137,11 @@ func (ct *Peer) Update(c *gin.Context) {
137 137
 func (ct *Peer) Delete(c *gin.Context) {
138 138
 	f := &admin.PeerForm{}
139 139
 	if err := c.ShouldBindJSON(f); err != nil {
140
-		response.Fail(c, 101, "系统错误")
140
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
141 141
 		return
142 142
 	}
143 143
 	id := f.RowId
144
-	errList := global.Validator.ValidVar(id, "required,gt=0")
144
+	errList := global.Validator.ValidVar(c, id, "required,gt=0")
145 145
 	if len(errList) > 0 {
146 146
 		response.Fail(c, 101, errList[0])
147 147
 		return
@@ -153,8 +153,8 @@ func (ct *Peer) Delete(c *gin.Context) {
153 153
 			response.Success(c, nil)
154 154
 			return
155 155
 		}
156
-		response.Fail(c, 101, err.Error())
156
+		response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
157 157
 		return
158 158
 	}
159
-	response.Fail(c, 101, "信息不存在")
159
+	response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
160 160
 }

+ 15 - 15
http/controller/admin/tag.go

@@ -30,14 +30,14 @@ func (ct *Tag) Detail(c *gin.Context) {
30 30
 	t := service.AllService.TagService.InfoById(uint(iid))
31 31
 	u := service.AllService.UserService.CurUser(c)
32 32
 	if !service.AllService.UserService.IsAdmin(u) && t.UserId != u.Id {
33
-		response.Fail(c, 101, "无权限")
33
+		response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
34 34
 		return
35 35
 	}
36 36
 	if t.Id > 0 {
37 37
 		response.Success(c, t)
38 38
 		return
39 39
 	}
40
-	response.Fail(c, 101, "信息不存在")
40
+	response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
41 41
 	return
42 42
 }
43 43
 
@@ -55,10 +55,10 @@ func (ct *Tag) Detail(c *gin.Context) {
55 55
 func (ct *Tag) Create(c *gin.Context) {
56 56
 	f := &admin.TagForm{}
57 57
 	if err := c.ShouldBindJSON(f); err != nil {
58
-		response.Fail(c, 101, "参数错误")
58
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
59 59
 		return
60 60
 	}
61
-	errList := global.Validator.ValidStruct(f)
61
+	errList := global.Validator.ValidStruct(c, f)
62 62
 	if len(errList) > 0 {
63 63
 		response.Fail(c, 101, errList[0])
64 64
 		return
@@ -70,7 +70,7 @@ func (ct *Tag) Create(c *gin.Context) {
70 70
 	}
71 71
 	err := service.AllService.TagService.Create(t)
72 72
 	if err != nil {
73
-		response.Fail(c, 101, "创建失败")
73
+		response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
74 74
 		return
75 75
 	}
76 76
 	response.Success(c, u)
@@ -93,7 +93,7 @@ func (ct *Tag) Create(c *gin.Context) {
93 93
 func (ct *Tag) List(c *gin.Context) {
94 94
 	query := &admin.TagQuery{}
95 95
 	if err := c.ShouldBindQuery(query); err != nil {
96
-		response.Fail(c, 101, "参数错误")
96
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
97 97
 		return
98 98
 	}
99 99
 	u := service.AllService.UserService.CurUser(c)
@@ -122,27 +122,27 @@ func (ct *Tag) List(c *gin.Context) {
122 122
 func (ct *Tag) Update(c *gin.Context) {
123 123
 	f := &admin.TagForm{}
124 124
 	if err := c.ShouldBindJSON(f); err != nil {
125
-		response.Fail(c, 101, "参数错误")
125
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
126 126
 		return
127 127
 	}
128
-	errList := global.Validator.ValidStruct(f)
128
+	errList := global.Validator.ValidStruct(c, f)
129 129
 	if len(errList) > 0 {
130 130
 		response.Fail(c, 101, errList[0])
131 131
 		return
132 132
 	}
133 133
 	if f.Id == 0 {
134
-		response.Fail(c, 101, "参数错误")
134
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
135 135
 		return
136 136
 	}
137 137
 	t := f.ToTag()
138 138
 	u := service.AllService.UserService.CurUser(c)
139 139
 	if !service.AllService.UserService.IsAdmin(u) && t.UserId != u.Id {
140
-		response.Fail(c, 101, "无权限")
140
+		response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
141 141
 		return
142 142
 	}
143 143
 	err := service.AllService.TagService.Update(t)
144 144
 	if err != nil {
145
-		response.Fail(c, 101, "更新失败")
145
+		response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
146 146
 		return
147 147
 	}
148 148
 	response.Success(c, nil)
@@ -162,11 +162,11 @@ func (ct *Tag) Update(c *gin.Context) {
162 162
 func (ct *Tag) Delete(c *gin.Context) {
163 163
 	f := &admin.TagForm{}
164 164
 	if err := c.ShouldBindJSON(f); err != nil {
165
-		response.Fail(c, 101, "系统错误")
165
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
166 166
 		return
167 167
 	}
168 168
 	id := f.Id
169
-	errList := global.Validator.ValidVar(id, "required,gt=0")
169
+	errList := global.Validator.ValidVar(c, id, "required,gt=0")
170 170
 	if len(errList) > 0 {
171 171
 		response.Fail(c, 101, errList[0])
172 172
 		return
@@ -174,7 +174,7 @@ func (ct *Tag) Delete(c *gin.Context) {
174 174
 	t := service.AllService.TagService.InfoById(f.Id)
175 175
 	u := service.AllService.UserService.CurUser(c)
176 176
 	if !service.AllService.UserService.IsAdmin(u) && t.UserId != u.Id {
177
-		response.Fail(c, 101, "无权限")
177
+		response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
178 178
 		return
179 179
 	}
180 180
 	if u.Id > 0 {
@@ -186,5 +186,5 @@ func (ct *Tag) Delete(c *gin.Context) {
186 186
 		response.Fail(c, 101, err.Error())
187 187
 		return
188 188
 	}
189
-	response.Fail(c, 101, "信息不存在")
189
+	response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
190 190
 }

+ 20 - 20
http/controller/admin/user.go

@@ -33,7 +33,7 @@ func (ct *User) Detail(c *gin.Context) {
33 33
 		response.Success(c, u)
34 34
 		return
35 35
 	}
36
-	response.Fail(c, 101, "信息不存在")
36
+	response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
37 37
 	return
38 38
 }
39 39
 
@@ -51,10 +51,10 @@ func (ct *User) Detail(c *gin.Context) {
51 51
 func (ct *User) Create(c *gin.Context) {
52 52
 	f := &admin.UserForm{}
53 53
 	if err := c.ShouldBindJSON(f); err != nil {
54
-		response.Fail(c, 101, "参数错误")
54
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
55 55
 		return
56 56
 	}
57
-	errList := global.Validator.ValidStruct(f)
57
+	errList := global.Validator.ValidStruct(c, f)
58 58
 	if len(errList) > 0 {
59 59
 		response.Fail(c, 101, errList[0])
60 60
 		return
@@ -62,7 +62,7 @@ func (ct *User) Create(c *gin.Context) {
62 62
 	u := f.ToUser()
63 63
 	err := service.AllService.UserService.Create(u)
64 64
 	if err != nil {
65
-		response.Fail(c, 101, "创建失败")
65
+		response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
66 66
 		return
67 67
 	}
68 68
 	response.Success(c, u)
@@ -84,7 +84,7 @@ func (ct *User) Create(c *gin.Context) {
84 84
 func (ct *User) List(c *gin.Context) {
85 85
 	query := &admin.UserQuery{}
86 86
 	if err := c.ShouldBindQuery(query); err != nil {
87
-		response.Fail(c, 101, "参数错误")
87
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
88 88
 		return
89 89
 	}
90 90
 	res := service.AllService.UserService.List(query.Page, query.PageSize, func(tx *gorm.DB) {
@@ -109,14 +109,14 @@ func (ct *User) List(c *gin.Context) {
109 109
 func (ct *User) Update(c *gin.Context) {
110 110
 	f := &admin.UserForm{}
111 111
 	if err := c.ShouldBindJSON(f); err != nil {
112
-		response.Fail(c, 101, "参数错误:"+err.Error())
112
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
113 113
 		return
114 114
 	}
115 115
 	if f.Id == 0 {
116
-		response.Fail(c, 101, "参数错误")
116
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
117 117
 		return
118 118
 	}
119
-	errList := global.Validator.ValidStruct(f)
119
+	errList := global.Validator.ValidStruct(c, f)
120 120
 	if len(errList) > 0 {
121 121
 		response.Fail(c, 101, errList[0])
122 122
 		return
@@ -124,7 +124,7 @@ func (ct *User) Update(c *gin.Context) {
124 124
 	u := f.ToUser()
125 125
 	err := service.AllService.UserService.Update(u)
126 126
 	if err != nil {
127
-		response.Fail(c, 101, "更新失败")
127
+		response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
128 128
 		return
129 129
 	}
130 130
 	response.Success(c, nil)
@@ -144,11 +144,11 @@ func (ct *User) Update(c *gin.Context) {
144 144
 func (ct *User) Delete(c *gin.Context) {
145 145
 	f := &admin.UserForm{}
146 146
 	if err := c.ShouldBindJSON(f); err != nil {
147
-		response.Fail(c, 101, "系统错误")
147
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
148 148
 		return
149 149
 	}
150 150
 	id := f.Id
151
-	errList := global.Validator.ValidVar(id, "required,gt=0")
151
+	errList := global.Validator.ValidVar(c, id, "required,gt=0")
152 152
 	if len(errList) > 0 {
153 153
 		response.Fail(c, 101, errList[0])
154 154
 		return
@@ -163,7 +163,7 @@ func (ct *User) Delete(c *gin.Context) {
163 163
 		response.Fail(c, 101, err.Error())
164 164
 		return
165 165
 	}
166
-	response.Fail(c, 101, "信息不存在")
166
+	response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
167 167
 }
168 168
 
169 169
 // UpdatePassword 修改密码
@@ -180,22 +180,22 @@ func (ct *User) Delete(c *gin.Context) {
180 180
 func (ct *User) UpdatePassword(c *gin.Context) {
181 181
 	f := &admin.UserPasswordForm{}
182 182
 	if err := c.ShouldBindJSON(f); err != nil {
183
-		response.Fail(c, 101, "参数错误")
183
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
184 184
 		return
185 185
 	}
186
-	errList := global.Validator.ValidStruct(f)
186
+	errList := global.Validator.ValidStruct(c, f)
187 187
 	if len(errList) > 0 {
188 188
 		response.Fail(c, 101, errList[0])
189 189
 		return
190 190
 	}
191 191
 	u := service.AllService.UserService.InfoById(f.Id)
192 192
 	if u.Id == 0 {
193
-		response.Fail(c, 101, "信息不存在")
193
+		response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
194 194
 		return
195 195
 	}
196 196
 	err := service.AllService.UserService.UpdatePassword(u, f.Password)
197 197
 	if err != nil {
198
-		response.Fail(c, 101, "更新失败")
198
+		response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
199 199
 		return
200 200
 	}
201 201
 	response.Success(c, nil)
@@ -237,11 +237,11 @@ func (ct *User) Current(c *gin.Context) {
237 237
 func (ct *User) ChangeCurPwd(c *gin.Context) {
238 238
 	f := &admin.ChangeCurPasswordForm{}
239 239
 	if err := c.ShouldBindJSON(f); err != nil {
240
-		response.Fail(c, 101, "参数错误")
240
+		response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
241 241
 		return
242 242
 	}
243 243
 
244
-	errList := global.Validator.ValidStruct(f)
244
+	errList := global.Validator.ValidStruct(c, f)
245 245
 	if len(errList) > 0 {
246 246
 		response.Fail(c, 101, errList[0])
247 247
 		return
@@ -249,12 +249,12 @@ func (ct *User) ChangeCurPwd(c *gin.Context) {
249 249
 	u := service.AllService.UserService.CurUser(c)
250 250
 	oldPwd := service.AllService.UserService.EncryptPassword(f.OldPassword)
251 251
 	if u.Password != oldPwd {
252
-		response.Fail(c, 101, "旧密码错误")
252
+		response.Fail(c, 101, response.TranslateMsg(c, "OldPasswordError"))
253 253
 		return
254 254
 	}
255 255
 	err := service.AllService.UserService.UpdatePassword(u, f.NewPassword)
256 256
 	if err != nil {
257
-		response.Fail(c, 101, "更新失败")
257
+		response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
258 258
 		return
259 259
 	}
260 260
 	response.Success(c, nil)

+ 32 - 39
http/controller/api/ab.go

@@ -68,38 +68,31 @@ func (a *Ab) UpAb(c *gin.Context) {
68 68
 	abf := &requstform.AddressBookForm{}
69 69
 	err := c.ShouldBindJSON(&abf)
70 70
 	if err != nil {
71
-		response.Error(c, "参数错误")
71
+		response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
72 72
 		return
73 73
 	}
74 74
 	abd := &requstform.AddressBookFormData{}
75 75
 	err = json.Unmarshal([]byte(abf.Data), abd)
76 76
 	if err != nil {
77
-		response.Error(c, "系统错误")
77
+		response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
78 78
 		return
79 79
 	}
80
-
81
-	//fmt.Println(abd)
82
-	//for _, peer := range abd.Peers {
83
-	//	fmt.Println(peer)
84
-	//}
85
-
86
-	user := service.AllService.UserService.CurUser(c)
87
-
88
-	err = service.AllService.AddressBookService.UpdateAddressBook(abd.Peers, user.Id)
80
+	tc := map[string]uint{}
81
+	err = json.Unmarshal([]byte(abd.TagColors), &tc)
89 82
 	if err != nil {
90
-		c.Abort()
83
+		response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
91 84
 		return
92 85
 	}
86
+	user := service.AllService.UserService.CurUser(c)
93 87
 
94
-	tc := map[string]uint{}
95
-	err = json.Unmarshal([]byte(abd.TagColors), &tc)
88
+	err = service.AllService.AddressBookService.UpdateAddressBook(abd.Peers, user.Id)
96 89
 	if err != nil {
97
-		response.Error(c, "系统错误")
90
+		response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
98 91
 		return
99
-	} else {
100
-		service.AllService.TagService.UpdateTags(user.Id, tc)
101 92
 	}
102 93
 
94
+	service.AllService.TagService.UpdateTags(user.Id, tc)
95
+
103 96
 	c.JSON(http.StatusOK, nil)
104 97
 }
105 98
 
@@ -134,19 +127,19 @@ func (a *Ab) TagAdd(c *gin.Context) {
134 127
 	t := &model.Tag{}
135 128
 	err := c.ShouldBindJSON(t)
136 129
 	if err != nil {
137
-		response.Error(c, "参数错误")
130
+		response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
138 131
 		return
139 132
 	}
140 133
 	u := service.AllService.UserService.CurUser(c)
141 134
 	tag := service.AllService.TagService.InfoByUserIdAndName(u.Id, t.Name)
142 135
 	if tag != nil && tag.Id != 0 {
143
-		response.Error(c, "已存在")
136
+		response.Error(c, response.TranslateMsg(c, "ItemExists"))
144 137
 		return
145 138
 	}
146 139
 	t.UserId = u.Id
147 140
 	err = service.AllService.TagService.Create(t)
148 141
 	if err != nil {
149
-		response.Error(c, "操作失败")
142
+		response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
150 143
 		return
151 144
 	}
152 145
 	c.String(http.StatusOK, "")
@@ -166,24 +159,24 @@ func (a *Ab) TagRename(c *gin.Context) {
166 159
 	t := &requstform.TagRenameForm{}
167 160
 	err := c.ShouldBindJSON(t)
168 161
 	if err != nil {
169
-		response.Error(c, "参数错误")
162
+		response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
170 163
 		return
171 164
 	}
172 165
 	u := service.AllService.UserService.CurUser(c)
173 166
 	tag := service.AllService.TagService.InfoByUserIdAndName(u.Id, t.Old)
174 167
 	if tag == nil || tag.Id == 0 {
175
-		response.Error(c, "参数错误")
168
+		response.Error(c, response.TranslateMsg(c, "ItemNotFound"))
176 169
 		return
177 170
 	}
178 171
 	ntag := service.AllService.TagService.InfoByUserIdAndName(u.Id, t.New)
179 172
 	if ntag != nil && ntag.Id != 0 {
180
-		response.Error(c, "已存在")
173
+		response.Error(c, response.TranslateMsg(c, "ItemExists"))
181 174
 		return
182 175
 	}
183 176
 	tag.Name = t.New
184 177
 	err = service.AllService.TagService.Update(tag)
185 178
 	if err != nil {
186
-		response.Error(c, "操作失败")
179
+		response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
187 180
 		return
188 181
 	}
189 182
 	c.String(http.StatusOK, "")
@@ -203,19 +196,19 @@ func (a *Ab) TagUpdate(c *gin.Context) {
203 196
 	t := &requstform.TagColorForm{}
204 197
 	err := c.ShouldBindJSON(t)
205 198
 	if err != nil {
206
-		response.Error(c, "参数错误")
199
+		response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
207 200
 		return
208 201
 	}
209 202
 	u := service.AllService.UserService.CurUser(c)
210 203
 	tag := service.AllService.TagService.InfoByUserIdAndName(u.Id, t.Name)
211 204
 	if tag == nil || tag.Id == 0 {
212
-		response.Error(c, "参数错误")
205
+		response.Error(c, response.TranslateMsg(c, "ItemNotFound"))
213 206
 		return
214 207
 	}
215 208
 	tag.Color = t.Color
216 209
 	err = service.AllService.TagService.Update(tag)
217 210
 	if err != nil {
218
-		response.Error(c, "操作失败")
211
+		response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
219 212
 		return
220 213
 	}
221 214
 	c.String(http.StatusOK, "")
@@ -235,7 +228,7 @@ func (a *Ab) TagDel(c *gin.Context) {
235 228
 	t := &[]string{}
236 229
 	err := c.ShouldBind(t)
237 230
 	if err != nil {
238
-		response.Error(c, "参数错误")
231
+		response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
239 232
 		return
240 233
 	}
241 234
 	//fmt.Println(t)
@@ -243,12 +236,12 @@ func (a *Ab) TagDel(c *gin.Context) {
243 236
 	for _, name := range *t {
244 237
 		tag := service.AllService.TagService.InfoByUserIdAndName(u.Id, name)
245 238
 		if tag == nil || tag.Id == 0 {
246
-			response.Error(c, "参数错误")
239
+			response.Error(c, response.TranslateMsg(c, "ItemNotFound"))
247 240
 			return
248 241
 		}
249 242
 		err = service.AllService.TagService.Delete(tag)
250 243
 		if err != nil {
251
-			response.Error(c, "操作失败")
244
+			response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
252 245
 			return
253 246
 		}
254 247
 	}
@@ -406,7 +399,7 @@ func (a *Ab) PeerAdd(c *gin.Context) {
406 399
 	f := &requstform.PersonalAddressBookForm{}
407 400
 	err := c.ShouldBindJSON(f)
408 401
 	if err != nil {
409
-		response.Error(c, "参数错误"+err.Error())
402
+		response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
410 403
 		return
411 404
 	}
412 405
 	fmt.Println(f)
@@ -415,7 +408,7 @@ func (a *Ab) PeerAdd(c *gin.Context) {
415 408
 	ab := f.ToAddressBook()
416 409
 	err = service.AllService.AddressBookService.AddAddressBook(ab)
417 410
 	if err != nil {
418
-		response.Error(c, "操作失败")
411
+		response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
419 412
 		return
420 413
 	}
421 414
 	c.String(http.StatusOK, "")
@@ -436,19 +429,19 @@ func (a *Ab) PeerDel(c *gin.Context) {
436 429
 	f := &[]string{}
437 430
 	err := c.ShouldBind(f)
438 431
 	if err != nil {
439
-		response.Error(c, "参数错误")
432
+		response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
440 433
 		return
441 434
 	}
442 435
 	u := service.AllService.UserService.CurUser(c)
443 436
 	for _, id := range *f {
444 437
 		ab := service.AllService.AddressBookService.InfoByUserIdAndId(u.Id, id)
445 438
 		if ab == nil || ab.RowId == 0 {
446
-			response.Error(c, "参数错误")
439
+			response.Error(c, response.TranslateMsg(c, "ItemNotFound"))
447 440
 			return
448 441
 		}
449 442
 		err = service.AllService.AddressBookService.Delete(ab)
450 443
 		if err != nil {
451
-			response.Error(c, "操作失败")
444
+			response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
452 445
 			return
453 446
 		}
454 447
 	}
@@ -472,22 +465,22 @@ func (a *Ab) PeerUpdate(c *gin.Context) {
472 465
 	f := &requstform.PersonalAddressBookForm{}
473 466
 	err := c.ShouldBindJSON(f)
474 467
 	if err != nil {
475
-		response.Error(c, "参数错误")
468
+		response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
476 469
 		return
477 470
 	}
478
-	fmt.Println(f)
471
+	//fmt.Println(f)
479 472
 	//return
480 473
 	u := service.AllService.UserService.CurUser(c)
481 474
 	ab := service.AllService.AddressBookService.InfoByUserIdAndId(u.Id, f.Id)
482 475
 	if ab == nil || ab.RowId == 0 {
483
-		response.Error(c, "参数错误")
476
+		response.Error(c, response.TranslateMsg(c, "ItemNotFound"))
484 477
 		return
485 478
 	}
486 479
 	nab := f.ToAddressBook()
487 480
 	nab.RowId = ab.RowId
488 481
 	err = service.AllService.AddressBookService.Update(nab)
489 482
 	if err != nil {
490
-		response.Error(c, "操作失败")
483
+		response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
491 484
 		return
492 485
 	}
493 486
 	c.String(http.StatusOK, "")

+ 2 - 2
http/controller/api/group.go

@@ -33,7 +33,7 @@ func (g *Group) Users(c *gin.Context) {
33 33
 	if !*u.IsAdmin {
34 34
 		gr := service.AllService.GroupService.InfoById(u.GroupId)
35 35
 		if gr.Type != model.GroupTypeShare {
36
-			response.Error(c, "不是管理员也不在分享组")
36
+			response.Error(c, response.TranslateMsg(c, "NoAccess"))
37 37
 			return
38 38
 		}
39 39
 	}
@@ -77,7 +77,7 @@ func (g *Group) Peers(c *gin.Context) {
77 77
 	if !*u.IsAdmin {
78 78
 		gr := service.AllService.GroupService.InfoById(u.GroupId)
79 79
 		if gr.Type != model.GroupTypeShare {
80
-			response.Error(c, "不是管理员也不在分享组")
80
+			response.Error(c, response.TranslateMsg(c, "NoAccess"))
81 81
 			return
82 82
 		}
83 83
 	}

+ 1 - 1
http/controller/api/index.go

@@ -37,7 +37,7 @@ func (i *Index) Index(c *gin.Context) {
37 37
 func (i *Index) Heartbeat(c *gin.Context) {
38 38
 	//b := &gin.H{}
39 39
 	//err := c.BindJSON(b)
40
-	//body : &map[id:ljwzhuwo modified_at:0 uuid:NGIxZTZjM2YtNmNkMy00YTMwLWFiNjQtMzQ0MTA0NGE5ZDgz ver:1.003e+06]
40
+	//body : &map[id:xxx modified_at:0 uuid:NGIxZTZjM2YtNmNkMy00YTMwLWFiNjQtMzQ0MTA0NGE5ZDgz ver:1.003e+06]
41 41
 	//fmt.Println(b, err, c.Request.Header)
42 42
 	//header : map[Accept:[*/*] Accept-Encoding:[gzip] Content-Length:[105] Content-Type:[application/json]]
43 43
 	c.JSON(http.StatusOK, gin.H{})

+ 4 - 4
http/controller/api/login.go

@@ -30,11 +30,11 @@ func (l *Login) Login(c *gin.Context) {
30 30
 	err := c.ShouldBindJSON(f)
31 31
 	//fmt.Println(f)
32 32
 	if err != nil {
33
-		response.Error(c, "参数错误")
33
+		response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
34 34
 		return
35 35
 	}
36 36
 
37
-	errList := global.Validator.ValidStruct(f)
37
+	errList := global.Validator.ValidStruct(c, f)
38 38
 	if len(errList) > 0 {
39 39
 		response.Error(c, errList[0])
40 40
 		return
@@ -43,7 +43,7 @@ func (l *Login) Login(c *gin.Context) {
43 43
 	u := service.AllService.UserService.InfoByUsernamePassword(f.Username, f.Password)
44 44
 
45 45
 	if u.Id == 0 {
46
-		response.Error(c, "用户名或密码错误")
46
+		response.Error(c, response.TranslateMsg(c, "UsernameOrPasswordError"))
47 47
 		return
48 48
 	}
49 49
 
@@ -95,7 +95,7 @@ func (l *Login) LoginOptions(c *gin.Context) {
95 95
 	}
96 96
 	common, err := json.Marshal(oidcItems)
97 97
 	if err != nil {
98
-		response.Error(c, "参数错误")
98
+		response.Error(c, response.TranslateMsg(c, "SystemError")+err.Error())
99 99
 		return
100 100
 	}
101 101
 	var res []string

+ 38 - 45
http/controller/api/ouath.go

@@ -29,17 +29,17 @@ func (o *Oauth) OidcAuth(c *gin.Context) {
29 29
 	f := &api.OidcAuthRequest{}
30 30
 	err := c.ShouldBindJSON(&f)
31 31
 	if err != nil {
32
-		response.Error(c, "参数错误")
32
+		response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
33 33
 		return
34 34
 	}
35 35
 	if f.Op != model.OauthTypeWebauth && f.Op != model.OauthTypeGoogle && f.Op != model.OauthTypeGithub {
36
-		response.Error(c, "参数错误")
36
+		response.Error(c, response.TranslateMsg(c, "ParamsError"))
37 37
 		return
38 38
 	}
39 39
 
40 40
 	err, code, url := service.AllService.OauthService.BeginAuth(f.Op)
41 41
 	if err != nil {
42
-		response.Error(c, err.Error())
42
+		response.Error(c, response.TranslateMsg(c, err.Error()))
43 43
 		return
44 44
 	}
45 45
 
@@ -72,12 +72,12 @@ func (o *Oauth) OidcAuthQuery(c *gin.Context) {
72 72
 	q := &api.OidcAuthQuery{}
73 73
 	err := c.ShouldBindQuery(q)
74 74
 	if err != nil {
75
-		response.Error(c, "参数错误")
75
+		response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
76 76
 		return
77 77
 	}
78 78
 	v := service.AllService.OauthService.GetOauthCache(q.Code)
79 79
 	if v == nil {
80
-		response.Error(c, "授权已过期,请重新授权")
80
+		response.Error(c, response.TranslateMsg(c, "OauthExpired"))
81 81
 		return
82 82
 	}
83 83
 	if v.UserId == 0 {
@@ -87,24 +87,20 @@ func (o *Oauth) OidcAuthQuery(c *gin.Context) {
87 87
 	}
88 88
 	u := service.AllService.UserService.InfoById(v.UserId)
89 89
 	//fmt.Println("auth success u", u)
90
-	if u.Id > 0 {
91
-		service.AllService.OauthService.DeleteOauthCache(q.Code)
92
-		ut := service.AllService.UserService.Login(u, &model.LoginLog{
93
-			UserId:   u.Id,
94
-			Client:   v.DeviceType,
95
-			Uuid:     v.Uuid,
96
-			Ip:       c.ClientIP(),
97
-			Type:     model.LoginLogTypeOauth,
98
-			Platform: v.DeviceOs,
99
-		})
100
-		c.JSON(http.StatusOK, apiResp.LoginRes{
101
-			AccessToken: ut.Token,
102
-			Type:        "access_token",
103
-			User:        *(&apiResp.UserPayload{}).FromUser(u),
104
-		})
105
-		return
106
-	}
107
-	response.Error(c, "用户不存在")
90
+	service.AllService.OauthService.DeleteOauthCache(q.Code)
91
+	ut := service.AllService.UserService.Login(u, &model.LoginLog{
92
+		UserId:   u.Id,
93
+		Client:   v.DeviceType,
94
+		Uuid:     v.Uuid,
95
+		Ip:       c.ClientIP(),
96
+		Type:     model.LoginLogTypeOauth,
97
+		Platform: v.DeviceOs,
98
+	})
99
+	c.JSON(http.StatusOK, apiResp.LoginRes{
100
+		AccessToken: ut.Token,
101
+		Type:        "access_token",
102
+		User:        *(&apiResp.UserPayload{}).FromUser(u),
103
+	})
108 104
 }
109 105
 
110 106
 // OauthCallback 回调
@@ -119,7 +115,7 @@ func (o *Oauth) OidcAuthQuery(c *gin.Context) {
119 115
 func (o *Oauth) OauthCallback(c *gin.Context) {
120 116
 	state := c.Query("state")
121 117
 	if state == "" {
122
-		c.String(http.StatusInternalServerError, "state为空")
118
+		c.String(http.StatusInternalServerError, response.TranslateParamMsg(c, "ParamIsEmpty", "state"))
123 119
 		return
124 120
 	}
125 121
 
@@ -127,7 +123,7 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
127 123
 	//从缓存中获取
128 124
 	v := service.AllService.OauthService.GetOauthCache(cacheKey)
129 125
 	if v == nil {
130
-		c.String(http.StatusInternalServerError, "授权已过期,请重新授权")
126
+		c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthExpired"))
131 127
 		return
132 128
 	}
133 129
 
@@ -138,34 +134,34 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
138 134
 		code := c.Query("code")
139 135
 		err, userData := service.AllService.OauthService.GithubCallback(code)
140 136
 		if err != nil {
141
-			c.String(http.StatusInternalServerError, "授权失败:"+err.Error())
137
+			c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthFailed")+response.TranslateMsg(c, err.Error()))
142 138
 			return
143 139
 		}
144 140
 		if ac == service.OauthActionTypeBind {
145 141
 			//fmt.Println("bind", ty, userData)
146 142
 			utr := service.AllService.OauthService.UserThirdInfo(ty, strconv.Itoa(userData.Id))
147 143
 			if utr.UserId > 0 {
148
-				c.String(http.StatusInternalServerError, "已经绑定其他账号")
144
+				c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthHasBindOtherUser"))
149 145
 				return
150 146
 			}
151 147
 			//绑定
152 148
 			u := service.AllService.UserService.InfoById(v.UserId)
153 149
 			if u == nil {
154
-				c.String(http.StatusInternalServerError, "用户不存在")
150
+				c.String(http.StatusInternalServerError, response.TranslateMsg(c, "ItemNotFound"))
155 151
 				return
156 152
 			}
157 153
 			//绑定github
158 154
 			err = service.AllService.OauthService.BindGithubUser(strconv.Itoa(userData.Id), userData.Login, v.UserId)
159 155
 			if err != nil {
160
-				c.String(http.StatusInternalServerError, "绑定失败")
156
+				c.String(http.StatusInternalServerError, response.TranslateMsg(c, "BindFail"))
161 157
 				return
162 158
 			}
163
-			c.String(http.StatusOK, "绑定成功")
159
+			c.String(http.StatusOK, response.TranslateMsg(c, "BindSuccess"))
164 160
 			return
165 161
 		} else if ac == service.OauthActionTypeLogin {
166 162
 			//登录
167 163
 			if v.UserId != 0 {
168
-				c.String(http.StatusInternalServerError, "授权已经成功")
164
+				c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthHasBeenSuccess"))
169 165
 				return
170 166
 			}
171 167
 			u := service.AllService.UserService.InfoByGithubId(strconv.Itoa(userData.Id))
@@ -183,19 +179,16 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
183 179
 				//自动注册
184 180
 				u = service.AllService.UserService.RegisterByGithub(userData.Login, strconv.Itoa(userData.Id))
185 181
 				if u.Id == 0 {
186
-					c.String(http.StatusInternalServerError, "注册失败")
182
+					c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthRegisterFailed"))
187 183
 					return
188 184
 				}
189 185
 			}
190 186
 
191 187
 			v.UserId = u.Id
192 188
 			service.AllService.OauthService.SetOauthCache(cacheKey, v, 0)
193
-			c.String(http.StatusOK, "授权成功")
189
+			c.String(http.StatusOK, response.TranslateMsg(c, "OauthSuccess"))
194 190
 			return
195 191
 		}
196
-		//返回js
197
-		c.Header("Content-Type", "text/html; charset=utf-8")
198
-		c.String(http.StatusOK, "授权错误")
199 192
 
200 193
 	}
201 194
 
@@ -203,7 +196,7 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
203 196
 		code := c.Query("code")
204 197
 		err, userData := service.AllService.OauthService.GoogleCallback(code)
205 198
 		if err != nil {
206
-			c.String(http.StatusInternalServerError, "授权失败:"+err.Error())
199
+			c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthFailed")+response.TranslateMsg(c, err.Error()))
207 200
 			return
208 201
 		}
209 202
 		//将空格替换成_
@@ -212,26 +205,26 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
212 205
 			//fmt.Println("bind", ty, userData)
213 206
 			utr := service.AllService.OauthService.UserThirdInfo(ty, userData.Email)
214 207
 			if utr.UserId > 0 {
215
-				c.String(http.StatusInternalServerError, "已经绑定其他账号")
208
+				c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthHasBindOtherUser"))
216 209
 				return
217 210
 			}
218 211
 			//绑定
219 212
 			u := service.AllService.UserService.InfoById(v.UserId)
220 213
 			if u == nil {
221
-				c.String(http.StatusInternalServerError, "用户不存在")
214
+				c.String(http.StatusInternalServerError, response.TranslateMsg(c, "ItemNotFound"))
222 215
 				return
223 216
 			}
224 217
 			//绑定
225 218
 			err = service.AllService.OauthService.BindGoogleUser(userData.Email, googleName, v.UserId)
226 219
 			if err != nil {
227
-				c.String(http.StatusInternalServerError, "绑定失败")
220
+				c.String(http.StatusInternalServerError, response.TranslateMsg(c, "BindFail"))
228 221
 				return
229 222
 			}
230
-			c.String(http.StatusOK, "绑定成功")
223
+			c.String(http.StatusOK, response.TranslateMsg(c, "BindSuccess"))
231 224
 			return
232 225
 		} else if ac == service.OauthActionTypeLogin {
233 226
 			if v.UserId != 0 {
234
-				c.String(http.StatusInternalServerError, "授权已经成功")
227
+				c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthHasBeenSuccess"))
235 228
 				return
236 229
 			}
237 230
 			u := service.AllService.UserService.InfoByGoogleEmail(userData.Email)
@@ -250,17 +243,17 @@ func (o *Oauth) OauthCallback(c *gin.Context) {
250 243
 				//自动注册
251 244
 				u = service.AllService.UserService.RegisterByGoogle(googleName, userData.Email)
252 245
 				if u.Id == 0 {
253
-					c.String(http.StatusInternalServerError, "注册失败")
246
+					c.String(http.StatusInternalServerError, response.TranslateMsg(c, "OauthRegisterFailed"))
254 247
 					return
255 248
 				}
256 249
 			}
257 250
 
258 251
 			v.UserId = u.Id
259 252
 			service.AllService.OauthService.SetOauthCache(cacheKey, v, 0)
260
-			c.String(http.StatusOK, "授权成功")
253
+			c.String(http.StatusOK, response.TranslateMsg(c, "OauthSuccess"))
261 254
 			return
262 255
 		}
263 256
 	}
264
-	c.String(http.StatusInternalServerError, "授权配置错误,请联系管理员")
257
+	c.String(http.StatusInternalServerError, response.TranslateMsg(c, "SystemError"))
265 258
 
266 259
 }

+ 2 - 2
http/controller/api/peer.go

@@ -27,7 +27,7 @@ func (p *Peer) SysInfo(c *gin.Context) {
27 27
 	f := &requstform.PeerForm{}
28 28
 	err := c.ShouldBindBodyWith(f, binding.JSON)
29 29
 	if err != nil {
30
-		response.Error(c, err.Error())
30
+		response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
31 31
 		return
32 32
 	}
33 33
 
@@ -36,7 +36,7 @@ func (p *Peer) SysInfo(c *gin.Context) {
36 36
 		pe = f.ToPeer()
37 37
 		err = service.AllService.PeerService.Create(pe)
38 38
 		if err != nil {
39
-			response.Error(c, err.Error())
39
+			response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
40 40
 			return
41 41
 		}
42 42
 	}

+ 1 - 1
http/middleware/rustauth.go

@@ -7,7 +7,7 @@ import (
7 7
 
8 8
 func RustAuth() gin.HandlerFunc {
9 9
 	return func(c *gin.Context) {
10
-
10
+		//fmt.Println(c.Request.Header)
11 11
 		//获取HTTP_AUTHORIZATION
12 12
 		token := c.GetHeader("Authorization")
13 13
 		if token == "" {

+ 1 - 1
http/request/admin/user.go

@@ -8,7 +8,7 @@ type UserForm struct {
8 8
 	Id       uint   `json:"id"`
9 9
 	Username string `json:"username" validate:"required,gte=4,lte=10"`
10 10
 	//Password string           `json:"password" validate:"required,gte=4,lte=20"`
11
-	Nickname string           `json:"nickname" validate:"required"`
11
+	Nickname string           `json:"nickname"`
12 12
 	Avatar   string           `json:"avatar"`
13 13
 	GroupId  uint             `json:"group_id" validate:"required"`
14 14
 	IsAdmin  *bool            `json:"is_admin" `

+ 48 - 0
http/response/response.go

@@ -1,7 +1,10 @@
1 1
 package response
2 2
 
3 3
 import (
4
+	"Gwen/global"
5
+	"fmt"
4 6
 	"github.com/gin-gonic/gin"
7
+	"github.com/nicksnyder/go-i18n/v2/i18n"
5 8
 	"net/http"
6 9
 )
7 10
 
@@ -51,3 +54,48 @@ type ServerConfigResponse struct {
51 54
 	RelayServer string `json:"relay_server"`
52 55
 	ApiServer   string `json:"api_server"`
53 56
 }
57
+
58
+func TranslateMsg(c *gin.Context, messageId string) string {
59
+	localizer := global.Localizer(c)
60
+	errMsg, err := localizer.LocalizeMessage(&i18n.Message{
61
+		ID: messageId,
62
+	})
63
+	if err != nil {
64
+		global.Logger.Warn("LocalizeMessage Error: " + err.Error())
65
+		errMsg = messageId
66
+	}
67
+	return errMsg
68
+}
69
+func TranslateTempMsg(c *gin.Context, messageId string, templateData map[string]interface{}) string {
70
+	localizer := global.Localizer(c)
71
+	errMsg, err := localizer.Localize(&i18n.LocalizeConfig{
72
+		DefaultMessage: &i18n.Message{
73
+			ID: messageId,
74
+		},
75
+		TemplateData: templateData,
76
+	})
77
+	if err != nil {
78
+		global.Logger.Warn("LocalizeMessage Error: " + err.Error())
79
+		errMsg = messageId
80
+	}
81
+	return errMsg
82
+}
83
+func TranslateParamMsg(c *gin.Context, messageId string, params ...string) string {
84
+	localizer := global.Localizer(c)
85
+	templateData := make(map[string]interface{})
86
+	for i, v := range params {
87
+		k := fmt.Sprintf("P%d", i)
88
+		templateData[k] = v
89
+	}
90
+	errMsg, err := localizer.Localize(&i18n.LocalizeConfig{
91
+		DefaultMessage: &i18n.Message{
92
+			ID: messageId,
93
+		},
94
+		TemplateData: templateData,
95
+	})
96
+	if err != nil {
97
+		global.Logger.Warn("LocalizeMessage Error: " + err.Error())
98
+		errMsg = messageId
99
+	}
100
+	return errMsg
101
+}

+ 111 - 0
resources/i18n/en.toml

@@ -0,0 +1,111 @@
1
+[Test]
2
+description = "test"
3
+one = "test1 "
4
+other = "Test2 {{.P0}}"
5
+
6
+[ParamsError]
7
+description = "Params validation failed."
8
+one = "Params validation failed."
9
+other = "Params validation failed."
10
+
11
+[OperationFailed]
12
+description = "OperationFailed."
13
+one = "the operation failed."
14
+other = "the operation failed."
15
+
16
+[OperationSuccess]
17
+description = "OperationSuccess."
18
+one = "the operation success."
19
+other = "the operation success."
20
+
21
+[ItemExists]
22
+description = "Item already exists."
23
+one = "Item already exists."
24
+other = "Item already exists."
25
+
26
+[ItemNotFound]
27
+description = "Item not found."
28
+one = "Item not found."
29
+other = "Item not found."
30
+
31
+[NoAccess]
32
+description = "No access."
33
+one = "No access."
34
+other = "No access."
35
+
36
+[UsernameOrPasswordError]
37
+description = "Username or password error."
38
+one = "Username or password error."
39
+other = "Username or password error."
40
+
41
+[SystemError]
42
+description = "System error."
43
+one = "System error."
44
+other = "System error."
45
+
46
+[ConfigNotFound]
47
+description = "Config not found."
48
+one = "Config not found."
49
+other = "Config not found."
50
+
51
+[OauthExpired]
52
+description = "Oauth expired."
53
+one = "Oauth expired, please try again."
54
+other = "Oauth expired,please try again."
55
+
56
+[OauthFailed]
57
+description = "Oauth failed."
58
+one = "Oauth failed."
59
+other = "Oauth failed."
60
+
61
+[OauthHasBindOtherUser]
62
+description = "Oauth has bind other user."
63
+one = "Oauth has bind other user."
64
+other = "Oauth has bind other user."
65
+
66
+[ParamIsEmpty]
67
+description = "Param is empty."
68
+one = "{{.P0}} is empty."
69
+other = "{{.P0}} is empty."
70
+
71
+[BindFail]
72
+description = "Bind fail."
73
+one = "Bind fail."
74
+other = "Bind fail."
75
+[BindSuccess]
76
+description = "Bind success."
77
+one = "Bind success."
78
+other = "Bind success."
79
+[OauthHasBeenSuccess]
80
+description = "Oauth has been success."
81
+one = "Oauth has been success."
82
+other = "Oauth has been success."
83
+[OauthSuccess]
84
+description = "Oauth success."
85
+one = "Oauth success."
86
+other = "Oauth success."
87
+[OauthRegisterSuccess]
88
+description = "Oauth register success."
89
+one = "Oauth register success."
90
+other = "Oauth register success."
91
+[OauthRegisterFailed]
92
+description = "Oauth register failed."
93
+one = "Oauth register failed."
94
+other = "Oauth register failed."
95
+[GetOauthTokenError]
96
+description = "Get oauth token error."
97
+one = "Get oauth token error."
98
+other = "Get oauth token error."
99
+[GetOauthUserInfoError]
100
+description = "Get oauth user info error."
101
+one = "Get oauth user info error."
102
+other = "Get oauth user info error."
103
+[DecodeOauthUserInfoError]
104
+description = "Decode oauth user info error."
105
+one = "Decode oauth user info error."
106
+other = "Decode oauth user info error."
107
+
108
+[OldPasswordError]
109
+description = "Old password error."
110
+one = "Old password error."
111
+other = "Old password error."

+ 112 - 0
resources/i18n/zh_CN.toml

@@ -0,0 +1,112 @@
1
+[Test]
2
+description = "test"
3
+one = "测试1 {{.P0}}"
4
+other = "测试2 {{.P0}}"
5
+
6
+[ParamsError]
7
+description = "Params validation failed."
8
+one = "参数错误。"
9
+other = "参数错误。"
10
+
11
+[OperationFailed]
12
+description = "OperationFailed."
13
+one = "操作失败。"
14
+other = "操作失败。"
15
+
16
+[OperationSuccess]
17
+description = "OperationSuccess."
18
+one = "操作成功。"
19
+other = "操作成功。"
20
+
21
+[ItemExists]
22
+description = "Item already exists."
23
+one = "数据已存在。"
24
+other = "数据已存在。"
25
+
26
+[ItemNotFound]
27
+description = "Item not found."
28
+one = "数据不存在。"
29
+other = "数据不存在。"
30
+
31
+[NoAccess]
32
+description = "No access."
33
+one = "无权限。"
34
+other = "无权限。"
35
+
36
+[UsernameOrPasswordError]
37
+description = "Username or password error."
38
+one = "用户名或密码错误。"
39
+other = "用户名或密码错误。"
40
+
41
+[SystemError]
42
+description = "System error."
43
+one = "系统错误。"
44
+other = "系统错误。"
45
+
46
+[ConfigNotFound]
47
+description = "Config not found."
48
+one = "配置不存在。"
49
+other = "配置不存在。"
50
+
51
+#授权过期
52
+[OauthExpired]
53
+description = "Oauth expired."
54
+one = "授权过期,请重新授权。"
55
+other = "授权过期,请重新授权。"
56
+
57
+[OauthFailed]
58
+description = "Oauth failed."
59
+one = "授权失败。"
60
+other = "授权失败。"
61
+
62
+[OauthHasBindOtherUser]
63
+description = "Oauth has bind other user."
64
+one = "授权已绑定其他用户。"
65
+other = "授权已绑定其他用户。"
66
+
67
+[ParamIsEmpty]
68
+description = "Param is empty."
69
+one = "{{.P0}} 为空。"
70
+other = "{{.P0}} 为空。"
71
+
72
+[BindFail]
73
+description = "Bind fail."
74
+one = "绑定失败。"
75
+other = "绑定失败。"
76
+[BindSuccess]
77
+description = "Bind success."
78
+one = "绑定成功。"
79
+other = "绑定成功。"
80
+[OauthHasBeenSuccess]
81
+description = "Oauth has been success."
82
+one = "授权已成功。"
83
+other = "授权已成功。"
84
+[OauthSuccess]
85
+description = "Oauth success."
86
+one = "授权成功。"
87
+other = "授权成功。"
88
+[OauthRegisterSuccess]
89
+description = "Oauth register success."
90
+one = "授权注册成功。"
91
+other = "授权注册成功。"
92
+[OauthRegisterFailed]
93
+description = "Oauth register failed."
94
+one = "授权注册失败。"
95
+other = "授权注册失败。"
96
+[GetOauthTokenError]
97
+description = "Get oauth token error."
98
+one = "获取授权token失败。"
99
+other = "获取授权token失败。"
100
+[GetOauthUserInfoError]
101
+description = "Get oauth user info error."
102
+one = "获取授权用户信息失败。"
103
+other = "获取授权用户信息失败。"
104
+[DecodeOauthUserInfoError]
105
+description = "Decode oauth user info error."
106
+one = "解析授权用户信息失败。"
107
+other = "解析授权用户信息失败。"
108
+
109
+[OldPasswordError]
110
+description = "Old password error."
111
+one = "旧密码错误。"
112
+other = "旧密码错误。"

+ 17 - 11
service/oauth.go

@@ -133,7 +133,7 @@ func (os *OauthService) BeginAuth(op string) (error error, code, url string) {
133 133
 		return err, code, conf.AuthCodeURL(code)
134 134
 	}
135 135
 
136
-	return errors.New("op错误"), code, ""
136
+	return err, code, ""
137 137
 }
138 138
 
139 139
 // GetOauthConfig 获取配置
@@ -141,7 +141,7 @@ func (os *OauthService) GetOauthConfig(op string) (error, *oauth2.Config) {
141 141
 	if op == model.OauthTypeGithub {
142 142
 		g := os.InfoByOp(model.OauthTypeGithub)
143 143
 		if g.Id == 0 || g.ClientId == "" || g.ClientSecret == "" || g.RedirectUrl == "" {
144
-			return errors.New("配置不存在"), nil
144
+			return errors.New("ConfigNotFound"), nil
145 145
 		}
146 146
 		return nil, &oauth2.Config{
147 147
 			ClientID:     g.ClientId,
@@ -154,7 +154,7 @@ func (os *OauthService) GetOauthConfig(op string) (error, *oauth2.Config) {
154 154
 	if op == model.OauthTypeGoogle {
155 155
 		g := os.InfoByOp(model.OauthTypeGoogle)
156 156
 		if g.Id == 0 || g.ClientId == "" || g.ClientSecret == "" || g.RedirectUrl == "" {
157
-			return errors.New("配置不存在"), nil
157
+			return errors.New("ConfigNotFound"), nil
158 158
 		}
159 159
 		return nil, &oauth2.Config{
160 160
 			ClientID:     g.ClientId,
@@ -164,7 +164,7 @@ func (os *OauthService) GetOauthConfig(op string) (error, *oauth2.Config) {
164 164
 			Scopes:       []string{"https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email"},
165 165
 		}
166 166
 	}
167
-	return errors.New("op错误"), nil
167
+	return errors.New("ConfigNotFound"), nil
168 168
 }
169 169
 
170 170
 func (os *OauthService) GithubCallback(code string) (error error, userData *GithubUserdata) {
@@ -175,7 +175,7 @@ func (os *OauthService) GithubCallback(code string) (error error, userData *Gith
175 175
 	token, err := oauthConfig.Exchange(context.Background(), code)
176 176
 	if err != nil {
177 177
 		global.Logger.Warn(fmt.Printf("oauthConfig.Exchange() failed: %s\n", err))
178
-		error = errors.New("获取token失败")
178
+		error = errors.New("GetOauthTokenError")
179 179
 		return
180 180
 	}
181 181
 
@@ -184,7 +184,7 @@ func (os *OauthService) GithubCallback(code string) (error error, userData *Gith
184 184
 	resp, err := client.Get("https://api.github.com/user")
185 185
 	if err != nil {
186 186
 		global.Logger.Warn("failed getting user info: %s\n", err)
187
-		error = errors.New("获取user info失败")
187
+		error = errors.New("GetOauthUserInfoError")
188 188
 		return
189 189
 	}
190 190
 	defer func(Body io.ReadCloser) {
@@ -197,7 +197,7 @@ func (os *OauthService) GithubCallback(code string) (error error, userData *Gith
197 197
 	// 在这里处理 GitHub 用户信息
198 198
 	if err = json.NewDecoder(resp.Body).Decode(&userData); err != nil {
199 199
 		global.Logger.Warn("failed decoding user info: %s\n", err)
200
-		error = errors.New("解析user info失败")
200
+		error = errors.New("DecodeOauthUserInfoError")
201 201
 		return
202 202
 	}
203 203
 	return
@@ -208,7 +208,7 @@ func (os *OauthService) GoogleCallback(code string) (error error, userData *Goog
208 208
 	token, err := oauthConfig.Exchange(context.Background(), code)
209 209
 	if err != nil {
210 210
 		global.Logger.Warn(fmt.Printf("oauthConfig.Exchange() failed: %s\n", err))
211
-		error = errors.New("获取token失败")
211
+		error = errors.New("GetOauthTokenError")
212 212
 		return
213 213
 	}
214 214
 	// 创建 HTTP 客户端,并将 access_token 添加到 Authorization 头中
@@ -216,7 +216,7 @@ func (os *OauthService) GoogleCallback(code string) (error error, userData *Goog
216 216
 	resp, err := client.Get("https://www.googleapis.com/oauth2/v2/userinfo")
217 217
 	if err != nil {
218 218
 		global.Logger.Warn("failed getting user info: %s\n", err)
219
-		error = errors.New("获取user info失败: " + err.Error())
219
+		error = errors.New("GetOauthUserInfoError")
220 220
 		return
221 221
 	}
222 222
 	defer func(Body io.ReadCloser) {
@@ -228,7 +228,7 @@ func (os *OauthService) GoogleCallback(code string) (error error, userData *Goog
228 228
 
229 229
 	if err = json.NewDecoder(resp.Body).Decode(&userData); err != nil {
230 230
 		global.Logger.Warn("failed decoding user info: %s\n", err)
231
-		error = errors.New("解析user info失败:" + err.Error())
231
+		error = errors.New("DecodeOauthUserInfoError")
232 232
 		return
233 233
 	}
234 234
 	return
@@ -258,7 +258,13 @@ func (os *OauthService) BindOauthUser(thirdType, openid, username string, userId
258 258
 }
259 259
 
260 260
 func (os *OauthService) UnBindGithubUser(userid uint) error {
261
-	return global.DB.Where("user_id = ? and third_type = ?", userid, model.OauthTypeGithub).Delete(&model.UserThird{}).Error
261
+	return os.UnBindThird(model.OauthTypeGithub, userid)
262
+}
263
+func (os *OauthService) UnBindGoogleUser(userid uint) error {
264
+	return os.UnBindThird(model.OauthTypeGoogle, userid)
265
+}
266
+func (os *OauthService) UnBindThird(thirdType string, userid uint) error {
267
+	return global.DB.Where("user_id = ? and third_type = ?", userid, thirdType).Delete(&model.UserThird{}).Error
262 268
 }
263 269
 
264 270
 // InfoById 根据id取用户信息