response.go 2.7 KB

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