response.go 5.11 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5
package resp

import (
	"encoding/json"
	"git.hilo.cn/hilo-common/mycontext"
hujiebin's avatar
hujiebin committed
6
	"git.hilo.cn/hilo-common/mylogrus"
hujiebin's avatar
hujiebin committed
7
	"git.hilo.cn/hilo-common/utils"
hujiebin's avatar
hujiebin committed
8 9 10
	"github.com/gin-gonic/gin"
	"hilo-group/myerr"
	"net/http"
hujiebin's avatar
hujiebin committed
11
	"time"
hujiebin's avatar
hujiebin committed
12 13 14 15 16 17 18 19
)

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
20
	Edata            interface{} `json:"edata"`            // 数据
hujiebin's avatar
hujiebin committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34
}

/**
 * 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
35 36 37 38 39 40
	}
	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
hujiebin's avatar
hujiebin committed
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 115 116
	}
	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{}) {
	traceId, _ := c.Get(mycontext.TRACEID)
	if _traceId, ok := traceId.(string); ok {
		c.Header("X-Trace-ID", _traceId)
	}

	var userId uint64 = 0
	if strUserId, ok := c.Get(mycontext.USERID); ok {
		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)
	}
}
hujiebin's avatar
hujiebin committed
117 118 119 120 121 122 123 124 125

type Page struct {
	Total uint        `json:"total"`
	Index int         `json:"index"`
	Data  interface{} `json:"data"`
}

func ResponsePageOk(c *gin.Context, data interface{}, total uint, index int) {
	// always return http.StatusOK
hujiebin's avatar
hujiebin committed
126 127 128 129 130
	pageData := Page{
		Total: total,
		Index: index,
		Data:  data,
	}
hujiebin's avatar
hujiebin committed
131 132 133 134
	response := Response{
		Code:             myerr.GetSuccessCode(),
		Message:          myerr.GetSuccessMsg(),
		OperationMessage: myerr.GetSuccessMsg(),
hujiebin's avatar
hujiebin committed
135 136 137 138 139 140
	}
	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
hujiebin's avatar
hujiebin committed
141 142 143 144 145 146 147 148 149 150
	}
	printResponseBody(c, &response)

	beginTime := time.Now()
	c.JSON(http.StatusOK, response)
	timeDiff := time.Now().Sub(beginTime)

	traceId, _ := c.Get(mycontext.TRACEID)
	mylogrus.MyLog.Infof("ResponsePageOk traceId: %s, JSON takes %v", traceId, timeDiff)
}
hujiebin's avatar
hujiebin committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164

type PageRespBase struct {
	NextPageIndex int         `json:"nextPageIndex"`
	HasNextPage   bool        `json:"hasNextPage"`
	Data          interface{} `json:"data"` // 需要具体自定义
}

// 分页返回
// 客户端入参 PageReqBase
// 服务端返回 PageRespBase
func ResponsePageBaseOk(c *gin.Context, data interface{}, nextPageIndex int, hasNextPage bool) {
	if data == nil {
		data = make([]interface{}, 0)
	}
hujiebin's avatar
hujiebin committed
165 166 167 168 169
	pageData := PageRespBase{
		NextPageIndex: nextPageIndex,
		HasNextPage:   hasNextPage,
		Data:          data,
	}
hujiebin's avatar
hujiebin committed
170 171 172 173
	response := Response{
		Code:             myerr.GetSuccessCode(),
		Message:          myerr.GetSuccessMsg(),
		OperationMessage: myerr.GetSuccessMsg(),
hujiebin's avatar
hujiebin committed
174 175 176 177 178 179
	}
	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
hujiebin's avatar
hujiebin committed
180 181 182
	}
	c.JSON(http.StatusOK, response)
}