enterprise/server/session/base.go

76 lines
1.3 KiB
Go

package session
import (
"encoding/json"
)
type BaseResponse struct {
Code int `json:"code"`
Message string `json:"message"`
}
type CommonResponse struct {
BaseResponse
Data interface{} `json:"data"`
}
type RawResponse struct {
BaseResponse
Data json.RawMessage `json:"data"`
}
type ListResponse struct {
BaseResponse
Data struct {
Total int64 `json:"total"`
TotalAmount string `json:"totalAmount,omitempty"`
Items interface{} `json:"items"`
} `json:"data,omitempty"`
}
var (
rspOk *BaseResponse
)
func init() {
rspOk = new(BaseResponse)
rspOk.Message = "OK"
rspOk.Code = 0
}
func NewRspOk() *BaseResponse {
return rspOk
}
func NewRawRsp(raw []byte) *RawResponse {
rsp := &RawResponse{
BaseResponse: BaseResponse{
Code: rspOk.Code,
Message: rspOk.Message,
},
}
rsp.Data = raw
return rsp
}
func NewListRsp(total int64, items interface{}) *ListResponse {
rsp := &ListResponse{
BaseResponse: BaseResponse{
Code: rspOk.Code,
Message: rspOk.Message,
},
}
rsp.Data.Total = total
rsp.Data.Items = items
return rsp
}
func NewRsp(data interface{}) *CommonResponse {
return &CommonResponse{
BaseResponse: BaseResponse{
Code: rspOk.Code,
Message: rspOk.Message,
},
Data: data,
}
}