feat: backend core - auth, user, role, permission, device, webhook, monitoring, cache, repository, service, middleware, API handlers

This commit is contained in:
2026-04-02 11:19:50 +08:00
parent e59a77bc49
commit dcc1f186f8
298 changed files with 62603 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
package response
// Response 统一响应结构
type Response struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}
// Success 成功响应
func Success(data interface{}) *Response {
return &Response{
Code: 0,
Message: "success",
Data: data,
}
}
// Error 错误响应
func Error(message string) *Response {
return &Response{
Code: -1,
Message: message,
}
}
// ErrorWithCode 带错误码的错误响应
func ErrorWithCode(code int, message string) *Response {
return &Response{
Code: code,
Message: message,
}
}
// WithData 带扩展数据的成功响应
func WithData(data interface{}, extra map[string]interface{}) *Response {
payload, ok := data.(map[string]interface{})
if !ok {
payload = map[string]interface{}{
"items": data,
}
}
for k, v := range extra {
payload[k] = v
}
resp := Success(payload)
return resp
}

View File

@@ -0,0 +1,34 @@
package response
import "testing"
func TestWithDataWrapsSlicesAndMergesExtra(t *testing.T) {
resp := WithData([]string{"a", "b"}, map[string]interface{}{
"total": 2,
"page": 1,
})
data, ok := resp.Data.(map[string]interface{})
if !ok {
t.Fatalf("expected map payload, got %T", resp.Data)
}
if data["total"] != 2 {
t.Fatalf("expected total=2, got %v", data["total"])
}
items, ok := data["items"].([]string)
if !ok || len(items) != 2 {
t.Fatalf("expected items slice to be preserved, got %#v", data["items"])
}
}
func TestWithDataPreservesMapPayload(t *testing.T) {
resp := WithData(map[string]interface{}{"user": "alice"}, map[string]interface{}{"page": 1})
data := resp.Data.(map[string]interface{})
if data["user"] != "alice" {
t.Fatalf("expected user=alice, got %v", data["user"])
}
if data["page"] != 1 {
t.Fatalf("expected page=1, got %v", data["page"])
}
}