response.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package libResponse
  2. import (
  3. "github.com/gogf/gf/v2/frame/g"
  4. "github.com/gogf/gf/v2/net/ghttp"
  5. "github.com/gogf/gf/v2/os/gview"
  6. "github.com/gogf/gf/v2/text/gstr"
  7. "github.com/gogf/gf/v2/util/gconv"
  8. )
  9. const (
  10. SuccessCode int = 0
  11. ErrorCode int = -1
  12. )
  13. type Response struct {
  14. // 代码
  15. Code int `json:"code" example:"200"`
  16. // 数据集
  17. Data interface{} `json:"data"`
  18. // 消息
  19. Msg string `json:"message"`
  20. }
  21. var response = new(Response)
  22. func JsonExit(r *ghttp.Request, code int, msg string, data ...interface{}) {
  23. response.JsonExit(r, code, msg, data...)
  24. }
  25. func RJson(r *ghttp.Request, code int, msg string, data ...interface{}) {
  26. response.RJson(r, code, msg, data...)
  27. }
  28. func SusJson(isExit bool, r *ghttp.Request, msg string, data ...interface{}) {
  29. response.SusJson(isExit, r, msg, data...)
  30. }
  31. func FailJson(isExit bool, r *ghttp.Request, msg string, data ...interface{}) {
  32. response.FailJson(isExit, r, msg, data...)
  33. }
  34. func WriteTpl(r *ghttp.Request, tpl string, view *gview.View, params ...gview.Params) error {
  35. return response.WriteTpl(r, tpl, view, params...)
  36. }
  37. // 返回JSON数据并退出当前HTTP执行函数。
  38. func (res *Response) JsonExit(r *ghttp.Request, code int, msg string, data ...interface{}) {
  39. res.RJson(r, code, msg, data...)
  40. r.ExitAll()
  41. }
  42. // 标准返回结果数据结构封装。
  43. // 返回固定数据结构的JSON:
  44. // code: 状态码(200:成功,302跳转,和http请求状态码一至);
  45. // msg: 请求结果信息;
  46. // data: 请求结果,根据不同接口返回结果的数据结构不同;
  47. func (res *Response) RJson(r *ghttp.Request, code int, msg string, data ...interface{}) {
  48. responseData := interface{}(nil)
  49. if len(data) > 0 {
  50. responseData = data[0]
  51. }
  52. response = &Response{
  53. Code: code,
  54. Msg: msg,
  55. Data: responseData,
  56. }
  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. // WriteTpl 模板输出
  74. func (res *Response) WriteTpl(r *ghttp.Request, tpl string, view *gview.View, params ...gview.Params) error {
  75. //绑定模板中需要用到的方法
  76. view.BindFuncMap(g.Map{
  77. // 根据长度i来切割字符串
  78. "subStr": func(str interface{}, i int) (s string) {
  79. s1 := gconv.String(str)
  80. if gstr.LenRune(s1) > i {
  81. s = gstr.SubStrRune(s1, 0, i) + "..."
  82. return s
  83. }
  84. return s1
  85. },
  86. })
  87. r.Response.Write(view.Parse(r.GetCtx(), tpl, params...))
  88. return nil
  89. }
  90. func (res *Response) Redirect(r *ghttp.Request, location string, code ...int) {
  91. r.Response.RedirectTo(location, code...)
  92. }