auth.go
3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package auth
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/query"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/service"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/svr"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/protocol"
"strings"
"time"
)
func Login(header *protocol.RequestHeader, request *protocol.LoginRequest) (rsp *protocol.LoginResponse, err error) {
var (
PartnerInfoService = service.NewPartnerInfoService(nil)
partnerInfo *domain.PartnerInfo
result bool = false
)
rsp = &protocol.LoginResponse{}
if partnerInfo, err = PartnerInfoService.GetPartnerInfo(&query.GetPartnerInfoQuery{Account: request.Phone}); err != nil {
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
}
switch request.GrantType {
case protocol.LoginByPassword:
if len(partnerInfo.Password) == 0 {
err = protocol.NewCustomMessage(1, "密码不能为空!")
return
}
if result = strings.EqualFold(partnerInfo.Password, request.Password); !result {
err = protocol.NewCustomMessage(1, "密码输入有误!")
return
}
break
case protocol.LoginBySmsCode:
if _, err = CheckSmsCode(request.Phone, request.Captcha); err != nil {
return
}
break
}
rsp.AuthCode, _ = utils.GenerateToken(partnerInfo.Id, protocol.AuthCodeExpire*time.Second)
return
}
func SmsCode(request *protocol.SmsCodeRequest) (rsp *protocol.LoginResponse, err error) {
var data map[string]interface{}
sms := svr.NewHttplibMmmSmsApiServiceGateway()
data, err = sms.SendSms(request.Phone)
if err != nil {
if msg, ok := data["msg"]; ok {
err = protocol.NewCustomMessage(1, msg.(string))
}
return
}
return
}
func AccessToken(request *protocol.AccessTokenRequest) (rsp *protocol.AccessTokenResponse, err error) {
var (
claim *utils.UserTokenClaims
)
rsp = &protocol.AccessTokenResponse{}
if claim, err = utils.ParseJWTToken(request.AuthCode); err != nil {
err = protocol.NewErrWithMessage(4139, err)
return
}
if claim.UserId <= 0 {
err = protocol.NewErrWithMessage(1, fmt.Errorf("jwt authCode (%v) valid", request.AuthCode))
return
}
rsp.AccessToken, _ = utils.GenerateToken(claim.UserId, protocol.TokenExpire*time.Second)
rsp.RefreshToken, _ = utils.GenerateToken(claim.UserId, protocol.RefreshTokenExipre*time.Second)
rsp.ExpiresIn = protocol.TokenExpire
return
}
func RefreshToken(request *protocol.RefreshTokenRequest) (rsp *protocol.RefreshTokenResponse, err error) {
var (
claim *utils.UserTokenClaims
)
rsp = &protocol.RefreshTokenResponse{}
if claim, err = utils.ParseJWTToken(request.RefreshToken); err != nil {
err = protocol.NewErrWithMessage(4139, err)
return
}
if claim.UserId <= 0 {
err = protocol.NewErrWithMessage(1, fmt.Errorf("jwt refrshToken (%v) valid", request.RefreshToken))
return
}
rsp.AccessToken, _ = utils.GenerateToken(claim.UserId, protocol.TokenExpire*time.Second)
rsp.RefreshToken, _ = utils.GenerateToken(claim.UserId, protocol.RefreshTokenExipre*time.Second)
rsp.ExpiresIn = protocol.TokenExpire
return
return
}
//验证短信验证码 T
func CheckSmsCode(phone, code string) (result bool, err error) {
sms := svr.NewHttplibMmmSmsApiServiceGateway()
var data map[string]interface{}
data, err = sms.CheckSmsCode(phone, code)
if err != nil {
if msg, ok := data["msg"]; ok {
err = protocol.NewCustomMessage(1, msg.(string))
}
return
}
return
}