From 2dffeebb60078f6ee1d59ac327c0ecce3fd200e9 Mon Sep 17 00:00:00 2001
From: lingling <kety1122@163.com>
Date: 星期一, 17 三月 2025 17:44:03 +0800
Subject: [PATCH] 添加后端用户逻辑

---
 LifePayment/LifePayment.Application/User/AccountService.cs |  152 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 146 insertions(+), 6 deletions(-)

diff --git a/LifePayment/LifePayment.Application/User/AccountService.cs b/LifePayment/LifePayment.Application/User/AccountService.cs
index e38604a..3f8cd2e 100644
--- a/LifePayment/LifePayment.Application/User/AccountService.cs
+++ b/LifePayment/LifePayment.Application/User/AccountService.cs
@@ -1,31 +1,46 @@
-锘縰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 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,
-               IRepository<LifePayUser, Guid> lifePayUserRepository
-)
+               IConfiguration configuration,
+               IIdentityModelAuthenticationService authenticator,
+               IIdentityUserAppService identityUserService,
+               IRepository<LifePayUser, Guid> lifePayUserRepository)
         {
+            _configuration = configuration;
             _wxManager = wxManager;
+            _identityUserService = identityUserService;
             _lifePayUserRepository = lifePayUserRepository;
+            _authenticator = authenticator;
         }
 
         #region 鏌ヨ
@@ -90,10 +105,135 @@
             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
 
-     
+
     }
 }
\ No newline at end of file

--
Gitblit v1.9.1