response.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package response
  2. import (
  3. "DataShare/util"
  4. "bufio"
  5. "bytes"
  6. "encoding/json"
  7. "fmt"
  8. "github.com/gin-gonic/gin"
  9. "net/http"
  10. "strings"
  11. )
  12. type Response struct {
  13. Code int `json:"code"`
  14. Data interface{} `json:"data"`
  15. Msg string `json:"msg"`
  16. }
  17. type ResponseXml struct {
  18. Code int `xml:"code"`
  19. Data interface{} `xml:"data"`
  20. Msg string `xml:"msg"`
  21. }
  22. const (
  23. ERR_OK = 0
  24. ERR_INVALID_PARAM = 1
  25. ERR_QUERY_DATABASE = 2
  26. ERR_NOT_FOUND = 3
  27. ERR_ALREADY_EXIST = 4
  28. ERR_SYS_ERROR = 5
  29. ERR_TRANS_ERROR = 6
  30. ERR_HOSPITAL_ERROR = 7
  31. ERR_FACE_DETECT_ERROR = 8
  32. ERR_INVALID_DATA = 9
  33. ERR_NOT_LOGIN = 10
  34. ERR_LOGIN_EXPIRED = 11
  35. ERR_INVALID_TOKEN = 12
  36. ERR_CHANGE_STATUS = 13
  37. ERR_UNSUPPORTED = 14
  38. ERR_LOGIN_FAILED = 15
  39. ERR_PERMISSION = 16
  40. ERR_FAILED = 99
  41. )
  42. type MyMap map[string]interface{}
  43. func Result(code int, data interface{}, msg string, c *gin.Context, format string) {
  44. if format == "XML" {
  45. xmlResp := &ResponseXml{
  46. Code: code,
  47. Data: data,
  48. Msg: msg,
  49. }
  50. dataByte,err :=json.Marshal(xmlResp)
  51. if err != nil {
  52. fmt.Println("Err:",err.Error())
  53. }
  54. //fmt.Println("jsondata:",string(dataByte))
  55. xmlByte := bytes.NewBuffer(make([]byte, 0))
  56. xmlWriter := bufio.NewWriter(xmlByte)
  57. err = util.ToXML(strings.NewReader(string(dataByte)),xmlWriter)
  58. if err != nil {
  59. fmt.Println("Err:",err.Error())
  60. c.JSON(http.StatusOK, Response{
  61. 99,
  62. nil,
  63. msg,
  64. })
  65. return
  66. }
  67. //fmt.Println("xml:",xmlByte.String())
  68. c.XML(http.StatusOK, xmlByte.String())
  69. }else{
  70. c.JSON(http.StatusOK, Response{
  71. code,
  72. data,
  73. msg,
  74. })
  75. }
  76. }
  77. func Ok(c *gin.Context, format string) {
  78. Result(ERR_OK, map[string]interface{}{}, "操作成功", c,format)
  79. }
  80. func OkWithMessage(message string, c *gin.Context, format string) {
  81. Result(ERR_OK, map[string]interface{}{}, message, c,format)
  82. }
  83. func OkWithData(data interface{}, c *gin.Context, format string) {
  84. Result(ERR_OK, data, "操作成功", c,format)
  85. }
  86. func OkDetailed(data interface{}, message string, c *gin.Context, format string) {
  87. Result(ERR_OK, data, message, c,format)
  88. }
  89. func Fail(c *gin.Context, format string) {
  90. Result(ERR_FAILED, map[string]interface{}{}, "操作失败", c,format)
  91. }
  92. func FailWithMessage(message string, c *gin.Context, format string) {
  93. Result(ERR_FAILED, map[string]interface{}{}, message, c,format)
  94. }
  95. func FailWithDetailed(code int, data interface{}, message string, c *gin.Context, format string) {
  96. Result(code, data, message, c,format)
  97. }