代码拉取完成,页面将自动刷新
package jk
import (
"encoding/json"
"net/http"
)
type Resp interface {
SetCode(code int)
SetMsg(msg string)
SetData(data any)
SetOther(other any)
ToJson() []byte
Value() any //返回可以JSON编码的对象
CodeValue() (int, any) //返回http状态码code和可以JSON编码的对象
CodeValueOK() (int, any) //返回http状态码200和可以JSON编码的对象
}
// NewResp 创建一个标准的响应体结构
func NewResp(code int, msg string, data any, other any) Resp {
return &stdResp{
Code: code,
Msg: msg,
Data: stdRespData{Data: data, Other: other},
}
}
// NewRespOK 创建一个标准的响应体结构,默认code = 200
func NewRespOK(dataOther ...any) Resp {
var rt = &stdResp{Code: http.StatusOK}
length := len(dataOther)
switch length {
case 0:
case 1:
rt.Data.Data = dataOther[0]
case 2:
rt.Data.Data = dataOther[0]
rt.Data.Other = dataOther[1]
default:
rt.Data.Data = dataOther[0]
rt.Data.Other = dataOther[1:]
}
return rt
}
// NewRespErr 创建一个标准的响应体结构,默认无Data
func NewRespErr(code int, msg string) Resp {
return &stdResp{
Code: code,
Msg: msg,
}
}
type stdResp struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data stdRespData `json:"data"`
}
type stdRespData struct {
Data any `json:"data"`
Other any `json:"other"`
}
func (r *stdResp) SetCode(code int) {
r.Code = code
}
func (r *stdResp) SetMsg(msg string) {
r.Msg = msg
}
func (r *stdResp) SetData(data any) {
r.Data.Data = data
}
func (r *stdResp) SetOther(other any) {
r.Data.Other = other
}
func (r *stdResp) ToJson() []byte {
marshal, _ := json.Marshal(r)
return marshal
}
func (r *stdResp) Value() any {
return r
}
func (r *stdResp) CodeValue() (int, any) {
return r.Code, r
}
func (r *stdResp) CodeValueOK() (int, any) {
return http.StatusOK, r
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。