代码拉取完成,页面将自动刷新
// Package response provides wrapper gin returns json data in the same format.
package response
import (
"encoding/json"
"fmt"
"net/http"
"gitee.com/jianguosun_admin/common_pkg/errcode"
"github.com/gin-gonic/gin"
)
// Result output data format
type Result struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
func newResp(code int, msg string, data interface{}) *Result {
resp := &Result{
Code: code,
Msg: msg,
}
// ensure that the data field is not nil on return, note that it is not nil when resp.data=[]interface {}, it is serialized to null
if data == nil {
resp.Data = &struct{}{}
} else {
resp.Data = data
}
return resp
}
var jsonContentType = []string{"application/json; charset=utf-8"}
func writeContentType(w http.ResponseWriter, value []string) {
header := w.Header()
if val := header["Content-Type"]; len(val) == 0 {
header["Content-Type"] = value
}
}
func writeJSON(c *gin.Context, code int, res interface{}) {
c.Writer.WriteHeader(code)
writeContentType(c.Writer, jsonContentType)
err := json.NewEncoder(c.Writer).Encode(res)
if err != nil {
fmt.Printf("json encode error, err = %s\n", err.Error())
}
}
func respJSONWithStatusCode(c *gin.Context, code int, msg string, data ...interface{}) {
var firstData interface{}
if len(data) > 0 {
firstData = data[0]
}
resp := newResp(code, msg, firstData)
writeJSON(c, code, resp)
}
// Output return json data by http status code
func Output(c *gin.Context, code int, msg ...interface{}) {
switch code {
case http.StatusOK:
respJSONWithStatusCode(c, http.StatusOK, "ok", msg...)
case http.StatusBadRequest:
respJSONWithStatusCode(c, http.StatusBadRequest, errcode.InvalidParams.Msg(), msg...)
case http.StatusUnauthorized:
respJSONWithStatusCode(c, http.StatusUnauthorized, errcode.Unauthorized.Msg(), msg...)
case http.StatusForbidden:
respJSONWithStatusCode(c, http.StatusForbidden, errcode.Forbidden.Msg(), msg...)
case http.StatusNotFound:
respJSONWithStatusCode(c, http.StatusNotFound, errcode.NotFound.Msg(), msg...)
case http.StatusRequestTimeout:
respJSONWithStatusCode(c, http.StatusRequestTimeout, errcode.Timeout.Msg(), msg...)
case http.StatusConflict:
respJSONWithStatusCode(c, http.StatusConflict, errcode.AlreadyExists.Msg(), msg...)
case http.StatusInternalServerError:
respJSONWithStatusCode(c, http.StatusInternalServerError, errcode.InternalServerError.Msg(), msg...)
case http.StatusTooManyRequests:
respJSONWithStatusCode(c, http.StatusTooManyRequests, errcode.LimitExceed.Msg(), msg...)
case http.StatusServiceUnavailable:
respJSONWithStatusCode(c, http.StatusServiceUnavailable, errcode.ServiceUnavailable.Msg(), msg...)
default:
respJSONWithStatusCode(c, code, http.StatusText(code), msg...)
}
}
// Out return json data by http status code, converted by errcode
func Out(c *gin.Context, err *errcode.Error, data ...interface{}) {
code := err.ToHTTPCode()
switch code {
case http.StatusOK:
respJSONWithStatusCode(c, http.StatusOK, "ok", data...)
case http.StatusBadRequest:
respJSONWithStatusCode(c, http.StatusBadRequest, err.Msg(), data...)
case http.StatusUnauthorized:
respJSONWithStatusCode(c, http.StatusUnauthorized, err.Msg(), data...)
case http.StatusForbidden:
respJSONWithStatusCode(c, http.StatusForbidden, err.Msg(), data...)
case http.StatusNotFound:
respJSONWithStatusCode(c, http.StatusNotFound, err.Msg(), data...)
case http.StatusRequestTimeout:
respJSONWithStatusCode(c, http.StatusRequestTimeout, err.Msg(), data...)
case http.StatusConflict:
respJSONWithStatusCode(c, http.StatusConflict, err.Msg(), data...)
case http.StatusInternalServerError:
respJSONWithStatusCode(c, http.StatusInternalServerError, err.Msg(), data...)
case http.StatusTooManyRequests:
respJSONWithStatusCode(c, http.StatusTooManyRequests, err.Msg(), data...)
case http.StatusServiceUnavailable:
respJSONWithStatusCode(c, http.StatusServiceUnavailable, err.Msg(), data...)
default:
respJSONWithStatusCode(c, http.StatusNotExtended, err.Msg(), data...)
}
}
// status code flat 200, custom error codes in data.code
func respJSONWith200(c *gin.Context, code int, msg string, data ...interface{}) {
var firstData interface{}
if len(data) > 0 {
firstData = data[0]
}
resp := newResp(code, msg, firstData)
writeJSON(c, http.StatusOK, resp)
}
// Success return success
func Success(c *gin.Context, data ...interface{}) {
respJSONWith200(c, 0, "ok", data...)
}
// Error return error
func Error(c *gin.Context, err *errcode.Error, data ...interface{}) {
respJSONWith200(c, err.Code(), err.Msg(), data...)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。