作者 yangfu

auth

[dev]
#数据库相关
data_source = "root:123456@tcp(127.0.0.1:3306)/ability_display"
#data_source = "root:sutianxia2015@tcp(115.29.205.99:3306)/ability_display"
#redis相关配置
... ...
[prod]
#数据库相关
data_source = "root:123456@tcp(127.0.0.1:3306)/ability_display"
... ...
package controllers
import (
"github.com/astaxie/beego/validation"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
)
type BaseController struct {
mybeego.BaseController
}
func (this *BaseController)Valid(obj interface{})(result bool ,msg *mybeego.Message){
/*校验*/
var err error
valid :=validation.Validation{}
result,err= valid.Valid(obj)
if err!=nil{
msg = mybeego.NewMessage(1)
return
}
if !result{
for _, err := range valid.Errors {
log.Error(err.Key, err.Message)
}
msg = mybeego.NewMessage(2)
return
}
return
}
\ No newline at end of file
... ...
package v1
import (
"encoding/json"
"gitlab.fjmaimaimai.com/mmm-go/ability/controllers"
"gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
"gitlab.fjmaimaimai.com/mmm-go/ability/services/auth"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
)
type AuthController struct {
controllers.BaseController
}
func(this *AuthController)Login(){
var msg *mybeego.Message
defer func(){
this.Resp(msg)
}()
var request *protocol.LoginRequest
if err:=json.Unmarshal(this.ByteBody,&request);err!=nil{
log.Error(err)
msg = mybeego.NewMessage(1)
return
}
if b,m :=this.Valid(request);!b{
msg = m
return
}
msg = auth.Login(request)
}
... ...
... ... @@ -30,6 +30,8 @@ func main() {
defer func(){
log.Info("app on stop!")
}()
log.Info("app on start!")
log.Info("Beego Run Mode:",beego.BConfig.RunMode)
beego.Run()
}
... ...
... ... @@ -189,3 +189,12 @@ func DeleteUserInfo(id int) (err error) {
}
return
}
func GetUserInfoByMobile(mobile string)(v *UserInfo, err error) {
o := orm.NewOrm()
sql :="select * from user_info where phone=?"
if err = o.Raw(sql,mobile).QueryRow(&v); err == nil {
return v, nil
}
return nil, err
}
... ...
... ... @@ -162,3 +162,12 @@ func DeleteUsers(id int) (err error) {
}
return
}
func GetUsersByMobile(mobile string)(v *Users, err error) {
o := orm.NewOrm()
sql :=`select * from users where username=?`
if err = o.Raw(sql,mobile).QueryRow(&v); err == nil {
return v, nil
}
return nil, err
}
... ...
package protocol
const (
LoginPassPord ="signInPassword"
LoginSmdcode ="signInCaptcha"
)
type LoginRequest struct {
Phone string `json:"phone" valid:"Required;Mobile"`
Code string `json:"code"`
GrantType string `json:"grantType" valid:"Required"`
PassWord string `json:"password"`
ClientId string `json:"clientId" valid:"Required"`
}
type LoginResponse struct {
AuthCode string `json:"authCode"`
}
... ...
... ... @@ -5,14 +5,23 @@ import (
"gitlab.fjmaimaimai.com/mmm-go/ability/controllers/v1"
)
var nsV1 *beego.Namespace
func init() {
ns :=beego.NewNamespace("/v1")
nsV1=beego.NewNamespace("/v1")
/*user controller*/
{
user :=&v1.UserController{}
ns.Router("/user/login",user,"post:Login")
nsV1.Router("/user/login",user,"post:Login")
}
beego.AddNamespace(ns)
/*auth controller*/
{
auth :=&v1.AuthController{}
nsV1.Router("/auth/login",auth,"post:Login")
}
beego.AddNamespace(nsV1)
}
... ...
package auth
import (
"gitlab.fjmaimaimai.com/mmm-go/ability/models"
"gitlab.fjmaimaimai.com/mmm-go/ability/protocol"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"strings"
)
func Login(request *protocol.LoginRequest)*mybeego.Message{
user,err:=models.GetUsersByMobile(request.Phone)
if err!=nil{
log.Error(err)
return mybeego.NewMessage(1)
}
switch request.GrantType {
case protocol.LoginPassPord:
if strings.Compare(user.Password,request.PassWord)==0{
goto Success
}
break
case protocol.LoginSmdcode:
goto Success
default:
return mybeego.NewErrMessage(2,"err grantType")
}
Success:
{
userInfo,err :=models.GetUserInfoByMobile(request.Phone)
if err!=nil{
log.Error(err)
return mybeego.NewMessage(1)
}
rsp :=protocol.LoginResponse{AuthCode:userInfo.Auth}
msg :=mybeego.NewMessage(0)
msg.Data =rsp
return msg
}
return mybeego.NewMessage(1)
}
\ No newline at end of file
... ...