lingling
2025-03-17 2dffeebb60078f6ee1d59ac327c0ecce3fd200e9
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
 
using LifePayment.Application.Contracts;
using LifePayment.Domain;
using LifePayment.Domain.Models;
using LifePayment.Domain.Shared;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;
using Volo.Abp.Identity.Application.Contracts.Account;
using Volo.Abp.IdentityModel;
using ZeroD.Util;
using static LifePayment.Domain.Shared.LifePaymentConstant;
 
 
namespace LifePayment.Application
{
    public class AccountService : ApplicationService, IAccountService
    {
        private readonly IWxManager _wxManager;
        private readonly IRepository<LifePayUser, Guid> _lifePayUserRepository;
        private readonly IConfiguration _configuration;
        private readonly IIdentityModelAuthenticationService _authenticator;
        private readonly IRepository<User, Guid> _userRepository;
        private readonly IIdentityUserAppService _identityUserService;
 
        public AccountService(
               IWxManager wxManager,
               IConfiguration configuration,
               IIdentityModelAuthenticationService authenticator,
               IIdentityUserAppService identityUserService,
               IRepository<LifePayUser, Guid> lifePayUserRepository)
        {
            _configuration = configuration;
            _wxManager = wxManager;
            _identityUserService = identityUserService;
            _lifePayUserRepository = lifePayUserRepository;
            _authenticator = authenticator;
        }
 
        #region 查询
 
        public async Task<WxMiniAppIndentityInfo> GetLifePayWxIndentity(string code)
        {
            var res = await _wxManager.GetWxOauth2AccessToken(code);
            var result = new WxMiniAppIndentityInfo
            {
                SessionKey = res.SessionKey,
                OpenId = res.OpenId,
                UnionId = res.UnionId
            };
 
            return result;
        }
 
        #endregion
 
        #region 操作
 
 
 
        #region life pay
 
        /// <summary>
        /// 手机验证码登录
        /// 版本说明:使用验证码管理去校验和失效对应业务的验证码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="UserFriendlyException"></exception>
        public async Task<Guid> LifePayPhoneMesssageCodeLogin(LifePayPhoneMesssageCodeLoginInput input)
        {
            //var vcodeType = VerificationCodeBusinessTypeEnum.LifePayPhoneMesssageCodeLogin;
 
            //var checkResult = await _verificationCodeManager.CheckVerificationCodeByBusinessType(vcodeType,
            //                                                                                     input.PhoneNumber,
            //                                                                                     input.Code,
            //                                                                                     true);
            //CheckExtensions.IfTrueThrowUserFriendlyException(!checkResult,
            //                                                 CustomeErrorMessage.SometingWrongOrSometing, "验证码", "已失效");
 
            var lifeUser = await _lifePayUserRepository.Where(x => x.PhoneNumber == input.PhoneNumber).FirstOrDefaultAsync();
            if (lifeUser == null)
            {
                lifeUser = new LifePayUser()
                {
                    Id = GuidGenerator.Create(),
                    PhoneNumber = input.PhoneNumber,
                    LastLoginTime = DateTime.Now
                };
 
                await _lifePayUserRepository.InsertAsync(lifeUser);
            }
            else
            {
                lifeUser.LastLoginTime = DateTime.Now;
                await _lifePayUserRepository.UpdateAsync(lifeUser);
            }
 
            return lifeUser.Id;
        }
 
        public async Task<IdentityModelTokenCacheItem> GetTokenForWeb(AccessRequestDto accessRequestDto, string webClientIp)
        {
            IdentityClientConfiguration config = new IdentityClientConfiguration
            {
                UserName = accessRequestDto.UserName,
                UserPassword = accessRequestDto.UserPassword,
                GrantType = accessRequestDto.GrantType == 1 ? "password" : "client_credentials",
                ClientId = accessRequestDto.ClientId,
                ClientSecret = "1q2w3e*",
                Authority = _configuration["AuthServer:Authority"],
                Scope = accessRequestDto.Scope + " offline_access",
            };
            var result = await _authenticator.GetAccessTokenAsync(config);
            var user = await _userRepository.Where(r => r.UserName == accessRequestDto.UserName && !r.IsDeleted).FirstOrDefaultAsync();
 
            // 记录日志
           // await PublishUserOperateHistoryEvent(LogsSpecies.Login, LogsSpecies.Login, user.Id, user.Id, creatorName: user.Name);
 
            return result;
        }
 
        public async Task<Guid> CreateAccount(CreateAccountInput input, bool isSend, bool isAdminCreate = false, string password = null)
        {
            if (input.ClientId == LifePaymentConstant.ClientId.Back)
            {
                CheckExtensions.IfTrueThrowUserFriendlyException(!input.CompanyOrgId.HasValue || !input.DepartmentOrgId.HasValue,
                                                                 "所属公司和部门不能为空");
            }
 
            var any = await _userRepository.Where(x => x.PhoneNumber == input.PhoneNumber && x.ClientId == input.ClientId).AnyAsync();
            CheckExtensions.IfTrueThrowUserFriendlyException(any,
                                                             CustomeErrorMessage.PhoneNumberRepeatSaveFail);
 
            password ??= GlobalRandom.GetRandomPassword();
 
            var res = await _identityUserService.CreateAsync(new IdentityUserCreateDto
            {
                Name = input.Name,
                PhoneNumber = input.PhoneNumber,
                UserName = input.UserName,
                RoleNames = input.RoleNames,
                ClientId = input.ClientId,
                Password = password,
            });
            var user = await _userRepository.InsertAsync(new User
            {
                Id = res.Id,
                Name = input.Name,
                UserName = input.UserName,
                PhoneNumber = input.PhoneNumber,
                ClientId = input.ClientId,
                OpenId = input.OpenId,
                LastLoginTime = DateTime.Now,
                ContactPhone = input.PhoneNumber,
                Contact = isAdminCreate ? input.Name : null,
                EnterpriseName = input.EnterpriseName,
                AuthType = input.AuthType,
                Remark = input.Remark,
                DepartmentOrgId = input.ClientId == LifePaymentConstant.ClientId.Back ? input.DepartmentOrgId : null,
                CompanyOrgId = input.ClientId == LifePaymentConstant.ClientId.Back ? input.CompanyOrgId : null
            });
            //if (input.ClientId == Constant.ClientType.Back)
            //{
            //    await _distributedEventBus.PublishAsync(new SendPhoneMessageInput
            //    {
            //        Phone = input.PhoneNumber,
            //        ProviderName = "CreateBackAccount",
            //        TemplateParams = new
            //        {
            //            account = input.UserName,
            //            pwd = password,
            //        },
            //        MessageType = PhoneMessageTypeEnum.Notice
            //    });
            //}
            //else
            //{
            //    if (isSend)
            //    {
            //        //await _commonManager.SendPhoneMessage(new SendPhoneMessageInput
            //        //{
            //        //    Phone = input.PhoneNumber,
            //        //    ProviderName = "CreateAccount",
            //        //    TemplateParams = new
            //        //    {
            //        //        name = input.Name,
            //        //        account = input.UserName,
            //        //        pwd = password,
            //        //    },
            //        //});
 
            //        await _distributedEventBus.PublishAsync(new SendPhoneMessageInput
            //        {
            //            Phone = input.PhoneNumber,
            //            ProviderName = "CreateAccountNotice",
            //            TemplateParams = new
            //            {
            //                account = input.UserName,
            //                password = password,
            //            },
            //            MessageType = PhoneMessageTypeEnum.Notice
            //        });
            //    }
            //}
 
            var logoption = LogsSpecies.UserRegister;
            var creatorName = user.Name;
 
            // 记录日志
            if (isAdminCreate)
            {
                logoption = LogsSpecies.Create;
                creatorName = CurrentUser.Name;
            }
 
            //await PublishUserOperateHistoryEvent(logoption, logoption, res.Id, res.Id, creatorName: creatorName);
 
            #region 添加IM用户
 
            //await PublishAddTencentUserEvent(user.Id, user.UserName, user.AvatarUrl);
 
            #endregion
 
            return res.Id;
        }
        #endregion
 
        #endregion
 
 
    }
}