Files
qrcodeService/QRCodeService/Controllers/GenController.cs
2021-02-23 09:51:50 +08:00

59 lines
1.8 KiB
C#

using Domain.AggregateModel.LinkAggregate;
using Microsoft.AspNetCore.Mvc;
using QRCodeService.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using Domain.AggregateModel.AppAggregate;
using Base62;
using QRCodeService.Extensions;
namespace QRCodeService.Controllers
{
/// <summary>
/// 生成二维码图片
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class GenController:ControllerBase
{
readonly ILinkRepository linkRepository;
readonly IAppRepository appRepository;
public GenController(ILinkRepository linkRepository, IAppRepository appRepository)
{
this.linkRepository = linkRepository;
this.appRepository = appRepository;
}
[HttpPost]
public async Task<IActionResult> CreateLink(GenInputModel model)
{
var app = await appRepository.GetAsync(model.AppId);
if (app == null)
{
return BadRequest();
}
using(var md5 = MD5.Create())
{
var result = md5.ComputeHash(Encoding.UTF8.GetBytes($"{model.AppId}{model.SuffixUrl}{model.Time}{app.AppKey}"));
var sign = BitConverter.ToString(result);
if (sign != model.Sign)
{
return BadRequest();
}
}
var shortCode = $"{model.SuffixUrl}".ToMD5().ToBase62();
var link = new Link(app.BaseUrl, model.SuffixUrl,1);
linkRepository.Add(link);
await linkRepository.UnitOfWork.SaveEntitiesAsync();
return Ok(link.ShortCode);
}
}
}