Files
jinko.yikatong/Src/Dlhis.Basic/Organization/Services/Implements/GainfoQrCodeService.cs

67 lines
2.2 KiB
C#

using System.Text;
using Dlhis.Entity.Basic.Entity;
using Kurisu.AspNetCore.Authentication.Abstractions;
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
using Kurisu.AspNetCore.Utils.Extensions;
namespace Dlhis.Basic.Organization.Services.Implements;
public class GainfoQrCodeService : IGainfoQrCodeService
{
private readonly IDbContext _dbContext;
private readonly ICurrentUser _currentUser;
public GainfoQrCodeService(IDbContext dbContext, ICurrentUser currentUser)
{
_dbContext = dbContext;
_currentUser = currentUser;
}
public async Task<string> GetMyQrCodeAsync(bool isNeedRefresh = false)
{
var jinkoCode = _currentUser.GetUserClaim("jinko-code");
jinkoCode.IsEmpty().ThrowIfTrue("无法获取二维码");
var now = DateTime.Now;
var gainfoQrCode = await _dbContext.Queryable<GainfoQrCodeEntity>().SingleAsync(x => x.UserCode == jinkoCode);
if (gainfoQrCode == null)
{
gainfoQrCode = new GainfoQrCodeEntity
{
UserCode = jinkoCode,
QrCode = GenerateQrCode(jinkoCode)
};
await _dbContext.InsertAsync(gainfoQrCode);
}
if (gainfoQrCode.ModifiedTime.AddMinutes(1) >= now && !isNeedRefresh)
{
return gainfoQrCode.QrCode;
}
gainfoQrCode.QrCode = GenerateQrCode(jinkoCode);
await _dbContext.UpdateAsync(gainfoQrCode);
return gainfoQrCode.QrCode;
}
//****************************************************************** help methods **********************************************************************************
private static string GenerateQrCode(string code)
{
var ts = DateTimeOffset.Now.ToUnixTimeMilliseconds();
//var gainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == code).SingleAsync();
//var qrCode = string.Concat("consumer", ":", code, ":", Guid.NewGuid()).Replace("-", "");
var qrCode = string.Concat("consumer", ":", code, ":");
return qrCode.ToArray().Select(x =>
{
var b = Encoding.ASCII.GetBytes(x.ToString());
int i = b[0];
return Convert.ToString(i);
}).Join(string.Empty) + ts;
}
}