作者 唐旭辉
1 package common 1 package common
2 2
3 -import "fmt" 3 +import (
  4 + "bytes"
  5 + "fmt"
  6 + "encoding/json"
  7 + "math/rand"
  8 +)
4 9
5 // Must panics if err is not nil. 10 // Must panics if err is not nil.
6 func Must(err error) { 11 func Must(err error) {
@@ -22,4 +27,32 @@ func Error2(v interface{}, err error) error { @@ -22,4 +27,32 @@ func Error2(v interface{}, err error) error {
22 27
23 func LogF(format string,args interface{})string{ 28 func LogF(format string,args interface{})string{
24 return fmt.Sprintf(format,args) 29 return fmt.Sprintf(format,args)
  30 +}
  31 +
  32 +func AssertJson(object interface{})string{
  33 + json,err :=json.Marshal(object)
  34 + if err!=nil{
  35 + return ""
  36 + }
  37 + return string(json)
  38 +}
  39 +
  40 +var randomChars = "ABCDEFGHJKMNPQRSTWXYZabcdefhjkmnprstwxyz2345678" /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
  41 +func RandomString(l int)string{
  42 + return RandomStringWithChars(l,randomChars)
  43 +}
  44 +
  45 +func RandomStringWithChars(l int,chars string)string{
  46 + if l<=0{
  47 + return ""
  48 + }
  49 + if len(chars)==0{
  50 + return ""
  51 + }
  52 + lenChars :=len(chars) -1
  53 + rsp :=bytes.NewBuffer(nil)
  54 + for i:=0;i<l;i++{
  55 + rsp.WriteByte(chars[rand.Intn(lenChars)])
  56 + }
  57 + return rsp.String()
25 } 58 }
  1 +package common
  2 +
  3 +import (
  4 + "testing"
  5 +)
  6 +
  7 +func Test_RandomString(t *testing.T){
  8 + input :=[]int{6,10,20,16,32}
  9 + for i:=range input{
  10 + l :=input[i]
  11 + out := RandomString(l)
  12 + if len(out)!=l{
  13 + t.Fatal("length not equal want :",l," out:",out)
  14 + }
  15 + }
  16 +}
  17 +
  18 +func Benchmark_RandomString(b *testing.B) {
  19 + input :=[]int{10,20,16,32}
  20 + l :=0
  21 + out :=""
  22 + for i:=0;i<b.N;i++{
  23 + l=i%4
  24 + l=input[l]
  25 + out = RandomString(l)
  26 + if len(out)!=l{
  27 + b.Fatal("length not equal want :",l," out:",out)
  28 + }
  29 + }
  30 +}
@@ -7,13 +7,6 @@ type Error struct { @@ -7,13 +7,6 @@ type Error struct {
7 err error 7 err error
8 } 8 }
9 9
10 -func (e Error)Error()string{  
11 - if e.err==nil{  
12 - return ""  
13 - }  
14 - return e.err.Error()  
15 -}  
16 -  
17 func NewError(code int,e error)Error{ 10 func NewError(code int,e error)Error{
18 return Error{ 11 return Error{
19 Code:code, 12 Code:code,
@@ -24,3 +17,14 @@ func NewError(code int,e error)Error{ @@ -24,3 +17,14 @@ func NewError(code int,e error)Error{
24 func NewErrorWithMsg(code int,msg string)Error{ 17 func NewErrorWithMsg(code int,msg string)Error{
25 return NewError(code,fmt.Errorf(msg)) 18 return NewError(code,fmt.Errorf(msg))
26 } 19 }
  20 +
  21 +func (e Error)Error()string{
  22 + if e.err==nil{
  23 + return ""
  24 + }
  25 + return e.err.Error()
  26 +}
  27 +
  28 +func(e Error)Unwrap()error{
  29 + return e.err
  30 +}
1 package common 1 package common
2 2
3 import ( 3 import (
  4 + "errors"
4 "fmt" 5 "fmt"
5 "testing" 6 "testing"
6 ) 7 )
@@ -12,4 +13,19 @@ func Test_Error(t *testing.T){ @@ -12,4 +13,19 @@ func Test_Error(t *testing.T){
12 13
13 emsg :=NewErrorWithMsg(2,"some error") 14 emsg :=NewErrorWithMsg(2,"some error")
14 t.Log(emsg,emsg.Code) 15 t.Log(emsg,emsg.Code)
  16 +}
  17 +
  18 +func Test_AssertError(t *testing.T){
  19 + var targetErr = NewError(1,fmt.Errorf("%v","some error"))
  20 + var e error =targetErr
  21 + if !errors.Is(e,targetErr){
  22 + t.Fatal("errors.Is not equal")
  23 + }
  24 + if errors.Unwrap(e) ==nil{
  25 + t.Fatal("errors.Unwrap not nil")
  26 + }
  27 + var commErr Error
  28 + if !errors.As(e,&commErr){
  29 + t.Fatal("errors.As error")
  30 + }
15 } 31 }
  1 +package log
  2 +
  3 +import (
  4 + "log"
  5 +)
  6 +
  7 +type ConsoleLog struct {
  8 +
  9 +}
  10 +
  11 +func newConsoleLog()Log{
  12 + return &ConsoleLog{}
  13 +}
  14 +
  15 +func(this *ConsoleLog)Debug(args ...interface{}){
  16 + //this.log.Debug(args...)
  17 + log.Println(args...)
  18 +}
  19 +
  20 +func(this *ConsoleLog)Info(args ...interface{}){
  21 + log.Println(args...)
  22 +}
  23 +
  24 +func(this *ConsoleLog)Warn(args ...interface{}){
  25 + log.Println(args...)
  26 +}
  27 +
  28 +func(this *ConsoleLog)Error(args ...interface{}){
  29 + log.Println(args...)
  30 +}
  31 +
  32 +func(this *ConsoleLog)Panic(args ...interface{}){
  33 + log.Println(args...)
  34 +}
  35 +
  36 +func(this *ConsoleLog)Fatal(args ...interface{}){
  37 + log.Println(args...)
  38 +}
@@ -12,7 +12,7 @@ type Log interface{ @@ -12,7 +12,7 @@ type Log interface{
12 } 12 }
13 13
14 var( 14 var(
15 - DefaultLog Log 15 + DefaultLog Log =newConsoleLog()
16 ) 16 )
17 17
18 func InitLog(conf config.Logger){ 18 func InitLog(conf config.Logger){
@@ -36,3 +36,4 @@ func Panic(args ...interface{}){ @@ -36,3 +36,4 @@ func Panic(args ...interface{}){
36 func Fatal(args ...interface{}){ 36 func Fatal(args ...interface{}){
37 DefaultLog.Fatal(args...) 37 DefaultLog.Fatal(args...)
38 } 38 }
  39 +
@@ -33,17 +33,16 @@ func (this *BaseController) Options() { @@ -33,17 +33,16 @@ func (this *BaseController) Options() {
33 func (this *BaseController) AllowCross() { 33 func (this *BaseController) AllowCross() {
34 this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*") 34 this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
35 this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") 35 this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
36 - //this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "uid, token,jwt, deviceid, appid,Content-Type,Authorization,from")  
37 - this.Ctx.WriteString("") 36 + this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "*")
  37 + //this.Ctx.WriteString("")
38 } 38 }
39 39
40 func (this *BaseController) Prepare() { 40 func (this *BaseController) Prepare() {
41 -  
42 this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*") 41 this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
  42 + this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "*")
43 if this.Ctx.Input.Method() == "OPTIONS" { 43 if this.Ctx.Input.Method() == "OPTIONS" {
44 this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE") 44 this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
45 - //this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "uid, token,jwt, deviceid, appid,Content-Type,Authorization,from")  
46 - this.Ctx.WriteString("") 45 + //this.Ctx.WriteString("")
47 return 46 return
48 } 47 }
49 48
@@ -118,34 +117,3 @@ func (this *BaseController) Finish() { @@ -118,34 +117,3 @@ func (this *BaseController) Finish() {
118 log.Info(fmt.Sprintf("<====Send to uid(%d) client: %d byte\nRequestId:%s RspBodyData: %s", this.RequestHead.Uid, length,this.RequestHead.GetRequestId(), string(strByte))) 117 log.Info(fmt.Sprintf("<====Send to uid(%d) client: %d byte\nRequestId:%s RspBodyData: %s", this.RequestHead.Uid, length,this.RequestHead.GetRequestId(), string(strByte)))
119 } 118 }
120 } 119 }
121 -  
122 -// BaseControllerCallBack  
123 -type BaseControllerCallBack struct {  
124 - beego.Controller  
125 - Query map[string]string  
126 - JSONBody map[string]interface{}  
127 - ByteBody []byte  
128 -}  
129 -  
130 -func (this *BaseControllerCallBack) Prepare() {  
131 - this.Query = map[string]string{}  
132 - input := this.Input()  
133 - for k := range input {  
134 - this.Query[k] = input.Get(k)  
135 - }  
136 -  
137 - if this.Ctx.Input.RequestBody != nil {  
138 - log.Info("RecvHead:", string(this.Ctx.Input.Header("Authorization")))  
139 - this.ByteBody = this.Ctx.Input.RequestBody  
140 - }  
141 -}  
142 -  
143 -func (this *BaseControllerCallBack) Resp(msg *Message) {  
144 - this.Data["json"] = msg  
145 - this.ServeJSON()  
146 -}  
147 -  
148 -func (this *BaseControllerCallBack) Finish() {  
149 - strByte, _ := json.Marshal(this.Data["json"])  
150 - log.Debug("<====Send to client:\n", string(strByte))  
151 -}  
@@ -3,7 +3,6 @@ package mybeego @@ -3,7 +3,6 @@ package mybeego
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "github.com/astaxie/beego" 5 "github.com/astaxie/beego"
6 - "time"  
7 ) 6 )
8 7
9 //type IMessage interface { 8 //type IMessage interface {
@@ -32,7 +31,7 @@ func NewMessage(code int) *Message { @@ -32,7 +31,7 @@ func NewMessage(code int) *Message {
32 return &Message{ 31 return &Message{
33 Errno: code, 32 Errno: code,
34 Errmsg: ErrnoMsg[code], 33 Errmsg: ErrnoMsg[code],
35 - SysTime: time.Now().Unix(), 34 + //SysTime: time.Now().Unix(),
36 } 35 }
37 } 36 }
38 37
@@ -64,4 +63,8 @@ func init() { @@ -64,4 +63,8 @@ func init() {
64 ErrnoMsg[4] = "您目前使用的版本过低,无法显示最新的相关内容,请使用最新版本。" 63 ErrnoMsg[4] = "您目前使用的版本过低,无法显示最新的相关内容,请使用最新版本。"
65 ErrnoMsg[5] = "描述包含敏感词,请重新编辑" 64 ErrnoMsg[5] = "描述包含敏感词,请重新编辑"
66 ErrnoMsg[6] ="重复提交,请稍后再试" 65 ErrnoMsg[6] ="重复提交,请稍后再试"
  66 +}
  67 +
  68 +func SetMessage(code int,msg string){
  69 + ErrnoMsg[code] = msg
67 } 70 }