1 [HtmlTargetElement("LayuiButton")]
2 public class LayuiButtonTag : TagHelper
3 {
4 #region 初始化
5 private const string PermCodeAttributeName = "PermCode";
6 private const string ClasstAttributeName = "class";
7 private const string LayEventAttributeName = "lay-event";
8 private const string LaySubmitAttributeName = "LaySubmit";
9 private const string LayIdAttributeName = "id";
10 private const string StyleAttributeName = "style";
11
12 [HtmlAttributeName(StyleAttributeName)]
13 public string Style { get; set; }
14
15 [HtmlAttributeName(LayIdAttributeName)]
16 public string Id { get; set; }
17
18 [HtmlAttributeName(LaySubmitAttributeName)]
19 public string LaySubmit { get; set; }
20
21 [HtmlAttributeName(LayEventAttributeName)]
22 public string LayEvent { get; set; }
23
24 [HtmlAttributeName(ClasstAttributeName)]
25 public string Class { get; set; }
26
27 [HtmlAttributeName(PermCodeAttributeName)]
28 public int PermCode { get; set; }
29
30 [HtmlAttributeNotBound]
31 [ViewContext]
32 public ViewContext ViewContext { get; set; }
33
34 #endregion
35 public override async void Process(TagHelperContext context, TagHelperOutput output)
36 {
37 context.ThrowIfNull();
38 output.ThrowIfNull();
39
40 var administrator = ViewContext.HttpContext.GetCurrentUser();
41 if (administrator == null)
42 return;
43
44 var childContent = await output.GetChildContentAsync();
45
46 if (((List<int>)ViewContext.ViewData["PermCodes"]).Contains(PermCode) || administrator.IsSuper)
47 {
48 foreach (var item in context.AllAttributes)
49 {
50 output.Attributes.Add(item.Name, item.Value);
51 }
52
53 output.TagName = "a";
54 output.TagMode = TagMode.StartTagAndEndTag;
55 output.Content.SetHtmlContent(childContent.GetContent());
56 }
57 else
58 {
59 output.TagName = "";
60 output.TagMode = TagMode.StartTagAndEndTag;
61 output.Content.SetHtmlContent("");
62 }
63 }
64 }