update:去除生成二维码图片验证添加二维码跳转时间戳和签名参数
This commit is contained in:
@@ -11,6 +11,11 @@ using MediatR;
|
||||
using QRCodeService.Application.Queries;
|
||||
using QRCodeService.Application.Commands;
|
||||
using QRCodeService.Infrastructure.Middlewares;
|
||||
using QRCodeService.Options;
|
||||
using QRCoder;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace QRCodeService.Controllers.Api
|
||||
{
|
||||
@@ -20,22 +25,31 @@ namespace QRCodeService.Controllers.Api
|
||||
{
|
||||
readonly IMediator mediator;
|
||||
readonly ILinkQueries linkQueries;
|
||||
readonly IOptions<ServiceOption> option;
|
||||
|
||||
public LinkController(IMediator mediator, ILinkQueries queries)
|
||||
public LinkController(IMediator mediator, ILinkQueries queries, IOptions<ServiceOption> option)
|
||||
{
|
||||
this.mediator = mediator;
|
||||
this.linkQueries = queries;
|
||||
this.option = option;
|
||||
}
|
||||
[Route("{shortCode}")]
|
||||
[Route("i/{shortCode}/")]
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Get(string shortCode)
|
||||
public async Task<IActionResult> GetQrCode(string shortCode,int img=0)
|
||||
{
|
||||
var link = await linkQueries.GetLinkAsync(shortCode);
|
||||
if (link == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
return Ok(link);
|
||||
var qrCodeGenerator = new QRCodeGenerator();
|
||||
var data = qrCodeGenerator.CreateQrCode($"{option.Value.BaseUrl}r/{link.ShortCode}", QRCodeGenerator.ECCLevel.Q);
|
||||
var qrCode = new QRCode(data);
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
qrCode.GetGraphic(10, Color.Black, Color.White, null, 15, 6, false).Save(stream, System.Drawing.Imaging.ImageFormat.Png);
|
||||
return File(stream.ToArray(), "image/png");
|
||||
}
|
||||
}
|
||||
|
||||
[CheckSign(typeof(CreateLinkModel))]
|
||||
|
||||
@@ -1,49 +0,0 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using QRCoder;
|
||||
using System.IO;
|
||||
using QRCodeService.Application.Queries;
|
||||
using QRCodeService.Models;
|
||||
using QRCodeService.Options;
|
||||
using QRCodeService.Infrastructure.Middlewares;
|
||||
|
||||
namespace QRCodeService.Controllers
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class ImageController : ControllerBase
|
||||
{
|
||||
readonly ILinkQueries linkQueries;
|
||||
readonly IAppQueries appQueries;
|
||||
readonly ServiceOption option;
|
||||
public ImageController(ILinkQueries linkQueries, IAppQueries appQueries,ServiceOption option)
|
||||
{
|
||||
this.linkQueries = linkQueries;
|
||||
this.appQueries = appQueries;
|
||||
this.option = option;
|
||||
}
|
||||
[CheckSign(typeof(GetQRCodeModel))]
|
||||
[Route("qrcode")]
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> GetImage(GetQRCodeModel input)
|
||||
{
|
||||
var link = await linkQueries.GetLinkAsync(input.ShortCode);
|
||||
if (link.AppId != input.AppId)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
var qrCodeGenerator = new QRCodeGenerator();
|
||||
var data = qrCodeGenerator.CreateQrCode($"{option.BaseUrl}r/{link.ShortCode}", QRCodeGenerator.ECCLevel.Q);
|
||||
var qrCode = new QRCode(data);
|
||||
using (var stream = new MemoryStream())
|
||||
{
|
||||
qrCode.GetGraphic(20).Save(stream, System.Drawing.Imaging.ImageFormat.Png);
|
||||
return File(stream.ToArray(), "image/png");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Http.Extensions;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.WebUtilities;
|
||||
using QRCodeService.Application.Queries;
|
||||
using QRCodeService.Extensions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -9,18 +12,35 @@ using System.Threading.Tasks;
|
||||
namespace QRCodeService.Controllers
|
||||
{
|
||||
[Route("r/{shortCode}")]
|
||||
public class RedirectController:Controller
|
||||
public class RedirectController : Controller
|
||||
{
|
||||
private readonly ILinkQueries queries;
|
||||
private readonly ILinkQueries linkQueries;
|
||||
private readonly IAppQueries appQueries;
|
||||
|
||||
public RedirectController(ILinkQueries queries)
|
||||
public RedirectController(ILinkQueries queries, IAppQueries appQueries)
|
||||
{
|
||||
this.queries = queries;
|
||||
this.linkQueries = queries;
|
||||
this.appQueries = appQueries;
|
||||
}
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Index(string shortCode)
|
||||
{
|
||||
var link = await queries.GetLinkAsync(shortCode);
|
||||
var link = await linkQueries.GetLinkAsync(shortCode);
|
||||
if (link == null)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
var app = await appQueries.GetAppAsync(link.AppId);
|
||||
if (app == null)
|
||||
{
|
||||
return BadRequest();
|
||||
}
|
||||
var t = DateTime.Now.ToString("yyyyMMddHHmmss");
|
||||
var sign = BitConverter.ToString($"{t}{app.Appkey}".ToMD5()).Replace("-", "");
|
||||
link.FullUrl = QueryHelpers.AddQueryString(link.FullUrl, new Dictionary<string, string> {
|
||||
{ "s",sign },
|
||||
{ "t",t}
|
||||
});
|
||||
return View(link);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user