作者 唐旭辉

统一响应结构修改

... ... @@ -12,6 +12,6 @@ RUN mkdir -p $APP_DIR
ADD . $APP_DIR
# Compile the binary and statically link
RUN cd $APP_DIR && CGO_ENABLED=0 go build -mod=vendor -ldflags '-d -w -s'
RUN cd $APP_DIR && CGO_ENABLED=0 go build -mod=vendor -ldflags '-d -w -s' -o oppmg
WORKDIR $APP_DIR
EXPOSE 8080
... ...
... ... @@ -7,6 +7,8 @@ import (
serveauth "oppmg/services/auth"
"oppmg/storage/redisdata"
"github.com/astaxie/beego"
"github.com/astaxie/beego/plugins/cors"
"github.com/astaxie/beego/context"
... ... @@ -70,10 +72,13 @@ var AuthToken = func(ctx *context.Context) {
ctx.Output.JSON(msg, false, false)
return
}
if storetoken.AccessToken != accesstoken {
msg = protocol.NewMesage("10025")
ctx.Output.JSON(msg, false, false)
return
if beego.BConfig.RunMode == "prod" {
//校验是否是单客户端操作
if storetoken.AccessToken != accesstoken {
msg = protocol.NewMesage("10025")
ctx.Output.JSON(msg, false, false)
return
}
}
ctx.Input.SetData(protocol.HeaderCompanyid, mtoken.CompanyID)
ctx.Input.SetData(protocol.HeaderUserid, mtoken.UID)
... ...
... ... @@ -2,12 +2,12 @@ package protocol
//RequestPageInfo 分页获取数据
type RequestPageInfo struct {
PageIndex int `json:"page_index"`
PageSize int `json:"page_size"`
PageIndex int `json:"page"`
PageSize int `json:"pageSize"`
}
//ResponsePageInfo 分页信息
type ResponsePageInfo struct {
TotalPage int `json:"total_page"`
CurrentPage int `json:"current_page"`
TotalPage int `json:"totalRow"`
CurrentPage int `json:"pageNumber"`
}
... ...
... ... @@ -31,15 +31,19 @@ type ErrorCode struct {
//ResponseMessage 统一返回消息结构体
type ResponseMessage struct {
ErrorCode
Data interface{} `json:"data"`
Errno int `json:"code"`
Errmsg string `json:"msg"`
Data interface{} `json:"data"`
}
func NewMesage(code string) *ResponseMessage {
return &ResponseMessage{
ErrorCode: SearchErr(code),
Data: nil,
ecode := SearchErr(code)
rsp := &ResponseMessage{
Errno: transformCode(ecode.Errno),
Errmsg: ecode.Errmsg,
Data: nil,
}
return rsp
}
//ErrWithMessage 自定义错误结构
... ... @@ -79,10 +83,7 @@ func (e ErrWithMessage) Unwrap() error {
//ParseToMessage 实现CustomErrParse的接口
func (e ErrWithMessage) ParseToMessage() *ResponseMessage {
return &ResponseMessage{
ErrorCode: e.ErrorCode,
Data: nil,
}
return NewMesage(e.Errno)
}
func SearchErr(code string) ErrorCode {
... ... @@ -92,13 +93,14 @@ func SearchErr(code string) ErrorCode {
func NewReturnResponse(data interface{}, eRR error) (msg *ResponseMessage) {
// var msg *ResponseMessage
if eRR == nil {
msg = NewMesage("00000")
msg = NewMesage("0")
msg.Data = data
return msg
}
if x, ok := eRR.(CustomErrParse); ok {
return x.ParseToMessage()
}
return NewMesage("1")
}
... ...
... ... @@ -2,12 +2,8 @@ package protocol
var errmessge ErrorMap = map[string]string{
//操作
"00000": "成功",
"1": "无效请求",
"2": "添加成功",
"3": "修改成功",
"4": "删除成功",
"0": "成功",
"1": "无效请求",
//角色相关
"10001": "请先删除该分组下的其他角色",
"10002": "请先删除该角色下的人员",
... ... @@ -22,3 +18,16 @@ var errmessge ErrorMap = map[string]string{
"10026": "登录凭证过期",
"10027": "无操作权限",
}
//错误码转换 ,兼容需要
func transformCode(code string) int {
switch code {
case "0":
return 0 //登录成功
case "10026":
return 2 //token过期
case "10024":
return 3 //token完全失效
}
return -1 //请求成功,但业务检查不通过
}
... ...