package micro import ( "encoding/json" "github.com/gogf/gf/v2/container/gvar" "github.com/gogf/gf/v2/util/gconv" ) const ( SUCCESS = 200 FAIL = 500 ERROR = 400 UNAUTHORIZED = 401 ) type Response struct { Code int64 `json:"code"` Message string `json:"message"` 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} } // Fail 失败 func Fail(msg string) *Response { return &Response{FAIL, msg, ""} } // Error 错误 func Error(msg string) *Response { return &Response{ERROR, msg, ""} } // Unauthorized 认证失败 func Unauthorized(msg string, data interface{}) *Response { return &Response{UNAUTHORIZED, msg, data} }