作者 yangfu

auth

  1 +[dev]
1 #数据库相关 2 #数据库相关
2 data_source = "root:123456@tcp(127.0.0.1:3306)/ability_display" 3 data_source = "root:123456@tcp(127.0.0.1:3306)/ability_display"
  4 +#data_source = "root:sutianxia2015@tcp(115.29.205.99:3306)/ability_display"
3 5
4 6
5 #redis相关配置 7 #redis相关配置
  1 +[prod]
1 #数据库相关 2 #数据库相关
2 data_source = "root:123456@tcp(127.0.0.1:3306)/ability_display" 3 data_source = "root:123456@tcp(127.0.0.1:3306)/ability_display"
3 4
  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 +}
  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 +}
@@ -30,6 +30,8 @@ func main() { @@ -30,6 +30,8 @@ func main() {
30 defer func(){ 30 defer func(){
31 log.Info("app on stop!") 31 log.Info("app on stop!")
32 }() 32 }()
  33 + log.Info("app on start!")
  34 + log.Info("Beego Run Mode:",beego.BConfig.RunMode)
33 beego.Run() 35 beego.Run()
34 } 36 }
35 37
@@ -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 +}
  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 +
  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 +}