zhengyuxuan
2025-04-01 86d7078a77afb507e377ff4f02c14d1dd2defb6e
LifePayment/LifePayment.Application/User/AccountService.cs
@@ -5,17 +5,22 @@
using LifePayment.Domain.Shared;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Nest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Caching;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Identity;
using Volo.Abp.Identity.Application.Contracts.Account;
using Volo.Abp.IdentityModel;
using Volo.Abp.Threading;
using ZeroD.Util;
using ZeroD.Util.Fadd;
using static LifePayment.Domain.Shared.LifePaymentConstant;
@@ -25,11 +30,14 @@
    {
        private readonly IWxManager _wxManager;
        private readonly IRepository<LifePayUser, Guid> _lifePayUserRepository;
        private readonly IRepository<LifePayChannles, Guid> _lifePayChannlesRepository;
        private readonly IConfiguration _configuration;
        private readonly IIdentityModelAuthenticationService _authenticator;
        private readonly IRepository<User, Guid> _userRepository;
        private readonly IRepository<UserChannle, Guid> _userChannleRep;
        private readonly IIdentityUserAppService _identityUserService;
        private readonly IDistributedCache<string> _distributedCache;
        public AccountService(
               IWxManager wxManager,
@@ -38,31 +46,75 @@
               IRepository<User, Guid> userRepository,
               IIdentityUserAppService identityUserService,
               IRepository<LifePayUser, Guid> lifePayUserRepository,
               IRepository<UserChannle, Guid> userChannleRep)
               IRepository<LifePayChannles, Guid> lifePayChannlesRepository,
               IRepository<UserChannle, Guid> userChannleRep,
               IDistributedCache<string> distributedCache)
        {
            _configuration = configuration;
            _wxManager = wxManager;
            _identityUserService = identityUserService;
            _lifePayUserRepository = lifePayUserRepository;
            _lifePayChannlesRepository = lifePayChannlesRepository;
            _authenticator = authenticator;
            _userRepository = userRepository;
            _userChannleRep = userChannleRep;
            _distributedCache = distributedCache;
        }
        #region 查询
        public async Task<WxMiniAppIndentityInfo> GetLifePayWxIndentity(string code)
        {
            var res = await _wxManager.GetWxOauth2AccessToken(code);
            var result = new WxMiniAppIndentityInfo
            var cacheData = await _distributedCache.GetAsync(code);
            var result = new WxMiniAppIndentityInfo();
            if (cacheData != null)
            {
                SessionKey = res.SessionKey,
                OpenId = res.OpenId,
                UnionId = res.UnionId
            };
                result.OpenId = cacheData;
            }
            else
            {
                var res = await _wxManager.GetWxOauth2AccessToken(code);
                result = new WxMiniAppIndentityInfo
                {
                    SessionKey = res.SessionKey,
                    OpenId = res.OpenId,
                    UnionId = res.UnionId
                };
                if (!string.IsNullOrEmpty(res.OpenId))
                {
                    await _distributedCache.SetAsync(code, res.OpenId, options: new Microsoft.Extensions.Caching.Distributed.DistributedCacheEntryOptions()
                    {
                        AbsoluteExpirationRelativeToNow = TimeSpan.FromHours(24)
                    });
                }
            }
            return result;
        }
        public async Task<BackClientUserInfoOutput> GetBackClientUserInfo()
        {
            var channlesNums = await _userChannleRep.Where(x => x.UserId == CurrentUser.Id).Select(s => s.ChannleId).Distinct().ToListAsync();
            var channleList = await _lifePayChannlesRepository.Where(x => channlesNums.Contains(x.ChannlesNum)).Select(s => new ChannelOutput()
            {
                Name = s.ChannlesName,
                ChannlesId = s.Id,
                ChannlesNum = s.ChannlesNum,
                SwitchType = s.SwitchType,
            }).ToListAsync();
            BackClientUserInfoOutput result = new BackClientUserInfoOutput()
            {
                IsBackClientUser = CurrentUser.ClientId == Constant.ClientType.Back,
                IsSystem = CurrentUser.IsSystem,
                ChannleList = channleList
            };
            return result;
        }
        #endregion
@@ -79,17 +131,8 @@
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="UserFriendlyException"></exception>
        public async Task<Guid> LifePayPhoneMesssageCodeLogin(LifePayPhoneMesssageCodeLoginInput input)
        public async Task<LifePayPhoneMesssageCodeLoginOutput> 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)
            {
@@ -110,7 +153,60 @@
                await _lifePayUserRepository.UpdateAsync(lifeUser);
            }
            return lifeUser.Id;
            List<ChannelOutput> channlesNum = new List<ChannelOutput>();
            var backClientUser = await _userRepository.Where(x => x.ClientId == Constant.ClientType.Back
            && x.PhoneNumber == input.PhoneNumber).FirstOrDefaultAsync();
            if (backClientUser != null)
            {
                var channlesNums = await _userChannleRep.Where(x => x.UserId == backClientUser.Id).Select(s => s.ChannleId).Distinct().ToListAsync();
                channlesNum = await _lifePayChannlesRepository.Where(x => channlesNums.Contains(x.ChannlesNum)).Select(s => new ChannelOutput()
                {
                    Name = s.ChannlesName,
                    ChannlesId = s.Id,
                    ChannlesNum = s.ChannlesNum,
                    SwitchType = s.SwitchType,
                }).ToListAsync();
            }
            var result = await LifePayUserMesssageByPhone(lifeUser.PhoneNumber);
            result.UserId = lifeUser.Id;
            return result;
        }
        public async Task<LifePayPhoneMesssageCodeLoginOutput> LifePayUserMesssageByIduser(Guid id)
        {
            var lifeUser = await _lifePayUserRepository.Where(x => x.Id == id).FirstOrDefaultAsync();
            CheckExtensions.IfTrueThrowUserFriendlyException(lifeUser == null,
                                                             "用户不存在");
            var result = await LifePayUserMesssageByPhone(lifeUser.PhoneNumber);
            result.UserId = id;
            return result;
        }
        public async Task<LifePayPhoneMesssageCodeLoginOutput> LifePayUserMesssageByPhone(string phoneNumber)
        {
            List<ChannelOutput> channlesNum = new List<ChannelOutput>();
            var backClientUser = await _userRepository.Where(x => x.ClientId == Constant.ClientType.Back
             && x.PhoneNumber == phoneNumber).FirstOrDefaultAsync();
            if (backClientUser != null)
            {
                var channlesNums = await _userChannleRep.Where(x => x.UserId == backClientUser.Id).Select(s => s.ChannleId).Distinct().ToListAsync();
                channlesNum = await _lifePayChannlesRepository.Where(x => channlesNums.Contains(x.ChannlesNum)).Select(s => new ChannelOutput()
                {
                    Name = s.ChannlesName,
                    ChannlesId = s.Id,
                    ChannlesNum = s.ChannlesNum,
                    SwitchType = s.SwitchType,
                }).ToListAsync();
            }
            LifePayPhoneMesssageCodeLoginOutput result = new LifePayPhoneMesssageCodeLoginOutput()
            {
                IsBackClientUser = backClientUser == null ? false : true,
                ChannlesNum = channlesNum
            };
            return result;
        }
        public async Task<IdentityModelTokenCacheItem> GetTokenForWeb(AccessRequestDto accessRequestDto, string webClientIp)
@@ -129,18 +225,18 @@
            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);
            // 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)
        {
            if (input.ClientId == LifePaymentConstant.ClientId.Back)
            {
                CheckExtensions.IfTrueThrowUserFriendlyException(!input.CompanyOrgId.HasValue || !input.DepartmentOrgId.HasValue,
                                                                 "所属公司和部门不能为空");
            }
            //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,
@@ -253,10 +349,15 @@
            return res.Id;
        }
        #endregion
        #endregion
    }
}