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; }
+ }
+}