统一初始beego mvc 的方法
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
902 B

  1. package response
  2. import (
  3. "bytes"
  4. "encoding/xml"
  5. "log"
  6. )
  7. type Xml struct {
  8. Code int `xml:"code"`
  9. Data interface{} `xml:"data"`
  10. Msg string `xml:"msg"`
  11. Status bool `xml:"status"`
  12. }
  13. func (x *Xml) String() string {
  14. b, err := xml.MarshalIndent(x, "", " ")
  15. if err != nil {
  16. log.Println("xml encode", err)
  17. }
  18. return xml.Header + bytes.NewBuffer(b).String()
  19. }
  20. func NewXmlByDefaultSuccess(data ...interface{}) Xml {
  21. return NewXml(true, 0, JsonMsgDefaultOk, data...)
  22. }
  23. func NewXmlByDefaultFailed(data ...interface{}) Xml {
  24. return NewXml(false, 1, JsonMsgDefaultFailed, data...)
  25. }
  26. func NewXml(status bool, code int, msg string, data ...interface{}) Xml {
  27. if len(data) > 0 {
  28. return Xml{
  29. Code: code,
  30. Data: data[0],
  31. Msg: msg,
  32. Status: status,
  33. }
  34. } else {
  35. return Xml{
  36. Code: code,
  37. Data: nil,
  38. Msg: msg,
  39. Status: status,
  40. }
  41. }
  42. }