auth.go
3.7 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
package service
import (
"fmt"
"github.com/linmadan/egglib-go/core/application"
"github.com/tiptok/gocomm/common"
"github.com/tiptok/godevp/pkg/application/auth/command"
"github.com/tiptok/godevp/pkg/application/factory"
domainx "github.com/tiptok/godevp/pkg/domain"
domain "github.com/tiptok/godevp/pkg/domain/users"
)
// 认证服务
type AuthService struct {
}
// 登录
func (authService *AuthService) Login(loginCommand *command.LoginCommand) (interface{}, error) {
if err := loginCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
usersRepository, _ := factory.CreateUsersRepository(map[string]interface{}{
"transactionContext": transactionContext,
})
var user *domain.Users
if user, err = usersRepository.FindOne(map[string]interface{}{"phone": loginCommand.Username}); err != nil || user == nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, "用户不存在")
}
if user.Passwd != loginCommand.Password {
return nil, application.ThrowError(application.BUSINESS_ERROR, "密码有误")
}
if user.Status != 1 {
return nil, application.ThrowError(application.BUSINESS_ERROR, "该账号已被禁用")
}
token, _ := common.GenerateToken(fmt.Sprintf("%v", user.Id), user.Passwd, common.WithExpire(domainx.TokenExpire), common.WithAddData(map[string]interface{}{"UserName": user.Name, "Phone": user.Phone}))
refreshToken, _ := common.GenerateToken(fmt.Sprintf("%v", user.Id), user.Passwd, common.WithExpire(domainx.RefreshTokenExpire), common.WithAddData(map[string]interface{}{"UserName": user.Name, "Phone": user.Phone}))
rsp := map[string]interface{}{
"accessToken": token,
"refreshToken": refreshToken,
"expiresIn": domainx.TokenExpire,
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return rsp, nil
}
// 获得当前用户数据
func (authService *AuthService) Profile(profileCommand *command.ProfileCommand) (interface{}, error) {
if err := profileCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil, nil
}
// 刷新权限
func (authService *AuthService) Refresh(refreshCommand *command.RefreshCommand) (interface{}, error) {
if err := refreshCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
claim, e := common.ParseJWTToken(refreshCommand.RefreshToken)
if e != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, "权限过期,请重新登录")
}
return authService.Login(&command.LoginCommand{Username: (claim.AddData["Phone"]).(string), Password: claim.Password})
}
func NewAuthService(options map[string]interface{}) *AuthService {
newAuthService := &AuthService{}
return newAuthService
}