return.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package app
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "net/http"
  5. )
  6. //func SetApiBaseOutputHead(code int, message string) models.ApiBaseOutputHead {
  7. // var outData ali.AliBaseResponse
  8. // outData.Code = code
  9. // outData.Msg = message
  10. // return outData
  11. //}
  12. //func ClientErr(c *gin.Context, msg string) {
  13. // var aliOut ali.AliBaseResponse
  14. // aliOut.Code = 400
  15. // aliOut.Msg = msg
  16. // c.JSON(http.StatusOK, aliOut)
  17. //}
  18. //
  19. //func ServerErr(c *gin.Context, msg string) {
  20. // var aliOut ali.AliBaseResponse
  21. // aliOut.Code = 500
  22. // aliOut.Msg = msg
  23. // c.JSON(http.StatusOK, aliOut)
  24. //}
  25. // 失败数据处理
  26. func Error(c *gin.Context, code int, err error, msg string) {
  27. var res Response
  28. if err != nil {
  29. res.Message = err.Error()
  30. } else {
  31. if msg != "" {
  32. res.Message = msg
  33. }
  34. }
  35. c.JSON(http.StatusOK, res.ReturnError(code))
  36. }
  37. // 通常成功数据处理
  38. func OK(c *gin.Context, data interface{}, msg string) {
  39. var res AliResponse
  40. res.Data = data
  41. if msg != "" {
  42. res.Message = msg
  43. }
  44. res.Code = 200
  45. c.JSON(http.StatusOK, res)
  46. }
  47. // 分页数据处理
  48. func PageOK(c *gin.Context, result interface{}, count int, pageIndex int, pageSize int, msg string) {
  49. var res PageRes
  50. res.Data.List = result
  51. res.Data.Count = count
  52. res.Data.PageIndex = pageIndex
  53. res.Data.PageSize = pageSize
  54. if msg != "" {
  55. res.Msg = msg
  56. }
  57. res.Code = 200
  58. c.JSON(http.StatusOK, res)
  59. }
  60. // 兼容函数
  61. func Custum(c *gin.Context, data gin.H) {
  62. c.JSON(http.StatusOK, data)
  63. }