Browse Source

change xorm.io

tags/v1.0.8
liangzy 3 years ago
parent
commit
42bbd3f0f5
5 changed files with 71 additions and 46 deletions
  1. +2
    -2
      mvc/controller.go
  2. +51
    -0
      response/json.go
  3. +0
    -44
      response/json/json.go
  4. +10
    -0
      response/json_test.go
  5. +8
    -0
      response/xml.go

+ 2
- 2
mvc/controller.go 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
- 0
response/json.go 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,
}
}
}

+ 0
- 44
response/json/json.go 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
- 0
response/json_test.go 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
- 0
response/xml.go 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"`
}

Loading…
Cancel
Save