2017-12-07 14:57:16 +08:00
|
|
|
package ghttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"gitee.com/johng/gf/g/encoding/gjson"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 服务端请求返回对象
|
|
|
|
type ServerResponse struct {
|
|
|
|
http.ResponseWriter
|
|
|
|
server *Server // 所属Server对象
|
|
|
|
}
|
|
|
|
|
|
|
|
// 返回的固定JSON数据结构
|
|
|
|
type ResponseJson struct {
|
|
|
|
Result int `json:"result"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
Data interface{} `json:"data"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 返回信息
|
2017-12-07 17:34:51 +08:00
|
|
|
func (r *ServerResponse) Write(content []byte) {
|
2017-12-07 14:57:16 +08:00
|
|
|
if r.Header().Get("Content-Type") == "" {
|
|
|
|
r.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
}
|
2017-12-07 17:34:51 +08:00
|
|
|
r.ResponseWriter.Write(content)
|
2017-12-07 14:57:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 返回固定格式的json
|
|
|
|
func (r *ServerResponse) ResponseJson(result int, message string, data interface{}) {
|
|
|
|
if r.Header().Get("Content-Type") == "" {
|
|
|
|
r.Header().Set("Content-Type", "application/json")
|
|
|
|
}
|
|
|
|
r.Write([]byte(gjson.Encode(ResponseJson{ result, message, data })))
|
|
|
|
}
|
|
|
|
|