60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
package response
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
const (
|
|
NeedSetup = 402
|
|
NeedLogin = 403
|
|
NoAccessPrivileges = 405
|
|
ParamsError = 100
|
|
ServerError = 500
|
|
)
|
|
|
|
type Response struct {
|
|
ErrorNo int `json:"errorNo"`
|
|
ErrorMsg string `json:"errorMsg"`
|
|
Data any `json:"data"`
|
|
}
|
|
|
|
func (p *Response) FPrint(w http.ResponseWriter) {
|
|
bytesData, err := json.Marshal(p)
|
|
if err != nil {
|
|
log.WithError(err).Error("serialize HTTP response failed")
|
|
http.Error(w, "\u54cd\u5e94\u5e8f\u5217\u5316\u5931\u8d25", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if observer, ok := w.(interface{ ObserveResult(int) }); ok {
|
|
observer.ObserveResult(p.ErrorNo)
|
|
}
|
|
// 仅通知经过业务确认的中间状态,不向日志观察器传递响应载荷或验证凭据。
|
|
if observer, ok := w.(interface{ ObservePending(bool) }); ok && p.ErrorNo == 0 {
|
|
if data, ok := p.Data.(map[string]any); ok {
|
|
pending, _ := data["pending"].(bool)
|
|
observer.ObservePending(pending)
|
|
}
|
|
}
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
if _, err := w.Write(bytesData); err != nil {
|
|
log.WithError(err).Error("write HTTP response failed")
|
|
}
|
|
}
|
|
|
|
func NewSuccessResponse(data any) *Response {
|
|
return &Response{
|
|
Data: data,
|
|
}
|
|
}
|
|
|
|
func NewErrorResponse(errorNo int, errorMsg string, data any) *Response {
|
|
return &Response{
|
|
ErrorNo: errorNo,
|
|
ErrorMsg: errorMsg,
|
|
Data: data,
|
|
}
|
|
}
|