response.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package response
  2. import (
  3. "gfast/library/utils"
  4. "github.com/gogf/gf/frame/g"
  5. "github.com/gogf/gf/net/ghttp"
  6. "github.com/gogf/gf/os/gmutex"
  7. "github.com/gogf/gf/os/gview"
  8. "github.com/gogf/gf/text/gstr"
  9. "github.com/gogf/gf/util/gconv"
  10. )
  11. const (
  12. SuccessCode int = 0
  13. ErrorCode int = -1
  14. )
  15. type Response struct {
  16. // 代码
  17. Code int `json:"code" example:"200"`
  18. // 数据集
  19. Data interface{} `json:"data"`
  20. // 消息
  21. Msg string `json:"msg"`
  22. }
  23. var (
  24. response = new(Response)
  25. mu = gmutex.New()
  26. )
  27. func JsonExit(r *ghttp.Request, code int, msg string, data ...interface{}) {
  28. response.JsonExit(r, code, msg, data...)
  29. }
  30. func RJson(r *ghttp.Request, code int, msg string, data ...interface{}) {
  31. response.RJson(r, code, msg, data...)
  32. }
  33. func SusJson(isExit bool, r *ghttp.Request, msg string, data ...interface{}) {
  34. response.SusJson(isExit, r, msg, data...)
  35. }
  36. func FailJson(isExit bool, r *ghttp.Request, msg string, data ...interface{}) {
  37. response.FailJson(isExit, r, msg, data...)
  38. }
  39. func WriteTpl(r *ghttp.Request, tpl string, view *gview.View, params ...gview.Params) error {
  40. return response.WriteTpl(r, tpl, view, params...)
  41. }
  42. // 返回JSON数据并退出当前HTTP执行函数。
  43. func (res *Response) JsonExit(r *ghttp.Request, code int, msg string, data ...interface{}) {
  44. res.RJson(r, code, msg, data...)
  45. r.Exit()
  46. }
  47. // 标准返回结果数据结构封装。
  48. // 返回固定数据结构的JSON:
  49. // code: 状态码(200:成功,302跳转,和http请求状态码一至);
  50. // msg: 请求结果信息;
  51. // data: 请求结果,根据不同接口返回结果的数据结构不同;
  52. func (res *Response) RJson(r *ghttp.Request, code int, msg string, data ...interface{}) {
  53. responseData := interface{}(nil)
  54. if len(data) > 0 {
  55. responseData = data[0]
  56. }
  57. response = &Response{
  58. Code: code,
  59. Msg: msg,
  60. Data: responseData,
  61. }
  62. r.SetParam("apiReturnRes", response)
  63. r.Response.WriteJson(response)
  64. }
  65. //成功返回JSON
  66. func (res *Response) SusJson(isExit bool, r *ghttp.Request, msg string, data ...interface{}) {
  67. if isExit {
  68. res.JsonExit(r, SuccessCode, msg, data...)
  69. }
  70. res.RJson(r, SuccessCode, msg, data...)
  71. }
  72. //失败返回JSON
  73. func (res *Response) FailJson(isExit bool, r *ghttp.Request, msg string, data ...interface{}) {
  74. if isExit {
  75. res.JsonExit(r, ErrorCode, msg, data...)
  76. }
  77. res.RJson(r, ErrorCode, msg, data...)
  78. }
  79. //模板输出
  80. func (res *Response) WriteTpl(r *ghttp.Request, tpl string, view *gview.View, params ...gview.Params) error {
  81. mu.Lock()
  82. defer mu.Unlock()
  83. //绑定模板中需要用到的方法
  84. view.BindFuncMap(g.Map{
  85. // 根据长度i来切割字符串
  86. "subStr": func(str interface{}, i int) (s string) {
  87. s1 := gconv.String(str)
  88. if gstr.LenRune(s1) > i {
  89. s = gstr.SubStrRune(s1, 0, i) + "..."
  90. return s
  91. }
  92. return s1
  93. },
  94. // 格式化时间戳 年月日
  95. "timeFormatYear": func(time interface{}) string {
  96. return utils.TimeStampToDate(gconv.Int64(time))
  97. },
  98. // 格式化时间戳 年月日时分秒
  99. "timeFormatDateTime": func(time interface{}) string {
  100. return utils.TimeStampToDateTime(gconv.Int64(time))
  101. },
  102. "add": func(a, b interface{}) int {
  103. return gconv.Int(a) + gconv.Int(b)
  104. },
  105. })
  106. //设置全局变量
  107. domain, _ := utils.GetDomain(r)
  108. view.Assigns(g.Map{
  109. "domain": domain,
  110. })
  111. return r.Response.WriteTpl(tpl, params...)
  112. }
  113. func (res *Response) Redirect(r *ghttp.Request, location string, code ...int) {
  114. r.Response.RedirectTo(location, code...)
  115. }