作者 tangxvhui
package common
import "fmt"
type Error struct {
Code int
err error
}
func (e Error)Error()string{
if e.err==nil{
return ""
}
return e.err.Error()
}
func NewError(code int,e error)Error{
return Error{
Code:code,
err:e,
}
}
func NewErrorWithMsg(code int,msg string)Error{
return NewError(code,fmt.Errorf(msg))
}
... ...
package common
import (
"fmt"
"testing"
)
func Test_Error(t *testing.T){
e :=NewError(1,fmt.Errorf("%v","some error"))
t.Log(e,e.Code)
t.Logf("%s",e)
emsg :=NewErrorWithMsg(2,"some error")
t.Log(emsg,emsg.Code)
}
\ No newline at end of file
... ...
... ... @@ -4,17 +4,21 @@ go 1.13
require (
github.com/astaxie/beego v1.10.0
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/garyburd/redigo v1.6.0
github.com/gin-gonic/gin v1.4.0
github.com/go-sql-driver/mysql v1.4.1 // indirect
github.com/google/go-cmp v0.2.0
github.com/gorilla/websocket v1.4.1
github.com/lib/pq v1.2.0 // indirect
github.com/mattn/go-sqlite3 v1.11.0 // indirect
github.com/opentracing-contrib/go-stdlib v0.0.0-20190519235532-cf7a6c988dc9
github.com/opentracing/opentracing-go v1.1.0
github.com/spf13/viper v1.4.0
github.com/uber-go/atomic v1.4.0 // indirect
github.com/uber/jaeger-client-go v2.16.0+incompatible
github.com/uber/jaeger-lib v2.0.0+incompatible
github.com/ugorji/go v1.1.7 // indirect
... ...
... ... @@ -35,7 +35,6 @@ func (this *BaseController) AllowCross() {
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
//this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "uid, token,jwt, deviceid, appid,Content-Type,Authorization,from")
this.Ctx.WriteString("")
}
func (this *BaseController) Prepare() {
... ... @@ -54,26 +53,11 @@ func (this *BaseController) Prepare() {
this.Query[k] = input.Get(k)
}
if this.Ctx.Input.RequestBody != nil {
// contentType := this.Ctx.Input.Header("Content-type")
// if strings.HasPrefix(contentType, "application/json") {
this.ByteBody = this.Ctx.Input.RequestBody[:]
if len(this.ByteBody) < 1 {
this.ByteBody = []byte("{}")
}
this.RequestHead = &RequestHead{}
this.RequestHead.Token = this.Ctx.Input.Header("token")
this.RequestHead.Version = this.Ctx.Input.Header("version")
this.RequestHead.Os = this.Ctx.Input.Header("os")
this.RequestHead.From = this.Ctx.Input.Header("from")
this.RequestHead.Screen = this.Ctx.Input.Header("screen")
this.RequestHead.Model = this.Ctx.Input.Header("model")
this.RequestHead.Channel = this.Ctx.Input.Header("channel")
this.RequestHead.Net = this.Ctx.Input.Header("net")
this.RequestHead.DeviceId = this.Ctx.Input.Header("deviceid")
this.RequestHead.Uid, _ = strconv.ParseInt(this.Ctx.Input.Header("uid"), 10, 64)
this.RequestHead.AppId, _ = strconv.Atoi(this.Ctx.Input.Header("appid"))
this.RequestHead.LoginIp = this.Ctx.Input.IP()
this.RequestHead.Jwt = this.Ctx.Input.Header("jwt")
this.RequestHead = this.GetRequestHead()
this.RequestHead.SetRequestId(fmt.Sprintf("%v.%v.%s",this.RequestHead.Uid,time.GetTimeByYyyymmddhhmmss(),this.Ctx.Request.URL))
log.Info(fmt.Sprintf("====>Recv data from uid(%d) client:\nHeadData: %s\nRequestId:%s BodyData: %s", this.RequestHead.Uid, this.Ctx.Request.Header,this.RequestHead.GetRequestId(), string(this.ByteBody)))
}
... ... @@ -98,6 +82,24 @@ func (this *BaseController) Prepare() {
}
}
func (this *BaseController)GetRequestHead()*RequestHead{
reqHead := &RequestHead{}
reqHead.Token = this.Ctx.Input.Header("token")
reqHead.Version = this.Ctx.Input.Header("version")
reqHead.Os = this.Ctx.Input.Header("os")
reqHead.From = this.Ctx.Input.Header("from")
reqHead.Screen = this.Ctx.Input.Header("screen")
reqHead.Model = this.Ctx.Input.Header("model")
reqHead.Channel = this.Ctx.Input.Header("channel")
reqHead.Net = this.Ctx.Input.Header("net")
reqHead.DeviceId = this.Ctx.Input.Header("deviceid")
reqHead.Uid, _ = strconv.ParseInt(this.Ctx.Input.Header("uid"), 10, 64)
reqHead.AppId, _ = strconv.Atoi(this.Ctx.Input.Header("appid"))
reqHead.LoginIp = this.Ctx.Input.IP()
reqHead.Jwt = this.Ctx.Input.Header("jwt")
return reqHead
}
func (this *BaseController) Resp(msg *Message) {
this.Data["json"] = msg
... ... @@ -105,11 +107,9 @@ func (this *BaseController) Resp(msg *Message) {
}
func (this *BaseController) Finish() {
if this.Ctx.Input.Method() == "OPTIONS" {
return
}
strByte, _ := json.Marshal(this.Data["json"])
length := len(strByte)
if length > 5000 {
... ...
... ... @@ -6,6 +6,12 @@ import (
"time"
)
//type IMessage interface {
// MsgCode() int
// ErrMsg() string
// Data() interface{}
//}
const (
TOTAL_SWITCH_ON int = 0 // 通行
TOTAL_SWITCH_OFF int = 1 // 关闭,系统停止受理
... ... @@ -13,9 +19,9 @@ const (
)
type Message struct {
Errno int `json:"errno"`
Errmsg string `json:"errmsg"`
SysTime int64 `json:"sys_time"`
Errno int `json:"code"`
Errmsg string `json:"msg"`
SysTime int64 `json:"-"`
Data interface{} `json:"data"`
}
... ... @@ -55,7 +61,7 @@ func init() {
ErrnoMsg[1] = "系统错误"
ErrnoMsg[2] = "参数错误"
ErrnoMsg[3] = "系统升级中"
ErrnoMsg[4] = "您目前使用的版本过低,无法显示最新的相关内容,请使用响单单APP最新版本。"
ErrnoMsg[4] = "您目前使用的版本过低,无法显示最新的相关内容,请使用最新版本。"
ErrnoMsg[5] = "描述包含敏感词,请重新编辑"
ErrnoMsg[6] ="重复提交,请稍后再试"
}
\ No newline at end of file
}
\ No newline at end of file
... ...