auth.go
13.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package service
import (
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/auth/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/auth/dto"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/auth/query"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/application/factory"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-user/pkg/domain"
"strconv"
"time"
)
// 认证服务
type AuthService struct {
}
// 企业注册
func (authService *AuthService) CompanySignUp(companySignUpCommand *command.CompanySignUpCommand) (interface{}, error) {
if err := companySignUpCommand.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()
}()
//var user *domain.User
signUpCompanyService, err := factory.CreateSignUpCompanyService(map[string]interface{}{
"transactionContext": transactionContext,
})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
companyInfo := &domain.CompanyInfo{
CompanyName: companySignUpCommand.CompanyName,
Scale: companySignUpCommand.Scale,
Logo: "",
Address: "",
IndustryCategory: companySignUpCommand.IndustryCategory,
RegisteredTime: time.Now(),
}
userInfo := &domain.UserInfo{
UserName: companySignUpCommand.Contacts,
Phone: companySignUpCommand.Phone,
}
if _, err = signUpCompanyService.SignUpCompany(companySignUpCommand.Phone, companySignUpCommand.Password, companyInfo, userInfo); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return struct{}{}, nil
}
// 注销账号 (添加用户时重新激活)
func (authService *AuthService) DestroyAccount(destroyAccountCommand *command.DestroyAccountCommand) (interface{}, error) {
if err := destroyAccountCommand.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()
}()
accountDestroyService, err := factory.CreatePgAuthAccountDestroyService(map[string]interface{}{
"transactionContext": transactionContext,
})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := accountDestroyService.DestroyAccount(nil, destroyAccountCommand.UserId); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return struct{}{}, nil
}
// 修改密码
func (authService *AuthService) PhoneAuthChangePassword(phoneAuthChangePasswordCommand *command.PhoneAuthChangePasswordCommand) (interface{}, error) {
if err := phoneAuthChangePasswordCommand.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()
}()
var userRepository domain.UserRepository
if value, err := factory.CreateUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
userRepository = value
}
var userBaseId int64
if user, err := userRepository.FindOne(map[string]interface{}{"userId": phoneAuthChangePasswordCommand.UserId}); err != nil {
if err == domain.ErrorNotFound {
return nil, application.ThrowError(application.TRANSACTION_ERROR, "该用户不存在")
}
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
} else {
userBaseId = user.UserBaseId
}
var userBaseRepository domain.UserBaseRepository
if value, err := factory.CreateUserBaseRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
userBaseRepository = value
}
userBase, err := userBaseRepository.FindOne(map[string]interface{}{"userBaseId": userBaseId})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err == domain.ErrorNotFound {
return nil, application.ThrowError(application.TRANSACTION_ERROR, "该用户不存在")
}
if err := userBase.CheckAccountPassword(userBase.Account, phoneAuthChangePasswordCommand.OldPassword); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := userBase.ResetPassword(userBase.Account, phoneAuthChangePasswordCommand.NewPassword); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if _, err = userBaseRepository.Save(userBase); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return struct {
}{}, nil
}
// 手机账号密码检查
func (authService *AuthService) PhoneAuthCheck(phoneAuthCheckCommand *command.PhoneAuthCheckCommand) (interface{}, error) {
if err := phoneAuthCheckCommand.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()
}()
var userBaseRepository domain.UserBaseRepository
if value, err := factory.CreateUserBaseRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
userBaseRepository = value
}
userBase, err := userBaseRepository.FindOne(map[string]interface{}{"account": phoneAuthCheckCommand.Phone})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err == domain.ErrorNotFound {
return nil, application.ThrowError(application.TRANSACTION_ERROR, "该用户不存在")
}
if err := userBase.CheckAccountPassword(phoneAuthCheckCommand.Phone, phoneAuthCheckCommand.Password); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return userBase, nil
}
// 重置密码(忘记密码)
func (authService *AuthService) PhoneAuthResetPassword(phoneAuthResetPasswordCommand *command.PhoneAuthResetPasswordCommand) (interface{}, error) {
if err := phoneAuthResetPasswordCommand.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()
}()
var userBaseRepository domain.UserBaseRepository
if value, err := factory.CreateUserBaseRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
} else {
userBaseRepository = value
}
userBase, err := userBaseRepository.FindOne(map[string]interface{}{"account": phoneAuthResetPasswordCommand.Phone})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err == domain.ErrorNotFound {
return nil, application.ThrowError(application.TRANSACTION_ERROR, "该用户不存在")
}
if err := userBase.ResetPassword(phoneAuthResetPasswordCommand.Phone, phoneAuthResetPasswordCommand.Password); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if _, err = userBaseRepository.Save(userBase); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return struct{}{}, nil
}
// 重置手机号
func (authService *AuthService) PhoneAuthResetPhone(phoneAuthResetPhoneCommand *command.PhoneAuthResetPhoneCommand) (interface{}, error) {
if err := phoneAuthResetPhoneCommand.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()
}()
resetPhoneService, err := factory.CreatePgAuthResetPhoneService(map[string]interface{}{
"transactionContext": transactionContext,
})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := resetPhoneService.ResetPhone(nil, phoneAuthResetPhoneCommand.OldPhone, phoneAuthResetPhoneCommand.NewPhone); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return struct{}{}, nil
}
// 刷新IM信息
func (authService *AuthService) RefreshIM(refreshIMCommand *command.RefreshIMCommand) (interface{}, error) {
if err := refreshIMCommand.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()
}()
imService, _ := factory.CreatePgImService(map[string]interface{}{
"transactionContext": transactionContext,
})
userId, _ := strconv.Atoi(refreshIMCommand.Phone)
imInfo, err := imService.InitOrUpdateUserIMInfo(int64(userId), refreshIMCommand.RefreshFlag)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return imInfo, nil
}
// 用户信息
func (authService *AuthService) UserInfo(userInfoQuery *query.UserInfoQuery) (interface{}, error) {
if err := userInfoQuery.ValidateQuery(); 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()
}()
userBaseRepository, _, _ := factory.FastPgUserBase(transactionContext, 0)
userBase, err := userBaseRepository.FindOne(map[string]interface{}{"account": userInfoQuery.Account})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
ubDto := &dto.UserBaseDto{}
ubDto.LoadDto(userBase)
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return ubDto, nil
}
func NewAuthService(options map[string]interface{}) *AuthService {
newAuthService := &AuthService{}
return newAuthService
}