change xorm.io

This commit is contained in:
liangzy 2020-07-10 10:51:40 +08:00
parent fa67fc4233
commit 42bbd3f0f5
5 changed files with 71 additions and 46 deletions

View File

@ -2,7 +2,7 @@ package mvc
import (
"github.com/astaxie/beego"
"golib.gaore.com/GaoreGo/beegoinit/response/json"
"golib.gaore.com/GaoreGo/beegoinit/response"
"strings"
)
@ -25,7 +25,7 @@ func (this *BaseController) Prepare() {
}
func (this *BaseController) RespJson(status bool, code int, msg string, data interface{}) {
this.Data["json"] = &json.Json{
this.Data["json"] = &response.Json{
Code: code,
Data: data,
Msg: msg,

51
response/json.go Normal file
View File

@ -0,0 +1,51 @@
package response
import (
"bytes"
"encoding/json"
"golib.gaore.com/GaoreGo/grlogs"
)
const (
JsonMsgDefaultOk = "ok"
JsonMsgDefaultFailed = "unknown"
)
type Json struct {
Code int `json:"code"`
Data interface{} `json:"data"`
Msg string `json:"msg"`
Status bool `json:"status"`
}
func (j *Json) String() string {
b, err := json.Marshal(j)
grlogs.Debug(err)
return bytes.NewBuffer(b).String()
}
func NewJsonByDefaultSuccess(data ...interface{}) *Json {
return NewJson(true, 0, JsonMsgDefaultOk, data...)
}
func NewJsonByDefaultFailed(data ...interface{}) *Json {
return NewJson(false, 1, JsonMsgDefaultFailed, data...)
}
func NewJson(status bool, code int, msg string, data ...interface{}) *Json {
if len(data) > 0 {
return &Json{
Code: code,
Data: data[0],
Msg: msg,
Status: status,
}
} else {
return &Json{
Code: code,
Data: nil,
Msg: msg,
Status: status,
}
}
}

View File

@ -1,44 +0,0 @@
package json
type Json struct {
Code int `json:"code"`
Data interface{} `json:"data"`
Msg string `json:"msg"`
Status bool `json:"status"`
}
func GetDefaultJsonSuccessResponse() *Json {
return &Json{
Code: 0,
Data: nil,
Msg: "ok",
Status: true,
}
}
func GetDefaultJsonFailedResponse() *Json {
return &Json{
Code: 1,
Data: nil,
Msg: "unknow",
Status: false,
}
}
func New(status bool, code int, msg string, data ...interface{}) *Json {
if len(data) > 0 {
return &Json{
Code: code,
Data: data[0],
Msg: msg,
Status: status,
}
} else {
return &Json{
Code: code,
Data: nil,
Msg: msg,
Status: status,
}
}
}

10
response/json_test.go Normal file
View File

@ -0,0 +1,10 @@
package response
import (
"fmt"
"testing"
)
func TestNewJsonByDefaultSuccess(t *testing.T) {
fmt.Println(NewJsonByDefaultSuccess([]int{1, 2, 3}).String())
}

8
response/xml.go Normal file
View File

@ -0,0 +1,8 @@
package response
type Xml struct {
Code int `xml:"code"`
Data interface{} `xml:"data"`
Msg string `xml:"msg"`
Status bool `xml:"status"`
}