response.go 3.5 KB

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