|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- package e
-
- import (
- "applet/app/utils"
- "encoding/json"
- "net/http"
-
- "github.com/gin-gonic/gin"
-
- "applet/app/utils/logx"
- )
-
-
-
- func GetMsg(code int) (int, string) {
- if msg, ok := MsgFlags[code]; ok {
- return code / 1000, msg
- }
- if http.StatusText(code) == "" {
- code = 200
- }
- return code, MsgFlags[ERR_BAD_REQUEST]
- }
-
-
- func OutSuc(c *gin.Context, data interface{}, fields map[string]interface{}) {
- res := gin.H{
- "code": 1,
- "msg": "ok",
- "data": data,
- }
- if fields != nil {
- for k, v := range fields {
- res[k] = v
- }
- }
- if utils.GetApiVersion(c) > 0 {
- jsonData, _ := json.Marshal(res)
- str := utils.ResultAes(c, jsonData)
- c.Writer.WriteString(str)
- } else {
- c.AbortWithStatusJSON(200, res)
- }
- }
-
- func OutSucPure(c *gin.Context, data interface{}, fields map[string]interface{}) {
- res := gin.H{
- "code": 1,
- "msg": "ok",
- "data": data,
- }
- if fields != nil {
- for k, v := range fields {
- res[k] = v
- }
- }
- c.Abort()
- c.PureJSON(200, res)
- }
-
-
- func OutErr(c *gin.Context, code int, err ...interface{}) {
- statusCode, msg := GetMsg(code)
- if len(err) > 0 && err[0] != nil {
- e := err[0]
- switch v := e.(type) {
- case E:
- statusCode = v.Code / 1000
- msg = v.Error()
- logx.Error(v.msg + ": " + v.st)
- case error:
- logx.Error(v)
- break
- case string:
- msg = v
- case int:
- if _, ok := MsgFlags[v]; ok {
- msg = MsgFlags[v]
- }
- }
- }
- if utils.GetApiVersion(c) > 0 {
- jsonData, _ := json.Marshal(gin.H{
- "code": code,
- "msg": msg,
- "data": []struct{}{},
- })
- str := utils.ResultAes(c, jsonData)
- if code > 100000 {
- code = int(utils.FloatFormat(float64(code/1000), 0))
- }
- c.Status(500)
- c.Writer.WriteString(str)
- } else {
- c.AbortWithStatusJSON(statusCode, gin.H{
- "code": code,
- "msg": msg,
- "data": []struct{}{},
- })
- }
- }
-
-
- func OutRedirect(c *gin.Context, code int, loc string) {
- if code < 301 || code > 308 {
- code = 303
- }
- c.Redirect(code, loc)
- c.Abort()
- }
|