123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- package resp
- import (
- "encoding/json"
- "eta_gn/eta_obs/global"
- "eta_gn/eta_obs/utils"
- "fmt"
- "github.com/gin-gonic/gin"
- "strings"
- )
- var (
- OK_CODE = 200 //业务成功
- FAIL_CODE = 400 //业务错误
- TOKEN_ERROR_CODE = 401 //toke异常
- NO_AUTH = 403 //没有权限
- SPECIFIC_FAIL_CODE = 4001 // 业务指定错误
- )
- type ResultData struct {
- Code int `json:"code" description:"状态码"`
- Msg string `json:"msg" description:"提示信息"`
- Data interface{} `json:"data" description:"返回数据"`
- ErrMsg string `json:"-" description:"错误信息,不用返回给前端,只是做日志记录"`
- }
- func result(code int, resultData ResultData, c *gin.Context) {
- jsonByte, _ := json.Marshal(resultData)
- token := c.Request.Header.Get("Authorization")
- if token == "" {
- token = c.DefaultQuery("authorization", "")
- if token == "" {
- token = c.DefaultQuery("Authorization", "")
- }
- }
- logSlice := make([]string, 0)
- logSlice = append(logSlice, fmt.Sprint("Url:", c.Request.RequestURI))
- logSlice = append(logSlice, fmt.Sprint("Token:", token))
- //logSlice = append(logSlice, fmt.Sprint("resultData:", string(jsonByte)))
- //记录错误日志
- if resultData.ErrMsg != "" {
- logSlice = append(logSlice, fmt.Sprint("ErrMsg:", resultData.ErrMsg))
- //global.LOG.Info(strings.Join(logSlice, ";"))
- }
- // 测试环境不加密
- if global.CONFIG.Serve.RunMode == "debug" {
- global.LOG.Info(strings.Join(logSlice, ";"))
- c.JSON(code, resultData)
- } else {
- global.LOG.Info(strings.Join(logSlice, ";"))
- encryptResult := utils.DesBase64Encrypt(jsonByte, global.CONFIG.Serve.DesKey)
- c.JSON(code, string(encryptResult))
- }
- c.Abort()
- }
- // OK 操作成功
- func Ok(msg string, c *gin.Context) {
- resultData := ResultData{
- Code: OK_CODE,
- Msg: msg,
- }
- result(200, resultData, c)
- }
- // OkData 成功返回数据
- func OkData(msg string, data interface{}, c *gin.Context) {
- resultData := ResultData{
- Code: OK_CODE,
- Msg: msg,
- Data: data,
- }
- result(200, resultData, c)
- }
- // Fail 操作失败
- func Fail(msg string, c *gin.Context) {
- resultData := ResultData{
- Code: FAIL_CODE,
- Msg: msg,
- }
- result(200, resultData, c)
- }
- // FailData 成功返回数据
- func FailData(msg string, data interface{}, c *gin.Context) {
- resultData := ResultData{
- Code: FAIL_CODE,
- Msg: msg,
- Data: data,
- }
- result(200, resultData, c)
- }
- // Custom 自定义状态码+操作成功
- func Custom(code int, msg string, c *gin.Context) {
- resultData := ResultData{
- Code: code,
- Msg: msg,
- }
- result(200, resultData, c)
- }
- // CustomData 自定义状态码+返回数据
- func CustomData(code int, msg string, data interface{}, c *gin.Context) {
- resultData := ResultData{
- Code: code,
- Msg: msg,
- Data: data,
- }
- result(200, resultData, c)
- }
- // TokenError token异常
- func TokenError(data interface{}, message, errMsg string, c *gin.Context) {
- resultData := ResultData{
- Code: TOKEN_ERROR_CODE,
- Msg: message,
- Data: data,
- ErrMsg: errMsg,
- }
- result(200, resultData, c)
- }
- // AuthError 没有权限
- func AuthError(data interface{}, message string, c *gin.Context) {
- resultData := ResultData{
- Code: NO_AUTH,
- Msg: message,
- Data: data,
- }
- result(200, resultData, c)
- }
- // SpecificFail 业务指定错误
- func SpecificFail(data interface{}, message string, c *gin.Context) {
- resultData := ResultData{
- Code: SPECIFIC_FAIL_CODE,
- Msg: message,
- Data: data,
- }
- result(200, resultData, c)
- }
- // FailMsg 操作失败
- func FailMsg(msg, errMsg string, c *gin.Context) {
- resultData := ResultData{
- Code: FAIL_CODE,
- Msg: msg,
- ErrMsg: errMsg,
- }
- result(200, resultData, c)
- }
- // Gn4AOperationResult 国能4A响应结果
- type Gn4AOperationResult struct {
- RequestId string `json:"requestID" description:"请求唯一标识"`
- ReturnFlag bool `json:"returnFlag" description:"处理结果标识: true-成功; false-失败"`
- ReturnCode string `json:"returnCode" description:"返回结果编号, returnFlag为true时, returnCode为0, 否则为错误编码"`
- ReturnMsg string `json:"returnMsg" description:"返回结果信息, 可自定义"`
- }
- // Gn4AResultData 4A返回结果
- func Gn4AResultData(flag bool, code, msg string, c *gin.Context) {
- resultData := Gn4AOperationResult{
- ReturnFlag: flag,
- ReturnCode: code,
- ReturnMsg: msg,
- }
- gnResult(200, resultData, c)
- }
- func gnResult(code int, resultData Gn4AOperationResult, c *gin.Context) {
- logSlice := make([]string, 0)
- logSlice = append(logSlice, fmt.Sprint("Url:", c.Request.RequestURI))
- // 记录错误日志
- if !resultData.ReturnFlag {
- logSlice = append(logSlice, fmt.Sprintf("ErrCode: %s, ErrMsg: %s", resultData.ReturnCode, resultData.ReturnMsg))
- }
- global.LOG.Info(strings.Join(logSlice, ";"))
- c.JSON(code, resultData)
- c.Abort()
- }
|