From 5bc4a0340f55124ebe5b87e393f42d8e96d837b6 Mon Sep 17 00:00:00 2001
From: zhengyiming <540361168@qq.com>
Date: 星期一, 31 三月 2025 14:37:05 +0800
Subject: [PATCH] fix: 调整ChannelOutput

---
 LifePayment/LifePayment.Application/User/AccountService.cs |  315 ++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 293 insertions(+), 22 deletions(-)

diff --git a/LifePayment/LifePayment.Application/User/AccountService.cs b/LifePayment/LifePayment.Application/User/AccountService.cs
index c6c47e0..1e32b00 100644
--- a/LifePayment/LifePayment.Application/User/AccountService.cs
+++ b/LifePayment/LifePayment.Application/User/AccountService.cs
@@ -1,42 +1,125 @@
-锘縰sing LifePayment.Application.Contracts;
+锘�
+using LifePayment.Application.Contracts;
+using LifePayment.Domain;
 using LifePayment.Domain.Models;
+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;
 
-namespace HumanResourcesServices.Application
+
+namespace LifePayment.Application
 {
     public class AccountService : ApplicationService, IAccountService
     {
-
-
+        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(
-
-               IRepository<LifePayUser, Guid> lifePayUserRepository
-)
+               IWxManager wxManager,
+               IConfiguration configuration,
+               IIdentityModelAuthenticationService authenticator,
+               IRepository<User, Guid> userRepository,
+               IIdentityUserAppService identityUserService,
+               IRepository<LifePayUser, Guid> lifePayUserRepository,
+               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 cacheData = await _distributedCache.GetAsync(code);
+            var result = new WxMiniAppIndentityInfo();
+            if (cacheData != null)
+            {
+                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
+            }).ToListAsync();
+
+            BackClientUserInfoOutput result = new BackClientUserInfoOutput()
+            {
+                IsBackClientUser = CurrentUser.ClientId == Constant.ClientType.Back,
+                IsSystem = CurrentUser.IsSystem,
+                ChannleList = channleList
+            };
+            return result;
+        }
 
 
         #endregion
 
         #region 鎿嶄綔
 
- 
+
 
         #region life pay
 
@@ -47,23 +130,15 @@
         /// <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)
             {
                 lifeUser = new LifePayUser()
                 {
                     Id = GuidGenerator.Create(),
+                    CreationChannleNum = input.CheckChannelId,
                     PhoneNumber = input.PhoneNumber,
                     LastLoginTime = DateTime.Now
                 };
@@ -72,17 +147,213 @@
             }
             else
             {
+                lifeUser.LastLoginChannleNum = input.CheckChannelId;
                 lifeUser.LastLoginTime = DateTime.Now;
                 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
+                }).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)
+            {
+                channlesNum = await _userChannleRep.Where(x => x.UserId == backClientUser.Id)
+                    .Select(s => new ChannelOutput()
+                    {
+                        Name = _lifePayChannlesRepository.Where(x => x.ChannlesNum == s.ChannleId).Select(r => r.ChannlesName).FirstOrDefault(),
+                        ChannlesNum = s.ChannleId
+                    }).ToListAsync();
+            }
+
+            LifePayPhoneMesssageCodeLoginOutput result = new LifePayPhoneMesssageCodeLoginOutput()
+            {
+                IsBackClientUser = backClientUser == null ? false : true,
+                ChannlesNum = channlesNum
+            };
+            return result;
+        }
+
+        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)
+        {
+            //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);
+
+            /// input.Password ??= GlobalRandom.GetRandomPassword();
+
+            CheckExtensions.IfTrueThrowUserFriendlyException(string.IsNullOrEmpty(input.Password),
+                                                             "璇疯緭鍏ュ瘑鐮�");
+            var res = await _identityUserService.CreateAsync(new IdentityUserCreateDto
+            {
+                Name = input.Name,
+                PhoneNumber = input.PhoneNumber,
+                UserName = input.UserName,
+                RoleNames = input.RoleNames,
+                ClientId = input.ClientId,
+                Password = input.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
+            });
+
+            List<UserChannle> userChannles = new List<UserChannle>();
+            foreach (var item in input.ChannlesId)
+            {
+                userChannles.Add(new UserChannle()
+                {
+                    Id = Guid.NewGuid(),
+                    ChannleId = item,
+                    UserId = user.Id
+                });
+            }
+
+            await _userChannleRep.InsertManyAsync(userChannles);
+            //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
 
-     
+
     }
+
 }
\ No newline at end of file

--
Gitblit v1.9.1