06488f0237
功能: - Go后端 (Gin + GORM + PostgreSQL) - UniApp用户端 (iOS/Android/小程序) - DaisyUI5后台管理 - JWT认证 + 微信登录 - 盲选加权算法 - 会员系统 + 优惠券 - 打分评价 + 偏好学习
40 lines
845 B
Go
40 lines
845 B
Go
package middleware
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// JSONResponse helper
|
|
func JSONResponse(c *gin.Context, status int, data interface{}) {
|
|
c.JSON(status, gin.H{
|
|
"code": status / 100,
|
|
"message": getMessage(status),
|
|
"data": data,
|
|
})
|
|
}
|
|
|
|
// JSONError sends a JSON error response
|
|
func JSONError(c *gin.Context, status int, msg string) {
|
|
c.JSON(status, gin.H{
|
|
"code": status / 100,
|
|
"message": msg,
|
|
"data": nil,
|
|
})
|
|
}
|
|
|
|
func getMessage(status int) string {
|
|
switch status {
|
|
case 200: return "ok"
|
|
case 201: return "created"
|
|
case 400: return "bad request"
|
|
case 401: return "unauthorized"
|
|
case 403: return "forbidden"
|
|
case 404: return "not found"
|
|
case 409: return "conflict"
|
|
case 422: return "unprocessable"
|
|
case 429: return "too many requests"
|
|
case 500: return "internal error"
|
|
default: return "error"
|
|
}
|
|
}
|