103 lines
2.2 KiB
Go
103 lines
2.2 KiB
Go
package controllers
|
|
|
|
import (
|
|
"PaymentCenter/app/constants/common"
|
|
"PaymentCenter/app/constants/errorcode"
|
|
"PaymentCenter/app/models/orderrequestlogmodel"
|
|
"PaymentCenter/app/services"
|
|
"PaymentCenter/app/utils"
|
|
"PaymentCenter/config"
|
|
"encoding/json"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
)
|
|
|
|
func ApiRes(c *gin.Context, data interface{}, code int, msg ...string) {
|
|
var log_id int64
|
|
message := ""
|
|
if utils.IsNil(data) {
|
|
data = struct{}{}
|
|
}
|
|
if len(msg) > 0 {
|
|
message = msg[0]
|
|
} else {
|
|
message = errorcode.GetMsg(code, "")
|
|
}
|
|
log, exists := GetApiLogId(c)
|
|
if exists {
|
|
log_id = log.(int64)
|
|
dataByte, _ := json.Marshal(data)
|
|
status := common.STATUS_ENABLE
|
|
if code == errorcode.Success {
|
|
status = common.STATUS_DISABLED
|
|
}
|
|
services.RequestLogUpdate(&orderrequestlogmodel.OrderRequestLog{
|
|
Id: log_id,
|
|
MerchantResponse: string(dataByte),
|
|
Status: status,
|
|
})
|
|
}
|
|
|
|
if code == errorcode.Success {
|
|
ApiSuccess(c, data, log_id, message)
|
|
} else {
|
|
ApiError(c, code, log_id, message)
|
|
}
|
|
}
|
|
|
|
func ApiSuccess(c *gin.Context, data interface{}, log_id int64, messageSlice ...string) {
|
|
var message string
|
|
if len(messageSlice) > 0 {
|
|
message = messageSlice[0]
|
|
}
|
|
if message == "" {
|
|
message = errorcode.GetMsg(errorcode.Success, c.GetHeader("local"))
|
|
}
|
|
if config.GetConf().Env == "production" {
|
|
c.String(http.StatusOK, EncriptJson(gin.H{
|
|
"code": errorcode.Success,
|
|
"message": message,
|
|
"data": data,
|
|
"trace_id": log_id,
|
|
}))
|
|
} else {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": errorcode.Success,
|
|
"message": message,
|
|
"data": data,
|
|
"trace_id": log_id,
|
|
})
|
|
}
|
|
|
|
c.Abort()
|
|
}
|
|
|
|
/**
|
|
* 失败时返回
|
|
*/
|
|
func ApiError(c *gin.Context, code int, log_id int64, msg ...string) {
|
|
message := ""
|
|
if len(msg) > 0 {
|
|
message = msg[0]
|
|
} else {
|
|
message = errorcode.GetMsg(code, "")
|
|
}
|
|
if config.GetConf().Env == "production" {
|
|
c.String(http.StatusOK, EncriptJson(gin.H{
|
|
"code": code,
|
|
"message": message,
|
|
"data": make(map[string]string),
|
|
"trace_id": log_id,
|
|
}))
|
|
} else {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": code,
|
|
"message": message,
|
|
"data": make(map[string]string),
|
|
"trace_id": log_id,
|
|
})
|
|
}
|
|
|
|
c.Abort()
|
|
}
|