51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
package http
|
||
|
||
import (
|
||
"encoding/json"
|
||
"net/http"
|
||
"time"
|
||
)
|
||
|
||
// Response 标准响应结构
|
||
type Response struct {
|
||
Code int `json:"code"` // 业务状态码,0表示成功
|
||
Message string `json:"message"` // 响应消息
|
||
Timestamp int64 `json:"timestamp"` // 时间戳
|
||
Data interface{} `json:"data"` // 响应数据
|
||
}
|
||
|
||
// PageResponse 分页响应结构
|
||
type PageResponse struct {
|
||
Code int `json:"code"`
|
||
Message string `json:"message"`
|
||
Timestamp int64 `json:"timestamp"`
|
||
Data *PageData `json:"data"`
|
||
}
|
||
|
||
// PageData 分页数据
|
||
type PageData struct {
|
||
List interface{} `json:"list"` // 数据列表
|
||
Total int64 `json:"total"` // 总记录数
|
||
Page int `json:"page"` // 当前页码
|
||
PageSize int `json:"pageSize"` // 每页大小
|
||
}
|
||
|
||
// writeJSON 写入JSON响应(内部方法)
|
||
// httpCode: HTTP状态码(200表示正常,500表示系统错误等)
|
||
// code: 业务状态码(0表示成功,非0表示业务错误)
|
||
// message: 响应消息
|
||
// data: 响应数据
|
||
func writeJSON(w http.ResponseWriter, httpCode, code int, message string, data interface{}) {
|
||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||
w.WriteHeader(httpCode)
|
||
|
||
response := Response{
|
||
Code: code,
|
||
Message: message,
|
||
Timestamp: time.Now().Unix(),
|
||
Data: data,
|
||
}
|
||
|
||
json.NewEncoder(w).Encode(response)
|
||
}
|