2020-07-10 10:51:40 +08:00
|
|
|
package response
|
|
|
|
|
2020-08-11 14:31:27 +08:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/xml"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
2020-07-10 10:51:40 +08:00
|
|
|
type Xml struct {
|
|
|
|
Code int `xml:"code"`
|
|
|
|
Data interface{} `xml:"data"`
|
|
|
|
Msg string `xml:"msg"`
|
|
|
|
Status bool `xml:"status"`
|
|
|
|
}
|
2020-08-11 14:31:27 +08:00
|
|
|
|
|
|
|
func (x *Xml) String() string {
|
|
|
|
b, err := xml.MarshalIndent(x, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
log.Println("xml encode", err)
|
|
|
|
}
|
|
|
|
return xml.Header + bytes.NewBuffer(b).String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewXmlByDefaultSuccess(data ...interface{}) Xml {
|
|
|
|
return NewXml(true, 0, JsonMsgDefaultOk, data...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewXmlByDefaultFailed(data ...interface{}) Xml {
|
|
|
|
return NewXml(false, 1, JsonMsgDefaultFailed, data...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewXml(status bool, code int, msg string, data ...interface{}) Xml {
|
|
|
|
if len(data) > 0 {
|
|
|
|
return Xml{
|
|
|
|
Code: code,
|
|
|
|
Data: data[0],
|
|
|
|
Msg: msg,
|
|
|
|
Status: status,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Xml{
|
|
|
|
Code: code,
|
|
|
|
Data: nil,
|
|
|
|
Msg: msg,
|
|
|
|
Status: status,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|