|
|
@@ -1,5 +1,11 @@
|
|
|
package micro
|
|
|
|
|
|
+import (
|
|
|
+ "encoding/json"
|
|
|
+ "github.com/gogf/gf/v2/container/gvar"
|
|
|
+ "github.com/gogf/gf/v2/util/gconv"
|
|
|
+)
|
|
|
+
|
|
|
const (
|
|
|
SUCCESS = 200
|
|
|
FAIL = 500
|
|
|
@@ -13,22 +19,59 @@ type Response struct {
|
|
|
Data any `json:"data"`
|
|
|
}
|
|
|
|
|
|
+// Succ 成功
|
|
|
+func (resp Response) SuccNil() {
|
|
|
+ resp.Succ("")
|
|
|
+}
|
|
|
+
|
|
|
+// Succ 成功
|
|
|
+func (resp Response) Succ(data any) {
|
|
|
+ resp.Code = SUCCESS
|
|
|
+ resp.Message = "success"
|
|
|
+ resp.Data = data
|
|
|
+}
|
|
|
+
|
|
|
+// IsSuccess 获取Data值转字符串
|
|
|
+func (resp Response) IsSuccess() bool {
|
|
|
+ return resp.Code == SUCCESS
|
|
|
+}
|
|
|
+
|
|
|
+// String 获取Data转字符串
|
|
|
+func (resp Response) String() string {
|
|
|
+ return gconv.String(resp.Data)
|
|
|
+}
|
|
|
+
|
|
|
+// Json 获取Data转JSON格式
|
|
|
+func (resp Response) Json() string {
|
|
|
+ str, _ := json.Marshal(resp)
|
|
|
+ return string(str)
|
|
|
+}
|
|
|
+
|
|
|
+// Get 获取Data值
|
|
|
+func (resp Response) Get(key string) *gvar.Var {
|
|
|
+ m := gconv.Map(resp.Data)
|
|
|
+ if m == nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ return gvar.New(m[key])
|
|
|
+}
|
|
|
+
|
|
|
// Success 成功
|
|
|
-func Success(data interface{}) Response {
|
|
|
- return Response{SUCCESS, "success", data}
|
|
|
+func Success(data interface{}) *Response {
|
|
|
+ return &Response{SUCCESS, "success", data}
|
|
|
}
|
|
|
|
|
|
// Fail 失败
|
|
|
-func Fail(msg string) Response {
|
|
|
- return Response{FAIL, msg, ""}
|
|
|
+func Fail(msg string) *Response {
|
|
|
+ return &Response{FAIL, msg, ""}
|
|
|
}
|
|
|
|
|
|
// Error 错误
|
|
|
-func Error(msg string) Response {
|
|
|
- return Response{ERROR, msg, ""}
|
|
|
+func Error(msg string) *Response {
|
|
|
+ return &Response{ERROR, msg, ""}
|
|
|
}
|
|
|
|
|
|
// Unauthorized 认证失败
|
|
|
-func Unauthorized(msg string, data interface{}) Response {
|
|
|
- return Response{UNAUTHORIZED, msg, data}
|
|
|
+func Unauthorized(msg string, data interface{}) *Response {
|
|
|
+ return &Response{UNAUTHORIZED, msg, data}
|
|
|
}
|