response.go 4.85 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"
hujiebin's avatar
hujiebin committed
6
	"git.hilo.cn/hilo-common/utils"
chenweijian's avatar
chenweijian committed
7 8
	"github.com/gin-gonic/gin"
	"hilo-user/myerr"
hujiebin's avatar
hujiebin committed
9
	"hilo-user/req"
chenweijian's avatar
chenweijian committed
10 11 12 13 14 15 16 17 18
	"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"`             // 数据
hujiebin's avatar
hujiebin committed
19
	Edata            interface{} `json:"edata"`            // 加密数据
chenweijian's avatar
chenweijian committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
}

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(),
hujiebin's avatar
hujiebin committed
41 42 43 44 45 46
	}
	if _, ok := c.Get(mycontext.InnerEncrypt); ok {
		//response.Edata = utils.EncryptionData(data, []byte("484194d4d0f968a7"))
		response.Edata = utils.EncryptionData(data, []byte("hilo!@#$%^&*()_+"))
	} else {
		response.Data = data
chenweijian's avatar
chenweijian committed
47 48 49 50 51 52
	}
	printResponseBody(c, &response)

	c.JSON(http.StatusOK, response)
}

hujiebin's avatar
hujiebin committed
53 54 55 56 57 58
// 分页返回
// 客户端入参 req.PageReqBase
// 服务端返回 req.PageRespBase
func ResponsePageBaseOk(c *gin.Context, data interface{}, nextPageIndex int, hasNextPage bool) {
	if data == nil {
		data = make([]interface{}, 0)
chenweijian's avatar
chenweijian committed
59
	}
hujiebin's avatar
hujiebin committed
60 61 62 63 64
	pageData := req.PageRespBase{
		NextPageIndex: nextPageIndex,
		HasNextPage:   hasNextPage,
		Data:          data,
	}
hujiebin's avatar
hujiebin committed
65 66 67 68
	response := Response{
		Code:             myerr.GetSuccessCode(),
		Message:          myerr.GetSuccessMsg(),
		OperationMessage: myerr.GetSuccessMsg(),
hujiebin's avatar
hujiebin committed
69 70 71 72 73 74
	}
	if _, ok := c.Get(mycontext.InnerEncrypt); ok {
		//response.Edata = utils.EncryptionData(data, []byte("484194d4d0f968a7"))
		response.Edata = utils.EncryptionData(pageData, []byte("hilo!@#$%^&*()_+"))
	} else {
		response.Data = pageData
chenweijian's avatar
chenweijian committed
75 76 77 78
	}
	c.JSON(http.StatusOK, response)
}

chenweijian's avatar
chenweijian committed
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
// 分页返回
// 客户端入参 req.PageReqBase
// 服务端返回 req.PageRespBase
func ResponsePageOk(c *gin.Context, data interface{}, total int64, pageSize, pageIndex int) {
	if data == nil {
		data = make([]interface{}, 0)
	}
	nextPageIndex := 0
	hasNextPage := false
	if (pageIndex-1)*pageSize+pageSize-1 < int(total) {
		nextPageIndex = pageIndex + 1
		hasNextPage = true
	}
	response := Response{
		Code:             myerr.GetSuccessCode(),
		Message:          myerr.GetSuccessMsg(),
		OperationMessage: myerr.GetSuccessMsg(),
		Data: req.PageRespBase{
			NextPageIndex: nextPageIndex,
			HasNextPage:   hasNextPage,
			Data:          data,
		},
	}
	c.JSON(http.StatusOK, response)
}

chenweijian's avatar
chenweijian committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
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
151
	traceId, _ := c.Get(mycontext.TRACEID)
chenweijian's avatar
chenweijian committed
152 153 154 155 156
	if _traceId, ok := traceId.(string); ok {
		c.Header("X-Trace-ID", _traceId)
	}

	var userId uint64 = 0
hujiebin's avatar
hujiebin committed
157
	if strUserId, ok := c.Get(mycontext.USERID); ok {
chenweijian's avatar
chenweijian committed
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
		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)
	}
}