Merge branch 'dev' of https://gitea.nxsir.cn/nanxun/ql_apimanager_backend into dev
This commit is contained in:
@@ -127,5 +127,30 @@ namespace Apimanager_backend.Services
|
||||
return mapper.Map<ApiInfoDto>(apiInfo);
|
||||
}
|
||||
#endregion
|
||||
#region 获取用于用户展示的api列表
|
||||
public async Task<List<ApiInfoDto>> GetUserApisAsync(int pageIndex, int pageSize, bool desc)
|
||||
{
|
||||
IQueryable<Api> query = context.Apis.Include(x => x.ApiRequestExamples).AsQueryable();
|
||||
//倒序
|
||||
if (desc)
|
||||
{
|
||||
query.OrderDescending();
|
||||
}
|
||||
//分页
|
||||
query = query.Skip((pageIndex - 1) * pageSize).Take(pageSize);
|
||||
return mapper.Map<List<ApiInfoDto>>(await query.ToListAsync());
|
||||
}
|
||||
#endregion
|
||||
#region 获取API对应的套餐
|
||||
public async Task<List<Apipackage>> GetApipackageAsync(int[] apiId)
|
||||
{
|
||||
if(apiId.Count() == 0)
|
||||
{
|
||||
throw new BaseException(1004,"id不能为空");
|
||||
}
|
||||
var packageItems = await context.apiPackageItems.Include(x => x.ApiPackage).Where(x => apiId.Contains(x.ApiId)).ToListAsync();
|
||||
return packageItems.Select(x => x.ApiPackage).ToList();
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,6 @@ namespace Apimanager_backend.Services
|
||||
{
|
||||
public interface IApiPackageItemService
|
||||
{
|
||||
Task<List<ApiPackageItem>> GetApiPackageItemsByApiIdAsync(int ApiId);
|
||||
public Task<List<ApiPackageItem>> GetApiPackageItemsByApiIdAsync(int ApiId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Apimanager_backend.Dtos;
|
||||
using Apimanager_backend.Models;
|
||||
|
||||
namespace Apimanager_backend.Services
|
||||
{
|
||||
@@ -61,6 +62,15 @@ namespace Apimanager_backend.Services
|
||||
/// <param name="endpoint">api路径</param>
|
||||
/// <returns></returns>
|
||||
public Task<ApiInfoDto> GetApiInfoByEndpointAsync(string endpoint);
|
||||
/// <summary>
|
||||
/// 获取用于用户展示的api列表
|
||||
/// </summary>
|
||||
/// <param name="pageIndex"></param>
|
||||
/// <param name="pageSize"></param>
|
||||
/// <param name="desc"></param>
|
||||
/// <returns></returns>
|
||||
|
||||
public Task<List<ApiInfoDto>> GetUserApisAsync(int pageIndex, int pageSize, bool desc);
|
||||
public Task<List<Apipackage>> GetApipackageAsync(int[] apiId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,5 +20,6 @@ namespace Apimanager_backend.Services
|
||||
/// <returns></returns>
|
||||
Task<Order> CreateOrderAsync(OrderDto order);
|
||||
Task<bool> UpdateOrderAsync(OrderDto order);
|
||||
Task<int> GetOrderNumAsync(int? userId = null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,6 +59,7 @@ namespace Apimanager_backend.Services
|
||||
/// <param name="time"></param>
|
||||
/// <returns></returns>
|
||||
public Task<UserPackage> DecuteUserPackageTimeAsync(int packageId,int userId,TimeSpan time);
|
||||
Task<bool> SetApiPackageItemAsync(int apiId,int packageId);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -5,6 +5,11 @@ namespace Apimanager_backend.Services
|
||||
public interface IPaymentConfigService
|
||||
{
|
||||
Task<PaymentConfig> GetPaymentConfigInfoByTypeAsync(string payType);
|
||||
Task<PaymentConfig> GetPaymentConfigInfoByIdAsync(int id);
|
||||
Task<PaymentConfig> GetPaymentAdminAsync(PayType payType, PaymentType paymentType);
|
||||
Task<List<PaymentConfigDto>> GetAllPaymentAsync();
|
||||
Task<List<PaymentConfig>> GetAllPaymentAdminAsync();
|
||||
Task<bool> AddPaymentAsync(PaymentConfig payment);
|
||||
Task<bool> UpdatePaymentAsync(PaymentConfig payment);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
using Apimanager_backend.Dtos;
|
||||
|
||||
namespace Apimanager_backend.Services
|
||||
{
|
||||
public interface ISystemInfoService
|
||||
{
|
||||
Task<SystemInfoDto> GetSystemInfoAsync();
|
||||
}
|
||||
}
|
||||
@@ -60,5 +60,11 @@ namespace Apimanager_backend.Services
|
||||
/// <param name="url"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> UpdateUserAvatarAsync(int userId,string url);
|
||||
/// <summary>
|
||||
/// 设置用户调用凭证
|
||||
/// </summary>
|
||||
/// <param name="userId"></param>
|
||||
/// <returns></returns>
|
||||
Task<string> SetUserTokenAsync(int userId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,6 +49,29 @@ namespace Apimanager_backend.Services
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
public async Task<int> GetOrderNumAsync(int? userId = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
int num = 0;
|
||||
if (userId != null)
|
||||
{
|
||||
num = await _apiContext.Orders.CountAsync(x => x.UserId == userId);
|
||||
}
|
||||
else
|
||||
{
|
||||
num = await _apiContext.Orders.CountAsync();
|
||||
}
|
||||
return num;
|
||||
}catch(Exception e)
|
||||
{
|
||||
logger.LogError(e, "查询订单数量:{Message}", e.Message);
|
||||
throw new BaseException(1005,"服务器内部错误");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#region 获取订单列表
|
||||
/// <summary>
|
||||
/// 获取订单列表
|
||||
@@ -60,10 +83,10 @@ namespace Apimanager_backend.Services
|
||||
/// <returns></returns>
|
||||
public async Task<List<Order>> GetOrdersAsync(int pageIndex, int pageSize, bool desc, int? userId)
|
||||
{
|
||||
IQueryable<Order> query = _apiContext.Orders.AsQueryable().OrderBy(x => x.Id);
|
||||
IQueryable<Order> query = _apiContext.Orders.Include(x => x.User).AsQueryable().OrderBy(x => x.Id);
|
||||
if(userId != null)
|
||||
{
|
||||
query.Where(x => x.UserId == userId);
|
||||
query = query.Where(x => x.UserId == userId);
|
||||
}
|
||||
//倒序
|
||||
if (desc)
|
||||
@@ -90,12 +113,12 @@ namespace Apimanager_backend.Services
|
||||
data.UpdatedAt = DateTime.Now;
|
||||
data.Status = order.Status;
|
||||
data.PaiAt = order.Status == OrderStatus.Completed ? DateTime.Now : null;
|
||||
var user = await _apiContext.Users.FirstOrDefaultAsync(x => x.Id == order.UserId);
|
||||
var user = await _apiContext.Users.FirstOrDefaultAsync(x => x.Id == data.UserId);
|
||||
if (user == null)
|
||||
{
|
||||
throw new BaseException(2004, "用户未找到");
|
||||
}
|
||||
user.Balance += order.Amount;
|
||||
user.Balance += data.Amount;
|
||||
_apiContext.Orders.Update(data);
|
||||
_apiContext.Users.Update(user);
|
||||
await _apiContext.SaveChangesAsync();
|
||||
|
||||
@@ -139,7 +139,7 @@ namespace Apimanager_backend.Services
|
||||
#region 获取套餐列表
|
||||
public async Task<List<PackageInfoDto>> GetAllPackagesAsync(int pageIndex, int pageSize, bool desc)
|
||||
{
|
||||
IQueryable<Apipackage> query = apiContext.Apipackages.Where(x => true).OrderBy(x => x.Id);
|
||||
IQueryable<Apipackage> query = apiContext.Apipackages.Include(x => x.ApiPackageItems).ThenInclude(x => x.Api).Where(x => true).OrderBy(x => x.Id);
|
||||
if(desc)
|
||||
{
|
||||
query = query.OrderDescending();
|
||||
@@ -160,9 +160,9 @@ namespace Apimanager_backend.Services
|
||||
public async Task<PackageInfoDto> PackageInfoByIdAsync(int packageId)
|
||||
{
|
||||
var packageInfo = await apiContext.Apipackages.FirstOrDefaultAsync(x => x.Id == packageId);
|
||||
if(packageInfo == null)
|
||||
if (packageInfo == null)
|
||||
{
|
||||
throw new BaseException(4004,"套餐未找到");
|
||||
throw new BaseException(4004, "套餐未找到");
|
||||
}
|
||||
return mapper.Map<PackageInfoDto>(packageInfo);
|
||||
}
|
||||
@@ -197,5 +197,23 @@ namespace Apimanager_backend.Services
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
#region 设置API关联套餐
|
||||
public async Task<bool> SetApiPackageItemAsync(int apiId, int packageId)
|
||||
{
|
||||
var api = await apiContext.Apis.SingleOrDefaultAsync(x => x.Id == apiId);
|
||||
if(api == null)
|
||||
{
|
||||
throw new BaseException(1004,"API不存在");
|
||||
}
|
||||
var package = await apiContext.Apipackages.SingleOrDefaultAsync(x => x.Id == packageId);
|
||||
if(package is null)
|
||||
{
|
||||
throw new BaseException(1004,"套餐不存在");
|
||||
}
|
||||
apiContext.apiPackageItems.Add(new ApiPackageItem() { ApiPackageId = package.Id,ApiId = api.Id});
|
||||
await apiContext.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,10 +47,23 @@ namespace Apimanager_backend.Services
|
||||
else
|
||||
{
|
||||
var baseDate = uPackage.ExpiryDate > DateTime.Now ? uPackage.ExpiryDate : DateTime.Now;
|
||||
uPackage.ExpiryDate = baseDate.AddMonths(1);
|
||||
uPackage.RemainingCalls += package.CallLimit;
|
||||
_context.UserPackages.Update(uPackage);
|
||||
}
|
||||
user.Balance -= package.Price;
|
||||
_context.Users.Update(user);
|
||||
Order order = new Order()
|
||||
{
|
||||
UserId = user.Id,
|
||||
OrderNumber = OrderNumberGenerator.Generate(user.Id),
|
||||
Amount = package.Price,
|
||||
OrderType = OrderType.Purchase,
|
||||
PaymentType = PaymentType.balance,
|
||||
Status = OrderStatus.Completed,
|
||||
PaiAt = DateTime.Now
|
||||
};
|
||||
_context.Orders.Add(order);
|
||||
await _context.SaveChangesAsync();
|
||||
await transaction.CommitAsync();
|
||||
return true;
|
||||
|
||||
@@ -17,12 +17,45 @@ namespace Apimanager_backend.Services
|
||||
_logger = logger;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<bool> AddPaymentAsync(PaymentConfig payment)
|
||||
{
|
||||
bool res = await _context.paymentConfigs.AnyAsync(x => x.PayType == payment.PayType && x.Method == payment.Method);
|
||||
if (res)
|
||||
{
|
||||
throw new BaseException(1006,"该支付方式已存在");
|
||||
}
|
||||
_context.paymentConfigs.Add(payment);
|
||||
await _context.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<List<PaymentConfig>> GetAllPaymentAdminAsync()
|
||||
{
|
||||
return await _context.paymentConfigs.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<List<PaymentConfigDto>> GetAllPaymentAsync()
|
||||
{
|
||||
var data = await _context.paymentConfigs.Where(x => x.IsEnabled).ToListAsync();
|
||||
return _mapper.Map<List<PaymentConfigDto>>(data);
|
||||
}
|
||||
|
||||
public Task<PaymentConfig> GetPaymentAdminAsync(PayType payType, PaymentType paymentType)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task<PaymentConfig> GetPaymentConfigInfoByIdAsync(int id)
|
||||
{
|
||||
var payment = await _context.paymentConfigs.SingleOrDefaultAsync(x => x.Id == id);
|
||||
if(payment is null)
|
||||
{
|
||||
throw new BaseException(1004,"未找到支付接口");
|
||||
}
|
||||
return payment;
|
||||
}
|
||||
|
||||
public async Task<PaymentConfig> GetPaymentConfigInfoByTypeAsync(string payType)
|
||||
{
|
||||
if(!Enum.TryParse(payType, true, out PaymentType parsedPayType))
|
||||
@@ -36,5 +69,31 @@ namespace Apimanager_backend.Services
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdatePaymentAsync(PaymentConfig payment)
|
||||
{
|
||||
var paymentConfig = await _context.paymentConfigs.FirstOrDefaultAsync(x => x.Id == payment.Id);
|
||||
if(paymentConfig == null)
|
||||
{
|
||||
_context.paymentConfigs.Add(payment);
|
||||
}
|
||||
else
|
||||
{
|
||||
paymentConfig.AppId = payment.AppId;
|
||||
paymentConfig.SecretKey = payment.SecretKey;
|
||||
paymentConfig.PublicKey = payment.PublicKey;
|
||||
paymentConfig.IsEnabled = payment.IsEnabled;
|
||||
paymentConfig.Url = payment.Url;
|
||||
paymentConfig.Description = payment.Description;
|
||||
paymentConfig.ExtraSettingsJson = payment.ExtraSettingsJson;
|
||||
paymentConfig.GatewayUrl = payment.GatewayUrl;
|
||||
paymentConfig.PayType = payment.PayType;
|
||||
paymentConfig.Method = payment.Method;
|
||||
paymentConfig.NotifyUrl = payment.NotifyUrl;
|
||||
_context.paymentConfigs.Update(paymentConfig);
|
||||
}
|
||||
await _context.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
using Apimanager_backend.Data;
|
||||
using Apimanager_backend.Dtos;
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Apimanager_backend.Services
|
||||
{
|
||||
public class SystemInfoService : ISystemInfoService
|
||||
{
|
||||
private readonly ApiContext _context;
|
||||
private readonly IMapper _mapper;
|
||||
public SystemInfoService(ApiContext apiContext,IMapper mapper)
|
||||
{
|
||||
_context = apiContext;
|
||||
_mapper = mapper;
|
||||
}
|
||||
public async Task<SystemInfoDto> GetSystemInfoAsync()
|
||||
{
|
||||
return new SystemInfoDto()
|
||||
{
|
||||
UserCount = await GetUserCount(),
|
||||
OrderCount = await GetOrderCount(),
|
||||
CallCount = await GetCallCount(),
|
||||
Money = await GetMoney(),
|
||||
RecentOrder = await GetRecentOrder(),
|
||||
RecentUser = await GetRecentUser()
|
||||
};
|
||||
}
|
||||
#region 获取用户数量
|
||||
private async Task<int> GetUserCount()
|
||||
{
|
||||
return await _context.Users.CountAsync();
|
||||
}
|
||||
#endregion
|
||||
#region 获取订单数量
|
||||
private async Task<int> GetOrderCount()
|
||||
{
|
||||
return await _context.Orders.CountAsync();
|
||||
}
|
||||
#endregion
|
||||
#region 获取API调用次数
|
||||
private async Task<int> GetCallCount()
|
||||
{
|
||||
return await _context.CallLogs.CountAsync();
|
||||
}
|
||||
#endregion
|
||||
#region 获取最近一周用户
|
||||
private async Task<List<UserInfoDto>> GetRecentUser()
|
||||
{
|
||||
DateTime dateTime = DateTime.Now.AddDays(-7);
|
||||
var users = _context.Users.Where(x => x.CreatedAt >= dateTime);
|
||||
return _mapper.Map<List<UserInfoDto>>(await users.ToListAsync());
|
||||
}
|
||||
#endregion
|
||||
#region 获取最近一周订单
|
||||
private async Task<List<OrderDto>> GetRecentOrder()
|
||||
{
|
||||
DateTime dateTime = DateTime.Now.AddDays(-7);
|
||||
var orders = _context.Orders.Include(x => x.User).Where(x => x.CreatedAt >= dateTime);
|
||||
return _mapper.Map<List<OrderDto>>(await orders.ToListAsync());
|
||||
}
|
||||
#endregion
|
||||
#region 获取金额数
|
||||
public async Task<decimal> GetMoney()
|
||||
{
|
||||
return await _context.Orders.Where(x => x.OrderType == Models.OrderType.Recharge).SumAsync(x => x.Amount);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
using StackExchange.Redis;
|
||||
using System.ComponentModel;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace Apimanager_backend.Services
|
||||
{
|
||||
@@ -125,5 +126,25 @@ namespace Apimanager_backend.Services
|
||||
await apiContext.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
#region 设置用户apikey
|
||||
public async Task<string> SetUserTokenAsync(int userId)
|
||||
{
|
||||
User? user = await apiContext.Users.FirstOrDefaultAsync(x => x.Id == userId);
|
||||
if (user is null)
|
||||
{
|
||||
throw new BaseException(2004,"用户未找到");
|
||||
}
|
||||
var tokenBytes = new byte[32];
|
||||
using (var rng = RandomNumberGenerator.Create())
|
||||
{
|
||||
rng.GetBytes(tokenBytes);
|
||||
}
|
||||
var tokenStr = Convert.ToBase64String(tokenBytes);
|
||||
user.ApiKey = tokenStr;
|
||||
apiContext.Users.Update(user);
|
||||
await apiContext.SaveChangesAsync();
|
||||
return tokenStr;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user