response.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package library
  2. import (
  3. "github.com/gogf/gf/frame/g"
  4. "github.com/gogf/gf/net/ghttp"
  5. "github.com/gogf/gf/os/gview"
  6. "github.com/gogf/gf/text/gstr"
  7. "github.com/gogf/gf/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:"msg"`
  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.Exit()
  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.SetParam("apiReturnRes", response)
  58. r.Response.WriteJson(response)
  59. }
  60. //成功返回JSON
  61. func (res *Response) SusJson(isExit bool, r *ghttp.Request, msg string, data ...interface{}) {
  62. if isExit {
  63. res.JsonExit(r, SuccessCode, msg, data...)
  64. }
  65. res.RJson(r, SuccessCode, msg, data...)
  66. }
  67. //失败返回JSON
  68. func (res *Response) FailJson(isExit bool, r *ghttp.Request, msg string, data ...interface{}) {
  69. if isExit {
  70. res.JsonExit(r, ErrorCode, msg, data...)
  71. }
  72. res.RJson(r, ErrorCode, msg, data...)
  73. }
  74. //模板输出
  75. func (res *Response) WriteTpl(r *ghttp.Request, tpl string, view *gview.View, params ...gview.Params) error {
  76. //绑定模板中需要用到的方法
  77. view.BindFuncMap(g.Map{
  78. // 根据长度i来切割字符串
  79. "subStr": func(str interface{}, i int) (s string) {
  80. s1 := gconv.String(str)
  81. if gstr.LenRune(s1) > i {
  82. s = gstr.SubStrRune(s1, 0, i) + "..."
  83. return s
  84. }
  85. return s1
  86. },
  87. // 格式化时间戳 年月日
  88. "timeFormatYear": func(time interface{}) string {
  89. return TimeStampToDate(gconv.Int64(time))
  90. },
  91. // 格式化时间戳 年月日时分秒
  92. "timeFormatDateTime": func(time interface{}) string {
  93. return TimeStampToDateTime(gconv.Int64(time))
  94. },
  95. "add": func(a, b interface{}) int {
  96. return gconv.Int(a) + gconv.Int(b)
  97. },
  98. })
  99. //设置全局变量
  100. domain, _ := GetDomain(r)
  101. view.Assigns(g.Map{
  102. "domain": domain,
  103. })
  104. return r.Response.WriteTpl(tpl, params...)
  105. }
  106. func (res *Response) Redirect(r *ghttp.Request, location string, code ...int) {
  107. r.Response.RedirectTo(location, code...)
  108. }
  109. type Respage struct {
  110. //总条数
  111. Total int
  112. //当前页
  113. CurrentPage int
  114. //数据
  115. List []interface{}
  116. }