代码拉取完成,页面将自动刷新
package response
import (
"gitee.com/xlizy/common-go/base/common_const"
"gitee.com/xlizy/common-go/base/enums/common_error"
"gitee.com/xlizy/common-go/utils/threadlocal"
"gitee.com/xlizy/common-go/utils/zlog"
"reflect"
"time"
)
type Response struct {
Success bool `json:"success"`
Code int32 `json:"code"`
Msg string `json:"msg,omitempty"`
ResponseTime string `json:"responseTime,omitempty"`
TraceId string `json:"traceId,omitempty"`
Data any `json:"data,omitempty"`
Extend map[string]string `json:"extend,omitempty"`
}
type PageResponse struct {
Total int64 `json:"total"`
Data any `json:"data"`
PageNum int `json:"pageNum"`
PageSize int `json:"pageSize"`
Pages int `json:"pages"`
Size int `json:"size"`
}
func Succ() *Response {
return &Response{Success: true, Code: 0, Msg: "", ResponseTime: time.Now().Format(common_const.DataFormat), TraceId: threadlocal.GetTraceId()}
}
func SuccMsg(msg string) *Response {
return &Response{Success: true, Code: 0, Msg: msg, ResponseTime: time.Now().Format(common_const.DataFormat), TraceId: threadlocal.GetTraceId(), Data: nil}
}
func Success(msg string, data any) *Response {
return &Response{Success: true, Code: 0, Msg: msg, ResponseTime: time.Now().Format(common_const.DataFormat), TraceId: threadlocal.GetTraceId(), Data: data}
}
func SuccessCus(msg string, data any, extend map[string]string) *Response {
return &Response{Success: true, Code: 0, Msg: msg, ResponseTime: time.Now().Format(common_const.DataFormat), TraceId: threadlocal.GetTraceId(), Data: data, Extend: extend}
}
func ErrorCus(code int32, msg string, data any) *Response {
return &Response{Success: false, Code: code, Msg: msg, ResponseTime: time.Now().Format(common_const.DataFormat), TraceId: threadlocal.GetTraceId(), Data: data}
}
func ErrorMsg(msg string) *Response {
return &Response{Success: false, Code: common_error.SYSTEM_ERROR.Code(), Msg: msg, ResponseTime: time.Now().Format(common_const.DataFormat), TraceId: threadlocal.GetTraceId(), Data: nil}
}
func Error(errType any, data any) (res *Response) {
res = &Response{Success: false, Code: common_error.SYS_ERR_ENUM_ERROR.Code(), Msg: common_error.SYS_ERR_ENUM_ERROR.Des(), ResponseTime: time.Now().Format(common_const.DataFormat), TraceId: threadlocal.GetTraceId(), Data: data}
defer func(r *Response) {
err := recover() // recover()内置函数,可以捕获到异常
if err != nil { //说明捕获到错误
}
}(res)
code := reflect.ValueOf(errType).MethodByName("Code").Call(nil)[0]
des := reflect.ValueOf(errType).MethodByName("Des").Call(nil)[0]
res.Code = int32(code.Int())
res.Msg = des.String()
return res
}
func RpcError(err error) *Response {
zlog.Info("调用rpc服务异常:{}", err.Error())
return &Response{
Success: false,
Code: common_error.RPC_EXECUTE_ERROR.Code(),
Msg: common_error.RPC_EXECUTE_ERROR.Des(),
ResponseTime: time.Now().Format(common_const.DataFormat),
TraceId: threadlocal.GetTraceId(),
}
}
func Page(total int64, data any, pageNum, pageSize, pages, size int) *PageResponse {
return &PageResponse{Total: total, Data: data, PageNum: pageNum, PageSize: pageSize, Pages: pages, Size: size}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。