response.go 3.65 KB
Newer Older
chenweijian's avatar
chenweijian committed
1 2 3 4
package resp

import (
	"encoding/json"
hujiebin's avatar
hujiebin committed
5
	"git.hilo.cn/hilo-common/mycontext"
chenweijian's avatar
chenweijian committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	"github.com/gin-gonic/gin"
	"hilo-user/myerr"
	"net/http"
)

type Response struct {
	Code             uint16      `json:"code"`             // 错误码
	Message          interface{} `json:"message"`          // 消息
	MessageData      interface{} `json:"messageData"`      // 消息详情
	OperationMessage interface{} `json:"operationMessage"` // 操作消息
	Data             interface{} `json:"data"`             // 数据
}

type GameResponse struct {
	RetCode      uint16      `json:"ret_code"`
	RetMsg       string      `json:"ret_msg"`
	SdkErrorCode uint16      `json:"sdk_error_code"`
	Data         interface{} `json:"data"`
}

/**
 * HTTP输出json信息
 * param: *gin.Context
 * param: error        err
 * param: interface{}  data
 */
func ResponseOk(c *gin.Context, data interface{}) {
	// always return http.StatusOK
	response := Response{
		Code:             myerr.GetSuccessCode(),
		Message:          myerr.GetSuccessMsg(),
		OperationMessage: myerr.GetSuccessMsg(),
		Data:             data,
	}
	printResponseBody(c, &response)

	c.JSON(http.StatusOK, response)
}

func GameResponseOk(c *gin.Context, data interface{}) {
	// always return http.StatusOK
	response := GameResponse{
		RetCode:      0,
		RetMsg:       myerr.GetSuccessMsg(),
		SdkErrorCode: 0,
		Data:         data,
	}
	printResponseBody(c, &response)
	c.JSON(http.StatusOK, response)
}

func GameResponseFail(c *gin.Context, err *myerr.GameError) {
	// always return http.StatusOK
	response := GameResponse{
		RetCode:      err.Code(),
		RetMsg:       err.Error(),
		SdkErrorCode: err.Code(),
		Data:         nil,
	}
	printResponseBody(c, &response)
	c.JSON(http.StatusOK, response)
}

func ResponseWaring(c *gin.Context, waringError *myerr.WaringError) {
	response := Response{
		Code:             waringError.GetCode(),
		Message:          waringError.GetMsg(),
		OperationMessage: waringError.GetMsg(),
		Data:             nil,
	}
	printResponseBody(c, &response)
	c.JSON(http.StatusOK, response)
}

func ResponseSysError(c *gin.Context, sysError *myerr.SysError) {
	response := Response{
		Code:             sysError.GetCode(),
		Message:          sysError.GetMsg(),
		OperationMessage: sysError.GetMsg(),
		Data:             nil,
	}
	printResponseBody(c, &response)
	c.JSON(http.StatusOK, response)
}

func ResponseBusiness(c *gin.Context, businessError *myerr.BusinessError) {
	response := Response{
		Code:             businessError.GetCode(),
		Message:          businessError.GetMsg(),
		MessageData:      businessError.GetData(),
		OperationMessage: businessError.GetMsg(),
		Data:             nil,
	}
	printResponseBody(c, &response)
	c.JSON(http.StatusOK, response)
}

func ResponseErrWithString(c *gin.Context, msg interface{}) {
	response := Response{
		Code:             myerr.GetSysErrorCode(),
		Message:          msg,
		OperationMessage: msg,
		Data:             nil,
	}
	printResponseBody(c, &response)
	c.JSON(http.StatusOK, response)
}

func printResponseBody(c *gin.Context, response interface{}) {
hujiebin's avatar
hujiebin committed
115
	traceId, _ := c.Get(mycontext.TRACEID)
chenweijian's avatar
chenweijian committed
116 117 118 119 120
	if _traceId, ok := traceId.(string); ok {
		c.Header("X-Trace-ID", _traceId)
	}

	var userId uint64 = 0
hujiebin's avatar
hujiebin committed
121
	if strUserId, ok := c.Get(mycontext.USERID); ok {
chenweijian's avatar
chenweijian committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
		userId = strUserId.(uint64)
	}

	buf, err := json.Marshal(response)
	body := ""
	if len(buf) < 1024 {
		body = string(buf)
	} else {
		body = string(buf[0:1024])
	}

	if err == nil {
		mycontext.CreateMyContext(c.Keys).Log.Infof("request rsp url:%s, traceId:%v, userId:%d, body:%s", c.Request.RequestURI, traceId, userId, body)
	} else {
		mycontext.CreateMyContext(c.Keys).Log.Error("request rsp body Marshal fail traceId:%v, err:%v", traceId, err)
	}
}