正在显示
10 个修改的文件
包含
157 行增加
和
3 行删除
controllers/base.go
0 → 100644
1 | +package controllers | ||
2 | + | ||
3 | +import ( | ||
4 | + "github.com/astaxie/beego/validation" | ||
5 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" | ||
6 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | ||
7 | +) | ||
8 | + | ||
9 | +type BaseController struct { | ||
10 | + mybeego.BaseController | ||
11 | +} | ||
12 | + | ||
13 | +func (this *BaseController)Valid(obj interface{})(result bool ,msg *mybeego.Message){ | ||
14 | + /*校验*/ | ||
15 | + var err error | ||
16 | + valid :=validation.Validation{} | ||
17 | + result,err= valid.Valid(obj) | ||
18 | + if err!=nil{ | ||
19 | + msg = mybeego.NewMessage(1) | ||
20 | + return | ||
21 | + } | ||
22 | + if !result{ | ||
23 | + for _, err := range valid.Errors { | ||
24 | + log.Error(err.Key, err.Message) | ||
25 | + } | ||
26 | + msg = mybeego.NewMessage(2) | ||
27 | + return | ||
28 | + } | ||
29 | + return | ||
30 | +} |
controllers/v1/auth.go
0 → 100644
1 | +package v1 | ||
2 | + | ||
3 | +import ( | ||
4 | + "encoding/json" | ||
5 | + "gitlab.fjmaimaimai.com/mmm-go/ability/controllers" | ||
6 | + "gitlab.fjmaimaimai.com/mmm-go/ability/protocol" | ||
7 | + "gitlab.fjmaimaimai.com/mmm-go/ability/services/auth" | ||
8 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | ||
9 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" | ||
10 | +) | ||
11 | + | ||
12 | +type AuthController struct { | ||
13 | + controllers.BaseController | ||
14 | +} | ||
15 | + | ||
16 | +func(this *AuthController)Login(){ | ||
17 | + var msg *mybeego.Message | ||
18 | + defer func(){ | ||
19 | + this.Resp(msg) | ||
20 | + }() | ||
21 | + var request *protocol.LoginRequest | ||
22 | + if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{ | ||
23 | + log.Error(err) | ||
24 | + msg = mybeego.NewMessage(1) | ||
25 | + return | ||
26 | + } | ||
27 | + if b,m :=this.Valid(request);!b{ | ||
28 | + msg = m | ||
29 | + return | ||
30 | + } | ||
31 | + msg = auth.Login(request) | ||
32 | +} |
@@ -189,3 +189,12 @@ func DeleteUserInfo(id int) (err error) { | @@ -189,3 +189,12 @@ func DeleteUserInfo(id int) (err error) { | ||
189 | } | 189 | } |
190 | return | 190 | return |
191 | } | 191 | } |
192 | + | ||
193 | +func GetUserInfoByMobile(mobile string)(v *UserInfo, err error) { | ||
194 | + o := orm.NewOrm() | ||
195 | + sql :="select * from user_info where phone=?" | ||
196 | + if err = o.Raw(sql,mobile).QueryRow(&v); err == nil { | ||
197 | + return v, nil | ||
198 | + } | ||
199 | + return nil, err | ||
200 | +} |
@@ -162,3 +162,12 @@ func DeleteUsers(id int) (err error) { | @@ -162,3 +162,12 @@ func DeleteUsers(id int) (err error) { | ||
162 | } | 162 | } |
163 | return | 163 | return |
164 | } | 164 | } |
165 | + | ||
166 | +func GetUsersByMobile(mobile string)(v *Users, err error) { | ||
167 | + o := orm.NewOrm() | ||
168 | + sql :=`select * from users where username=?` | ||
169 | + if err = o.Raw(sql,mobile).QueryRow(&v); err == nil { | ||
170 | + return v, nil | ||
171 | + } | ||
172 | + return nil, err | ||
173 | +} |
protocol/auth.go
0 → 100644
1 | +package protocol | ||
2 | + | ||
3 | +const ( | ||
4 | + LoginPassPord ="signInPassword" | ||
5 | + LoginSmdcode ="signInCaptcha" | ||
6 | +) | ||
7 | + | ||
8 | +type LoginRequest struct { | ||
9 | + Phone string `json:"phone" valid:"Required;Mobile"` | ||
10 | + Code string `json:"code"` | ||
11 | + GrantType string `json:"grantType" valid:"Required"` | ||
12 | + PassWord string `json:"password"` | ||
13 | + ClientId string `json:"clientId" valid:"Required"` | ||
14 | +} | ||
15 | + | ||
16 | +type LoginResponse struct { | ||
17 | + AuthCode string `json:"authCode"` | ||
18 | +} | ||
19 | + |
@@ -5,14 +5,23 @@ import ( | @@ -5,14 +5,23 @@ import ( | ||
5 | "gitlab.fjmaimaimai.com/mmm-go/ability/controllers/v1" | 5 | "gitlab.fjmaimaimai.com/mmm-go/ability/controllers/v1" |
6 | ) | 6 | ) |
7 | 7 | ||
8 | +var nsV1 *beego.Namespace | ||
9 | + | ||
8 | func init() { | 10 | func init() { |
9 | - ns :=beego.NewNamespace("/v1") | 11 | + nsV1=beego.NewNamespace("/v1") |
10 | /*user controller*/ | 12 | /*user controller*/ |
11 | { | 13 | { |
12 | user :=&v1.UserController{} | 14 | user :=&v1.UserController{} |
13 | - ns.Router("/user/login",user,"post:Login") | 15 | + nsV1.Router("/user/login",user,"post:Login") |
14 | } | 16 | } |
15 | - beego.AddNamespace(ns) | 17 | + |
18 | + /*auth controller*/ | ||
19 | + { | ||
20 | + auth :=&v1.AuthController{} | ||
21 | + nsV1.Router("/auth/login",auth,"post:Login") | ||
22 | + } | ||
23 | + beego.AddNamespace(nsV1) | ||
16 | } | 24 | } |
17 | 25 | ||
18 | 26 | ||
27 | + |
services/auth/auth.go
0 → 100644
1 | +package auth | ||
2 | + | ||
3 | +import ( | ||
4 | + "gitlab.fjmaimaimai.com/mmm-go/ability/models" | ||
5 | + "gitlab.fjmaimaimai.com/mmm-go/ability/protocol" | ||
6 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" | ||
7 | + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" | ||
8 | + "strings" | ||
9 | +) | ||
10 | + | ||
11 | +func Login(request *protocol.LoginRequest)*mybeego.Message{ | ||
12 | + user,err:=models.GetUsersByMobile(request.Phone) | ||
13 | + if err!=nil{ | ||
14 | + log.Error(err) | ||
15 | + return mybeego.NewMessage(1) | ||
16 | + } | ||
17 | + switch request.GrantType { | ||
18 | + case protocol.LoginPassPord: | ||
19 | + if strings.Compare(user.Password,request.PassWord)==0{ | ||
20 | + goto Success | ||
21 | + } | ||
22 | + break | ||
23 | + case protocol.LoginSmdcode: | ||
24 | + goto Success | ||
25 | + default: | ||
26 | + return mybeego.NewErrMessage(2,"err grantType") | ||
27 | + } | ||
28 | + Success: | ||
29 | + { | ||
30 | + userInfo,err :=models.GetUserInfoByMobile(request.Phone) | ||
31 | + if err!=nil{ | ||
32 | + log.Error(err) | ||
33 | + return mybeego.NewMessage(1) | ||
34 | + } | ||
35 | + rsp :=protocol.LoginResponse{AuthCode:userInfo.Auth} | ||
36 | + msg :=mybeego.NewMessage(0) | ||
37 | + msg.Data =rsp | ||
38 | + return msg | ||
39 | + } | ||
40 | + return mybeego.NewMessage(1) | ||
41 | +} |
-
请 注册 或 登录 后发表评论