二维码对接

This commit is contained in:
2021-02-25 15:40:58 +08:00
parent f9c9d13c68
commit dfbc8f737d
4 changed files with 46 additions and 6 deletions

View File

@@ -25,7 +25,7 @@ namespace QRCodeService.Application.Queries
{ {
connection.Open(); connection.Open();
var link = await connection.QueryAsync<Link>( var link = await connection.QueryAsync<Link>(
@"SELECT ShortCode,FullUrl FROM Link WHERE ShortCode = @shortCode",new {shortCode }); @"SELECT ShortCode,FullUrl,AppId FROM Link WHERE ShortCode = @shortCode",new {shortCode });
return link.SingleOrDefault(); return link.SingleOrDefault();
} }
} }

View File

@@ -11,5 +11,7 @@ namespace QRCodeService.Application.Queries
public string ShortCode { get; set; } public string ShortCode { get; set; }
public string FullUrl { get; set; } public string FullUrl { get; set; }
public int AppId { get; set; }
} }
} }

View File

@@ -6,6 +6,8 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using QRCoder; using QRCoder;
using System.IO; using System.IO;
using QRCodeService.Application.Queries;
using QRCodeService.Models;
namespace QRCodeService.Controllers namespace QRCodeService.Controllers
{ {
@@ -13,17 +15,40 @@ namespace QRCodeService.Controllers
[ApiController] [ApiController]
public class ImageController : ControllerBase public class ImageController : ControllerBase
{ {
[Route("{shortCode}")] readonly ILinkQueries linkQueries;
[HttpGet] readonly IAppQueries appQueries;
public IActionResult Get(string shortCode)
public ImageController(ILinkQueries linkQueries, IAppQueries appQueries)
{ {
this.linkQueries = linkQueries;
this.appQueries = appQueries;
}
[Route("qrcode")]
[HttpPost]
public async Task<IActionResult> GetImage(GetQRCodeModel input)
{
var app = await appQueries.GetAppAsync(input.AppId);
if (app == null)
{
return BadRequest();
}
if (!await input.CheckValidAsync(app.Appkey))
{
return BadRequest();
}
var link = await linkQueries.GetLinkAsync(input.ShortCode);
if (link.AppId != input.AppId)
{
return BadRequest();
}
var qrCodeGenerator = new QRCodeGenerator(); var qrCodeGenerator = new QRCodeGenerator();
var data = qrCodeGenerator.CreateQrCode("www.baidu.com", QRCodeGenerator.ECCLevel.Q); var data = qrCodeGenerator.CreateQrCode($"http://localhost:5000/{link.ShortCode}", QRCodeGenerator.ECCLevel.Q);
var qrCode = new QRCode(data); var qrCode = new QRCode(data);
using (var stream = new MemoryStream()) using (var stream = new MemoryStream())
{ {
qrCode.GetGraphic(20).Save(stream, System.Drawing.Imaging.ImageFormat.Png); qrCode.GetGraphic(20).Save(stream, System.Drawing.Imaging.ImageFormat.Png);
return File(stream.ToArray(), "image/png", "qrcode.png"); return File(stream.ToArray(), "image/png");
} }
} }
} }

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace QRCodeService.Models
{
public class GetQRCodeModel:CheckSignModel
{
public string ShortCode { get; set; }
}
}