39 lines
878 B
Go
39 lines
878 B
Go
package http
|
||
|
||
import (
|
||
"encoding/json"
|
||
"net/http"
|
||
"time"
|
||
)
|
||
|
||
// Response 标准响应结构
|
||
type Response struct {
|
||
Code int `json:"code"`
|
||
Message string `json:"message"`
|
||
Timestamp int64 `json:"timestamp"`
|
||
Data interface{} `json:"data"`
|
||
}
|
||
|
||
// PageData 分页数据
|
||
type PageData struct {
|
||
List interface{} `json:"list"`
|
||
Total int64 `json:"total"`
|
||
Page int `json:"page"`
|
||
PageSize int `json:"pageSize"`
|
||
}
|
||
|
||
// writeResponse 统一 JSON 出参(HTTP 恒 200)
|
||
func writeResponse(w http.ResponseWriter, code int, message string, data interface{}) {
|
||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||
w.WriteHeader(http.StatusOK)
|
||
|
||
response := Response{
|
||
Code: code,
|
||
Message: message,
|
||
Timestamp: time.Now().Unix(),
|
||
Data: data,
|
||
}
|
||
|
||
_ = json.NewEncoder(w).Encode(response)
|
||
}
|