-
Notifications
You must be signed in to change notification settings - Fork 3
/
StlQRCode.cs
72 lines (62 loc) · 2.08 KB
/
StlQRCode.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System.Threading.Tasks;
using SSCMS.Parse;
using SSCMS.Plugins;
using SSCMS.Repositories;
using SSCMS.Services;
using SSCMS.Utils;
namespace SSCMS.QRCode
{
public class StlQRCode : IPluginParseAsync
{
private const string AttributeUrl = "url";
private const string AttributeSize = "size";
private readonly IPathManager _pathManager;
private readonly ISiteRepository _siteRepository;
public StlQRCode(IPathManager pathManager, ISiteRepository siteRepository)
{
_pathManager = pathManager;
_siteRepository = siteRepository;
}
public string ElementName => "stl:qrcode";
public async Task<string> ParseAsync(IParseStlContext context)
{
var url = string.Empty;
var size = 0;
foreach (var name in context.StlAttributes.AllKeys)
{
var value = context.StlAttributes[name];
if (StringUtils.EqualsIgnoreCase(name, AttributeUrl))
{
url = await context.ParseAsync(value);
}
else if (StringUtils.EqualsIgnoreCase(name, AttributeSize))
{
size = TranslateUtils.ToInt(value);
}
}
url = string.IsNullOrEmpty(url) ? "location.href" : $"'{url}'";
var guid = StringUtils.Guid();
var site = await _siteRepository.GetAsync(context.SiteId);
var libUrl = _pathManager.GetApiHostUrl(site, "/assets/qrcode/qrcode.min.js");
return size == 0
? $@"
<div class=""qrcode"" id=""{guid}""></div>
<script type=""text/javascript"" src=""{libUrl}""></script>
<script type=""text/javascript"">
new QRCode(document.getElementById('{guid}'), {url});
</script>
"
: $@"
<div class=""qrcode"" id=""{guid}""></div>
<script type=""text/javascript"" src=""{libUrl}""></script>
<script type=""text/javascript"">
new QRCode(document.getElementById('{guid}'), {{
text: {url},
width: {size},
height: {size}
}});
</script>
";
}
}
}