forked from siteserver/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasePage.cs
More file actions
345 lines (290 loc) · 10.5 KB
/
BasePage.cs
File metadata and controls
345 lines (290 loc) · 10.5 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaiRong.Core;
using SiteServer.CMS.Core;
using SiteServer.CMS.Core.Permissions;
using SiteServer.CMS.Core.Share;
using SiteServer.CMS.Plugin;
using SiteServer.Plugin.Hooks;
namespace SiteServer.BackgroundPages
{
public class BasePage : Page
{
public Literal LtlBreadCrumb; // 面包屑(头部导航 + 左边一级二级菜单 + 其他)
private MessageUtils.Message.EMessageType _messageType;
private string _message = string.Empty;
private string _scripts = string.Empty;
protected virtual bool IsAccessable => false; // 页面默认情况下是不能直接访问
protected virtual bool IsSinglePage => false; // 是否为单页(即是否需要放在框架页内运行,false表示需要)
protected bool IsForbidden { get; private set; }
public RequestBody Body { get; private set; }
private void SetMessage(MessageUtils.Message.EMessageType messageType, Exception ex, string message)
{
_messageType = messageType;
_message = ex != null ? $"{message}<!-- {ex} -->" : message;
}
protected override void OnLoadComplete(EventArgs e)
{
base.OnLoadComplete(e);
try
{
foreach (var pageAdmin in PluginManager.GetHooks<IPageAdmin>())
{
pageAdmin.OnLoadComplete(e);
}
}
catch (Exception ex)
{
LogUtils.AddErrorLog(ex, "插件");
}
}
protected override void OnPreLoad(EventArgs e)
{
base.OnPreLoad(e);
try
{
foreach (var pageAdmin in PluginManager.GetHooks<IPageAdmin>())
{
pageAdmin.OnPreLoad(e);
}
}
catch (Exception ex)
{
LogUtils.AddErrorLog(ex, "插件");
}
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Body = new RequestBody();
if (!IsAccessable && !Body.IsAdministratorLoggin) // 如果页面不能直接访问且又没有登录则直接跳登录页
{
IsForbidden = true;
PageUtils.RedirectToLoginPage();
}
//防止csrf攻击
Response.AddHeader("X-Frame-Options", "SAMEORIGIN");
}
protected override void Render(HtmlTextWriter writer)
{
if (!string.IsNullOrEmpty(_message))
{
MessageUtils.SaveMessage(_messageType, _message);
}
base.Render(writer);
if (!IsAccessable && !IsSinglePage) // 页面不能直接访问且不是单页,需要加一段框架检测代码,检测页面是否运行在框架内
{
writer.Write($@"<script type=""text/javascript"">
if (window.top.location.href.toLowerCase().indexOf(""main.aspx"") == -1){{
window.top.location.href = ""{PageInitialization.GetRedirectUrl()}"";
}}
</script>");
}
if (!string.IsNullOrEmpty(_scripts))
{
writer.Write(@"<script type=""text/javascript"">{0}</script>", _scripts);
}
}
public void AddScript(string script)
{
_scripts += script;
}
public void AddWaitAndRedirectScript(string redirectUrl)
{
_scripts += $@"
setTimeout(function() {{
location.href = '{redirectUrl}';
}}, 1500);
$('.operation-area').hide();
";
}
public void AddWaitAndScript(string scripts)
{
_scripts += $@"
setTimeout(function() {{
{scripts}
}}, 1500);
$('.operation-area').hide();
";
}
public void FailMessage(Exception ex, string message)
{
SetMessage(MessageUtils.Message.EMessageType.Error, ex, message);
}
public void FailMessage(string message)
{
SetMessage(MessageUtils.Message.EMessageType.Error, null, message);
}
public void SuccessMessage(string message)
{
SetMessage(MessageUtils.Message.EMessageType.Success, null, message);
}
public void SuccessMessage()
{
SuccessMessage("操作成功!");
}
public void InfoMessage(string message)
{
SetMessage(MessageUtils.Message.EMessageType.Info, null, message);
}
public void SuccessDeleteMessage()
{
SuccessMessage(MessageUtils.DeleteSuccess);
}
public void SuccessUpdateMessage()
{
SuccessMessage(MessageUtils.UpdateSuccess);
}
public void SuccessCheckMessage()
{
SuccessMessage(MessageUtils.CheckSuccess);
}
public void SuccessInsertMessage()
{
SuccessMessage(MessageUtils.InsertSuccess);
}
public void FailInsertMessage(Exception ex)
{
FailMessage(ex, MessageUtils.InsertFail);
}
public void FailUpdateMessage(Exception ex)
{
FailMessage(ex, MessageUtils.UpdateFail);
}
public void FailDeleteMessage(Exception ex)
{
FailMessage(ex, MessageUtils.DeleteFail);
}
public void FailCheckMessage(Exception ex)
{
FailMessage(ex, MessageUtils.CheckFail);
}
public string MaxLengthText(string str, int length)
{
return StringUtils.MaxLengthText(str, length);
}
public Control FindControlBySelfAndChildren(string controlId)
{
return ControlUtils.FindControlBySelfAndChildren(controlId, this);
}
public void BreadCrumbPlugins(string pageTitle, string permission)
{
if (LtlBreadCrumb != null)
{
var pageUrl = PathUtils.GetFileName(Request.FilePath);
LtlBreadCrumb.Text = StringUtils.GetBreadCrumbHtml(AppManager.IdPlugins, pageUrl, pageTitle, string.Empty);
}
if (!string.IsNullOrEmpty(permission))
{
PermissionsManager.VerifyAdministratorPermissions(Body.AdministratorName, permission);
}
}
public void BreadCrumbUsers(string pageTitle, string permission)
{
if (LtlBreadCrumb != null)
{
var pageUrl = PathUtils.GetFileName(Request.FilePath);
LtlBreadCrumb.Text = StringUtils.GetBreadCrumbHtml(AppManager.IdUsers, pageUrl, pageTitle, string.Empty);
}
if (!string.IsNullOrEmpty(permission))
{
PermissionsManager.VerifyAdministratorPermissions(Body.AdministratorName, permission);
}
}
public void BreadCrumbSettings(string pageTitle, string permission)
{
if (LtlBreadCrumb != null)
{
var pageUrl = PathUtils.GetFileName(Request.FilePath);
LtlBreadCrumb.Text = StringUtils.GetBreadCrumbHtml(AppManager.IdSettings, pageUrl, pageTitle, string.Empty);
}
if (!string.IsNullOrEmpty(permission))
{
PermissionsManager.VerifyAdministratorPermissions(Body.AdministratorName, permission);
}
}
public virtual void Submit_OnClick(object sender, EventArgs e)
{
PageUtils.CloseModalPage(Page);
}
public static string GetShowHintScript()
{
return GetShowHintScript("操作进行中");
}
public static string GetShowHintScript(string message)
{
return GetShowHintScript(message, 120);
}
public static string GetShowHintScript(string message, int top)
{
return $@"hideBoxAndShowHint(this, '{message}, 请稍候...', {top});";
}
public void ClientScriptRegisterClientScriptBlock(string key, string script)
{
if (!ClientScript.IsStartupScriptRegistered(key))
{
ClientScript.RegisterClientScriptBlock(GetType(), key, script);
}
}
public void ClientScriptRegisterStartupScript(string key, string script)
{
if (!ClientScript.IsStartupScriptRegistered(key))
{
ClientScript.RegisterStartupScript(GetType(), key, script);
}
}
public bool ClientScriptIsStartupScriptRegistered(string key)
{
return ClientScript.IsStartupScriptRegistered(key);
}
public static string GetShowImageScript(string imageClientId, string publishmentSystemUrl)
{
return GetShowImageScript("this", imageClientId, publishmentSystemUrl);
}
public static string GetShowImageScript(string objString, string imageClientId, string publishmentSystemUrl)
{
return
$"showImage({objString}, '{imageClientId}', '{PageUtils.ApplicationPath}', '{publishmentSystemUrl}')";
}
public static List<Analytics> ParseJsonStringToName(string json)
{
var analytics = new List<Analytics>();
if (json.IndexOf("count", StringComparison.Ordinal) != -1)
{
json = json.Substring(json.IndexOf("count", StringComparison.Ordinal));
json = json.TrimEnd('}');
json = json.TrimEnd(']');
json = json.TrimEnd('}');
json = json.Replace("}", "");
json = json.Replace("{", "");
json = json.Replace("\"", "");
var arr = json.Split(',');
var a = new Analytics[arr.Length / 2];
for (var i = 0; i < arr.Length / 2; i++)
{
a[i] = new Analytics();
}
for (var i = 0; i < arr.Length; i++)
{
var arr1 = arr[i].Split(':');
var j = i / 2;
if (arr1[0] == "count")
{
a[j].Count = Convert.ToInt32((arr1[1]));
}
if (arr1[0] == "metric")
{
a[j].Metric = arr1[1];
}
if (i % 2 == 1)
{
analytics.Add(a[i / 2]);
}
}
}
return analytics;
}
}
}