|
|
@@ -23,9 +23,31 @@ type Response struct {
|
|
|
Msg string `json:"msg"`
|
|
|
}
|
|
|
|
|
|
-// 返回JSON数据并退出当前HTTP执行函数。
|
|
|
+var response *Response
|
|
|
+
|
|
|
func JsonExit(r *ghttp.Request, code int, msg string, data ...interface{}) {
|
|
|
- RJson(r, code, msg, data...)
|
|
|
+ response.JsonExit(r, code, msg, data...)
|
|
|
+}
|
|
|
+
|
|
|
+func RJson(r *ghttp.Request, code int, msg string, data ...interface{}) {
|
|
|
+ response.RJson(r, code, msg, data...)
|
|
|
+}
|
|
|
+
|
|
|
+func SusJson(isExit bool, r *ghttp.Request, msg string, data ...interface{}) {
|
|
|
+ response.SusJson(isExit, r, msg, data...)
|
|
|
+}
|
|
|
+
|
|
|
+func FailJson(isExit bool, r *ghttp.Request, msg string, data ...interface{}) {
|
|
|
+ response.FailJson(isExit, r, msg, data...)
|
|
|
+}
|
|
|
+
|
|
|
+func WriteTpl(r *ghttp.Request, tpl string, view *gview.View, params ...gview.Params) error {
|
|
|
+ return response.WriteTpl(r, tpl, view, params...)
|
|
|
+}
|
|
|
+
|
|
|
+// 返回JSON数据并退出当前HTTP执行函数。
|
|
|
+func (res *Response) JsonExit(r *ghttp.Request, code int, msg string, data ...interface{}) {
|
|
|
+ res.RJson(r, code, msg, data...)
|
|
|
r.Exit()
|
|
|
}
|
|
|
|
|
|
@@ -34,38 +56,38 @@ func JsonExit(r *ghttp.Request, code int, msg string, data ...interface{}) {
|
|
|
// code: 状态码(200:成功,302跳转,和http请求状态码一至);
|
|
|
// msg: 请求结果信息;
|
|
|
// data: 请求结果,根据不同接口返回结果的数据结构不同;
|
|
|
-func RJson(r *ghttp.Request, code int, msg string, data ...interface{}) {
|
|
|
+func (res *Response) RJson(r *ghttp.Request, code int, msg string, data ...interface{}) {
|
|
|
responseData := interface{}(nil)
|
|
|
if len(data) > 0 {
|
|
|
responseData = data[0]
|
|
|
}
|
|
|
- result := Response{
|
|
|
+ response = &Response{
|
|
|
Code: code,
|
|
|
Msg: msg,
|
|
|
Data: responseData,
|
|
|
}
|
|
|
- r.SetParam("apiReturnRes", result)
|
|
|
- r.Response.WriteJson(result)
|
|
|
+ r.SetParam("apiReturnRes", response)
|
|
|
+ r.Response.WriteJson(response)
|
|
|
}
|
|
|
|
|
|
//成功返回JSON
|
|
|
-func SusJson(isExit bool, r *ghttp.Request, msg string, data ...interface{}) {
|
|
|
+func (res *Response) SusJson(isExit bool, r *ghttp.Request, msg string, data ...interface{}) {
|
|
|
if isExit {
|
|
|
- JsonExit(r, SuccessCode, msg, data...)
|
|
|
+ res.JsonExit(r, SuccessCode, msg, data...)
|
|
|
}
|
|
|
- RJson(r, SuccessCode, msg, data...)
|
|
|
+ res.RJson(r, SuccessCode, msg, data...)
|
|
|
}
|
|
|
|
|
|
//失败返回JSON
|
|
|
-func FailJson(isExit bool, r *ghttp.Request, msg string, data ...interface{}) {
|
|
|
+func (res *Response) FailJson(isExit bool, r *ghttp.Request, msg string, data ...interface{}) {
|
|
|
if isExit {
|
|
|
- JsonExit(r, ErrorCode, msg, data...)
|
|
|
+ res.JsonExit(r, ErrorCode, msg, data...)
|
|
|
}
|
|
|
- RJson(r, ErrorCode, msg, data...)
|
|
|
+ res.RJson(r, ErrorCode, msg, data...)
|
|
|
}
|
|
|
|
|
|
//模板输出
|
|
|
-func WriteTpl(r *ghttp.Request, tpl string, view *gview.View, params ...gview.Params) error {
|
|
|
+func (res *Response) WriteTpl(r *ghttp.Request, tpl string, view *gview.View, params ...gview.Params) error {
|
|
|
//绑定模板中需要用到的方法
|
|
|
view.BindFuncMap(g.Map{
|
|
|
// 根据长度i来切割字符串
|
|
|
@@ -107,3 +129,7 @@ func WriteTpl(r *ghttp.Request, tpl string, view *gview.View, params ...gview.Pa
|
|
|
})
|
|
|
return r.Response.WriteTpl(tpl, params...)
|
|
|
}
|
|
|
+
|
|
|
+func (res *Response) Redirect(r *ghttp.Request, location string, code ...int) {
|
|
|
+ r.Response.RedirectTo(location, code...)
|
|
|
+}
|