-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathStlLogin.cs
110 lines (96 loc) · 4.32 KB
/
StlLogin.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using System.Collections.Specialized;
using System.Threading.Tasks;
using System.Web;
using SSCMS.Configuration;
using SSCMS.Parse;
using SSCMS.Plugins;
using SSCMS.Repositories;
using SSCMS.Services;
using SSCMS.Utils;
namespace SSCMS.Login.Core
{
public class StlLogin : IPluginParseAsync
{
public string ElementName => "stl:login";
public const string AttributeType = "type";
public const string AttributeUrl = "url";
public const string AttributeRedirectUrl = "redirectUrl";
private readonly IPathManager _pathManager;
private readonly ISiteRepository _siteRepository;
public StlLogin(IPathManager pathManager, ISiteRepository siteRepository)
{
_pathManager = pathManager;
_siteRepository = siteRepository;
}
public async Task<string> ParseAsync(IParseStlContext context)
{
var type = string.Empty;
var url = string.Empty;
var redirectUrl = await context.GetCurrentUrlAsync();
var attributes = new NameValueCollection();
foreach (var name in context.StlAttributes.AllKeys)
{
var value = context.StlAttributes[name];
if (StringUtils.EqualsIgnoreCase(name, AttributeType))
{
type = await context.ParseAsync(value);
}
else if (StringUtils.EqualsIgnoreCase(name, AttributeUrl))
{
url = await context.ParseAsync(value);
}
else if (StringUtils.EqualsIgnoreCase(name, AttributeRedirectUrl))
{
redirectUrl = await context.ParseAsync(value);
}
else
{
attributes.Add(name, await context.ParseAsync(value));
}
}
var site = await _siteRepository.GetAsync(context.SiteId);
var apiUrl = _pathManager.GetApiHostUrl(site, Constants.ApiPrefix);
if (!string.IsNullOrEmpty(url))
{
var parsedUrl = string.Empty;
if (StringUtils.EqualsIgnoreCase(url, OAuthType.Weibo.Value))
{
parsedUrl = $"{ApiUtils.GetAuthUrl(OAuthType.Weibo)}?redirectUrl={HttpUtility.UrlEncode(redirectUrl)}";
}
else if (StringUtils.EqualsIgnoreCase(url, OAuthType.Weixin.Value))
{
parsedUrl = $"{ApiUtils.GetAuthUrl(OAuthType.Weixin)}?redirectUrl={HttpUtility.UrlEncode(redirectUrl)}";
}
else if (StringUtils.EqualsIgnoreCase(url, OAuthType.Qq.Value))
{
parsedUrl = $"{ApiUtils.GetAuthUrl(OAuthType.Qq)}?redirectUrl={HttpUtility.UrlEncode(redirectUrl)}";
}
else if (StringUtils.EqualsIgnoreCase(url, "logout"))
{
parsedUrl = _pathManager.GetApiHostUrl(site, $"assets/login/templates/logout/index.html?apiUrl={HttpUtility.UrlEncode(apiUrl)}&redirectUrl={HttpUtility.UrlEncode(redirectUrl)}");
}
if (!string.IsNullOrEmpty(parsedUrl))
{
if (context.IsStlEntity)
{
return parsedUrl;
}
attributes["href"] = parsedUrl;
return $@"<a {TranslateUtils.ToAttributesString(attributes)}>{context.StlInnerHtml}</a>";
}
}
if (string.IsNullOrEmpty(type))
{
type = "login-account";
}
var elementId = $"iframe_{StringUtils.GetShortGuid(false)}";
var libUrl = _pathManager.GetApiHostUrl(site, "assets/login/lib/iframe-resizer-3.6.3/iframeResizer.min.js");
var pageUrl = await _pathManager.GetSiteUrlAsync(site, $"assets/login/templates/{type}/index.html?apiUrl={HttpUtility.UrlEncode(apiUrl)}&redirectUrl={HttpUtility.UrlEncode(redirectUrl)}",false);
return $@"
<iframe id=""{elementId}"" frameborder=""0"" scrolling=""no"" src=""{pageUrl}"" style=""width: 1px;min-width: 100%;""></iframe>
<script type=""text/javascript"" src=""{libUrl}""></script>
<script type=""text/javascript"">iFrameResize({{log: false}}, '#{elementId}')</script>
";
}
}
}