Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
d6b0b8ea36 | |||
23d7841ccf | |||
247490c35d |
@ -23,7 +23,7 @@ type option struct {
|
|||||||
|
|
||||||
func WithName(name string) Option {
|
func WithName(name string) Option {
|
||||||
return func(o *option) {
|
return func(o *option) {
|
||||||
if name == "" {
|
if name != "" {
|
||||||
o.name = name
|
o.name = name
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -31,7 +31,7 @@ func WithName(name string) Option {
|
|||||||
|
|
||||||
func WithVersion(version string) Option {
|
func WithVersion(version string) Option {
|
||||||
return func(o *option) {
|
return func(o *option) {
|
||||||
if version == "" {
|
if version != "" {
|
||||||
o.version = version
|
o.version = version
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -39,7 +39,7 @@ func WithVersion(version string) Option {
|
|||||||
|
|
||||||
func WithAddress(address string) Option {
|
func WithAddress(address string) Option {
|
||||||
return func(o *option) {
|
return func(o *option) {
|
||||||
if address == "" {
|
if address != "" {
|
||||||
o.address = address
|
o.address = address
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
19
resp/msg.go
19
resp/msg.go
@ -1,15 +1,16 @@
|
|||||||
package resp
|
package resp
|
||||||
|
|
||||||
const (
|
const (
|
||||||
Msg200 = "操作成功"
|
Msg200 = "操作成功"
|
||||||
Msg400 = "参数错误"
|
Msg400 = "参数错误"
|
||||||
Msg401 = "该账号登录已失效, 请重新登录"
|
Msg401 = "该账号登录已失效, 请重新登录"
|
||||||
Msg401NoMulti = "用户已在其他地方登录"
|
Msg401NoMulti = "用户已在其他地方登录"
|
||||||
Msg403 = "权限不足"
|
Msg401Inactive = "当前用户尚未生效, 请稍后再试"
|
||||||
Msg404 = "资源不存在"
|
Msg403 = "权限不足"
|
||||||
Msg500 = "服务器开小差了"
|
Msg404 = "资源不存在"
|
||||||
Msg501 = "服务不可用"
|
Msg500 = "服务器开小差了"
|
||||||
Msg503 = "服务不可用或正在升级, 请联系管理员"
|
Msg501 = "服务不可用"
|
||||||
|
Msg503 = "服务不可用或正在升级, 请联系管理员"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Msg(status int) string {
|
func Msg(status int) string {
|
||||||
|
52
resp/resp.go
52
resp/resp.go
@ -43,23 +43,45 @@ func RE(c *gin.Context, err error) {
|
|||||||
|
|
||||||
func _r(c *gin.Context, r *res, args ...any) {
|
func _r(c *gin.Context, r *res, args ...any) {
|
||||||
length := len(args)
|
length := len(args)
|
||||||
switch length {
|
|
||||||
case 0:
|
if length == 0 {
|
||||||
break
|
goto END
|
||||||
case 1:
|
|
||||||
if msg, ok := args[0].(string); ok {
|
|
||||||
r.Msg = msg
|
|
||||||
}
|
|
||||||
case 2:
|
|
||||||
r.Data = args[1]
|
|
||||||
case 3:
|
|
||||||
r.Err = args[2]
|
|
||||||
case 4:
|
|
||||||
if code, err := cast.ToIntE(args[3]); err == nil {
|
|
||||||
r.Code = code
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if length >= 4 {
|
||||||
|
goto H4
|
||||||
|
}
|
||||||
|
|
||||||
|
if length >= 3 {
|
||||||
|
goto H3
|
||||||
|
}
|
||||||
|
|
||||||
|
if length >= 2 {
|
||||||
|
goto H2
|
||||||
|
}
|
||||||
|
if length >= 1 {
|
||||||
|
goto H1
|
||||||
|
}
|
||||||
|
|
||||||
|
H4:
|
||||||
|
if code, err := cast.ToIntE(args[3]); err == nil {
|
||||||
|
r.Code = code
|
||||||
|
}
|
||||||
|
H3:
|
||||||
|
if es, ok := args[2].(error); ok {
|
||||||
|
r.Err = es.Error()
|
||||||
|
} else {
|
||||||
|
r.Err = args[2]
|
||||||
|
}
|
||||||
|
H2:
|
||||||
|
r.Data = args[1]
|
||||||
|
H1:
|
||||||
|
if msg, ok := args[0].(string); ok {
|
||||||
|
r.Msg = msg
|
||||||
|
}
|
||||||
|
|
||||||
|
END:
|
||||||
|
|
||||||
if r.Msg == "" {
|
if r.Msg == "" {
|
||||||
r.Msg = Msg(r.Status)
|
r.Msg = Msg(r.Status)
|
||||||
}
|
}
|
||||||
|
9
resp/resp_test.go
Normal file
9
resp/resp_test.go
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package resp
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestResp(t *testing.T) {
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user