feat: 完成账号绑定功能

This commit is contained in:
橙子
2024-11-03 01:38:12 +08:00
parent 5e096a277c
commit 5054391f6b
9 changed files with 329 additions and 85 deletions

View File

@@ -0,0 +1,10 @@
namespace Yi.Framework.Bbs.Domain.Shared.Etos;
/// <summary>
/// 临时用户绑定到正式用户
/// </summary>
public class BindAccountEto
{
public Guid NewUserId { get; set; }
public Guid OldUserId { get; set; }
}

View File

@@ -0,0 +1,47 @@
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus;
using Yi.Framework.Bbs.Domain.Entities;
using Yi.Framework.Bbs.Domain.Shared.Etos;
using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.Bbs.Domain.EventHandlers;
/// <summary>
/// 临时账号绑定到正式账号,钱钱 (累加),禁用临时账号(修改)
/// </summary>
public class BindAccountForBbsEventHandler : ILocalEventHandler<BindAccountEto>, ITransientDependency
{
private readonly ISqlSugarRepository<BbsUserExtraInfoEntity> _bbsUserRepository;
private readonly ISqlSugarRepository<UserAggregateRoot> _userRepository;
public BindAccountForBbsEventHandler(ISqlSugarRepository<BbsUserExtraInfoEntity> bbsUserRepository,
ISqlSugarRepository<UserAggregateRoot> userRepository)
{
_bbsUserRepository = bbsUserRepository;
_userRepository = userRepository;
}
public async Task HandleEventAsync(BindAccountEto eventData)
{
//禁用临时用户
var oldUser = await _userRepository.GetFirstAsync(x => x.Id == eventData.OldUserId);
if (oldUser is null || oldUser.State == false)
{
throw new UserFriendlyException("无法将无效用户进行绑定");
}
oldUser.State = false;
await _userRepository.UpdateAsync(oldUser);
//账户钱转移
var bbsOldUser = await _bbsUserRepository.GetFirstAsync(x => x.UserId == eventData.OldUserId);
var bbsNewUser = await _bbsUserRepository.GetFirstAsync(x => x.UserId == eventData.NewUserId);
if (bbsNewUser is not null)
{
bbsNewUser.Money += bbsOldUser?.Money ?? 0;
await _bbsUserRepository.UpdateAsync(bbsNewUser);
}
}
}

View File

@@ -0,0 +1,20 @@
namespace Yi.Framework.DigitalCollectibles.Application.Contracts.Dtos.InvitationCode;
public class InvitationCodeGetOutputDto
{
/// <summary>
/// 是否填写了邀请码(是否被邀请)
/// </summary>
public bool IsInvited { get; set; }
/// <summary>
/// 积分-邀请数量
/// </summary>
public int PointsNumber { get; set; }
/// <summary>
/// 邀请码
/// </summary>
public string InvitationCode { get; set; }
}

View File

@@ -1,8 +1,13 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Application.Services;
using Volo.Abp.EventBus.Local;
using Volo.Abp.Users;
using Yi.Framework.Bbs.Domain.Shared.Etos;
using Yi.Framework.DigitalCollectibles.Application.Contracts.Dtos.Account;
using Yi.Framework.DigitalCollectibles.Domain.Shared.Consts;
using Yi.Framework.DigitalCollectibles.Domain.Shared.Enums;
using Yi.Framework.DigitalCollectibles.Domain.Shared.Etos;
using Yi.Framework.Rbac.Application.Contracts.Dtos.Account;
using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.Rbac.Domain.Shared.Enums;
@@ -19,13 +24,14 @@ public class WeChatMiniProgramAccountService : ApplicationService
private readonly IWeChatMiniProgramManager _weChatMiniProgramManager;
private readonly IAuthService _authService;
private readonly IAccountService _accountService;
private readonly ILocalEventBus _localEventBus;
public WeChatMiniProgramAccountService(IWeChatMiniProgramManager weChatMiniProgramManager, IAuthService authService,
IAccountService accountService)
IAccountService accountService, ILocalEventBus localEventBus)
{
_weChatMiniProgramManager = weChatMiniProgramManager;
_authService = authService;
_accountService = accountService;
_localEventBus = localEventBus;
}
/// <summary>
@@ -63,8 +69,11 @@ public class WeChatMiniProgramAccountService : ApplicationService
/// <param name="input"></param>
/// <exception cref="UserFriendlyException"></exception>
[HttpPost("wechat/mini-program/account/bind")]
[Authorize]
public async Task PostBindAsync(BindInput input)
{
var userId = CurrentUser.GetId();
//验证手机号
await _accountService.ValidationPhoneCaptchaAsync(ValidationPhoneTypeEnum.Bind, input.Phone, input.Code);
//校验手机号与验证码
@@ -80,8 +89,17 @@ public class WeChatMiniProgramAccountService : ApplicationService
//验证手机号的验证码
var openId = (await _weChatMiniProgramManager.Code2SessionAsync(new Code2SessionInput(input.JsCode))).openid;
await PostBindToAuthAsync(userInfo.User.Id, openId, userInfo.User.UserName);
//发送账号绑定的事件,不同领域对账号数据进行迁移
//bbs钱钱 (累加),禁用临时账号(修改)
//dc: 价值、积分 (累加)
await _localEventBus.PublishAsync(new BindAccountEto
{
NewUserId = userInfo.User.Id,
OldUserId = userId
},false);
}
private async Task PostBindToAuthAsync(Guid userId, string openId, string? name = null)

View File

@@ -2,7 +2,9 @@
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Application.Services;
using Volo.Abp.Users;
using Yi.Framework.DigitalCollectibles.Application.Contracts.Dtos.InvitationCode;
using Yi.Framework.DigitalCollectibles.Domain.Entities;
using Yi.Framework.DigitalCollectibles.Domain.Managers;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.DigitalCollectibles.Application.Services;
@@ -12,11 +14,11 @@ namespace Yi.Framework.DigitalCollectibles.Application.Services;
/// </summary>
public class InvitationCodeService : ApplicationService
{
private readonly ISqlSugarRepository<InvitationCodeAggregateRoot> _repository;
private readonly InvitationCodeManager _invitationCodeManager;
public InvitationCodeService(ISqlSugarRepository<InvitationCodeAggregateRoot> repository)
public InvitationCodeService(InvitationCodeManager invitationCodeManager)
{
_repository = repository;
_invitationCodeManager = invitationCodeManager;
}
/// <summary>
@@ -24,16 +26,17 @@ public class InvitationCodeService : ApplicationService
/// </summary>
/// <returns></returns>
[Authorize]
public async Task<object> GetAsync()
public async Task<InvitationCodeGetOutputDto> GetAsync()
{
var userId = CurrentUser.GetId();
var entity = await _repository.GetFirstAsync(x => x.UserId == userId);
if (entity is null)
var entity = await _invitationCodeManager.TryGetOrAddAsync(userId);
var output = new InvitationCodeGetOutputDto
{
return new { IsInvited=false, PointsNumber=0 };
}
return new { entity.IsInvited, entity.PointsNumber };
IsInvited = entity.IsInvited,
PointsNumber = entity.PointsNumber,
InvitationCode = entity.InvitationCode
};
return output;
}
/// <summary>
@@ -42,48 +45,10 @@ public class InvitationCodeService : ApplicationService
/// <param name="invitedUserId"></param>
/// <exception cref="UserFriendlyException"></exception>
[Authorize]
public async Task SetAsync([FromQuery] Guid invitedUserId)
[HttpPost("invitation-code/{code}")]
public async Task SetAsync([FromRoute] string code)
{
var userId = CurrentUser.GetId();
var entity = await _repository.GetFirstAsync(x => x.UserId == userId);
if (entity is null)
{
await _repository.InsertAsync(new InvitationCodeAggregateRoot
{
UserId = userId,
IsInvited = true,
PointsNumber = 0
});
}
else
{
if (entity.IsInvited)
{
throw new UserFriendlyException("你已填写过邀请码,无法再次填写");
}
else
{
entity.IsInvited = false;
await _repository.UpdateAsync(entity);
}
}
var invitedEntity = await _repository.GetFirstAsync(x => x.UserId == invitedUserId);
if (entity is null)
{
await _repository.InsertAsync(new InvitationCodeAggregateRoot
{
UserId = invitedUserId,
IsInvited = false,
PointsNumber = 1
});
}
else
{
invitedEntity.PointsNumber += 1;
await _repository.UpdateAsync(invitedEntity);
}
await _invitationCodeManager.SetAsync(userId, code);
}
}

View File

@@ -1,27 +0,0 @@
using SqlSugar;
using Volo.Abp.Domain.Entities;
namespace Yi.Framework.DigitalCollectibles.Domain.Entities;
/// <summary>
/// 藏品用户信息表
/// </summary>
[SugarTable("DC_CollectiblesUserExtraInfo")]
public class CollectiblesUserExtraInfoEntity: Entity<Guid>
{
/// <summary>
/// 用户id
/// </summary>
public Guid UserId { get; set; }
/// <summary>
/// 手机号
/// </summary>
public string Phone{ get; set; }
/// <summary>
/// 微信openid
/// </summary>
public string WeChatOpenId { get; set; }
}

View File

@@ -4,8 +4,18 @@ using Volo.Abp.Domain.Entities.Auditing;
namespace Yi.Framework.DigitalCollectibles.Domain.Entities;
[SugarTable("DC_InvitationCode")]
public class InvitationCodeAggregateRoot:FullAuditedAggregateRoot<Guid>
public class InvitationCodeAggregateRoot : FullAuditedAggregateRoot<Guid>
{
public InvitationCodeAggregateRoot()
{
}
public InvitationCodeAggregateRoot(Guid userId, string invitationCode)
{
this.UserId = userId;
this.InvitationCode = invitationCode;
}
/// <summary>
/// 谁的邀请码
/// </summary>
@@ -14,14 +24,35 @@ public class InvitationCodeAggregateRoot:FullAuditedAggregateRoot<Guid>
/// <summary>
/// 是否填写了邀请码(是否被邀请)
/// </summary>
public bool IsInvited { get; set; }
public bool IsInvited { get; set; } = false;
/// <summary>
/// 积分-邀请数量
/// </summary>
public int PointsNumber { get; set; }
public int PointsNumber { get; set; } = 0;
/// <summary>
/// 邀请码
/// </summary>
public string InvitationCode { get; set; }
/// <summary>
/// 这个人填写了邀请码(不能再进行填写)
/// </summary>
public void SetInvite()
{
IsInvited = true;
}
/// <summary>
/// 别人填写了这个用户的邀请码(这个用户积分+1
/// </summary>
public void SetInvited()
{
PointsNumber += 1;
}
//不做记录
// /// <summary>

View File

@@ -0,0 +1,93 @@
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus;
using Yi.Framework.Bbs.Domain.Shared.Etos;
using Yi.Framework.DigitalCollectibles.Domain.Entities;
using Yi.Framework.DigitalCollectibles.Domain.Entities.Record;
using Yi.Framework.DigitalCollectibles.Domain.Managers;
using Yi.Framework.DigitalCollectibles.Domain.Shared.Etos;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.DigitalCollectibles.Domain.EventHandlers;
/// <summary>
/// 临时账号绑定到正式账号,价值、积分 (累加)
/// </summary>
public class BindAccountForCollectiblesEventHandler : ILocalEventHandler<BindAccountEto>, ITransientDependency
{
private readonly InvitationCodeManager _invitationCodeManager;
private readonly ISqlSugarRepository<CollectiblesUserStoreAggregateRoot> _collectiblesUserStoreRepository;
private readonly ISqlSugarRepository<MiningPoolRecordAggregateRoot> _miningPoolRecordRepository;
private readonly ISqlSugarRepository<MarketRecordAggregateRoot> _marketRecordRepository;
private readonly ISqlSugarRepository<MarketGoodsAggregateRoot> _marketGoodsRepository;
public BindAccountForCollectiblesEventHandler(InvitationCodeManager invitationCodeManager, ISqlSugarRepository<CollectiblesUserStoreAggregateRoot> collectiblesUserStoreRepository, ISqlSugarRepository<MiningPoolRecordAggregateRoot> miningPoolRecordRepository, ISqlSugarRepository<MarketRecordAggregateRoot> marketRecordRepository, ISqlSugarRepository<MarketGoodsAggregateRoot> marketGoodsRepository)
{
_invitationCodeManager = invitationCodeManager;
_collectiblesUserStoreRepository = collectiblesUserStoreRepository;
_miningPoolRecordRepository = miningPoolRecordRepository;
_marketRecordRepository = marketRecordRepository;
_marketGoodsRepository = marketGoodsRepository;
}
public async Task HandleEventAsync(BindAccountEto eventData)
{
var oldEntity = await _invitationCodeManager.TryGetOrAddAsync(eventData.OldUserId);
var newEntity = await _invitationCodeManager.TryGetOrAddAsync(eventData.NewUserId);
newEntity.PointsNumber += oldEntity.PointsNumber;
//临时账号邀请了,老的账号没有邀请,覆盖
if (newEntity.IsInvited == false && oldEntity.IsInvited == true)
{
newEntity.IsInvited = true;
}
await _invitationCodeManager._repository.UpdateAsync(newEntity);
//藏品转移
var oldUserStore= await _collectiblesUserStoreRepository.GetListAsync(x => x.UserId == eventData.OldUserId);
if (oldUserStore.Count>0)
{
oldUserStore?.ForEach(x=>x.UserId=eventData.NewUserId);
await _collectiblesUserStoreRepository.UpdateRangeAsync(oldUserStore);
}
//挖矿记录转移
var miningPoolRecord= await _miningPoolRecordRepository.GetListAsync(x => x.UserId == eventData.OldUserId);
if (miningPoolRecord.Count>0)
{
miningPoolRecord?.ForEach(x=>x.UserId=eventData.NewUserId);
await _miningPoolRecordRepository.UpdateRangeAsync(miningPoolRecord);
}
//交易记录转移
var marketRecord= await _marketRecordRepository.GetListAsync(x => x.SellUserId == eventData.OldUserId||x.BuyId==eventData.OldUserId);
if (marketRecord.Count>0)
{
marketRecord?.ForEach(x =>
{
if (x.SellUserId == eventData.OldUserId)
{
x.SellUserId = eventData.NewUserId;
}
if (x.BuyId == eventData.OldUserId)
{
x.BuyId = eventData.NewUserId;
}
});
await _marketRecordRepository.UpdateRangeAsync(marketRecord);
}
//商城物品交易转移
var marketGoods= await _marketGoodsRepository.GetListAsync(x => x.SellUserId == eventData.OldUserId);
if (marketGoods.Count>0)
{
marketGoods?.ForEach(x=>x.SellUserId=eventData.NewUserId);
await _marketGoodsRepository.UpdateRangeAsync(marketGoods);
}
}
}

View File

@@ -0,0 +1,87 @@
using Volo.Abp.Domain.Services;
using Yi.Framework.DigitalCollectibles.Domain.Entities;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.DigitalCollectibles.Domain.Managers;
/// <summary>
/// 邀请码领域服务
/// </summary>
public class InvitationCodeManager : DomainService
{
public readonly ISqlSugarRepository<InvitationCodeAggregateRoot> _repository;
public InvitationCodeManager(ISqlSugarRepository<InvitationCodeAggregateRoot> repository)
{
_repository = repository;
}
/// <summary>
/// 填写邀请码
/// </summary>
public async Task SetAsync(Guid writeUserId, string invitationCode)
{
var entityOrNull = await _repository.GetFirstAsync(x => x.InvitationCode == invitationCode);
if (entityOrNull is null)
{
throw new UserFriendlyException("无效邀请码,请检查");
}
if (entityOrNull.UserId==writeUserId)
{
throw new UserFriendlyException("你不能邀请自己");
}
//被邀请的人
var entity = entityOrNull;
entity.SetInvited();
//填写邀请码的人
var writeEntity = await TryGetOrAddAsync(writeUserId);
writeEntity.SetInvite();
await _repository.UpdateRangeAsync(new List<InvitationCodeAggregateRoot> { entity, writeEntity });
}
public async Task<InvitationCodeAggregateRoot> TryGetOrAddAsync(Guid userId, int InitPointsNumber = 0)
{
var entity = await _repository.FindAsync(x => x.UserId == userId);
if (entity is null)
{
string invitationCode = string.Empty;
//循环到邀请码没有重复为止
var isExist = true;
while (isExist)
{
invitationCode = CreateInvitationCode(4);
if (!await _repository.IsAnyAsync(x => x.InvitationCode == invitationCode))
{
isExist = false;
}
}
var insertEntity = new InvitationCodeAggregateRoot(userId, invitationCode)
{
PointsNumber = InitPointsNumber
};
entity = await _repository.InsertReturnEntityAsync(insertEntity);
}
return entity;
}
private string CreateInvitationCode(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random = new Random();
char[] stringChars = new char[length];
for (int i = 0; i < length; i++)
{
stringChars[i] = chars[random.Next(chars.Length)];
}
return new string(stringChars);
}
}