service.go
5.4 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package service
import (
"errors"
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/auth/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/auth/dto"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/web/auth/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/cache"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/allied_creation_user"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/infrastructure/service_gateway/sms_serve"
)
type AuthService struct{}
//AuthLogin 用户登录
func (srv AuthService) AuthLogin(loginCommand *command.LoginCommand) (interface{}, error) {
var (
result interface{}
err error
)
switch loginCommand.GrantType {
case "signInPassword":
//账号密码登录
result, err = srv.SignInPassword(loginCommand.Phone, loginCommand.Password)
case "signInCaptcha":
//手机验证码登录
result, err = srv.SignInCaptcha(loginCommand.Phone, loginCommand.Captcha)
default:
err = errors.New("登录方式无法解析")
}
return result, err
}
//SignInPassword 使用账号密码校验
func (srv AuthService) SignInPassword(account string, password string) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
_, err := creationUserGateway.AuthCheckPassword(allied_creation_user.ReqAuthCheckPassword{
Password: password,
Phone: account,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
ltoken := domain.LoginToken{
UserId: 0,
Account: account,
Platform: domain.LoginPlatformApp,
CompanyId: 0,
}
authcode, err := ltoken.GenerateAuthCode()
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
result := map[string]string{
"authCode": authcode,
}
return result, nil
}
//SignInCaptcha 使用手机验证码登录
func (srv AuthService) SignInCaptcha(phone string, captcha string) (interface{}, error) {
smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
err := smsServeGateway.CheckSmsCode(phone, captcha)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
ltoken := domain.LoginToken{
UserId: 0,
Account: phone,
Platform: domain.LoginPlatformApp,
CompanyId: 0,
}
authcode, err := ltoken.GenerateAuthCode()
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
result := map[string]string{
"authCode": authcode,
}
return result, nil
}
//GetCompanyOrgsByUser 获取登录用户的公司组织列表
func (srv AuthService) GetCompanyOrgsByUser(queryParam query.GetCompanyOrgsByUserQuery) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(queryParam.Operator)
result, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
Phone: queryParam.Phone,
})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
var (
companys []dto.CompanyItem
orgs []dto.OrgItem
)
for _, v := range result.Users {
companys = append(companys, dto.CompanyItem{
CompanyId: v.Company.CompanyId,
CompanyName: v.Company.CompanyName,
})
for _, vv := range v.UserOrg {
orgs = append(orgs, dto.OrgItem{
OrganizationId: vv.OrgID,
OrganizationName: vv.OrgName,
CompanyId: v.Company.CompanyId,
})
}
}
data := map[string]interface{}{
"companys": companys,
"organizations": orgs,
}
return data, nil
}
//GetQrcode 获取扫码登录需要的二维码
func (srv AuthService) GetQrcode() (interface{}, error) {
qrmsg := domain.QrcodeMessage{}
imgBase64, err := qrmsg.GenerateImageBase64()
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
qrCache := cache.LoginQrcodeCache{}
err = qrCache.Save(qrmsg)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
data := map[string]interface{}{
"image": imgBase64,
"token": qrmsg.Token,
}
return data, nil
}
//QrcodeLoginStatus 询问扫码登录状态
func (srv AuthService) QrcodeLoginStatus(queryParam query.QrcodeLoginStatusQuery) (interface{}, error) {
qrmsg := domain.QrcodeMessage{}
err := qrmsg.ParseToken(queryParam.Token)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
qrCache := cache.LoginQrcodeCache{}
qrmsgCache, err := qrCache.Get(qrmsg.Id)
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
loginToken := domain.LoginToken{
UserId: qrmsgCache.UserId,
UserBaseId: qrmsgCache.UserBaseId,
Account: qrmsgCache.Account,
Platform: domain.LoginPlatformWeb,
CompanyId: qrmsgCache.CompanyId,
OrgId: qrmsgCache.OrgId,
}
accessToken, err := loginToken.GenerateAccessToken()
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
_ = accessToken
//TODO 填充token数据
data := map[string]interface{}{
"isLogin": qrmsgCache.IsLogin,
"accessToken": "",
}
return data, nil
}