作者 yangfu

basecontroller genmessage

@@ -2,14 +2,16 @@ package controllers @@ -2,14 +2,16 @@ package controllers
2 2
3 import ( 3 import (
4 "github.com/astaxie/beego/validation" 4 "github.com/astaxie/beego/validation"
5 - "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego" 5 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/common"
6 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log" 6 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
  7 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
7 ) 8 )
8 9
9 type BaseController struct { 10 type BaseController struct {
10 mybeego.BaseController 11 mybeego.BaseController
11 } 12 }
12 13
  14 +//Valid valid struct
13 func (this *BaseController)Valid(obj interface{})(result bool ,msg *mybeego.Message){ 15 func (this *BaseController)Valid(obj interface{})(result bool ,msg *mybeego.Message){
14 /*校验*/ 16 /*校验*/
15 var err error 17 var err error
@@ -28,3 +30,20 @@ func (this *BaseController)Valid(obj interface{})(result bool ,msg *mybeego.Mess @@ -28,3 +30,20 @@ func (this *BaseController)Valid(obj interface{})(result bool ,msg *mybeego.Mess
28 } 30 }
29 return 31 return
30 } 32 }
  33 +
  34 +//GenMessage genarate a response message
  35 +func (this *BaseController)GenMessage(rsp interface{},err error)*mybeego.Message{
  36 + var msg *mybeego.Message
  37 + if err==nil{
  38 + msg = mybeego.NewMessage(0)
  39 + msg.Data = rsp
  40 + return msg
  41 + }
  42 + if e,ok :=err.(common.Error);ok{
  43 + msg = mybeego.NewMessage(e.Code)
  44 + msg.Data = rsp
  45 + return msg
  46 + }
  47 + msg = mybeego.NewMessage(1)
  48 + return msg
  49 +}
@@ -28,5 +28,5 @@ func(this *AuthController)Login(){ @@ -28,5 +28,5 @@ func(this *AuthController)Login(){
28 msg = m 28 msg = m
29 return 29 return
30 } 30 }
31 - msg = auth.Login(request) 31 + msg = this.GenMessage(auth.Login(request))
32 } 32 }
1 package auth 1 package auth
2 2
3 import ( 3 import (
  4 + "fmt"
4 "gitlab.fjmaimaimai.com/mmm-go/ability/models" 5 "gitlab.fjmaimaimai.com/mmm-go/ability/models"
5 "gitlab.fjmaimaimai.com/mmm-go/ability/protocol" 6 "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" 7 "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/log"
  8 + "gitlab.fjmaimaimai.com/mmm-go/gocomm/pkg/mybeego"
8 "strings" 9 "strings"
9 ) 10 )
10 11
11 -func Login(request *protocol.LoginRequest)*mybeego.Message{  
12 - user,err:=models.GetUsersByMobile(request.Phone) 12 +func Login(request *protocol.LoginRequest)(rsp *protocol.LoginResponse,err error){
  13 + var (
  14 + user *models.Users
  15 + userInfo *models.UserInfo
  16 + )
  17 + user,err =models.GetUsersByMobile(request.Phone)
13 if err!=nil{ 18 if err!=nil{
14 log.Error(err) 19 log.Error(err)
15 - return mybeego.NewMessage(1) 20 + return
16 } 21 }
17 switch request.GrantType { 22 switch request.GrantType {
18 case protocol.LoginPassPord: 23 case protocol.LoginPassPord:
@@ -23,19 +28,20 @@ func Login(request *protocol.LoginRequest)*mybeego.Message{ @@ -23,19 +28,20 @@ func Login(request *protocol.LoginRequest)*mybeego.Message{
23 case protocol.LoginSmdcode: 28 case protocol.LoginSmdcode:
24 goto Success 29 goto Success
25 default: 30 default:
26 - return mybeego.NewErrMessage(2,"err grantType") 31 + err =fmt.Errorf("grantType error")
  32 + return
27 } 33 }
28 Success: 34 Success:
29 { 35 {
30 - userInfo,err :=models.GetUserInfoByMobile(request.Phone) 36 + userInfo,err =models.GetUserInfoByMobile(request.Phone)
31 if err!=nil{ 37 if err!=nil{
32 log.Error(err) 38 log.Error(err)
33 - return mybeego.NewMessage(1) 39 + return
34 } 40 }
35 rsp :=protocol.LoginResponse{AuthCode:userInfo.Auth} 41 rsp :=protocol.LoginResponse{AuthCode:userInfo.Auth}
36 msg :=mybeego.NewMessage(0) 42 msg :=mybeego.NewMessage(0)
37 msg.Data =rsp 43 msg.Data =rsp
38 - return msg 44 + return
39 } 45 }
40 - return mybeego.NewMessage(1) 46 + return
41 } 47 }