将工厂改成黑盒模式,降低用户使用成本
This commit is contained in:
@@ -30,56 +30,12 @@ type PageData struct {
|
||||
PageSize int `json:"pageSize"` // 每页大小
|
||||
}
|
||||
|
||||
// Success 成功响应
|
||||
// data: 响应数据,可以为nil
|
||||
func Success(w http.ResponseWriter, data interface{}) {
|
||||
WriteJSON(w, http.StatusOK, 0, "success", data)
|
||||
}
|
||||
|
||||
// SuccessWithMessage 带消息的成功响应
|
||||
func SuccessWithMessage(w http.ResponseWriter, message string, data interface{}) {
|
||||
WriteJSON(w, http.StatusOK, 0, message, data)
|
||||
}
|
||||
|
||||
// Error 错误响应
|
||||
// code: 业务错误码,非0表示业务错误
|
||||
// message: 错误消息
|
||||
func Error(w http.ResponseWriter, code int, message string) {
|
||||
WriteJSON(w, http.StatusOK, code, message, nil)
|
||||
}
|
||||
|
||||
// SystemError 系统错误响应(返回HTTP 500)
|
||||
// message: 错误消息
|
||||
func SystemError(w http.ResponseWriter, message string) {
|
||||
WriteJSON(w, http.StatusInternalServerError, 500, message, nil)
|
||||
}
|
||||
|
||||
// BadRequest 请求错误响应(HTTP 400)
|
||||
func BadRequest(w http.ResponseWriter, message string) {
|
||||
WriteJSON(w, http.StatusBadRequest, 400, message, nil)
|
||||
}
|
||||
|
||||
// Unauthorized 未授权响应(HTTP 401)
|
||||
func Unauthorized(w http.ResponseWriter, message string) {
|
||||
WriteJSON(w, http.StatusUnauthorized, 401, message, nil)
|
||||
}
|
||||
|
||||
// Forbidden 禁止访问响应(HTTP 403)
|
||||
func Forbidden(w http.ResponseWriter, message string) {
|
||||
WriteJSON(w, http.StatusForbidden, 403, message, nil)
|
||||
}
|
||||
|
||||
// NotFound 未找到响应(HTTP 404)
|
||||
func NotFound(w http.ResponseWriter, message string) {
|
||||
WriteJSON(w, http.StatusNotFound, 404, message, nil)
|
||||
}
|
||||
|
||||
// WriteJSON 写入JSON响应
|
||||
// writeJSON 写入JSON响应(内部方法)
|
||||
// httpCode: HTTP状态码(200表示正常,500表示系统错误等)
|
||||
// code: 业务状态码(0表示成功,非0表示业务错误)
|
||||
// message: 响应消息
|
||||
// data: 响应数据
|
||||
func WriteJSON(w http.ResponseWriter, httpCode, code int, message string, data interface{}) {
|
||||
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)
|
||||
|
||||
@@ -92,47 +48,3 @@ func WriteJSON(w http.ResponseWriter, httpCode, code int, message string, data i
|
||||
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
// SuccessPage 分页成功响应
|
||||
// list: 数据列表
|
||||
// total: 总记录数
|
||||
// page: 当前页码
|
||||
// pageSize: 每页大小
|
||||
func SuccessPage(w http.ResponseWriter, list interface{}, total int64, page, pageSize int) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
response := PageResponse{
|
||||
Code: 0,
|
||||
Message: "success",
|
||||
Timestamp: time.Now().Unix(),
|
||||
Data: &PageData{
|
||||
List: list,
|
||||
Total: total,
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
},
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
// SuccessPageWithMessage 带消息的分页成功响应
|
||||
func SuccessPageWithMessage(w http.ResponseWriter, message string, list interface{}, total int64, page, pageSize int) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
response := PageResponse{
|
||||
Code: 0,
|
||||
Message: message,
|
||||
Timestamp: time.Now().Unix(),
|
||||
Data: &PageData{
|
||||
List: list,
|
||||
Total: total,
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
},
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user