Files
nl-video-api/utility/response/response.go
2025-08-03 00:11:15 +08:00

146 lines
3.7 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package response
import (
"time"
"github.com/gogf/gf/v2/net/ghttp"
)
// Response 统一响应结构
type Response struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data"`
Timestamp int64 `json:"timestamp"`
}
// 状态码定义 - 与API文档保持一致
const (
CodeSuccess = 200 // 请求成功
CodeError = 400 // 通用错误/请求参数错误
CodeInvalidParam = 400 // 参数错误
CodeUnauthorized = 401 // 未授权
CodeForbidden = 403 // 权限不足
CodeNotFound = 404 // 资源不存在
CodeServerError = 500 // 服务器内部错误
CodeInternalError = 500 // 内部错误(别名)
CodeTokenExpired = 401 // Token过期
CodeTokenInvalid = 401 // Token无效
)
// 状态码对应消息
var codeMsg = map[int]string{
CodeSuccess: "success",
CodeError: "请求参数错误",
CodeUnauthorized: "未授权",
CodeForbidden: "权限不足",
CodeNotFound: "资源不存在",
CodeServerError: "服务器内部错误",
}
// Success 成功响应 - 始终返回HTTP 200
func Success(r *ghttp.Request, data interface{}) {
r.Response.Status = 200 // 强制设置HTTP状态码为200
r.Response.WriteJson(Response{
Code: 200,
Message: "success",
Data: data,
Timestamp: time.Now().Unix(),
})
}
// Error 错误响应 - 始终返回HTTP 200错误信息在code和message中
func Error(r *ghttp.Request, code int, msg ...string) {
message := codeMsg[code]
if len(msg) > 0 && msg[0] != "" {
message = msg[0]
}
r.Response.Status = 200 // 强制设置HTTP状态码为200
r.Response.WriteJson(Response{
Code: code,
Message: message,
Data: nil,
Timestamp: time.Now().Unix(),
})
}
// Json 自定义响应 - 始终返回HTTP 200
func Json(r *ghttp.Request, code int, msg string, data interface{}) {
r.Response.Status = 200 // 强制设置HTTP状态码为200
r.Response.WriteJson(Response{
Code: code,
Message: msg,
Data: data,
Timestamp: time.Now().Unix(),
})
}
// GetTimestamp 将time.Time转换为时间戳
func GetTimestamp(t time.Time) int64 {
return t.Unix()
}
// FormatTimestamp 将时间戳转换为时间戳(保持原样)
func FormatTimestamp(timestamp int) int64 {
if timestamp == 0 {
return 0
}
return int64(timestamp)
}
// FormatTimestampToDate 将时间戳转换为日期字符串
func FormatTimestampToDate(timestamp int) string {
if timestamp == 0 {
return ""
}
return time.Unix(int64(timestamp), 0).Format("2006-01-02")
}
// FormatUserResponse 格式化用户响应数据,将时间戳转换为格式化时间
func FormatUserResponse(user interface{}) map[string]interface{} {
result := make(map[string]interface{})
switch u := user.(type) {
case map[string]interface{}:
for k, v := range u {
switch k {
case "created_at", "updated_at", "last_login_time", "vip_expire_time":
if timestamp, ok := v.(int); ok {
result[k] = FormatTimestamp(timestamp)
} else {
result[k] = v
}
default:
result[k] = v
}
}
default:
// 如果不是map类型直接返回原数据
return map[string]interface{}{"data": user}
}
return result
}
// SuccessWithFormattedTime 成功响应并格式化时间字段
func SuccessWithFormattedTime(r *ghttp.Request, data interface{}) {
var formattedData interface{}
switch d := data.(type) {
case []interface{}:
// 处理数组数据
var formattedArray []interface{}
for _, item := range d {
formattedArray = append(formattedArray, FormatUserResponse(item))
}
formattedData = formattedArray
case map[string]interface{}:
// 处理单个对象
formattedData = FormatUserResponse(d)
default:
// 其他类型直接返回
formattedData = data
}
Success(r, formattedData)
}