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:
@ -166,62 +166,6 @@ public class UserJournalService(
|
||||
/// <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)
|
||||
{
|
||||
if (input.JournalId <= 0)
|
||||
@ -461,7 +405,7 @@ public class UserJournalService(
|
||||
|
||||
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()
|
||||
|
||||
Reference in New Issue
Block a user