2026-07-15 14:05:22 +08:00
|
|
|
package response
|
2026-07-15 10:44:32 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Response struct {
|
2026-07-15 14:05:22 +08:00
|
|
|
Code int `json:"code"`
|
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
Data interface{} `json:"data,omitempty"`
|
|
|
|
|
Details string `json:"details,omitempty"`
|
2026-07-15 10:44:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type PaginatedData struct {
|
|
|
|
|
Total int64 `json:"total"`
|
|
|
|
|
Items interface{} `json:"items"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
CodeSuccess = 0
|
|
|
|
|
|
2026-07-15 14:05:22 +08:00
|
|
|
CodeLDAPAuthFailed = 10001
|
|
|
|
|
CodeInvalidCredentials = 10002
|
|
|
|
|
CodeTokenExpired = 10003
|
|
|
|
|
CodeSSONotConfigured = 10004
|
|
|
|
|
CodeSSOGenerateFailed = 10005
|
2026-07-15 10:44:32 +08:00
|
|
|
|
2026-07-15 14:05:22 +08:00
|
|
|
CodeTaskCreateFailed = 20001
|
|
|
|
|
CodeTaskExecTimeout = 20002
|
2026-07-15 10:44:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var codeMessages = map[int]string{
|
|
|
|
|
CodeSuccess: "success",
|
|
|
|
|
CodeLDAPAuthFailed: "LDAP 认证失败",
|
|
|
|
|
CodeInvalidCredentials: "用户名或密码错误",
|
|
|
|
|
CodeTokenExpired: "Token 已过期",
|
|
|
|
|
CodeSSONotConfigured: "子系统未配置 SSO",
|
|
|
|
|
CodeSSOGenerateFailed: "SSO 跳转生成失败",
|
|
|
|
|
CodeTaskCreateFailed: "Ansible 任务创建失败",
|
|
|
|
|
CodeTaskExecTimeout: "Ansible 任务执行超时",
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-15 14:05:22 +08:00
|
|
|
func Message(code int) string {
|
2026-07-15 10:44:32 +08:00
|
|
|
if msg, ok := codeMessages[code]; ok {
|
|
|
|
|
return msg
|
|
|
|
|
}
|
|
|
|
|
return "未知错误"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Success(c *gin.Context, data interface{}) {
|
|
|
|
|
c.JSON(http.StatusOK, Response{
|
|
|
|
|
Code: CodeSuccess,
|
2026-07-15 14:05:22 +08:00
|
|
|
Message: Message(CodeSuccess),
|
2026-07-15 10:44:32 +08:00
|
|
|
Data: data,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SuccessWithMessage(c *gin.Context, message string, data interface{}) {
|
|
|
|
|
c.JSON(http.StatusOK, Response{
|
|
|
|
|
Code: CodeSuccess,
|
|
|
|
|
Message: message,
|
|
|
|
|
Data: data,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func SuccessWithPaginated(c *gin.Context, total int64, items interface{}) {
|
|
|
|
|
c.JSON(http.StatusOK, Response{
|
|
|
|
|
Code: CodeSuccess,
|
2026-07-15 14:05:22 +08:00
|
|
|
Message: Message(CodeSuccess),
|
2026-07-15 10:44:32 +08:00
|
|
|
Data: PaginatedData{
|
|
|
|
|
Total: total,
|
|
|
|
|
Items: items,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-15 14:05:22 +08:00
|
|
|
func Error(c *gin.Context, httpCode, bizCode int, details string) {
|
2026-07-15 10:44:32 +08:00
|
|
|
c.JSON(httpCode, Response{
|
|
|
|
|
Code: bizCode,
|
2026-07-15 14:05:22 +08:00
|
|
|
Message: Message(bizCode),
|
2026-07-15 10:44:32 +08:00
|
|
|
Details: details,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-15 14:05:22 +08:00
|
|
|
func ErrorWithMessage(c *gin.Context, httpCode, bizCode int, message, details string) {
|
2026-07-15 10:44:32 +08:00
|
|
|
c.JSON(httpCode, Response{
|
|
|
|
|
Code: bizCode,
|
|
|
|
|
Message: message,
|
|
|
|
|
Details: details,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BadRequest(c *gin.Context, details string) {
|
|
|
|
|
Error(c, http.StatusBadRequest, CodeInvalidCredentials, details)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Unauthorized(c *gin.Context, details string) {
|
|
|
|
|
Error(c, http.StatusUnauthorized, CodeTokenExpired, details)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Forbidden(c *gin.Context, details string) {
|
|
|
|
|
c.JSON(http.StatusForbidden, Response{
|
|
|
|
|
Code: http.StatusForbidden,
|
|
|
|
|
Message: "无权限访问",
|
|
|
|
|
Details: details,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NotFound(c *gin.Context, details string) {
|
|
|
|
|
c.JSON(http.StatusNotFound, Response{
|
|
|
|
|
Code: http.StatusNotFound,
|
|
|
|
|
Message: "资源不存在",
|
|
|
|
|
Details: details,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func InternalError(c *gin.Context, details string) {
|
|
|
|
|
c.JSON(http.StatusInternalServerError, Response{
|
|
|
|
|
Code: http.StatusInternalServerError,
|
|
|
|
|
Message: "服务器内部错误",
|
|
|
|
|
Details: details,
|
|
|
|
|
})
|
|
|
|
|
}
|