service.go
11.2 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
package service
import (
"github.com/google/uuid"
"github.com/linmadan/egglib-go/core/application"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/mobile/user/command"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/mobile/user/dto"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/application/mobile/user/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_basic"
"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"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"
)
type UserService struct {
}
//SendSmsCaptcha 发送验证码短信
func (srv UserService) SendSmsCaptcha(smsCodeCommand *command.SendSmsCodeCommand) error {
if smsCodeCommand.Flag == 1 {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
userBase, err := creationUserGateway.AuthUserBaseInfo(allied_creation_user.ReqAuthUserBase{
Account: smsCodeCommand.Phone,
})
if err != nil || userBase.UserInfo.Phone != smsCodeCommand.Phone {
return application.ThrowError(application.TRANSACTION_ERROR, "输入的手机号不是平台用户,请重新输入")
}
}
smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
err := smsServeGateway.SendSms(smsCodeCommand.Phone)
if err != nil {
return application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return nil
}
//CheckSmsCode 验证手机短信验证码
func (srv UserService) CheckSmsCode(smsCodeCommand *command.CheckSmsCodeCommand) (interface{}, error) {
smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
err := smsServeGateway.CheckSmsCode(smsCodeCommand.Phone, smsCodeCommand.SmsCode)
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
uid := uuid.New()
pcc := cache.PhoneCheckCache{}
if err := pcc.Add(uid.String(), cache.PhoneCheckItem{
Phone: smsCodeCommand.Phone,
SmsCodeIdentity: uid.String(),
Action: smsCodeCommand.Action,
}); err != nil {
log.Logger.Error(err.Error())
return nil, application.ThrowError(application.BUSINESS_ERROR, "系统错误")
}
return map[string]interface{}{
"smsCodeIdentity": uid.String(),
}, nil
}
//ChangePassword 修改密码
func (srv UserService) ChangePassword(changePasswordCommand *command.ChangePasswordCommand) (interface{}, error) {
smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
if len(changePasswordCommand.OldPassword) > 0 {
// 2.重置密码
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
_, err := creationUserGateway.AuthChangePassword(allied_creation_user.ReqAuthChangePassword{
OldPassword: changePasswordCommand.OldPassword,
NewPassword: changePasswordCommand.NewPassword,
UserId: changePasswordCommand.Operator.UserId,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
} else {
err := smsServeGateway.CheckSmsCode(changePasswordCommand.Phone, changePasswordCommand.SmsCode)
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
// 2.重置密码
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
_, err = creationUserGateway.AuthResetPassword(allied_creation_user.ReqAuthResetPassword{
Phone: changePasswordCommand.Phone,
Password: changePasswordCommand.NewPassword,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
}
return struct{}{}, nil
}
//ChangePhone 修改手机号
func (srv UserService) ChangePhone(resetPhoneCommand *command.ResetPhoneCommand) (interface{}, error) {
smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
err := smsServeGateway.CheckSmsCode(resetPhoneCommand.NewPhone, resetPhoneCommand.SmsCode)
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
// 2.重置手机号
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
_, err = creationUserGateway.AuthResetPhone(allied_creation_user.ReqAuthResetPhone{
UserId: resetPhoneCommand.Operator.UserId,
OldPhone: resetPhoneCommand.Operator.Phone,
NewPhone: resetPhoneCommand.NewPhone,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
return resetPhoneCommand, nil
}
//UpdateUserBaseInfo 更新用户信息
func (srv UserService) UpdateUserBaseInfo(updateUserInfoCommand *command.UpdateUserInfoCommand) (interface{}, error) {
if err := updateUserInfoCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
user, err := creationUserGateway.UserGet(allied_creation_user.ReqGetUser{
UserId: int(updateUserInfoCommand.Operator.UserId),
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, "用户不存在")
}
if _, ok := updateUserInfoCommand.BodyKV["avatar"]; !ok {
updateUserInfoCommand.Avatar = user.UserInfo.Avatar
}
if _, ok := updateUserInfoCommand.BodyKV["email"]; !ok {
updateUserInfoCommand.Email = user.UserInfo.Email
}
if _, ok := updateUserInfoCommand.BodyKV["phone"]; !ok {
updateUserInfoCommand.Phone = user.UserInfo.Phone
}
if _, ok := updateUserInfoCommand.BodyKV["userName"]; !ok {
updateUserInfoCommand.UserName = user.UserInfo.UserName
}
if _, ok := updateUserInfoCommand.BodyKV["smsCode"]; ok && len(updateUserInfoCommand.SmsCode) > 0 {
smsServeGateway := sms_serve.NewHttplibHttplibSmsServe()
err := smsServeGateway.CheckSmsCode(updateUserInfoCommand.Phone, updateUserInfoCommand.SmsCode)
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
}
_, err = creationUserGateway.UserUpdateBaseInfo(allied_creation_user.ReqUserUpdateBaseInfo{
UserId: int64(user.UserId),
UserName: updateUserInfoCommand.UserName,
Avatar: updateUserInfoCommand.Avatar,
Phone: updateUserInfoCommand.Phone,
Email: updateUserInfoCommand.Email,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
return map[string]interface{}{
"avatar": updateUserInfoCommand.Avatar,
"userName": updateUserInfoCommand.UserName,
}, nil
}
// CompanySignUp 企业注册
func (srv UserService) DestroyAccount(destroyAccountCommand *command.DestroyAccountCommand) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(domain.Operator{})
result, err := creationUserGateway.AuthDestroyAccount(allied_creation_user.ReqAuthDestroyAccount{
//UserId: destroyAccountCommand.Operator.UserId,
Account: destroyAccountCommand.Operator.Phone,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
return result, err
}
//DepartmentsUsers 部门用户列表
func (srv UserService) DepartmentsUsers(departmentsUsersQuery *query.DepartmentsUsersQuery) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
departmentsUsersQuery.Operator)
orgs, err := creationUserGateway.OrgGetSubDepartment(allied_creation_user.ReqOrgGetSubDepartment{
OrgId: departmentsUsersQuery.Operator.OrgId,
})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
users, err := creationUserGateway.UserSearch(allied_creation_user.ReqUserSearch{
//Offset: 0,
//Limit: 999,
CompanyId: departmentsUsersQuery.Operator.CompanyId,
OrganizationId: departmentsUsersQuery.Operator.OrgId,
UserType: domain.UserTypeEmployee, //TODO:是否要共创用户
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
departmentUsersDto := &dto.DepartmentUsersDto{}
if err := departmentUsersDto.LoadDto(departmentsUsersQuery.Type, orgs, users); err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
return departmentUsersDto, nil
}
//MessagesList 消息列表
func (srv UserService) MessagesList(cmd *query.MessagesListQuery) (int64, interface{}, error) {
gateway := allied_creation_basic.NewHttplibAlliedCreationBasic(
cmd.Operator)
messages, err := gateway.NoticePersonal(allied_creation_basic.ReqNoticePersonal{
PageIndex: cmd.PageNumber,
PageSize: cmd.PageSize,
UserBaseId: cmd.Operator.UserBaseId,
})
if err != nil {
return 0, nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
var results []*dto.MessageDto
for i := 0; i < len(messages.List); i++ {
mItem := messages.List[i]
message := &dto.MessageDto{
MsgId: mItem.NoticePersonalID,
MsgContent: mItem.Content,
MsgTime: mItem.CreatedAt.Unix() * 1000,
Read: mItem.IsRead,
}
message.MsgType = message.LoadMsgType(mItem.Module, mItem.ModuleAction)
results = append(results, message)
}
return messages.Count, results, nil
}
//MessagesList 消息列表
func (srv UserService) MessagesMarkRead(cmd *command.MessageMarkReadCommand) (interface{}, error) {
gateway := allied_creation_basic.NewHttplibAlliedCreationBasic(
cmd.Operator)
_, err := gateway.ReadNotice(allied_creation_basic.ReqReadNotice{
MsgId: cmd.MsgId,
ReadAll: cmd.ReadAll,
UserBaseId: cmd.Operator.UserBaseId,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
return struct{}{}, nil
}
// 设置收藏菜单
func (srv UserService) UpdateMenuFavorite(menuFavoriteCommand *command.MenuFavoriteCommand) (interface{}, error) {
if err := menuFavoriteCommand.ValidateCommand(); err != nil {
return nil, application.ThrowError(application.ARG_ERROR, err.Error())
}
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(
menuFavoriteCommand.Operator,
)
result, err := creationUserGateway.FavoriteMenusUpadate(allied_creation_user.ReqFavoriteMenusUpdate{
UserId: menuFavoriteCommand.Operator.UserId,
FavoriteMenus: menuFavoriteCommand.FavoriteMenus,
Action: menuFavoriteCommand.Action,
})
if err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return result, nil
}
// 共创组织列表
func (srv UserService) CooperationOrg(operator domain.Operator) (interface{}, error) {
creationUserGateway := allied_creation_user.NewHttplibAlliedCreationUser(operator)
orgs, err := creationUserGateway.OrgSearch(allied_creation_user.ReqOrgSearch{
IsOrg: domain.IsOrgFlag,
Limit: 50,
})
if err != nil {
return nil, application.ThrowError(application.BUSINESS_ERROR, err.Error())
}
var ret = make([]interface{}, 0)
for i := range orgs.Orgs {
item := orgs.Orgs[i]
ret = append(ret, map[string]interface{}{
"orgId": item.OrgID,
"orgName": item.OrgName,
})
}
return map[string]interface{}{
"orgs": ret,
}, nil
}