34 lines
747 B
Go
34 lines
747 B
Go
|
package corelib
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
// JsonResponse 统一的JSON响应结构体
|
||
|
type JsonResponse struct {
|
||
|
Status int `json:"status"`
|
||
|
Message string `json:"message"`
|
||
|
Data any `json:"data,omitempty"` // 使用 any 类型来存储任意类型的数据
|
||
|
}
|
||
|
|
||
|
// WriteJsonResponse 用于写入JSON响应
|
||
|
func WriteJsonResponse(w http.ResponseWriter, status int, message string, data any) {
|
||
|
w.Header().Set("Content-Type", "application/json")
|
||
|
w.WriteHeader(status)
|
||
|
response := JsonResponse{
|
||
|
Status: status,
|
||
|
Message: message,
|
||
|
Data: data,
|
||
|
}
|
||
|
err := json.NewEncoder(w).Encode(response)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func Success(w http.ResponseWriter) {
|
||
|
WriteJsonResponse(w, 200, "OK", nil)
|
||
|
}
|