作者 yangfu

basecontroller genmessage

... ... @@ -2,14 +2,16 @@ package controllers
import (
"github.com/astaxie/beego/validation"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
)
type BaseController struct {
mybeego.BaseController
}
//Valid valid struct
func (this *BaseController)Valid(obj interface{})(result bool ,msg *mybeego.Message){
/*校验*/
var err error
... ... @@ -27,4 +29,21 @@ func (this *BaseController)Valid(obj interface{})(result bool ,msg *mybeego.Mess
return
}
return
}
//GenMessage genarate a response message
func (this *BaseController)GenMessage(rsp interface{},err error)*mybeego.Message{
var msg *mybeego.Message
if err==nil{
msg = mybeego.NewMessage(0)
msg.Data = rsp
return msg
}
if e,ok :=err.(common.Error);ok{
msg = mybeego.NewMessage(e.Code)
msg.Data = rsp
return msg
}
msg = mybeego.NewMessage(1)
return msg
}
\ No newline at end of file
... ...
... ... @@ -28,5 +28,5 @@ func(this *AuthController)Login(){
msg = m
return
}
msg = auth.Login(request)
msg = this.GenMessage(auth.Login(request))
}
... ...
package auth
import (
"fmt"
"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"
"gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
"strings"
)
func Login(request *protocol.LoginRequest)*mybeego.Message{
user,err:=models.GetUsersByMobile(request.Phone)
func Login(request *protocol.LoginRequest)(rsp *protocol.LoginResponse,err error){
var (
user *models.Users
userInfo *models.UserInfo
)
user,err =models.GetUsersByMobile(request.Phone)
if err!=nil{
log.Error(err)
return mybeego.NewMessage(1)
return
}
switch request.GrantType {
case protocol.LoginPassPord:
... ... @@ -23,19 +28,20 @@ func Login(request *protocol.LoginRequest)*mybeego.Message{
case protocol.LoginSmdcode:
goto Success
default:
return mybeego.NewErrMessage(2,"err grantType")
err =fmt.Errorf("grantType error")
return
}
Success:
{
userInfo,err :=models.GetUserInfoByMobile(request.Phone)
userInfo,err =models.GetUserInfoByMobile(request.Phone)
if err!=nil{
log.Error(err)
return mybeego.NewMessage(1)
return
}
rsp :=protocol.LoginResponse{AuthCode:userInfo.Auth}
msg :=mybeego.NewMessage(0)
msg.Data =rsp
return msg
return
}
return mybeego.NewMessage(1)
return
}
\ No newline at end of file
... ...