feat 员工消费二维码

This commit is contained in:
2024-06-05 17:51:03 +08:00
parent b93cff743c
commit 662bd2e245
4 changed files with 127 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
using Dlhis.Basic.Organization.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace Dlhis.Basic.Organization.Controllers;
/// <summary>
/// 档案二维码
/// </summary>
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class GainfoQrCodeController : ControllerBase
{
private readonly IGainfoQrCodeService _gainfoQrCodeService;
public GainfoQrCodeController(IGainfoQrCodeService gainfoQrCodeService)
{
_gainfoQrCodeService = gainfoQrCodeService;
}
/// <summary>
/// 获取我的消费二维码
/// </summary>
/// <param name="isNeedRefresh">是否主动刷新</param>
/// <returns></returns>
[HttpGet("consumer")]
public async Task<string> GetMyQrCode(bool isNeedRefresh = false)
{
return await _gainfoQrCodeService.GetMyQrCodeAsync(isNeedRefresh);
}
}

View File

@@ -0,0 +1,6 @@
namespace Dlhis.Basic.Organization.Services;
public interface IGainfoQrCodeService : IScopeDependency
{
Task<string> GetMyQrCodeAsync(bool isNeedRefresh = false);
}

View File

@@ -0,0 +1,66 @@
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 = "09992534";
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 gainfo = await _dbContext.Queryable<GainfoEntity>().Where(x => x.Code == code).SingleAsync();
var qrCode = string.Concat("consumer", ":", code, ":", Guid.NewGuid()).Replace("-", "");
return qrCode.ToArray().Select(x =>
{
var b = Encoding.ASCII.GetBytes(x.ToString());
int i = b[0];
return Convert.ToString(i);
}).Join(string.Empty);
}
}

View File

@@ -0,0 +1,23 @@
using Kurisu.AspNetCore.DataAccess.SqlSugar;
using SqlSugar;
namespace Dlhis.Entity.Basic.Entity;
/// <summary>
/// 档案二维码
/// </summary>
[SugarTable(TableName = "GainfoQrCode", TableDescription = "档案二维码")]
public class GainfoQrCodeEntity : SugarBaseEntity
{
/// <summary>
/// 工号
/// </summary>
[SugarColumn(IsNullable = false, ColumnDescription = "工号", UniqueGroupNameList = new[] { "unique_GainfoQrCode_UserCode" })]
public string UserCode { get; set; }
/// <summary>
/// 二维码
/// </summary>
[SugarColumn(IsNullable = false, ColumnDescription = "二维码", UniqueGroupNameList = new[] { "unique_GainfoQrCode_QrCode" })]
public string QrCode { get; set; }
}