update:去除生成二维码图片验证添加二维码跳转时间戳和签名参数
This commit is contained in:
@@ -11,6 +11,11 @@ using MediatR;
|
|||||||
using QRCodeService.Application.Queries;
|
using QRCodeService.Application.Queries;
|
||||||
using QRCodeService.Application.Commands;
|
using QRCodeService.Application.Commands;
|
||||||
using QRCodeService.Infrastructure.Middlewares;
|
using QRCodeService.Infrastructure.Middlewares;
|
||||||
|
using QRCodeService.Options;
|
||||||
|
using QRCoder;
|
||||||
|
using System.Drawing;
|
||||||
|
using System.IO;
|
||||||
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
namespace QRCodeService.Controllers.Api
|
namespace QRCodeService.Controllers.Api
|
||||||
{
|
{
|
||||||
@@ -20,22 +25,31 @@ namespace QRCodeService.Controllers.Api
|
|||||||
{
|
{
|
||||||
readonly IMediator mediator;
|
readonly IMediator mediator;
|
||||||
readonly ILinkQueries linkQueries;
|
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.mediator = mediator;
|
||||||
this.linkQueries = queries;
|
this.linkQueries = queries;
|
||||||
|
this.option = option;
|
||||||
}
|
}
|
||||||
[Route("{shortCode}")]
|
[Route("i/{shortCode}/")]
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<IActionResult> Get(string shortCode)
|
public async Task<IActionResult> GetQrCode(string shortCode,int img=0)
|
||||||
{
|
{
|
||||||
var link = await linkQueries.GetLinkAsync(shortCode);
|
var link = await linkQueries.GetLinkAsync(shortCode);
|
||||||
if (link == null)
|
if (link == null)
|
||||||
{
|
{
|
||||||
return NotFound();
|
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))]
|
[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.Application.Queries;
|
||||||
|
using QRCodeService.Extensions;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@@ -11,16 +14,33 @@ namespace QRCodeService.Controllers
|
|||||||
[Route("r/{shortCode}")]
|
[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]
|
[HttpGet]
|
||||||
public async Task<IActionResult> Index(string shortCode)
|
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);
|
return View(link);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,6 +49,10 @@
|
|||||||
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
|
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Folder Include="Services\" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ProjectExtensions><VisualStudio><UserProperties properties_4launchsettings_1json__JsonSchema="" /></VisualStudio></ProjectExtensions>
|
<ProjectExtensions><VisualStudio><UserProperties properties_4launchsettings_1json__JsonSchema="" /></VisualStudio></ProjectExtensions>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ namespace QRCodeService
|
|||||||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "QRCodeService", Version = "v1" });
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "QRCodeService", Version = "v1" });
|
||||||
});
|
});
|
||||||
//配置
|
//配置
|
||||||
services.Configure<ServiceOption>(Configuration.GetSection("hosting"));
|
services.Configure<ServiceOption>(Configuration.GetSection("Hosting"));
|
||||||
|
|
||||||
//validator
|
//validator
|
||||||
services.AddTransient<IValidator<CreateLinkCommand>, CreateLinkCommandValidator>();
|
services.AddTransient<IValidator<CreateLinkCommand>, CreateLinkCommandValidator>();
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
loading...
|
loading...
|
||||||
</div>
|
</div>
|
||||||
<script>
|
<script>
|
||||||
location.href = "@Model.FullUrl"
|
location.href = "@Html.Raw(Model.FullUrl)"
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Reference in New Issue
Block a user