正在显示
19 个修改的文件
包含
386 行增加
和
401 行删除
ability
0 → 100755
不能预览此文件类型
1 | package controllers | 1 | package controllers |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | + "crypto/sha256" | ||
5 | + "encoding/hex" | ||
4 | "fmt" | 6 | "fmt" |
5 | "strconv" | 7 | "strconv" |
6 | "strings" | 8 | "strings" |
7 | - "crypto/sha256" | ||
8 | - "encoding/hex" | ||
9 | 9 | ||
10 | - "gitlab.fjmaimaimai.com/mmm-go/ability/protocol" | ||
11 | - s_auth "gitlab.fjmaimaimai.com/mmm-go/ability/services/auth" | 10 | + "ability/protocol" |
11 | + s_auth "ability/services/auth" | ||
12 | 12 | ||
13 | - "gitlab.fjmaimaimai.com/mmm-go/gocomm/common" | ||
14 | - "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | ||
15 | - "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" | ||
16 | "github.com/astaxie/beego" | 13 | "github.com/astaxie/beego" |
17 | "github.com/astaxie/beego/context" | 14 | "github.com/astaxie/beego/context" |
18 | "github.com/astaxie/beego/validation" | 15 | "github.com/astaxie/beego/validation" |
19 | "github.com/prometheus/client_golang/prometheus" | 16 | "github.com/prometheus/client_golang/prometheus" |
17 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/common" | ||
18 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | ||
19 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" | ||
20 | ) | 20 | ) |
21 | 21 | ||
22 | -var( | 22 | +var ( |
23 | //prometheus 监控endpoint | 23 | //prometheus 监控endpoint |
24 | HTTPReqTotal *prometheus.CounterVec | 24 | HTTPReqTotal *prometheus.CounterVec |
25 | auth s_auth.IAuthService = &s_auth.AuthService{} | 25 | auth s_auth.IAuthService = &s_auth.AuthService{} |
@@ -29,10 +29,10 @@ type BaseController struct { | @@ -29,10 +29,10 @@ type BaseController struct { | ||
29 | mybeego.BaseController | 29 | mybeego.BaseController |
30 | } | 30 | } |
31 | 31 | ||
32 | -func init(){ | 32 | +func init() { |
33 | // HistogramVec 是一组Histogram | 33 | // HistogramVec 是一组Histogram |
34 | - HTTPReqTotal= prometheus.NewCounterVec(prometheus.CounterOpts{ | ||
35 | - Name: "request_count_vec",//http_requests_total | 34 | + HTTPReqTotal = prometheus.NewCounterVec(prometheus.CounterOpts{ |
35 | + Name: "request_count_vec", //http_requests_total | ||
36 | Help: "total number of http requests made.", | 36 | Help: "total number of http requests made.", |
37 | }, []string{"method", "path"}) | 37 | }, []string{"method", "path"}) |
38 | // 这里的"method"、"path"、"status" 都是label , "status" | 38 | // 这里的"method"、"path"、"status" 都是label , "status" |
@@ -43,18 +43,20 @@ func init(){ | @@ -43,18 +43,20 @@ func init(){ | ||
43 | // log.Error(err) | 43 | // log.Error(err) |
44 | //} | 44 | //} |
45 | } | 45 | } |
46 | + | ||
46 | var DefaultController *BaseController = &BaseController{} | 47 | var DefaultController *BaseController = &BaseController{} |
48 | + | ||
47 | //Valid valid struct | 49 | //Valid valid struct |
48 | -func (this *BaseController)Valid(obj interface{})(result bool ,msg *mybeego.Message){ | 50 | +func (this *BaseController) Valid(obj interface{}) (result bool, msg *mybeego.Message) { |
49 | /*校验*/ | 51 | /*校验*/ |
50 | var err error | 52 | var err error |
51 | - valid :=validation.Validation{} | ||
52 | - result,err= valid.Valid(obj) | ||
53 | - if err!=nil{ | 53 | + valid := validation.Validation{} |
54 | + result, err = valid.Valid(obj) | ||
55 | + if err != nil { | ||
54 | msg = mybeego.NewMessage(1) | 56 | msg = mybeego.NewMessage(1) |
55 | return | 57 | return |
56 | } | 58 | } |
57 | - if !result{ | 59 | + if !result { |
58 | for _, err := range valid.Errors { | 60 | for _, err := range valid.Errors { |
59 | log.Error(err.Key, err.Message) | 61 | log.Error(err.Key, err.Message) |
60 | } | 62 | } |
@@ -63,16 +65,17 @@ func (this *BaseController)Valid(obj interface{})(result bool ,msg *mybeego.Mess | @@ -63,16 +65,17 @@ func (this *BaseController)Valid(obj interface{})(result bool ,msg *mybeego.Mess | ||
63 | } | 65 | } |
64 | return | 66 | return |
65 | } | 67 | } |
68 | + | ||
66 | //GenMessage genarate a response message | 69 | //GenMessage genarate a response message |
67 | -func (this *BaseController)GenMessage(rsp interface{},err error)*mybeego.Message{ | 70 | +func (this *BaseController) GenMessage(rsp interface{}, err error) *mybeego.Message { |
68 | var msg *mybeego.Message | 71 | var msg *mybeego.Message |
69 | - if err==nil{ | 72 | + if err == nil { |
70 | msg = mybeego.NewMessage(0) | 73 | msg = mybeego.NewMessage(0) |
71 | msg.Data = rsp | 74 | msg.Data = rsp |
72 | return msg | 75 | return msg |
73 | } | 76 | } |
74 | //log.Error(err) | 77 | //log.Error(err) |
75 | - if e,ok :=err.(common.Error);ok{ | 78 | + if e, ok := err.(common.Error); ok { |
76 | msg = mybeego.NewMessage(e.Code) | 79 | msg = mybeego.NewMessage(e.Code) |
77 | msg.Data = rsp | 80 | msg.Data = rsp |
78 | return msg | 81 | return msg |
@@ -80,21 +83,22 @@ func (this *BaseController)GenMessage(rsp interface{},err error)*mybeego.Message | @@ -80,21 +83,22 @@ func (this *BaseController)GenMessage(rsp interface{},err error)*mybeego.Message | ||
80 | msg = mybeego.NewMessage(1) | 83 | msg = mybeego.NewMessage(1) |
81 | return msg | 84 | return msg |
82 | } | 85 | } |
86 | + | ||
83 | //获取请求头信息 | 87 | //获取请求头信息 |
84 | -func GetRequestHeader(ctx *context.Context)*protocol.RequestHeader{ | ||
85 | - h :=&protocol.RequestHeader{} | 88 | +func GetRequestHeader(ctx *context.Context) *protocol.RequestHeader { |
89 | + h := &protocol.RequestHeader{} | ||
86 | h.AccessToken = ctx.Input.Header("x-mmm-accesstoken") | 90 | h.AccessToken = ctx.Input.Header("x-mmm-accesstoken") |
87 | h.AppProject = ctx.Input.Header("x-mmm-appproject") | 91 | h.AppProject = ctx.Input.Header("x-mmm-appproject") |
88 | h.DeviceType = ctx.Input.Header("x-mmm-devicetype") | 92 | h.DeviceType = ctx.Input.Header("x-mmm-devicetype") |
89 | h.Sign = ctx.Input.Header("x-mmm-sign") | 93 | h.Sign = ctx.Input.Header("x-mmm-sign") |
90 | h.Uuid = ctx.Input.Header("x-mmm-uuid") | 94 | h.Uuid = ctx.Input.Header("x-mmm-uuid") |
91 | h.TimeStamp = ctx.Input.Header("x-mmm-timestamp") | 95 | h.TimeStamp = ctx.Input.Header("x-mmm-timestamp") |
92 | - h.Uid,_=strconv.ParseInt(ctx.Input.Header("uid"),10,64)//需要uid写入到header里面 | 96 | + h.Uid, _ = strconv.ParseInt(ctx.Input.Header("uid"), 10, 64) //需要uid写入到header里面 |
93 | return h | 97 | return h |
94 | } | 98 | } |
95 | 99 | ||
96 | //过滤器 | 100 | //过滤器 |
97 | -func FilterComm(ctx *context.Context){ | 101 | +func FilterComm(ctx *context.Context) { |
98 | //if strings.HasSuffix(ctx.Request.RequestURI,"login"){ | 102 | //if strings.HasSuffix(ctx.Request.RequestURI,"login"){ |
99 | // return | 103 | // return |
100 | //} | 104 | //} |
@@ -102,26 +106,26 @@ func FilterComm(ctx *context.Context){ | @@ -102,26 +106,26 @@ func FilterComm(ctx *context.Context){ | ||
102 | //统计 | 106 | //统计 |
103 | MetricCounter(ctx) | 107 | MetricCounter(ctx) |
104 | 108 | ||
105 | - if beego.BConfig.RunMode!="prod"{ | 109 | + if beego.BConfig.RunMode != "prod" { |
106 | return | 110 | return |
107 | } | 111 | } |
108 | 112 | ||
109 | //1.检查签名 | 113 | //1.检查签名 |
110 | - if !CheckSign(ctx){ | 114 | + if !CheckSign(ctx) { |
111 | return | 115 | return |
112 | } | 116 | } |
113 | //2.检查token是否有效 | 117 | //2.检查token是否有效 |
114 | - if !CheckToken(ctx){ | 118 | + if !CheckToken(ctx) { |
115 | return | 119 | return |
116 | } | 120 | } |
117 | //3.查重uuid | 121 | //3.查重uuid |
118 | - if !CheckUuid(ctx){ | 122 | + if !CheckUuid(ctx) { |
119 | return | 123 | return |
120 | } | 124 | } |
121 | return | 125 | return |
122 | } | 126 | } |
123 | 127 | ||
124 | -func MetricCounter(ctx *context.Context){ | 128 | +func MetricCounter(ctx *context.Context) { |
125 | // 请求数加1 | 129 | // 请求数加1 |
126 | HTTPReqTotal.With(prometheus.Labels{ | 130 | HTTPReqTotal.With(prometheus.Labels{ |
127 | "method": ctx.Request.Method, | 131 | "method": ctx.Request.Method, |
@@ -129,70 +133,73 @@ func MetricCounter(ctx *context.Context){ | @@ -129,70 +133,73 @@ func MetricCounter(ctx *context.Context){ | ||
129 | //"status": strconv.Itoa(c.Writer.Status()), | 133 | //"status": strconv.Itoa(c.Writer.Status()), |
130 | }).Inc() | 134 | }).Inc() |
131 | } | 135 | } |
136 | + | ||
132 | //检查签名 | 137 | //检查签名 |
133 | -func CheckSign(ctx *context.Context)(result bool){ | ||
134 | - var( | 138 | +func CheckSign(ctx *context.Context) (result bool) { |
139 | + var ( | ||
135 | h *protocol.RequestHeader | 140 | h *protocol.RequestHeader |
136 | sign string | 141 | sign string |
137 | signHex string | 142 | signHex string |
138 | ) | 143 | ) |
139 | result = true | 144 | result = true |
140 | - h =GetRequestHeader(ctx) | 145 | + h = GetRequestHeader(ctx) |
141 | //1.检查签名 | 146 | //1.检查签名 |
142 | - sign =fmt.Sprintf("v!(MmM%v%v%vMmM)i^",h.TimeStamp,h.Uuid,h.AccessToken) | ||
143 | - sha256:=sha256.New() | 147 | + sign = fmt.Sprintf("v!(MmM%v%v%vMmM)i^", h.TimeStamp, h.Uuid, h.AccessToken) |
148 | + sha256 := sha256.New() | ||
144 | sha256.Write([]byte(sign)) | 149 | sha256.Write([]byte(sign)) |
145 | signHex = hex.EncodeToString(sha256.Sum(nil)) | 150 | signHex = hex.EncodeToString(sha256.Sum(nil)) |
146 | - if strings.Compare(signHex,h.Sign)!=0{ | ||
147 | - msg :=mybeego.NewMessage(113) | ||
148 | - log.Error(fmt.Sprintf("%v req:%v resp:%v %v",ctx.Request.RequestURI,common.AssertJson(h),common.AssertJson(msg),signHex)) | 151 | + if strings.Compare(signHex, h.Sign) != 0 { |
152 | + msg := mybeego.NewMessage(113) | ||
153 | + log.Error(fmt.Sprintf("%v req:%v resp:%v %v", ctx.Request.RequestURI, common.AssertJson(h), common.AssertJson(msg), signHex)) | ||
149 | ctx.Output.JSON(msg, false, false) | 154 | ctx.Output.JSON(msg, false, false) |
150 | - result =false | 155 | + result = false |
151 | return | 156 | return |
152 | } | 157 | } |
153 | return | 158 | return |
154 | } | 159 | } |
160 | + | ||
155 | //检查access_token | 161 | //检查access_token |
156 | -func CheckToken(ctx *context.Context)(result bool){ | 162 | +func CheckToken(ctx *context.Context) (result bool) { |
157 | var ( | 163 | var ( |
158 | msg *mybeego.Message | 164 | msg *mybeego.Message |
159 | ) | 165 | ) |
160 | result = true | 166 | result = true |
161 | - defer func(){ | ||
162 | - if msg!=nil{ | ||
163 | - result =false | ||
164 | - ctx.Output.JSON(msg,false,false) | 167 | + defer func() { |
168 | + if msg != nil { | ||
169 | + result = false | ||
170 | + ctx.Output.JSON(msg, false, false) | ||
165 | } | 171 | } |
166 | }() | 172 | }() |
167 | token := ctx.Input.Header("x-mmm-accesstoken") | 173 | token := ctx.Input.Header("x-mmm-accesstoken") |
168 | - if rsp,err:=auth.CheckToken(&protocol.CheckTokenRequest{Token:token});(err!=nil || rsp.UserInfo==nil){ | ||
169 | - msg = DefaultController.GenMessage(rsp,err) | ||
170 | - log.Error(fmt.Sprintf("%v req:%v resp:%v",ctx.Request.RequestURI,token,common.AssertJson(msg))) | 174 | + if rsp, err := auth.CheckToken(&protocol.CheckTokenRequest{Token: token}); err != nil || rsp.UserInfo == nil { |
175 | + msg = DefaultController.GenMessage(rsp, err) | ||
176 | + log.Error(fmt.Sprintf("%v req:%v resp:%v", ctx.Request.RequestURI, token, common.AssertJson(msg))) | ||
171 | return | 177 | return |
172 | - }else{ | ||
173 | - if rsp.UserInfo!=nil{ | 178 | + } else { |
179 | + if rsp.UserInfo != nil { | ||
174 | //设置附加数据 | 180 | //设置附加数据 |
175 | - ctx.Request.Header.Add("uid",fmt.Sprintf("%v",rsp.UserInfo.Uuid)) | 181 | + ctx.Request.Header.Add("uid", fmt.Sprintf("%v", rsp.UserInfo.Uuid)) |
176 | } | 182 | } |
177 | } | 183 | } |
178 | return | 184 | return |
179 | } | 185 | } |
186 | + | ||
180 | //检查Uuid | 187 | //检查Uuid |
181 | -func CheckUuid(ctx *context.Context)(result bool){ | 188 | +func CheckUuid(ctx *context.Context) (result bool) { |
182 | var ( | 189 | var ( |
183 | msg *mybeego.Message | 190 | msg *mybeego.Message |
184 | ) | 191 | ) |
185 | result = true | 192 | result = true |
186 | - defer func(){ | ||
187 | - if msg!=nil{ | ||
188 | - result =false | ||
189 | - ctx.Output.JSON(msg,false,false) | 193 | + defer func() { |
194 | + if msg != nil { | ||
195 | + result = false | ||
196 | + ctx.Output.JSON(msg, false, false) | ||
190 | } | 197 | } |
191 | }() | 198 | }() |
192 | uuid := ctx.Input.Header("x-mmm-uuid") | 199 | uuid := ctx.Input.Header("x-mmm-uuid") |
193 | - msg = DefaultController.GenMessage(auth.CheckUuid(&protocol.CheckUuidRequest{Uuid:uuid})) | ||
194 | - if msg!=nil{ | ||
195 | - log.Error(fmt.Sprintf("%v req:%v resp:%v",ctx.Request.RequestURI,uuid,common.AssertJson(msg))) | 200 | + msg = DefaultController.GenMessage(auth.CheckUuid(&protocol.CheckUuidRequest{Uuid: uuid})) |
201 | + if msg != nil { | ||
202 | + log.Error(fmt.Sprintf("%v req:%v resp:%v", ctx.Request.RequestURI, uuid, common.AssertJson(msg))) | ||
196 | } | 203 | } |
197 | return | 204 | return |
198 | } | 205 | } |
@@ -3,15 +3,15 @@ package v1 | @@ -3,15 +3,15 @@ package v1 | ||
3 | import ( | 3 | import ( |
4 | "encoding/json" | 4 | "encoding/json" |
5 | 5 | ||
6 | - "gitlab.fjmaimaimai.com/mmm-go/ability/controllers" | ||
7 | - "gitlab.fjmaimaimai.com/mmm-go/ability/protocol" | ||
8 | - s_auth "gitlab.fjmaimaimai.com/mmm-go/ability/services/auth" | 6 | + "ability/controllers" |
7 | + "ability/protocol" | ||
8 | + s_auth "ability/services/auth" | ||
9 | 9 | ||
10 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | 10 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" |
11 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" | 11 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" |
12 | ) | 12 | ) |
13 | 13 | ||
14 | -var( | 14 | +var ( |
15 | auth s_auth.IAuthService = &s_auth.AuthService{} | 15 | auth s_auth.IAuthService = &s_auth.AuthService{} |
16 | ) | 16 | ) |
17 | 17 | ||
@@ -19,19 +19,20 @@ type AuthController struct { | @@ -19,19 +19,20 @@ type AuthController struct { | ||
19 | controllers.BaseController | 19 | controllers.BaseController |
20 | } | 20 | } |
21 | 21 | ||
22 | -//Login | ||
23 | -func(this *AuthController)Login(){ | 22 | +// Login |
23 | +// @router /login [post] | ||
24 | +func (this *AuthController) Login() { | ||
24 | var msg *mybeego.Message | 25 | var msg *mybeego.Message |
25 | - defer func(){ | 26 | + defer func() { |
26 | this.Resp(msg) | 27 | this.Resp(msg) |
27 | }() | 28 | }() |
28 | var request *protocol.LoginRequest | 29 | var request *protocol.LoginRequest |
29 | - if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{ | 30 | + if err := json.Unmarshal(this.ByteBody, &request); err != nil { |
30 | log.Error(err) | 31 | log.Error(err) |
31 | msg = mybeego.NewMessage(1) | 32 | msg = mybeego.NewMessage(1) |
32 | return | 33 | return |
33 | } | 34 | } |
34 | - if b,m :=this.Valid(request);!b{ | 35 | + if b, m := this.Valid(request); !b { |
35 | msg = m | 36 | msg = m |
36 | return | 37 | return |
37 | } | 38 | } |
@@ -39,18 +40,19 @@ func(this *AuthController)Login(){ | @@ -39,18 +40,19 @@ func(this *AuthController)Login(){ | ||
39 | } | 40 | } |
40 | 41 | ||
41 | //SmsCode | 42 | //SmsCode |
42 | -func(this *AuthController)SmsCode(){ | 43 | +// @router /smsCode [post] |
44 | +func (this *AuthController) SmsCode() { | ||
43 | var msg *mybeego.Message | 45 | var msg *mybeego.Message |
44 | - defer func(){ | 46 | + defer func() { |
45 | this.Resp(msg) | 47 | this.Resp(msg) |
46 | }() | 48 | }() |
47 | var request *protocol.SmsCodeRequest | 49 | var request *protocol.SmsCodeRequest |
48 | - if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{ | 50 | + if err := json.Unmarshal(this.ByteBody, &request); err != nil { |
49 | log.Error(err) | 51 | log.Error(err) |
50 | msg = mybeego.NewMessage(1) | 52 | msg = mybeego.NewMessage(1) |
51 | return | 53 | return |
52 | } | 54 | } |
53 | - if b,m :=this.Valid(request);!b{ | 55 | + if b, m := this.Valid(request); !b { |
54 | msg = m | 56 | msg = m |
55 | return | 57 | return |
56 | } | 58 | } |
@@ -58,18 +60,19 @@ func(this *AuthController)SmsCode(){ | @@ -58,18 +60,19 @@ func(this *AuthController)SmsCode(){ | ||
58 | } | 60 | } |
59 | 61 | ||
60 | //UpdateDevice | 62 | //UpdateDevice |
61 | -func(this *AuthController)UpdateDevice(){ | 63 | +// @router /updateDevice [post] |
64 | +func (this *AuthController) UpdateDevice() { | ||
62 | var msg *mybeego.Message | 65 | var msg *mybeego.Message |
63 | - defer func(){ | 66 | + defer func() { |
64 | this.Resp(msg) | 67 | this.Resp(msg) |
65 | }() | 68 | }() |
66 | var request *protocol.UpdateDeviceRequest | 69 | var request *protocol.UpdateDeviceRequest |
67 | - if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{ | 70 | + if err := json.Unmarshal(this.ByteBody, &request); err != nil { |
68 | log.Error(err) | 71 | log.Error(err) |
69 | msg = mybeego.NewMessage(1) | 72 | msg = mybeego.NewMessage(1) |
70 | return | 73 | return |
71 | } | 74 | } |
72 | - if b,m :=this.Valid(request);!b{ | 75 | + if b, m := this.Valid(request); !b { |
73 | msg = m | 76 | msg = m |
74 | return | 77 | return |
75 | } | 78 | } |
@@ -77,18 +80,19 @@ func(this *AuthController)UpdateDevice(){ | @@ -77,18 +80,19 @@ func(this *AuthController)UpdateDevice(){ | ||
77 | } | 80 | } |
78 | 81 | ||
79 | //AccessToken | 82 | //AccessToken |
80 | -func(this *AuthController)AccessToken(){ | 83 | +// @router /accessToken [post] |
84 | +func (this *AuthController) AccessToken() { | ||
81 | var msg *mybeego.Message | 85 | var msg *mybeego.Message |
82 | - defer func(){ | 86 | + defer func() { |
83 | this.Resp(msg) | 87 | this.Resp(msg) |
84 | }() | 88 | }() |
85 | var request *protocol.AccessTokenRequest | 89 | var request *protocol.AccessTokenRequest |
86 | - if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{ | 90 | + if err := json.Unmarshal(this.ByteBody, &request); err != nil { |
87 | log.Error(err) | 91 | log.Error(err) |
88 | msg = mybeego.NewMessage(1) | 92 | msg = mybeego.NewMessage(1) |
89 | return | 93 | return |
90 | } | 94 | } |
91 | - if b,m :=this.Valid(request);!b{ | 95 | + if b, m := this.Valid(request); !b { |
92 | msg = m | 96 | msg = m |
93 | return | 97 | return |
94 | } | 98 | } |
@@ -96,18 +100,19 @@ func(this *AuthController)AccessToken(){ | @@ -96,18 +100,19 @@ func(this *AuthController)AccessToken(){ | ||
96 | } | 100 | } |
97 | 101 | ||
98 | //RefreshToken | 102 | //RefreshToken |
99 | -func(this *AuthController)RefreshToken(){ | 103 | +// @router /refreshToken [post] |
104 | +func (this *AuthController) RefreshToken() { | ||
100 | var msg *mybeego.Message | 105 | var msg *mybeego.Message |
101 | - defer func(){ | 106 | + defer func() { |
102 | this.Resp(msg) | 107 | this.Resp(msg) |
103 | }() | 108 | }() |
104 | var request *protocol.RefreshTokenRequest | 109 | var request *protocol.RefreshTokenRequest |
105 | - if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{ | 110 | + if err := json.Unmarshal(this.ByteBody, &request); err != nil { |
106 | log.Error(err) | 111 | log.Error(err) |
107 | msg = mybeego.NewMessage(1) | 112 | msg = mybeego.NewMessage(1) |
108 | return | 113 | return |
109 | } | 114 | } |
110 | - if b,m :=this.Valid(request);!b{ | 115 | + if b, m := this.Valid(request); !b { |
111 | msg = m | 116 | msg = m |
112 | return | 117 | return |
113 | } | 118 | } |
1 | package v1 | 1 | package v1 |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | - "gitlab.fjmaimaimai.com/mmm-go/ability/controllers" | ||
5 | - "gitlab.fjmaimaimai.com/mmm-go/ability/protocol" | ||
6 | - "gitlab.fjmaimaimai.com/mmm-go/ability/services/upload" | 4 | + "ability/controllers" |
5 | + "ability/protocol" | ||
6 | + "ability/services/upload" | ||
7 | 7 | ||
8 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | 8 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" |
9 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" | 9 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" |
@@ -13,13 +13,14 @@ type UploadController struct { | @@ -13,13 +13,14 @@ type UploadController struct { | ||
13 | controllers.BaseController | 13 | controllers.BaseController |
14 | } | 14 | } |
15 | 15 | ||
16 | -//Image | ||
17 | -func(this *UploadController)Image(){ | ||
18 | - var( | 16 | +// Image |
17 | +// @router /image [post] | ||
18 | +func (this *UploadController) Image() { | ||
19 | + var ( | ||
19 | msg *mybeego.Message | 20 | msg *mybeego.Message |
20 | err error | 21 | err error |
21 | ) | 22 | ) |
22 | - defer func(){ | 23 | + defer func() { |
23 | this.Resp(msg) | 24 | this.Resp(msg) |
24 | }() | 25 | }() |
25 | var request = &protocol.FileRequest{} | 26 | var request = &protocol.FileRequest{} |
@@ -32,20 +33,21 @@ func(this *UploadController)Image(){ | @@ -32,20 +33,21 @@ func(this *UploadController)Image(){ | ||
32 | // msg = m | 33 | // msg = m |
33 | // return | 34 | // return |
34 | //} | 35 | //} |
35 | - if request.Files,err =this.GetFiles("file");err!=nil{ | 36 | + if request.Files, err = this.GetFiles("file"); err != nil { |
36 | log.Error(err) | 37 | log.Error(err) |
37 | return | 38 | return |
38 | } | 39 | } |
39 | msg = this.GenMessage(upload.Image(request)) | 40 | msg = this.GenMessage(upload.Image(request)) |
40 | } | 41 | } |
41 | 42 | ||
42 | -//Voice | ||
43 | -func(this *UploadController)Voice(){ | 43 | +// Image |
44 | +// @router /voice [post] | ||
45 | +func (this *UploadController) Voice() { | ||
44 | var ( | 46 | var ( |
45 | msg *mybeego.Message | 47 | msg *mybeego.Message |
46 | err error | 48 | err error |
47 | ) | 49 | ) |
48 | - defer func(){ | 50 | + defer func() { |
49 | this.Resp(msg) | 51 | this.Resp(msg) |
50 | }() | 52 | }() |
51 | var request = &protocol.FileRequest{} | 53 | var request = &protocol.FileRequest{} |
@@ -58,7 +60,7 @@ func(this *UploadController)Voice(){ | @@ -58,7 +60,7 @@ func(this *UploadController)Voice(){ | ||
58 | // msg = m | 60 | // msg = m |
59 | // return | 61 | // return |
60 | //} | 62 | //} |
61 | - if request.Files,err =this.GetFiles("file");err!=nil{ | 63 | + if request.Files, err = this.GetFiles("file"); err != nil { |
62 | log.Error(err) | 64 | log.Error(err) |
63 | return | 65 | return |
64 | } | 66 | } |
@@ -3,34 +3,31 @@ package v1 | @@ -3,34 +3,31 @@ package v1 | ||
3 | import ( | 3 | import ( |
4 | "encoding/json" | 4 | "encoding/json" |
5 | 5 | ||
6 | - "gitlab.fjmaimaimai.com/mmm-go/ability/controllers" | ||
7 | - "gitlab.fjmaimaimai.com/mmm-go/ability/protocol" | ||
8 | - "gitlab.fjmaimaimai.com/mmm-go/ability/services/version" | 6 | + "ability/controllers" |
7 | + "ability/protocol" | ||
8 | + "ability/services/version" | ||
9 | 9 | ||
10 | - "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" | ||
11 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | 10 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" |
12 | - | 11 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" |
13 | ) | 12 | ) |
14 | 13 | ||
15 | type VersionController struct { | 14 | type VersionController struct { |
16 | controllers.BaseController | 15 | controllers.BaseController |
17 | } | 16 | } |
18 | 17 | ||
19 | - | ||
20 | - | ||
21 | //GetLatestVersionInfo | 18 | //GetLatestVersionInfo |
22 | -func(this *VersionController)GetLatestVersionInfo(){ | 19 | +func (this *VersionController) GetLatestVersionInfo() { |
23 | var msg *mybeego.Message | 20 | var msg *mybeego.Message |
24 | - defer func(){ | 21 | + defer func() { |
25 | this.Resp(msg) | 22 | this.Resp(msg) |
26 | }() | 23 | }() |
27 | var request *protocol.GetLatestVersionInfoRequest | 24 | var request *protocol.GetLatestVersionInfoRequest |
28 | - if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{ | 25 | + if err := json.Unmarshal(this.ByteBody, &request); err != nil { |
29 | log.Error(err) | 26 | log.Error(err) |
30 | msg = mybeego.NewMessage(1) | 27 | msg = mybeego.NewMessage(1) |
31 | return | 28 | return |
32 | } | 29 | } |
33 | - if b,m :=this.Valid(request);!b{ | 30 | + if b, m := this.Valid(request); !b { |
34 | msg = m | 31 | msg = m |
35 | return | 32 | return |
36 | } | 33 | } |
1 | -module gitlab.fjmaimaimai.com/mmm-go/ability | 1 | +module ability |
2 | 2 | ||
3 | go 1.12 | 3 | go 1.12 |
4 | 4 | ||
5 | require ( | 5 | require ( |
6 | github.com/astaxie/beego v1.10.0 | 6 | github.com/astaxie/beego v1.10.0 |
7 | github.com/go-sql-driver/mysql v1.4.1 | 7 | github.com/go-sql-driver/mysql v1.4.1 |
8 | + github.com/prometheus/client_golang v1.1.0 | ||
8 | github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 | 9 | github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 |
9 | gitlab.fjmaimaimai.com/mmm-go/gocomm v0.0.1 | 10 | gitlab.fjmaimaimai.com/mmm-go/gocomm v0.0.1 |
10 | google.golang.org/appengine v1.6.2 // indirect | 11 | google.golang.org/appengine v1.6.2 // indirect |
1 | package repository | 1 | package repository |
2 | 2 | ||
3 | -import "gitlab.fjmaimaimai.com/mmm-go/ability/models" | 3 | +import "ability/models" |
4 | 4 | ||
5 | type IUserRepository interface { | 5 | type IUserRepository interface { |
6 | - GetUsersByMobile(mobile string)(v *models.Users, err error) | ||
7 | - GetUserInfoByMobile(mobile string)(v *models.UserInfo, err error) | 6 | + GetUsersByMobile(mobile string) (v *models.Users, err error) |
7 | + GetUserInfoByMobile(mobile string) (v *models.UserInfo, err error) | ||
8 | } | 8 | } |
9 | 9 | ||
10 | -func assertImplement(){ | 10 | +func assertImplement() { |
11 | var _ IUserRepository = (*UserRepository)(nil) | 11 | var _ IUserRepository = (*UserRepository)(nil) |
12 | } | 12 | } |
13 | 13 | ||
14 | -type UserRepository struct {} | 14 | +type UserRepository struct{} |
15 | 15 | ||
16 | -func (r *UserRepository)GetUsersByMobile(mobile string)(v *models.Users, err error) { | 16 | +func (r *UserRepository) GetUsersByMobile(mobile string) (v *models.Users, err error) { |
17 | return models.GetUsersByMobile(mobile) | 17 | return models.GetUsersByMobile(mobile) |
18 | } | 18 | } |
19 | 19 | ||
20 | -func (r *UserRepository)GetUserInfoByMobile(mobile string)(v *models.UserInfo, err error) { | 20 | +func (r *UserRepository) GetUserInfoByMobile(mobile string) (v *models.UserInfo, err error) { |
21 | return models.GetUserInfoByMobile(mobile) | 21 | return models.GetUserInfoByMobile(mobile) |
22 | } | 22 | } |
1 | package main | 1 | package main |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | + "ability/controllers" | ||
5 | + "ability/protocol" | ||
6 | + _ "ability/routers" | ||
4 | "github.com/astaxie/beego" | 7 | "github.com/astaxie/beego" |
5 | _ "github.com/go-sql-driver/mysql" | 8 | _ "github.com/go-sql-driver/mysql" |
6 | - "gitlab.fjmaimaimai.com/mmm-go/ability/controllers" | ||
7 | - "gitlab.fjmaimaimai.com/mmm-go/ability/protocol" | ||
8 | - _ "gitlab.fjmaimaimai.com/mmm-go/ability/routers" | ||
9 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/config" | 9 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/config" |
10 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | 10 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" |
11 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/orm" | 11 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/orm" |
12 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/redis" | 12 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/redis" |
13 | ) | 13 | ) |
14 | 14 | ||
15 | -func init(){ | 15 | +func init() { |
16 | log.InitLog(config.Logger{ | 16 | log.InitLog(config.Logger{ |
17 | - Filename:"app.log", | ||
18 | - Level:"7", | 17 | + Filename: "app.log", |
18 | + Level: "7", | ||
19 | }) | 19 | }) |
20 | - err:= redis.InitWithDb(100,beego.AppConfig.String("redis_add_port"),beego.AppConfig.String("redis_auth"),"0") | ||
21 | - if err!=nil{ | 20 | + err := redis.InitWithDb(100, beego.AppConfig.String("redis_add_port"), beego.AppConfig.String("redis_auth"), "0") |
21 | + if err != nil { | ||
22 | log.Fatal(err) | 22 | log.Fatal(err) |
23 | panic(err) | 23 | panic(err) |
24 | } | 24 | } |
25 | orm.NewBeeormEngine(config.Mysql{ | 25 | orm.NewBeeormEngine(config.Mysql{ |
26 | - DataSource:beego.AppConfig.String("data_source"), | 26 | + DataSource: beego.AppConfig.String("data_source"), |
27 | MaxIdle: 100, | 27 | MaxIdle: 100, |
28 | - MaxOpen:100, | 28 | + MaxOpen: 100, |
29 | }) | 29 | }) |
30 | } | 30 | } |
31 | 31 | ||
32 | func main() { | 32 | func main() { |
33 | - defer func(){ | 33 | + defer func() { |
34 | log.Info("app on stop!") | 34 | log.Info("app on stop!") |
35 | }() | 35 | }() |
36 | - beego.InsertFilter("/*",beego.BeforeRouter,controllers.FilterComm) | 36 | + beego.InsertFilter("/*", beego.BeforeRouter, controllers.FilterComm) |
37 | protocol.InitMessageCode() | 37 | protocol.InitMessageCode() |
38 | log.Info("app on start!") | 38 | log.Info("app on start!") |
39 | - log.Info("Beego Run Mode:",beego.BConfig.RunMode) | 39 | + log.Info("Beego Run Mode:", beego.BConfig.RunMode) |
40 | 40 | ||
41 | beego.Run() | 41 | beego.Run() |
42 | } | 42 | } |
43 | - |
1 | package protocol | 1 | package protocol |
2 | 2 | ||
3 | -import "gitlab.fjmaimaimai.com/mmm-go/ability/models" | 3 | +import "ability/models" |
4 | 4 | ||
5 | const ( | 5 | const ( |
6 | - LoginTypePassPord ="signInPassword" | ||
7 | - LoginTypeSmdcode ="signInCaptcha" | 6 | + LoginTypePassPord = "signInPassword" |
7 | + LoginTypeSmdcode = "signInCaptcha" | ||
8 | ) | 8 | ) |
9 | 9 | ||
10 | -var Nums =[]byte("0123456789") | 10 | +var Nums = []byte("0123456789") |
11 | 11 | ||
12 | type RequestHeader struct { | 12 | type RequestHeader struct { |
13 | TimeStamp string | 13 | TimeStamp string |
@@ -35,7 +35,7 @@ type LoginResponse struct { | @@ -35,7 +35,7 @@ type LoginResponse struct { | ||
35 | type SmsCodeRequest struct { | 35 | type SmsCodeRequest struct { |
36 | Phone string `json:"phone" valid:"Required;Mobile"` | 36 | Phone string `json:"phone" valid:"Required;Mobile"` |
37 | Content string `json:"-"` | 37 | Content string `json:"-"` |
38 | - SendType string `json:"send_type"`//sms_login_code sms_change_mobile | 38 | + SendType string `json:"send_type"` //sms_login_code sms_change_mobile |
39 | 39 | ||
40 | } | 40 | } |
41 | type SmsCodeResponse struct { | 41 | type SmsCodeResponse struct { |
@@ -93,11 +93,9 @@ type CheckTokenResponse struct { | @@ -93,11 +93,9 @@ type CheckTokenResponse struct { | ||
93 | UserInfo *models.UserInfo | 93 | UserInfo *models.UserInfo |
94 | } | 94 | } |
95 | 95 | ||
96 | - | ||
97 | /*CheckUuid */ | 96 | /*CheckUuid */ |
98 | type CheckUuidRequest struct { | 97 | type CheckUuidRequest struct { |
99 | Uuid string | 98 | Uuid string |
100 | } | 99 | } |
101 | type CheckUuidResponse struct { | 100 | type CheckUuidResponse struct { |
102 | } | 101 | } |
103 | - |
1 | package routers | 1 | package routers |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | + "ability/controllers/v1" | ||
4 | "github.com/astaxie/beego" | 5 | "github.com/astaxie/beego" |
5 | "github.com/prometheus/client_golang/prometheus/promhttp" | 6 | "github.com/prometheus/client_golang/prometheus/promhttp" |
6 | - "gitlab.fjmaimaimai.com/mmm-go/ability/controllers/v1" | ||
7 | ) | 7 | ) |
8 | 8 | ||
9 | var nsV1 *beego.Namespace | 9 | var nsV1 *beego.Namespace |
10 | 10 | ||
11 | func init() { | 11 | func init() { |
12 | - nsV1=beego.NewNamespace("/v1") | ||
13 | - /*user controller*/ | ||
14 | - { | ||
15 | - user :=&v1.UserController{} | ||
16 | - nsV1.Router("/user/login",user,"post:Login") | ||
17 | - } | ||
18 | - | ||
19 | - /*auth controller*/ | ||
20 | - { | ||
21 | - auth :=&v1.AuthController{} | ||
22 | - nsV1.Router("/auth/login",auth,"post:Login") | ||
23 | - nsV1.Router("/auth/accessToken",auth,"post:AccessToken") | ||
24 | - nsV1.Router("/auth/refreshToken",auth,"post:RefreshToken") | ||
25 | - nsV1.Router("/auth/smsCode",auth,"post:SmsCode") | ||
26 | - } | ||
27 | - | ||
28 | - /*image controller*/ | ||
29 | - { | ||
30 | - upload :=&v1.UploadController{} | ||
31 | - nsV1.Router("/upload/image",upload,"post:Image") | ||
32 | - nsV1.Router("/upload/voice",upload,"post:Voice") | ||
33 | - } | ||
34 | - | ||
35 | - { | ||
36 | - version :=&v1.VersionController{} | ||
37 | - nsV1.Router("/version/getLatestVersionInfo",version,"post:GetLatestVersionInfo") | ||
38 | - } | ||
39 | - | ||
40 | - beego.SetStaticPath("/file/ab",beego.AppConfig.String("source_path")) | ||
41 | - beego.Handler("/metrics", promhttp.Handler()) | 12 | + nsV1 := beego.NewNamespace("v1", |
13 | + beego.NSNamespace("auth", beego.NSInclude(&v1.AuthController{})), | ||
14 | + beego.NSNamespace("upload", beego.NSInclude(&v1.UploadController{})), | ||
15 | + beego.NSNamespace("version", beego.NSInclude(&v1.VersionController{})), | ||
16 | + ) | ||
42 | beego.AddNamespace(nsV1) | 17 | beego.AddNamespace(nsV1) |
18 | + beego.SetStaticPath("/file/ab", beego.AppConfig.String("source_path")) | ||
19 | + beego.Handler("/metrics", promhttp.Handler()) | ||
43 | } | 20 | } |
44 | - | ||
45 | - | ||
46 | - |
@@ -8,183 +8,190 @@ import ( | @@ -8,183 +8,190 @@ import ( | ||
8 | "strings" | 8 | "strings" |
9 | "time" | 9 | "time" |
10 | 10 | ||
11 | - "gitlab.fjmaimaimai.com/mmm-go/ability/internal/repository" | ||
12 | - "gitlab.fjmaimaimai.com/mmm-go/ability/models" | ||
13 | - "gitlab.fjmaimaimai.com/mmm-go/ability/protocol" | ||
14 | - s_sms "gitlab.fjmaimaimai.com/mmm-go/ability/services/sms" | 11 | + "ability/internal/repository" |
12 | + "ability/models" | ||
13 | + "ability/protocol" | ||
14 | + s_sms "ability/services/sms" | ||
15 | 15 | ||
16 | + "github.com/astaxie/beego" | ||
16 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/common" | 17 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/common" |
17 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | 18 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" |
18 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/redis" | 19 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/redis" |
19 | comm_time "gitlab.fjmaimaimai.com/mmm-go/gocomm/time" | 20 | comm_time "gitlab.fjmaimaimai.com/mmm-go/gocomm/time" |
20 | - "github.com/astaxie/beego" | ||
21 | ) | 21 | ) |
22 | 22 | ||
23 | type IAuthService interface { | 23 | type IAuthService interface { |
24 | - Login(request *protocol.LoginRequest)(rsp *protocol.LoginResponse,err error) | ||
25 | - AccessToken(request *protocol.AccessTokenRequest)(rsp *protocol.AccessTokenResponse,err error) | ||
26 | - RefreshToken(request *protocol.RefreshTokenRequest)(rsp *protocol.RefreshTokenResponse,err error) | ||
27 | - UpdateDevice(request *protocol.UpdateDeviceRequest)(rsp *protocol.UpdateDeviceResponse,err error) | ||
28 | - CheckToken(request *protocol.CheckTokenRequest)(rsp *protocol.CheckTokenResponse,err error) | ||
29 | - CheckUuid(request *protocol.CheckUuidRequest)(rsp *protocol.CheckUuidResponse,err error) | ||
30 | - SmsCode(request *protocol.SmsCodeRequest)(rsp *protocol.SmsCodeResponse,err error) | 24 | + Login(request *protocol.LoginRequest) (rsp *protocol.LoginResponse, err error) |
25 | + AccessToken(request *protocol.AccessTokenRequest) (rsp *protocol.AccessTokenResponse, err error) | ||
26 | + RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshTokenResponse, err error) | ||
27 | + UpdateDevice(request *protocol.UpdateDeviceRequest) (rsp *protocol.UpdateDeviceResponse, err error) | ||
28 | + CheckToken(request *protocol.CheckTokenRequest) (rsp *protocol.CheckTokenResponse, err error) | ||
29 | + CheckUuid(request *protocol.CheckUuidRequest) (rsp *protocol.CheckUuidResponse, err error) | ||
30 | + SmsCode(request *protocol.SmsCodeRequest) (rsp *protocol.SmsCodeResponse, err error) | ||
31 | } | 31 | } |
32 | 32 | ||
33 | -type AuthService struct {} | 33 | +type AuthService struct{} |
34 | 34 | ||
35 | -func assertImplement(){ | 35 | +func assertImplement() { |
36 | var _ IAuthService = (*AuthService)(nil) | 36 | var _ IAuthService = (*AuthService)(nil) |
37 | } | 37 | } |
38 | 38 | ||
39 | -var( | 39 | +var ( |
40 | //服务 | 40 | //服务 |
41 | sms s_sms.ISmsService = &s_sms.YunPianSmsService{} | 41 | sms s_sms.ISmsService = &s_sms.YunPianSmsService{} |
42 | 42 | ||
43 | //仓储 | 43 | //仓储 |
44 | - UserRepository repository.IUserRepository =&repository.UserRepository{} | 44 | + UserRepository repository.IUserRepository = &repository.UserRepository{} |
45 | ) | 45 | ) |
46 | 46 | ||
47 | //登录 | 47 | //登录 |
48 | -func (s *AuthService)Login(request *protocol.LoginRequest)(rsp *protocol.LoginResponse,err error){ | 48 | +func (s *AuthService) Login(request *protocol.LoginRequest) (rsp *protocol.LoginResponse, err error) { |
49 | var ( | 49 | var ( |
50 | user *models.Users | 50 | user *models.Users |
51 | userInfo *models.UserInfo | 51 | userInfo *models.UserInfo |
52 | result bool | 52 | result bool |
53 | ) | 53 | ) |
54 | - user,err =UserRepository.GetUsersByMobile(request.Phone) | ||
55 | - if err!=nil{ | 54 | + user, err = UserRepository.GetUsersByMobile(request.Phone) |
55 | + if err != nil { | ||
56 | log.Error(err) | 56 | log.Error(err) |
57 | - err =common.NewError(2020,err)//账号不存在 | 57 | + err = common.NewError(2020, err) //账号不存在 |
58 | return | 58 | return |
59 | } | 59 | } |
60 | switch request.GrantType { | 60 | switch request.GrantType { |
61 | case protocol.LoginTypePassPord: | 61 | case protocol.LoginTypePassPord: |
62 | - if strings.Compare(user.Password,request.PassWord)==0{ | 62 | + if strings.Compare(user.Password, request.PassWord) == 0 { |
63 | goto Success | 63 | goto Success |
64 | - }else{ | ||
65 | - err =common.NewError(2021,err)//登录密码错误 | 64 | + } else { |
65 | + err = common.NewError(2021, err) //登录密码错误 | ||
66 | return | 66 | return |
67 | } | 67 | } |
68 | break | 68 | break |
69 | case protocol.LoginTypeSmdcode: | 69 | case protocol.LoginTypeSmdcode: |
70 | - if result,err=CheckSmsCode(request.Phone,request.Code,protocol.SmsLoginCode);result && err==nil{ | 70 | + if result, err = CheckSmsCode(request.Phone, request.Code, protocol.SmsLoginCode); result && err == nil { |
71 | goto Success | 71 | goto Success |
72 | - }else{ | 72 | + } else { |
73 | return | 73 | return |
74 | } | 74 | } |
75 | default: | 75 | default: |
76 | - err =fmt.Errorf("grantType error") | 76 | + err = fmt.Errorf("grantType error") |
77 | return | 77 | return |
78 | } | 78 | } |
79 | - Success: | 79 | +Success: |
80 | { | 80 | { |
81 | - userInfo,err =UserRepository.GetUserInfoByMobile(request.Phone) | ||
82 | - if err!=nil{ | 81 | + userInfo, err = UserRepository.GetUserInfoByMobile(request.Phone) |
82 | + if err != nil { | ||
83 | log.Error(err) | 83 | log.Error(err) |
84 | return | 84 | return |
85 | } | 85 | } |
86 | - rsp =&protocol.LoginResponse{AuthCode:userInfo.Auth} | 86 | + rsp = &protocol.LoginResponse{AuthCode: userInfo.Auth} |
87 | return | 87 | return |
88 | } | 88 | } |
89 | return | 89 | return |
90 | } | 90 | } |
91 | + | ||
91 | //更新设备信息 | 92 | //更新设备信息 |
92 | -func (s *AuthService)UpdateDevice(request *protocol.UpdateDeviceRequest)(rsp *protocol.UpdateDeviceResponse,err error){ | ||
93 | - return nil,nil | 93 | +func (s *AuthService) UpdateDevice(request *protocol.UpdateDeviceRequest) (rsp *protocol.UpdateDeviceResponse, err error) { |
94 | + return nil, nil | ||
94 | } | 95 | } |
96 | + | ||
95 | //获取accessToken | 97 | //获取accessToken |
96 | -func (s *AuthService)AccessToken(request *protocol.AccessTokenRequest)(rsp *protocol.AccessTokenResponse,err error){ | 98 | +func (s *AuthService) AccessToken(request *protocol.AccessTokenRequest) (rsp *protocol.AccessTokenResponse, err error) { |
97 | var ( | 99 | var ( |
98 | userInfo *models.UserInfo | 100 | userInfo *models.UserInfo |
99 | ) | 101 | ) |
100 | - userInfo,err = models.GetUserInfoByClientId(request.ClientId) | ||
101 | - if err!=nil{ | 102 | + userInfo, err = models.GetUserInfoByClientId(request.ClientId) |
103 | + if err != nil { | ||
102 | return | 104 | return |
103 | } | 105 | } |
104 | - if strings.Compare(userInfo.Auth,request.AuthCode)!=0{ | ||
105 | - err = common.NewErrorWithMsg(2,"auth code error.") | 106 | + if strings.Compare(userInfo.Auth, request.AuthCode) != 0 { |
107 | + err = common.NewErrorWithMsg(2, "auth code error.") | ||
106 | return | 108 | return |
107 | } | 109 | } |
108 | //valid token | 110 | //valid token |
109 | rsp = &protocol.AccessTokenResponse{ | 111 | rsp = &protocol.AccessTokenResponse{ |
110 | - RefreshToken:userInfo.RefreshToken, | ||
111 | - AccessToken:userInfo.AccessToken, | ||
112 | - ExpiresIn:3600, | 112 | + RefreshToken: userInfo.RefreshToken, |
113 | + AccessToken: userInfo.AccessToken, | ||
114 | + ExpiresIn: 3600, | ||
113 | } | 115 | } |
114 | return | 116 | return |
115 | } | 117 | } |
118 | + | ||
116 | //刷新token | 119 | //刷新token |
117 | -func (s *AuthService)RefreshToken(request *protocol.RefreshTokenRequest)(rsp *protocol.RefreshTokenResponse,err error){ | 120 | +func (s *AuthService) RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshTokenResponse, err error) { |
118 | var ( | 121 | var ( |
119 | userInfo *models.UserInfo | 122 | userInfo *models.UserInfo |
120 | newAccess *protocol.Access | 123 | newAccess *protocol.Access |
121 | ) | 124 | ) |
122 | - userInfo,err = models.GetUserInfoByClientId(request.ClientId) | ||
123 | - if err!=nil{ | 125 | + userInfo, err = models.GetUserInfoByClientId(request.ClientId) |
126 | + if err != nil { | ||
124 | return | 127 | return |
125 | } | 128 | } |
126 | - if strings.Compare(userInfo.RefreshToken,request.RefreshToken)!=0{ | ||
127 | - err = common.NewErrorWithMsg(2,"refresh token error.") | 129 | + if strings.Compare(userInfo.RefreshToken, request.RefreshToken) != 0 { |
130 | + err = common.NewErrorWithMsg(2, "refresh token error.") | ||
128 | return | 131 | return |
129 | } | 132 | } |
130 | - request.Uid,request.LoginType = userInfo.Id,"mobile" | ||
131 | - if newAccess,err =refreshToken(request);err!=nil{ | 133 | + request.Uid, request.LoginType = userInfo.Id, "mobile" |
134 | + if newAccess, err = refreshToken(request); err != nil { | ||
132 | return | 135 | return |
133 | } | 136 | } |
134 | rsp = &protocol.RefreshTokenResponse{ | 137 | rsp = &protocol.RefreshTokenResponse{ |
135 | - AccessToken:newAccess.AccessToken, | ||
136 | - RefreshToken:newAccess.RefreshToken, | ||
137 | - ExpiresIn:3600, | 138 | + AccessToken: newAccess.AccessToken, |
139 | + RefreshToken: newAccess.RefreshToken, | ||
140 | + ExpiresIn: 3600, | ||
138 | } | 141 | } |
139 | return | 142 | return |
140 | } | 143 | } |
144 | + | ||
141 | //刷新token loginType mobile im web | 145 | //刷新token loginType mobile im web |
142 | -func refreshToken(request *protocol.RefreshTokenRequest)(rsp *protocol.Access,err error){ | ||
143 | - if request.Uid==0{ | 146 | +func refreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.Access, err error) { |
147 | + if request.Uid == 0 { | ||
144 | return | 148 | return |
145 | } | 149 | } |
146 | - return nil,nil | 150 | + return nil, nil |
147 | } | 151 | } |
152 | + | ||
148 | //检查token有效性 | 153 | //检查token有效性 |
149 | -func (s *AuthService)CheckToken(request *protocol.CheckTokenRequest)(rsp *protocol.CheckTokenResponse,err error){ | 154 | +func (s *AuthService) CheckToken(request *protocol.CheckTokenRequest) (rsp *protocol.CheckTokenResponse, err error) { |
150 | var ( | 155 | var ( |
151 | userInfo *models.UserInfo | 156 | userInfo *models.UserInfo |
152 | ) | 157 | ) |
153 | - if len(request.Token)==0{ | ||
154 | - err = common.NewErrorWithMsg(4141,"token not empty") | 158 | + if len(request.Token) == 0 { |
159 | + err = common.NewErrorWithMsg(4141, "token not empty") | ||
155 | return | 160 | return |
156 | } | 161 | } |
157 | - userInfo,err =models.GetUserInfoByToken(request.Token) | ||
158 | - if err!=nil{ | ||
159 | - err =common.NewError(4141,err) | 162 | + userInfo, err = models.GetUserInfoByToken(request.Token) |
163 | + if err != nil { | ||
164 | + err = common.NewError(4141, err) | ||
160 | return | 165 | return |
161 | } | 166 | } |
162 | - rsp =&protocol.CheckTokenResponse{ | ||
163 | - UserInfo:userInfo, | ||
164 | - IsValid:true, | 167 | + rsp = &protocol.CheckTokenResponse{ |
168 | + UserInfo: userInfo, | ||
169 | + IsValid: true, | ||
165 | } | 170 | } |
166 | return | 171 | return |
167 | } | 172 | } |
173 | + | ||
168 | //检查uuid 是否重复 | 174 | //检查uuid 是否重复 |
169 | -func (s *AuthService)CheckUuid(request *protocol.CheckUuidRequest)(rsp *protocol.CheckUuidResponse,err error){ | 175 | +func (s *AuthService) CheckUuid(request *protocol.CheckUuidRequest) (rsp *protocol.CheckUuidResponse, err error) { |
170 | var ( | 176 | var ( |
171 | logUuid *models.LogUuid | 177 | logUuid *models.LogUuid |
172 | ) | 178 | ) |
173 | - if len(request.Uuid)==0{ | ||
174 | - err = common.NewErrorWithMsg(4142,"uuid not empty") | 179 | + if len(request.Uuid) == 0 { |
180 | + err = common.NewErrorWithMsg(4142, "uuid not empty") | ||
175 | } | 181 | } |
176 | - logUuid,err=models.GetLogUuidByUuid(request.Uuid) | ||
177 | - if err==nil && logUuid!=nil{ | ||
178 | - err = common.NewErrorWithMsg(4142,"uuid not valid") | 182 | + logUuid, err = models.GetLogUuidByUuid(request.Uuid) |
183 | + if err == nil && logUuid != nil { | ||
184 | + err = common.NewErrorWithMsg(4142, "uuid not valid") | ||
179 | } | 185 | } |
180 | - models.AddLogUuid(&models.LogUuid{Uuid:request.Uuid}) | ||
181 | - rsp =&protocol.CheckUuidResponse{} | 186 | + models.AddLogUuid(&models.LogUuid{Uuid: request.Uuid}) |
187 | + rsp = &protocol.CheckUuidResponse{} | ||
182 | return | 188 | return |
183 | } | 189 | } |
190 | + | ||
184 | //短信验证码 T | 191 | //短信验证码 T |
185 | -func (s *AuthService)SmsCode(request *protocol.SmsCodeRequest)(rsp *protocol.SmsCodeResponse,err error){ | ||
186 | - var( | ||
187 | - value,key,msgContent string | 192 | +func (s *AuthService) SmsCode(request *protocol.SmsCodeRequest) (rsp *protocol.SmsCodeResponse, err error) { |
193 | + var ( | ||
194 | + value, key, msgContent string | ||
188 | smsInfo *protocol.SmsInfo | 195 | smsInfo *protocol.SmsInfo |
189 | ) | 196 | ) |
190 | msgContent = `【买买买信息科技】{{.Code}}({{.AppName}}手机验证码,请完成验证),如非本人操作,请忽略本短信` | 197 | msgContent = `【买买买信息科技】{{.Code}}({{.AppName}}手机验证码,请完成验证),如非本人操作,请忽略本短信` |
@@ -192,95 +199,96 @@ func (s *AuthService)SmsCode(request *protocol.SmsCodeRequest)(rsp *protocol.Sms | @@ -192,95 +199,96 @@ func (s *AuthService)SmsCode(request *protocol.SmsCodeRequest)(rsp *protocol.Sms | ||
192 | case protocol.SmsLoginCode: | 199 | case protocol.SmsLoginCode: |
193 | case protocol.SmsChangeMobile: | 200 | case protocol.SmsChangeMobile: |
194 | default: | 201 | default: |
195 | - err = common.NewErrorWithMsg(2,"send_type error.") | 202 | + err = common.NewErrorWithMsg(2, "send_type error.") |
196 | return | 203 | return |
197 | } | 204 | } |
198 | key = request.SendType | 205 | key = request.SendType |
199 | //check user phone exists | 206 | //check user phone exists |
200 | - if !redis.Hexists(key,request.Phone){ | 207 | + if !redis.Hexists(key, request.Phone) { |
201 | smsInfo = &protocol.SmsInfo{ | 208 | smsInfo = &protocol.SmsInfo{ |
202 | - CreateTime:time.Now().Unix(), | 209 | + CreateTime: time.Now().Unix(), |
203 | } | 210 | } |
204 | goto Send | 211 | goto Send |
205 | - }else{ | ||
206 | - if value,err =redis.Hget(key,request.Phone);err!=nil{ | 212 | + } else { |
213 | + if value, err = redis.Hget(key, request.Phone); err != nil { | ||
207 | log.Error(err) | 214 | log.Error(err) |
208 | return | 215 | return |
209 | } | 216 | } |
210 | - if err=json.Unmarshal([]byte(value),&smsInfo);err!=nil{ | 217 | + if err = json.Unmarshal([]byte(value), &smsInfo); err != nil { |
211 | log.Error(err) | 218 | log.Error(err) |
212 | return | 219 | return |
213 | } | 220 | } |
214 | //第二天重置 | 221 | //第二天重置 |
215 | - if smsInfo.LastTime<comm_time.GetUnixTimeByYyyymmdd(){ | ||
216 | - smsInfo.Count=0 | 222 | + if smsInfo.LastTime < comm_time.GetUnixTimeByYyyymmdd() { |
223 | + smsInfo.Count = 0 | ||
217 | smsInfo.CreateTime = time.Now().Unix() | 224 | smsInfo.CreateTime = time.Now().Unix() |
218 | } | 225 | } |
219 | - if smsInfo.Count>100{//TODO:limit send time | 226 | + if smsInfo.Count > 100 { //TODO:limit send time |
220 | return | 227 | return |
221 | } | 228 | } |
222 | goto Send | 229 | goto Send |
223 | } | 230 | } |
224 | - Send: | 231 | +Send: |
225 | { | 232 | { |
226 | - smsInfo.Code = common.RandomStringWithChars(6,string(protocol.Nums)) | ||
227 | - smsInfo.LastTime=time.Now().Unix() | ||
228 | - smsInfo.ErrorCount =0 | 233 | + smsInfo.Code = common.RandomStringWithChars(6, string(protocol.Nums)) |
234 | + smsInfo.LastTime = time.Now().Unix() | ||
235 | + smsInfo.ErrorCount = 0 | ||
229 | //Todo Lock | 236 | //Todo Lock |
230 | - smsInfo.Count +=1 | ||
231 | - if err=redis.Hset(key,request.Phone,common.AssertJson(smsInfo),-1);err!=nil{ | 237 | + smsInfo.Count += 1 |
238 | + if err = redis.Hset(key, request.Phone, common.AssertJson(smsInfo), -1); err != nil { | ||
232 | return | 239 | return |
233 | } | 240 | } |
234 | - tp :=template.New("sms_code") | 241 | + tp := template.New("sms_code") |
235 | tp.Parse(msgContent) | 242 | tp.Parse(msgContent) |
236 | - buf :=bytes.NewBuffer(nil) | 243 | + buf := bytes.NewBuffer(nil) |
237 | tp.Execute( | 244 | tp.Execute( |
238 | buf, | 245 | buf, |
239 | map[string]string{ | 246 | map[string]string{ |
240 | - "Code":smsInfo.Code, | ||
241 | - "AppName":beego.BConfig.AppName, | ||
242 | - },) | 247 | + "Code": smsInfo.Code, |
248 | + "AppName": beego.BConfig.AppName, | ||
249 | + }) | ||
243 | request.Content = buf.String() | 250 | request.Content = buf.String() |
244 | err = sms.Send(request) | 251 | err = sms.Send(request) |
245 | rsp = &protocol.SmsCodeResponse{ | 252 | rsp = &protocol.SmsCodeResponse{ |
246 | - Code:smsInfo.Code, | 253 | + Code: smsInfo.Code, |
247 | } | 254 | } |
248 | } | 255 | } |
249 | return | 256 | return |
250 | } | 257 | } |
258 | + | ||
251 | //验证短信验证码 T | 259 | //验证短信验证码 T |
252 | -func CheckSmsCode(phone ,code ,sendType string)(result bool,err error){ | ||
253 | - var( | 260 | +func CheckSmsCode(phone, code, sendType string) (result bool, err error) { |
261 | + var ( | ||
254 | value string | 262 | value string |
255 | smsInfo *protocol.SmsInfo | 263 | smsInfo *protocol.SmsInfo |
256 | ) | 264 | ) |
257 | - result =false | ||
258 | - if value,err =redis.Hget(sendType,phone);err!=nil{//protocol.SmsLoginCode | ||
259 | - err = common.NewErrorWithMsg(1009,"smscode expire") | 265 | + result = false |
266 | + if value, err = redis.Hget(sendType, phone); err != nil { //protocol.SmsLoginCode | ||
267 | + err = common.NewErrorWithMsg(1009, "smscode expire") | ||
260 | return | 268 | return |
261 | } | 269 | } |
262 | - if err=json.Unmarshal([]byte(value),&smsInfo);err!=nil{ | 270 | + if err = json.Unmarshal([]byte(value), &smsInfo); err != nil { |
263 | return | 271 | return |
264 | } | 272 | } |
265 | - if smsInfo.ErrorCount>=5{ | ||
266 | - err = common.NewErrorWithMsg(1011,"smscode over error times") | 273 | + if smsInfo.ErrorCount >= 5 { |
274 | + err = common.NewErrorWithMsg(1011, "smscode over error times") | ||
267 | return | 275 | return |
268 | } | 276 | } |
269 | - if (smsInfo.LastTime+60*5)<time.Now().Unix(){ | ||
270 | - err = common.NewErrorWithMsg(1009,fmt.Sprintf("smscode expire %v < %v",(smsInfo.LastTime+60*5),time.Now().Unix())) | 277 | + if (smsInfo.LastTime + 60*5) < time.Now().Unix() { |
278 | + err = common.NewErrorWithMsg(1009, fmt.Sprintf("smscode expire %v < %v", (smsInfo.LastTime+60*5), time.Now().Unix())) | ||
271 | goto Fail | 279 | goto Fail |
272 | } | 280 | } |
273 | - if smsInfo.Code == code{ | 281 | + if smsInfo.Code == code { |
274 | result = true | 282 | result = true |
275 | return | 283 | return |
276 | - }else{ | ||
277 | - err = common.NewErrorWithMsg(1012,"smscode error") | 284 | + } else { |
285 | + err = common.NewErrorWithMsg(1012, "smscode error") | ||
278 | goto Fail | 286 | goto Fail |
279 | } | 287 | } |
280 | - Fail: | 288 | +Fail: |
281 | { | 289 | { |
282 | - smsInfo.ErrorCount +=1 | ||
283 | - if err=redis.Hset(sendType,phone,common.AssertJson(smsInfo),-1);err!=nil{ | 290 | + smsInfo.ErrorCount += 1 |
291 | + if err = redis.Hset(sendType, phone, common.AssertJson(smsInfo), -1); err != nil { | ||
284 | return | 292 | return |
285 | } | 293 | } |
286 | } | 294 | } |
@@ -3,35 +3,35 @@ package auth | @@ -3,35 +3,35 @@ package auth | ||
3 | import ( | 3 | import ( |
4 | "testing" | 4 | "testing" |
5 | 5 | ||
6 | - "gitlab.fjmaimaimai.com/mmm-go/ability/protocol" | ||
7 | - "gitlab.fjmaimaimai.com/mmm-go/ability/tests" | 6 | + "ability/protocol" |
7 | + "ability/tests" | ||
8 | ) | 8 | ) |
9 | 9 | ||
10 | -func init(){ | 10 | +func init() { |
11 | tests.Init() | 11 | tests.Init() |
12 | } | 12 | } |
13 | 13 | ||
14 | -func Test_SmsCode(t *testing.T){ | 14 | +func Test_SmsCode(t *testing.T) { |
15 | var ( | 15 | var ( |
16 | resp *protocol.SmsCodeResponse | 16 | resp *protocol.SmsCodeResponse |
17 | err error | 17 | err error |
18 | out bool | 18 | out bool |
19 | ) | 19 | ) |
20 | - input :=[]*protocol.SmsCodeRequest{ | ||
21 | - {Phone:"18860183051",SendType:"sms_login_code"}, | ||
22 | - {Phone:"18860183052",SendType:"sms_login_code"}, | ||
23 | - {Phone:"18860183052",SendType:"sms_login_code"}, | ||
24 | - {Phone:"18860183053",SendType:"sms_change_mobile"}, | ||
25 | - {Phone:"18860183053",SendType:"sms_change_mobile"}, | ||
26 | - {Phone:"18860183054",SendType:"sms_change_mobile"}, | 20 | + input := []*protocol.SmsCodeRequest{ |
21 | + {Phone: "18860183051", SendType: "sms_login_code"}, | ||
22 | + {Phone: "18860183052", SendType: "sms_login_code"}, | ||
23 | + {Phone: "18860183052", SendType: "sms_login_code"}, | ||
24 | + {Phone: "18860183053", SendType: "sms_change_mobile"}, | ||
25 | + {Phone: "18860183053", SendType: "sms_change_mobile"}, | ||
26 | + {Phone: "18860183054", SendType: "sms_change_mobile"}, | ||
27 | } | 27 | } |
28 | var s IAuthService = &AuthService{} | 28 | var s IAuthService = &AuthService{} |
29 | - for i:=range input{ | ||
30 | - if resp,err =s.SmsCode(input[i]);err!=nil{ | ||
31 | - t.Fatal("send sms code error. input:",input[i],err) | 29 | + for i := range input { |
30 | + if resp, err = s.SmsCode(input[i]); err != nil { | ||
31 | + t.Fatal("send sms code error. input:", input[i], err) | ||
32 | } | 32 | } |
33 | - if out,err =CheckSmsCode(input[i].Phone,resp.Code,input[i].SendType);err!=nil || !out{ | ||
34 | - t.Fatal("check sms code error.",input[i].Phone,input[i].SendType,resp.Code,err) | 33 | + if out, err = CheckSmsCode(input[i].Phone, resp.Code, input[i].SendType); err != nil || !out { |
34 | + t.Fatal("check sms code error.", input[i].Phone, input[i].SendType, resp.Code, err) | ||
35 | } | 35 | } |
36 | } | 36 | } |
37 | } | 37 | } |
1 | package sms | 1 | package sms |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | - "gitlab.fjmaimaimai.com/mmm-go/ability/protocol" | 4 | + "ability/protocol" |
5 | 5 | ||
6 | "github.com/astaxie/beego" | 6 | "github.com/astaxie/beego" |
7 | "github.com/astaxie/beego/httplib" | 7 | "github.com/astaxie/beego/httplib" |
@@ -10,44 +10,44 @@ import ( | @@ -10,44 +10,44 @@ import ( | ||
10 | ) | 10 | ) |
11 | 11 | ||
12 | type ISmsService interface { | 12 | type ISmsService interface { |
13 | - Send(request *protocol.SmsCodeRequest)(err error) | 13 | + Send(request *protocol.SmsCodeRequest) (err error) |
14 | } | 14 | } |
15 | 15 | ||
16 | -type YunPianSmsService struct {} | 16 | +type YunPianSmsService struct{} |
17 | 17 | ||
18 | -func assertImplement(){ | 18 | +func assertImplement() { |
19 | var _ ISmsService = (*YunPianSmsService)(nil) | 19 | var _ ISmsService = (*YunPianSmsService)(nil) |
20 | } | 20 | } |
21 | -//发送 | ||
22 | 21 | ||
22 | +//发送 | ||
23 | 23 | ||
24 | -func(s *YunPianSmsService)Send(request *protocol.SmsCodeRequest)(err error){ | 24 | +func (s *YunPianSmsService) Send(request *protocol.SmsCodeRequest) (err error) { |
25 | var ( | 25 | var ( |
26 | resp *YunPianResponse | 26 | resp *YunPianResponse |
27 | ) | 27 | ) |
28 | - log.Debug("[sms] mobile:",request.Phone," content:",request.Content) | ||
29 | - if beego.BConfig.RunMode!="prod"{ | 28 | + log.Debug("[sms] mobile:", request.Phone, " content:", request.Content) |
29 | + if beego.BConfig.RunMode != "prod" { | ||
30 | return | 30 | return |
31 | } | 31 | } |
32 | - post:= httplib.Post(beego.AppConfig.String("yunpian_sms_sdk_url")) | ||
33 | - post.Param("apikey",beego.AppConfig.String("yunpian_app_key")) | ||
34 | - post.Param("mobile",request.Phone) | ||
35 | - post.Param("text",request.Content) | ||
36 | - if err= post.ToJSON(&resp);err!=nil{ | 32 | + post := httplib.Post(beego.AppConfig.String("yunpian_sms_sdk_url")) |
33 | + post.Param("apikey", beego.AppConfig.String("yunpian_app_key")) | ||
34 | + post.Param("mobile", request.Phone) | ||
35 | + post.Param("text", request.Content) | ||
36 | + if err = post.ToJSON(&resp); err != nil { | ||
37 | return | 37 | return |
38 | } | 38 | } |
39 | - if resp.Code!=0 || resp.Mobile!=request.Phone{ | ||
40 | - log.Error("yunpian send sms code:",resp.Code," error msg:",resp.Msg) | ||
41 | - err = common.NewErrorWithMsg(1,resp.Msg) | 39 | + if resp.Code != 0 || resp.Mobile != request.Phone { |
40 | + log.Error("yunpian send sms code:", resp.Code, " error msg:", resp.Msg) | ||
41 | + err = common.NewErrorWithMsg(1, resp.Msg) | ||
42 | } | 42 | } |
43 | return nil | 43 | return nil |
44 | } | 44 | } |
45 | 45 | ||
46 | type YunPianResponse struct { | 46 | type YunPianResponse struct { |
47 | Code int `json:"code"` //0代表发送成功,其他code代表出错,详细见"返回值说明"页面 | 47 | Code int `json:"code"` //0代表发送成功,其他code代表出错,详细见"返回值说明"页面 |
48 | - Msg string `json:"msg"`//例如""发送成功"",或者相应错误信息 | ||
49 | - Count int `json:"count"`//发送成功短信的计费条数(计费条数:70个字一条,超出70个字时按每67字一条计费) | ||
50 | - Mobile string `json:"string"`//发送手机号 | 48 | + Msg string `json:"msg"` //例如""发送成功"",或者相应错误信息 |
49 | + Count int `json:"count"` //发送成功短信的计费条数(计费条数:70个字一条,超出70个字时按每67字一条计费) | ||
50 | + Mobile string `json:"string"` //发送手机号 | ||
51 | Fee float64 `json:"fee"` //扣费金额,单位:元,类型:双精度浮点型/double | 51 | Fee float64 `json:"fee"` //扣费金额,单位:元,类型:双精度浮点型/double |
52 | Sid int64 `json:"sid"` //短信id,64位整型 | 52 | Sid int64 `json:"sid"` //短信id,64位整型 |
53 | } | 53 | } |
@@ -9,7 +9,7 @@ import ( | @@ -9,7 +9,7 @@ import ( | ||
9 | "path/filepath" | 9 | "path/filepath" |
10 | "time" | 10 | "time" |
11 | 11 | ||
12 | - "gitlab.fjmaimaimai.com/mmm-go/ability/protocol" | 12 | + "ability/protocol" |
13 | 13 | ||
14 | "github.com/astaxie/beego" | 14 | "github.com/astaxie/beego" |
15 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/common" | 15 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/common" |
@@ -18,16 +18,14 @@ import ( | @@ -18,16 +18,14 @@ import ( | ||
18 | ) | 18 | ) |
19 | 19 | ||
20 | //上传图片 | 20 | //上传图片 |
21 | -func Image(request *protocol.FileRequest)(rsp *protocol.FileResponse,err error){ | ||
22 | - var ( | ||
23 | - | ||
24 | - ) | ||
25 | - for i:=range request.Files{ | ||
26 | - f :=request.Files[i] | ||
27 | - subfix:=path.Ext(f.Filename) | 21 | +func Image(request *protocol.FileRequest) (rsp *protocol.FileResponse, err error) { |
22 | + var () | ||
23 | + for i := range request.Files { | ||
24 | + f := request.Files[i] | ||
25 | + subfix := path.Ext(f.Filename) | ||
28 | //文件格式不符合 | 26 | //文件格式不符合 |
29 | - if !(subfix==".jpg" || subfix==".gif" || subfix==".png"){ | ||
30 | - err = common.NewErrorWithMsg(2,"file format error") | 27 | + if !(subfix == ".jpg" || subfix == ".gif" || subfix == ".png") { |
28 | + err = common.NewErrorWithMsg(2, "file format error") | ||
31 | return | 29 | return |
32 | } | 30 | } |
33 | } | 31 | } |
@@ -35,57 +33,55 @@ func Image(request *protocol.FileRequest)(rsp *protocol.FileResponse,err error){ | @@ -35,57 +33,55 @@ func Image(request *protocol.FileRequest)(rsp *protocol.FileResponse,err error){ | ||
35 | return UploadFile(request) | 33 | return UploadFile(request) |
36 | } | 34 | } |
37 | 35 | ||
38 | -func Voice(request *protocol.FileRequest)(rsp *protocol.FileResponse,err error){ | ||
39 | - var ( | ||
40 | - | ||
41 | - ) | 36 | +func Voice(request *protocol.FileRequest) (rsp *protocol.FileResponse, err error) { |
37 | + var () | ||
42 | request.FileType = protocol.FileVoice | 38 | request.FileType = protocol.FileVoice |
43 | return UploadFile(request) | 39 | return UploadFile(request) |
44 | } | 40 | } |
45 | 41 | ||
46 | -func UploadFile(request *protocol.FileRequest)(rsp *protocol.FileResponse,err error){ | ||
47 | - if request.FileType==""{ | ||
48 | - request.FileType =protocol.FileImage | 42 | +func UploadFile(request *protocol.FileRequest) (rsp *protocol.FileResponse, err error) { |
43 | + if request.FileType == "" { | ||
44 | + request.FileType = protocol.FileImage | ||
49 | } | 45 | } |
50 | var ( | 46 | var ( |
51 | src multipart.File | 47 | src multipart.File |
52 | dst *os.File | 48 | dst *os.File |
53 | virtualPath string = beego.AppConfig.String("source_virtual_path") //虚拟路径 | 49 | virtualPath string = beego.AppConfig.String("source_virtual_path") //虚拟路径 |
54 | - sourcePath string = filepath.Join(beego.AppConfig.String("source_path"),request.FileType) //真实路径 | ||
55 | - date,filename string | 50 | + sourcePath string = filepath.Join(beego.AppConfig.String("source_path"), request.FileType) //真实路径 |
51 | + date, filename string | ||
56 | ) | 52 | ) |
57 | - rsp =&protocol.FileResponse{} | ||
58 | - date =comm_time.GetTimeByYyyymmdd() | ||
59 | - sourcePath = filepath.Join(sourcePath,date) | ||
60 | - if _,err=os.Stat(sourcePath);err!=nil{ | 53 | + rsp = &protocol.FileResponse{} |
54 | + date = comm_time.GetTimeByYyyymmdd() | ||
55 | + sourcePath = filepath.Join(sourcePath, date) | ||
56 | + if _, err = os.Stat(sourcePath); err != nil { | ||
61 | log.Error(err) | 57 | log.Error(err) |
62 | - if err=os.MkdirAll(sourcePath,0777);err!=nil{ | 58 | + if err = os.MkdirAll(sourcePath, 0777); err != nil { |
63 | log.Error(err) | 59 | log.Error(err) |
64 | return | 60 | return |
65 | } | 61 | } |
66 | } | 62 | } |
67 | - virtualPath=beego.AppConfig.String("source_host")+filepath.Join(virtualPath,request.FileType,date) | ||
68 | - for i:=range request.Files{ | ||
69 | - f :=request.Files[i] | ||
70 | - subfix:=path.Ext(f.Filename) | ||
71 | - filename =fmt.Sprintf("%v_%v%v",time.Now().Unix(),common.RandomString(32),subfix) | ||
72 | - src,err=f.Open() | ||
73 | - if err!=nil{ | 63 | + virtualPath = beego.AppConfig.String("source_host") + filepath.Join(virtualPath, request.FileType, date) |
64 | + for i := range request.Files { | ||
65 | + f := request.Files[i] | ||
66 | + subfix := path.Ext(f.Filename) | ||
67 | + filename = fmt.Sprintf("%v_%v%v", time.Now().Unix(), common.RandomString(32), subfix) | ||
68 | + src, err = f.Open() | ||
69 | + if err != nil { | ||
74 | log.Error(err) | 70 | log.Error(err) |
75 | return | 71 | return |
76 | } | 72 | } |
77 | defer src.Close() | 73 | defer src.Close() |
78 | - dst,err =os.OpenFile(filepath.Join(sourcePath,filename), os.O_RDWR | os.O_CREATE |os.O_TRUNC,0777) //file/ab/ 静态文件目录 | ||
79 | - if err!=nil{ | 74 | + dst, err = os.OpenFile(filepath.Join(sourcePath, filename), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777) //file/ab/ 静态文件目录 |
75 | + if err != nil { | ||
80 | log.Error(err) | 76 | log.Error(err) |
81 | return | 77 | return |
82 | } | 78 | } |
83 | defer dst.Close() | 79 | defer dst.Close() |
84 | - if _,err =io.Copy(dst,src);err!=nil{ | 80 | + if _, err = io.Copy(dst, src); err != nil { |
85 | log.Error(err) | 81 | log.Error(err) |
86 | return | 82 | return |
87 | } | 83 | } |
88 | - rsp.Paths = append(rsp.Paths,filepath.Join(virtualPath,filename)) | 84 | + rsp.Paths = append(rsp.Paths, filepath.Join(virtualPath, filename)) |
89 | } | 85 | } |
90 | return | 86 | return |
91 | } | 87 | } |
1 | package upload | 1 | package upload |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | - "testing" | ||
5 | - "mime/multipart" | ||
6 | "bytes" | 4 | "bytes" |
5 | + "mime/multipart" | ||
6 | + "testing" | ||
7 | 7 | ||
8 | - "gitlab.fjmaimaimai.com/mmm-go/ability/protocol" | ||
9 | - "gitlab.fjmaimaimai.com/mmm-go/ability/tests" | 8 | + "ability/protocol" |
9 | + "ability/tests" | ||
10 | ) | 10 | ) |
11 | 11 | ||
12 | -func init(){ | 12 | +func init() { |
13 | tests.Init() | 13 | tests.Init() |
14 | } | 14 | } |
15 | 15 | ||
16 | - | ||
17 | -func Test_Image(t *testing.T){ | 16 | +func Test_Image(t *testing.T) { |
18 | input := &protocol.FileRequest{} | 17 | input := &protocol.FileRequest{} |
19 | 18 | ||
20 | var bufReader bytes.Buffer | 19 | var bufReader bytes.Buffer |
21 | - mpWriter:=multipart.NewWriter(&bufReader) | ||
22 | - fw,err :=mpWriter.CreateFormFile("file_a","a.jpg")//a.txt | ||
23 | - if err!=nil{ | 20 | + mpWriter := multipart.NewWriter(&bufReader) |
21 | + fw, err := mpWriter.CreateFormFile("file_a", "a.jpg") //a.txt | ||
22 | + if err != nil { | ||
24 | t.Fatal(err) | 23 | t.Fatal(err) |
25 | return | 24 | return |
26 | } | 25 | } |
27 | fw.Write([]byte("this is a txt")) | 26 | fw.Write([]byte("this is a txt")) |
28 | - mpWriter.WriteField("name","tiprok") | ||
29 | - | 27 | + mpWriter.WriteField("name", "tiprok") |
30 | 28 | ||
31 | - fwb,err :=mpWriter.CreateFormFile("file_a","b.png") | ||
32 | - if err!=nil{ | 29 | + fwb, err := mpWriter.CreateFormFile("file_a", "b.png") |
30 | + if err != nil { | ||
33 | t.Fatal(err) | 31 | t.Fatal(err) |
34 | return | 32 | return |
35 | } | 33 | } |
36 | fwb.Write([]byte("this is b txt")) | 34 | fwb.Write([]byte("this is b txt")) |
37 | mpWriter.Close() | 35 | mpWriter.Close() |
38 | 36 | ||
39 | - mpReader:=multipart.NewReader(&bufReader,mpWriter.Boundary()) | ||
40 | - form,err :=mpReader.ReadForm(100) | ||
41 | - if err!=nil{ | 37 | + mpReader := multipart.NewReader(&bufReader, mpWriter.Boundary()) |
38 | + form, err := mpReader.ReadForm(100) | ||
39 | + if err != nil { | ||
42 | t.Fatal(err) | 40 | t.Fatal(err) |
43 | } | 41 | } |
44 | - for _,files :=range form.File{ | 42 | + for _, files := range form.File { |
45 | //for i:=range files{ | 43 | //for i:=range files{ |
46 | // file :=files[i] | 44 | // file :=files[i] |
47 | // if f,err :=file.Open();err!=nil{ | 45 | // if f,err :=file.Open();err!=nil{ |
@@ -53,11 +51,11 @@ func Test_Image(t *testing.T){ | @@ -53,11 +51,11 @@ func Test_Image(t *testing.T){ | ||
53 | // | 51 | // |
54 | // } | 52 | // } |
55 | //} | 53 | //} |
56 | - input.Files = append(input.Files,files...) | 54 | + input.Files = append(input.Files, files...) |
57 | } | 55 | } |
58 | 56 | ||
59 | - rsp,err :=Image(input) | ||
60 | - if err!=nil{ | 57 | + rsp, err := Image(input) |
58 | + if err != nil { | ||
61 | t.Fatal(err) | 59 | t.Fatal(err) |
62 | } | 60 | } |
63 | t.Log(rsp) | 61 | t.Log(rsp) |
@@ -4,7 +4,7 @@ package user | @@ -4,7 +4,7 @@ package user | ||
4 | //( | 4 | //( |
5 | // "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" | 5 | // "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" |
6 | // "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | 6 | // "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" |
7 | -// "gitlab.fjmaimaimai.com/mmm-go/ability/models" | 7 | +// "ability/models" |
8 | //) | 8 | //) |
9 | // | 9 | // |
10 | //func GetUserList(mobile string)*mybeego.Message{ | 10 | //func GetUserList(mobile string)*mybeego.Message{ |
@@ -3,37 +3,38 @@ package version | @@ -3,37 +3,38 @@ package version | ||
3 | import ( | 3 | import ( |
4 | "strconv" | 4 | "strconv" |
5 | 5 | ||
6 | - "gitlab.fjmaimaimai.com/mmm-go/ability/models" | ||
7 | - "gitlab.fjmaimaimai.com/mmm-go/ability/protocol" | 6 | + "ability/models" |
7 | + "ability/protocol" | ||
8 | ) | 8 | ) |
9 | 9 | ||
10 | -func GetLatestVersionInfo(request *protocol.GetLatestVersionInfoRequest)(rsp *protocol.GetLatestVersionInfoResponse,err error){ | 10 | +func GetLatestVersionInfo(request *protocol.GetLatestVersionInfoRequest) (rsp *protocol.GetLatestVersionInfoResponse, err error) { |
11 | var ( | 11 | var ( |
12 | versionInfo *protocol.VersionInfo | 12 | versionInfo *protocol.VersionInfo |
13 | ) | 13 | ) |
14 | - versionInfo,err = GetVersionInfo(request.VersionNo,request.Channel) | ||
15 | - if err!=nil{ | 14 | + versionInfo, err = GetVersionInfo(request.VersionNo, request.Channel) |
15 | + if err != nil { | ||
16 | return | 16 | return |
17 | } | 17 | } |
18 | - rsp =&protocol.GetLatestVersionInfoResponse{ | ||
19 | - Version:versionInfo, | 18 | + rsp = &protocol.GetLatestVersionInfoResponse{ |
19 | + Version: versionInfo, | ||
20 | } | 20 | } |
21 | return | 21 | return |
22 | } | 22 | } |
23 | + | ||
23 | //获取版本信息 | 24 | //获取版本信息 |
24 | -func GetVersionInfo(versionNo,channel int)(v *protocol.VersionInfo,err error){ | ||
25 | - cfgAppVersion,err :=models.GetCfgAppVersion(versionNo,channel) | ||
26 | - if err!=nil{ | 25 | +func GetVersionInfo(versionNo, channel int) (v *protocol.VersionInfo, err error) { |
26 | + cfgAppVersion, err := models.GetCfgAppVersion(versionNo, channel) | ||
27 | + if err != nil { | ||
27 | return | 28 | return |
28 | } | 29 | } |
29 | - v =&protocol.VersionInfo{ | ||
30 | - VersionName:cfgAppVersion.VersionName, | ||
31 | - VersionNo:strconv.Itoa(cfgAppVersion.VersionNo), | ||
32 | - Title:cfgAppVersion.Title, | ||
33 | - Content:cfgAppVersion.Content, | ||
34 | - DownloadPage:cfgAppVersion.DownloadPage, | ||
35 | - DownloadFile:cfgAppVersion.DownloadFile, | ||
36 | - UpdateType:cfgAppVersion.Type, | 30 | + v = &protocol.VersionInfo{ |
31 | + VersionName: cfgAppVersion.VersionName, | ||
32 | + VersionNo: strconv.Itoa(cfgAppVersion.VersionNo), | ||
33 | + Title: cfgAppVersion.Title, | ||
34 | + Content: cfgAppVersion.Content, | ||
35 | + DownloadPage: cfgAppVersion.DownloadPage, | ||
36 | + DownloadFile: cfgAppVersion.DownloadFile, | ||
37 | + UpdateType: cfgAppVersion.Type, | ||
37 | } | 38 | } |
38 | return | 39 | return |
39 | } | 40 | } |
1 | package tests | 1 | package tests |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | + "ability/protocol" | ||
4 | "github.com/astaxie/beego" | 5 | "github.com/astaxie/beego" |
5 | _ "github.com/go-sql-driver/mysql" | 6 | _ "github.com/go-sql-driver/mysql" |
6 | - "gitlab.fjmaimaimai.com/mmm-go/ability/protocol" | ||
7 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/config" | 7 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/config" |
8 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | 8 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" |
9 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/orm" | 9 | "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/orm" |
@@ -16,28 +16,28 @@ import ( | @@ -16,28 +16,28 @@ import ( | ||
16 | 16 | ||
17 | var one sync.Once | 17 | var one sync.Once |
18 | 18 | ||
19 | -func Init(){ | ||
20 | - one.Do(func(){ | 19 | +func Init() { |
20 | + one.Do(func() { | ||
21 | _, file, _, _ := runtime.Caller(0) | 21 | _, file, _, _ := runtime.Caller(0) |
22 | - apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".." + string(filepath.Separator)))) | 22 | + apppath, _ := filepath.Abs(filepath.Dir(filepath.Join(file, ".."+string(filepath.Separator)))) |
23 | beego.TestBeegoInit(apppath) | 23 | beego.TestBeegoInit(apppath) |
24 | 24 | ||
25 | - path,_:=os.Getwd() | ||
26 | - filename :="app.conf" | 25 | + path, _ := os.Getwd() |
26 | + filename := "app.conf" | ||
27 | beego.LoadAppConfig("ini", filepath.Join(path, "conf", filename)) | 27 | beego.LoadAppConfig("ini", filepath.Join(path, "conf", filename)) |
28 | log.InitLog(config.Logger{ | 28 | log.InitLog(config.Logger{ |
29 | - Filename:"app.log", | ||
30 | - Level:"3", //7 | 29 | + Filename: "app.log", |
30 | + Level: "3", //7 | ||
31 | }) | 31 | }) |
32 | - err:= redis.InitWithDb(100,beego.AppConfig.String("redis_add_port"),beego.AppConfig.String("redis_auth"),"0") | ||
33 | - if err!=nil{ | 32 | + err := redis.InitWithDb(100, beego.AppConfig.String("redis_add_port"), beego.AppConfig.String("redis_auth"), "0") |
33 | + if err != nil { | ||
34 | log.Fatal(err) | 34 | log.Fatal(err) |
35 | panic(err) | 35 | panic(err) |
36 | } | 36 | } |
37 | orm.NewBeeormEngine(config.Mysql{ | 37 | orm.NewBeeormEngine(config.Mysql{ |
38 | - DataSource:beego.AppConfig.String("data_source"), | 38 | + DataSource: beego.AppConfig.String("data_source"), |
39 | MaxIdle: 100, | 39 | MaxIdle: 100, |
40 | - MaxOpen:100, | 40 | + MaxOpen: 100, |
41 | }) | 41 | }) |
42 | protocol.InitMessageCode() | 42 | protocol.InitMessageCode() |
43 | }) | 43 | }) |
@@ -6,7 +6,7 @@ package tests | @@ -6,7 +6,7 @@ package tests | ||
6 | // "testing" | 6 | // "testing" |
7 | // "runtime" | 7 | // "runtime" |
8 | // "path/filepath" | 8 | // "path/filepath" |
9 | -// _ "gitlab.fjmaimaimai.com/mmm-go/ability/routers" | 9 | +// _ "ability/routers" |
10 | // | 10 | // |
11 | // "github.com/astaxie/beego" | 11 | // "github.com/astaxie/beego" |
12 | // . "github.com/smartystreets/goconvey/convey" | 12 | // . "github.com/smartystreets/goconvey/convey" |
@@ -36,4 +36,3 @@ package tests | @@ -36,4 +36,3 @@ package tests | ||
36 | // }) | 36 | // }) |
37 | // }) | 37 | // }) |
38 | //} | 38 | //} |
39 | - |
-
请 注册 或 登录 后发表评论