response.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package library
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/encoding/gurl"
  5. "github.com/gogf/gf/net/ghttp"
  6. "github.com/gogf/gf/os/gview"
  7. )
  8. const (
  9. SuccessCode int = 0
  10. ErrorCode int = -1
  11. )
  12. type Response struct {
  13. // 代码
  14. Code int `json:"code" example:"200"`
  15. // 数据集
  16. Data interface{} `json:"data"`
  17. // 消息
  18. Msg string `json:"msg"`
  19. }
  20. var response = new(Response)
  21. func JsonExit(r *ghttp.Request, code int, msg string, data ...interface{}) {
  22. response.JsonExit(r, code, msg, data...)
  23. }
  24. func RJson(r *ghttp.Request, code int, msg string, data ...interface{}) {
  25. response.RJson(r, code, msg, data...)
  26. }
  27. func SusJson(isExit bool, r *ghttp.Request, msg string, data ...interface{}) {
  28. response.SusJson(isExit, r, msg, data...)
  29. }
  30. func FailJson(isExit bool, r *ghttp.Request, msg string, data ...interface{}) {
  31. response.FailJson(isExit, r, msg, data...)
  32. }
  33. func WriteTpl(r *ghttp.Request, tpl string, view *gview.View, params ...gview.Params) error {
  34. return response.WriteTpl(r, tpl, view, params...)
  35. }
  36. // 返回JSON数据并退出当前HTTP执行函数。
  37. func (res *Response) JsonExit(r *ghttp.Request, code int, msg string, data ...interface{}) {
  38. res.RJson(r, code, msg, data...)
  39. r.Exit()
  40. }
  41. // 标准返回结果数据结构封装。
  42. // 返回固定数据结构的JSON:
  43. // code: 状态码(200:成功,302跳转,和http请求状态码一至);
  44. // msg: 请求结果信息;
  45. // data: 请求结果,根据不同接口返回结果的数据结构不同;
  46. func (res *Response) RJson(r *ghttp.Request, code int, msg string, data ...interface{}) {
  47. responseData := interface{}(nil)
  48. if len(data) > 0 {
  49. responseData = data[0]
  50. }
  51. response = &Response{
  52. Code: code,
  53. Msg: msg,
  54. Data: responseData,
  55. }
  56. r.SetParam("apiReturnRes", response)
  57. r.Response.WriteJson(response)
  58. }
  59. //成功返回JSON
  60. func (res *Response) SusJson(isExit bool, r *ghttp.Request, msg string, data ...interface{}) {
  61. if isExit {
  62. res.JsonExit(r, SuccessCode, msg, data...)
  63. }
  64. res.RJson(r, SuccessCode, msg, data...)
  65. }
  66. //失败返回JSON
  67. func (res *Response) FailJson(isExit bool, r *ghttp.Request, msg string, data ...interface{}) {
  68. if isExit {
  69. res.JsonExit(r, ErrorCode, msg, data...)
  70. }
  71. res.RJson(r, ErrorCode, msg, data...)
  72. }
  73. //模板输出
  74. func (res *Response) WriteTpl(r *ghttp.Request, tpl string, view *gview.View, params ...gview.Params) error {
  75. //设置全局变量
  76. urlInfo, _ := gurl.ParseURL(r.GetUrl(), -1)
  77. view.Assign("urlInfo", urlInfo)
  78. r.SetView(view)
  79. err := r.Response.WriteTpl(tpl, params...)
  80. if err != nil {
  81. fmt.Println(err.Error())
  82. r.Response.WriteExit(err.Error())
  83. }
  84. return nil
  85. }
  86. func (res *Response) Redirect(r *ghttp.Request, location string, code ...int) {
  87. r.Response.RedirectTo(location, code...)
  88. }