HuanMengProject/src/0-core/HuanMeng.MiaoYu.Code/Users/UserBLL.cs
2024-09-03 17:10:36 +08:00

278 lines
12 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using HuanMeng.DotNetCore.Base;
using HuanMeng.MiaoYu.Code.Base;
using HuanMeng.MiaoYu.Code.DataAccess;
using HuanMeng.MiaoYu.Code.Users.UserAccount;
using HuanMeng.MiaoYu.Code.Users.UserAccount.Contract;
using HuanMeng.MiaoYu.Code.Users.UserAccount.PhoneAccount;
using HuanMeng.MiaoYu.Model.Dto;
using HuanMeng.MiaoYu.Model.Dto.Account;
using HuanMeng.MiaoYu.Model.Dto.Character;
using HuanMeng.MiaoYu.Model.Dto.Shop;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using XLib.DotNetCore.CacheHelper;
using static System.Net.Mime.MediaTypeNames;
namespace HuanMeng.MiaoYu.Code.Users
{
/// <summary>
/// 用户类
/// </summary>
public class UserBLL : MiaoYuBase
{
public UserBLL(IServiceProvider serviceProvider) : base(serviceProvider)
{
}
/// <summary>
/// 发送手机号码
/// </summary>
/// <param name="PhoneNumber"></param>
/// <returns></returns>
public async Task<BaseResponse<bool>> SendPhoneNumber(string PhoneNumber)
{
PhoneAccountLogin phoneAccountLogin = new PhoneAccountLogin(VerificationCodeManager, TencentConfig, Dao);
var msg = await phoneAccountLogin.SendPhone(PhoneNumber);
return msg;
}
/// <summary>
/// 登录
/// </summary>
/// <param name="requestLoginModel"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public async Task<BaseResponse<ResponseAccountLogIn>> AccountLogIn(RequestLoginModel requestLoginModel)
{
IUserAccount userAccount = null;
BaseLoginParams loginParams = null;
string ip = HttpContextAccessor.HttpContext.GetClientIpAddress();
if (requestLoginModel.LoginType == 0)
{
userAccount = new TokenAccountLogin(JwtAuthManager, Dao);
loginParams = new TokenLoginParams()
{
Token = requestLoginModel.Token,
Ip = ip
};
}
else if (requestLoginModel.LoginType == 1)
{
userAccount = new PhoneAccountLogin(VerificationCodeManager, TencentConfig, Dao);
loginParams = new PhoneLoginParams()
{
PhoneNumber = requestLoginModel.PhoneNumber,
VerificationCode = requestLoginModel.VerificationCode,
Ip = ip
};
}
else
{
throw new Exception("不支持的登录方式");
}
var accountInfo = await userAccount.LoginAsync(loginParams);
if (string.IsNullOrEmpty(accountInfo.Token))
{
var claims = new[]
{
//new Claim(ClaimTypes.Name,accountInfo.NickName),
new Claim("NickName",accountInfo.NickName),
new Claim("UserId",accountInfo.UserId.ToString()),
};
var jwtAuthResulttre = JwtAuthManager.GenerateTokens(accountInfo.NickName, claims, DateTime.Now);
accountInfo.Token = jwtAuthResulttre.AccessToken;
}
ResponseAccountLogIn responseAccountLogIn = new ResponseAccountLogIn()
{
Token = accountInfo.Token,
NickName = accountInfo.NickName,
UserId = accountInfo.UserId,
};
return new BaseResponse<ResponseAccountLogIn>(ResonseCode.Success, "登录成功", responseAccountLogIn) { };
}
/// <summary>
/// 获取用户信息
/// </summary>
/// <returns></returns>
public async Task<BaseResponse<ResponseUserInfo>> GetUserInfo()
{
var user = await Dao.daoDbMiaoYu.context.T_User.FirstOrDefaultAsync(it => it.Id == _UserId);
var userData = await Dao.daoDbMiaoYu.context.T_User_Data.FirstOrDefaultAsync(it => it.Id == _UserId);
//获取用户余额
UserInfoBLL userInfoBLL = new UserInfoBLL(Dao, _UserId);
var Currency = userInfoBLL[UserCurrencyType.]?.CurrencyMoney;
//var Currency = user.GetUserCurrencyMoney(UserCurrencyType.语珠, Dao);
var RemainingChatCount = userInfoBLL[UserCurrencyType.].CurrencyMoney;
//var RemainingChatCount = user.GetUserCurrencyMoney(UserCurrencyType.聊天次数, Dao);
//var memoryCard = userInfoBLL[UserCurrencyType.记忆卡].CurrencyMoney;
var memoryCard = Dao.daoDbMiaoYu.context.T_User_MemoryCard.Where(it => it.UserId == _UserId && it.CharacterId == 0).Count();
//var memoryCard = user.GetUserCurrencyMoney(UserCurrencyType.记忆卡, Dao);
//获取聊天次数
var hasTalked = Dao.daoDbMiaoYu.context.T_User_Chat.Where(it => it.UserId == _UserId && !it.IsDelete && it.TotalToken > 0).Count();
List<CreateCharacterInfo> characters = new List<CreateCharacterInfo>();
return new BaseResponse<ResponseUserInfo>(ResonseCode.Success, "请求成功", new ResponseUserInfo
{
NickName = user.NickName,
UserId = user.Id,
Currency = (int)Currency,
UserIconUrl = string.IsNullOrEmpty(userData.UserIconUrl) ? "https://cos.shhuanmeng.com/default.png" : userData.UserIconUrl,
RemainingChatCount = (int)RemainingChatCount,//这里先写1我不会decimal转int
HasTalked = hasTalked,
Photographs = 0,
MemoryCardCount = (int)memoryCard,
CharacterInfo = new List<CreateCharacterInfo>
{
//new CreateCharacterInfo
//{
// CharacterId = 1,
// CharacterName = "林婉儿",
// BgImage = "https://cos.shhuanmeng.com/image/icon/%E6%9E%97%E5%A9%89%E5%84%BF.png"
//},
//new CreateCharacterInfo
//{
// CharacterId = 2,
// CharacterName = "赵灵儿",
// BgImage = "https://cos.shhuanmeng.com/image/icon/%E6%9E%97%E5%A9%89%E5%84%BF.png"
//}
//// 可以继续添加更多的 CreateCharacterInfo 对象
},
InviteNewUser = new InviteNewUserDto
{
ImgUrl = "https://cos.shhuanmeng.com/image/icon/20240720205857.png",
Type = 0
}
});
}
/// <summary>
/// 交易记录
/// </summary>
/// <returns></returns>
public async Task<BaseResponse<List<TransactionDto>>> GetTransactionRecords()
{
if (_UserId == 0)
{
return new BaseResponse<List<TransactionDto>>(ResonseCode.Success, "", new List<TransactionDto>());
}
var logs = await Dao.daoDbMiaoYu.context.T_User_Currency_Log.Where(it => it.UserId == _UserId && !(it.CurrencyType == (int)UserCurrencyType. && it.ConsumeType == 0) && !it.IsHide).OrderByDescending(it => it.Id).Take(50).ToListAsync();
List<TransactionDto> list = new List<TransactionDto>();
foreach (var item in logs)
{
if (string.IsNullOrEmpty(item.Title))
{
continue;
}
TransactionDto transactionDto = new TransactionDto();
transactionDto.TransactionType = 0;
transactionDto.TransactionTime = item.CreateTime;
transactionDto.CurrencyType = item.CurrencyType;
transactionDto.TransactionContent = item.Title;
transactionDto.TransactionAmount = $"{(item.Consume > 0 ? "+" : "")}{item.Consume.ToString("0.##")}{(item.CurrencyType == 1 ? UserCurrencyType.语珠.ToString() : "")}";
list.Add(transactionDto);
}
return new BaseResponse<List<TransactionDto>>(ResonseCode.Success, "", list);
}
/// <summary>
/// 获取未使用的记忆卡列表
/// </summary>
/// <returns></returns>
public async Task<BaseResponse<List<PropInfoDto>>> GetMemoryCardInfo()
{
var list = await Dao.daoDbMiaoYu.context.T_User_MemoryCard.Where(it => it.UserId == _UserId && it.CharacterId == 0).ToListAsync();
List<PropInfoDto> p = new List<PropInfoDto>();
foreach (var item in list)
{
PropInfoDto propInfoDto = new PropInfoDto()
{
PropId = item.Id,
ImgUrl = item.Image,
PropName = item.Name,
};
p.Add(propInfoDto);
}
return new BaseResponse<List<PropInfoDto>>(ResonseCode.Success, "", p);
}
/// <summary>
/// 使用记忆卡
/// </summary>
/// <param name="memoryCardRequest"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public async Task<BaseResponse<bool>> UseMemoryCard(MemoryCardRequest memoryCardRequest)
{
if (memoryCardRequest.PropId == 0)
{
throw new Exception("道具id不对");
}
var memory = await Dao.daoDbMiaoYu.context.T_User_MemoryCard.Where(it => it.UserId == _UserId && it.Id == memoryCardRequest.PropId).FirstOrDefaultAsync();
if (memory == null)
{
throw new Exception("道具不存在");
}
if (memory.CharacterId != 0)
{
throw new Exception("记忆卡已经被使用");
}
var key = $"Memory:lock:{_UserId}:{memoryCardRequest.CharacterId}:{memoryCardRequest.PropId}";
if (!RedisCache.StringSetLock(key, "", 10))
{
throw new Exception("记忆卡重复使用");
}
memory.CharacterId = memoryCardRequest.CharacterId;
memory.Remark = $"记忆卡使用时间{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}";
await Dao.daoDbMiaoYu.context.SaveChangesAsync();
RedisCache.KeyDelete(key);
var x = $"{Dao.daoDbMiaoYu.context.TenantInfo.TenantId}:User:{_UserId}:MemoryCard";
MemoryCacheHelper.DelCache(x);
return new BaseResponse<bool>(ResonseCode.Success, "使用成功", true);
}
/// <summary>
/// 账号注销
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public async Task<BaseResponse<bool>> Logout()
{
if (_UserId == 0)
{
throw new Exception("请先登录");
}
//
var user = await Dao.daoDbMiaoYu.context.T_User.Where(it => it.Id == _UserId).FirstOrDefaultAsync();
//
var userAccount = await Dao.daoDbMiaoYu.context.T_User_Phone_Account.Where(it => it.UserId == _UserId).FirstOrDefaultAsync();
if (user == null || userAccount == null)
{
throw new Exception("账号不存在");
}
user.State = 1;
userAccount.IsLogout = true;
Dao.daoDbMiaoYu.context.SaveChanges();
return new BaseResponse<bool>(ResonseCode.Success, "", true);
//userAccount.p
}
}
}