refactor(common&service): adjust id range and remove unused qr code method
1. 调整RandomIdHelper的默认ID生成范围从16位改为13位 2. 从IUserJournalService和UserJournalService中移除废弃的单例生成二维码方法 3. 为相关DTO添加JSON数字序列化/反序列化配置,处理前后端数字类型兼容问题 4. 修改二维码内容序列化逻辑,将数字转为字符串避免精度丢失
This commit is contained in:
@ -7,8 +7,8 @@ namespace QYZH.InteractiveMagazine.Common.Helpers;
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public static class RandomIdHelper
|
public static class RandomIdHelper
|
||||||
{
|
{
|
||||||
private const long DefaultMinValue = 1_000_000_000_000_000_000L;
|
private const long DefaultMinValue = 1_000_000_000_000L;
|
||||||
private const long DefaultMaxValue = 9_000_000_000_000_000_000L;
|
private const long DefaultMaxValue = 9_000_000_000_000_000L;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 生成不可预测的正数长整型ID
|
/// 生成不可预测的正数长整型ID
|
||||||
|
|||||||
@ -31,14 +31,6 @@ public interface IUserJournalService : IBaseService<UserJournal>
|
|||||||
/// <param name="id">绑定记录Id</param>
|
/// <param name="id">绑定记录Id</param>
|
||||||
Task UnbindJournalAsync(long userId, long id);
|
Task UnbindJournalAsync(long userId, long id);
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 生成期刊二维码记录
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="input">生成输入</param>
|
|
||||||
/// <param name="operatorName">操作人名称</param>
|
|
||||||
/// <returns>二维码记录</returns>
|
|
||||||
Task<UserJournalQrCodeOutput> CreateQrCodeAsync(CreateUserJournalQrCodeInput input, string operatorName);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 批量提交期刊二维码生成任务
|
/// 批量提交期刊二维码生成任务
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
using QYZH.InteractiveMagazine.Models.Enum;
|
using QYZH.InteractiveMagazine.Models.Enum;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace QYZH.InteractiveMagazine.Models.Dto;
|
namespace QYZH.InteractiveMagazine.Models.Dto;
|
||||||
|
|
||||||
@ -10,11 +11,13 @@ public class BindJournalInput
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 期刊模板Id(扫码解析的期刊定义Id)
|
/// 期刊模板Id(扫码解析的期刊定义Id)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
|
||||||
public long JournalId { get; set; }
|
public long JournalId { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 实例化期刊Id(扫码解析的具体期刊实例Id,可选)
|
/// 实例化期刊Id(扫码解析的具体期刊实例Id,可选)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
|
||||||
public long Id { get; set; }
|
public long Id { get; set; }
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -146,6 +149,7 @@ public class CreateUserJournalQrCodeOutput
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 二维码记录Id列表
|
/// 二维码记录Id列表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[JsonNumberHandling(JsonNumberHandling.WriteAsString)]
|
||||||
public List<long> RecordIds { get; set; } = [];
|
public List<long> RecordIds { get; set; } = [];
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -209,6 +213,7 @@ public class UserJournalQrCodeOutput
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 二维码记录Id
|
/// 二维码记录Id
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[JsonNumberHandling(JsonNumberHandling.WriteAsString)]
|
||||||
public long Id { get; set; }
|
public long Id { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -270,5 +275,6 @@ public class DeleteUserJournalQrCodeInput
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 二维码记录Id列表
|
/// 二维码记录Id列表
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
|
||||||
public List<long> Ids { get; set; } = [];
|
public List<long> Ids { get; set; } = [];
|
||||||
}
|
}
|
||||||
|
|||||||
@ -166,62 +166,6 @@ public class UserJournalService(
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 生成期刊二维码记录
|
/// 生成期刊二维码记录
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task<UserJournalQrCodeOutput> CreateQrCodeAsync(CreateUserJournalQrCodeInput input, string operatorName)
|
|
||||||
{
|
|
||||||
if (input.JournalId <= 0)
|
|
||||||
{
|
|
||||||
throw new BusinessException("期刊Id不能为空", ResultCode.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
|
|
||||||
var journal = await journalRepository.GetByIdAsync(input.JournalId);
|
|
||||||
if (journal == null || journal.IsDeleted)
|
|
||||||
{
|
|
||||||
throw new BusinessException("期刊不存在", ResultCode.NOT_FOUND);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (journal.Status != (int)JournalStatusEnum.Published)
|
|
||||||
{
|
|
||||||
throw new BusinessException("该期刊暂未发布,无法生成二维码", ResultCode.UNPROCESSABLE_ENTITY);
|
|
||||||
}
|
|
||||||
|
|
||||||
var recordId = await GenerateUniqueQrCodeIdAsync();
|
|
||||||
var record = new UserJournal
|
|
||||||
{
|
|
||||||
Id = recordId,
|
|
||||||
UserId = null,
|
|
||||||
JournalId = input.JournalId,
|
|
||||||
Type = 0,
|
|
||||||
Status = (int)UserJournalStatusEnum.Active,
|
|
||||||
IsDeleted = false,
|
|
||||||
CreatedBy = operatorName,
|
|
||||||
CreatedAt = DateTime.Now,
|
|
||||||
UpdatedBy = operatorName,
|
|
||||||
UpdatedAt = DateTime.Now
|
|
||||||
};
|
|
||||||
|
|
||||||
var qrCodeContent = BuildQrCodeContent(record.JournalId, record.Id);
|
|
||||||
var qrCodeKey = $"journal/qrcode/{record.JournalId}/{record.Id}.png";
|
|
||||||
using var qrCodeStream = new MemoryStream(QrCodeHelper.GeneratePng(qrCodeContent));
|
|
||||||
var uploadedKey = ossService.PutObject(qrCodeKey, qrCodeStream);
|
|
||||||
if (string.IsNullOrWhiteSpace(uploadedKey))
|
|
||||||
{
|
|
||||||
throw new BusinessException("二维码图片上传失败,请稍后重试", ResultCode.GLOBAL_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
record.QrCodeUrl = uploadedKey;
|
|
||||||
|
|
||||||
var result = await userJournalRepository.InsertAsync(record);
|
|
||||||
if (!result)
|
|
||||||
{
|
|
||||||
throw new BusinessException("生成二维码失败,请稍后重试", ResultCode.GLOBAL_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
return MapQrCodeOutput(record, journal, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 分页查询期刊二维码记录
|
|
||||||
/// </summary>
|
|
||||||
public async Task<CreateUserJournalQrCodeOutput> CreateQrCodesAsync(CreateUserJournalQrCodeInput input, string operatorName)
|
public async Task<CreateUserJournalQrCodeOutput> CreateQrCodesAsync(CreateUserJournalQrCodeInput input, string operatorName)
|
||||||
{
|
{
|
||||||
if (input.JournalId <= 0)
|
if (input.JournalId <= 0)
|
||||||
@ -461,7 +405,7 @@ public class UserJournalService(
|
|||||||
|
|
||||||
private static string BuildQrCodeContent(long journalId, long id)
|
private static string BuildQrCodeContent(long journalId, long id)
|
||||||
{
|
{
|
||||||
return JsonSerializer.Serialize(new { JournalId = journalId, Id = id });
|
return JsonSerializer.Serialize(new { JournalId = journalId.ToString(), Id = id.ToString() });
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<long> GenerateUniqueQrCodeIdAsync()
|
private async Task<long> GenerateUniqueQrCodeIdAsync()
|
||||||
|
|||||||
Reference in New Issue
Block a user