Files
go-common/http/response.go

39 lines
878 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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)
}