48 Star 107 Fork 11

喵了个咪 / phalgo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
response.go 1.87 KB
一键复制 编辑 原始数据 按行查看 历史
// PhalGo-Response
// 返回json参数,默认结构code,data,msg
// 喵了个咪 <wenzhenxi@vip.qq.com> 2016/5/11
// 依赖情况:
// "github.com/labstack/echo" 必须基于echo路由
package phalgo
import (
"github.com/labstack/echo"
"net/http"
)
type Response struct {
Context echo.Context
parameter *RetParameter
}
type RetParameter struct {
Code int `json:"code";xml:"code"`
Data interface{} `json:"data";xml:"data"`
Msg string `json:"msg";xml:"msg"`
}
const DefaultCode = 1
var HttpStatus = http.StatusOK
// 初始化Response
func NewResponse(c echo.Context) *Response {
R := new(Response)
R.Context = c
R.parameter = new(RetParameter)
R.parameter.Data = nil
return R
}
// 设置返回的Status值默认http.StatusOK
func (this *Response)SetStatus(i int) {
HttpStatus = i
}
func (this *Response)SetMsg(s string) {
this.parameter.Msg = s
}
func (this *Response)SetData(d interface{}) {
this.parameter.Data = d
}
// 返回自定自定义的消息格式
func (this *Response)RetCustomize(code int, d interface{}, msg string) error {
this.parameter.Code = code
this.parameter.Data = d
this.parameter.Msg = msg
return this.Ret(this.parameter)
}
// 返回成功的结果 默认code为1
func (this *Response)RetSuccess(d interface{}) error {
this.parameter.Code = DefaultCode
this.parameter.Data = d
return this.Ret(this.parameter)
}
// 返回失败结果
func (this *Response)RetError(e error, c int) error {
this.parameter.Code = c
this.parameter.Msg = e.Error()
return this.Ret(this.parameter)
}
// 返回结果
func (this *Response)Ret(par interface{}) error {
switch RetType {
case 2:
return this.Context.XML(HttpStatus, par)
default:
return this.Context.JSON(HttpStatus, par)
}
}
// 输出返回结果
func (this *Response)Write(b []byte) {
_, e := this.Context.Response().Write(b)
if e != nil {
print(e.Error())
}
}
Go
1
https://gitee.com/wenzhenxi/phalgo.git
git@gitee.com:wenzhenxi/phalgo.git
wenzhenxi
phalgo
phalgo
master

搜索帮助