From dfbc8f737d7a154452f29ef981b4979cac5eeb23 Mon Sep 17 00:00:00 2001
From: zhangqi <2794379662@qq.com>
Date: Thu, 25 Feb 2021 15:40:58 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BA=8C=E7=BB=B4=E7=A0=81=E5=AF=B9=E6=8E=A5?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Application/Queries/LinkQueries.cs | 2 +-
.../Application/Queries/LinkViewModel.cs | 2 ++
QRCodeService/Controllers/ImageController.cs | 35 ++++++++++++++++---
QRCodeService/Models/GetQRCodeModel.cs | 13 +++++++
4 files changed, 46 insertions(+), 6 deletions(-)
create mode 100644 QRCodeService/Models/GetQRCodeModel.cs
diff --git a/QRCodeService/Application/Queries/LinkQueries.cs b/QRCodeService/Application/Queries/LinkQueries.cs
index cbec2be..ffd2f77 100644
--- a/QRCodeService/Application/Queries/LinkQueries.cs
+++ b/QRCodeService/Application/Queries/LinkQueries.cs
@@ -25,7 +25,7 @@ namespace QRCodeService.Application.Queries
{
connection.Open();
var link = await connection.QueryAsync(
- @"SELECT ShortCode,FullUrl FROM Link WHERE ShortCode = @shortCode",new {shortCode });
+ @"SELECT ShortCode,FullUrl,AppId FROM Link WHERE ShortCode = @shortCode",new {shortCode });
return link.SingleOrDefault();
}
}
diff --git a/QRCodeService/Application/Queries/LinkViewModel.cs b/QRCodeService/Application/Queries/LinkViewModel.cs
index 07f566d..b587803 100644
--- a/QRCodeService/Application/Queries/LinkViewModel.cs
+++ b/QRCodeService/Application/Queries/LinkViewModel.cs
@@ -11,5 +11,7 @@ namespace QRCodeService.Application.Queries
public string ShortCode { get; set; }
public string FullUrl { get; set; }
+
+ public int AppId { get; set; }
}
}
diff --git a/QRCodeService/Controllers/ImageController.cs b/QRCodeService/Controllers/ImageController.cs
index 51b3dd8..1bf3b78 100644
--- a/QRCodeService/Controllers/ImageController.cs
+++ b/QRCodeService/Controllers/ImageController.cs
@@ -6,6 +6,8 @@ using System.Linq;
using System.Threading.Tasks;
using QRCoder;
using System.IO;
+using QRCodeService.Application.Queries;
+using QRCodeService.Models;
namespace QRCodeService.Controllers
{
@@ -13,17 +15,40 @@ namespace QRCodeService.Controllers
[ApiController]
public class ImageController : ControllerBase
{
- [Route("{shortCode}")]
- [HttpGet]
- public IActionResult Get(string shortCode)
+ readonly ILinkQueries linkQueries;
+ readonly IAppQueries appQueries;
+
+ public ImageController(ILinkQueries linkQueries, IAppQueries appQueries)
{
+ this.linkQueries = linkQueries;
+ this.appQueries = appQueries;
+ }
+
+ [Route("qrcode")]
+ [HttpPost]
+ public async Task 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 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);
using (var stream = new MemoryStream())
{
qrCode.GetGraphic(20).Save(stream, System.Drawing.Imaging.ImageFormat.Png);
- return File(stream.ToArray(), "image/png", "qrcode.png");
+ return File(stream.ToArray(), "image/png");
}
}
}
diff --git a/QRCodeService/Models/GetQRCodeModel.cs b/QRCodeService/Models/GetQRCodeModel.cs
new file mode 100644
index 0000000..8a8dd93
--- /dev/null
+++ b/QRCodeService/Models/GetQRCodeModel.cs
@@ -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; }
+ }
+}