-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsub.aspx.cs
More file actions
354 lines (317 loc) · 12.4 KB
/
sub.aspx.cs
File metadata and controls
354 lines (317 loc) · 12.4 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
346
347
348
349
350
351
352
353
354
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
public partial class SubmitForm : System.Web.UI.Page
{
private LearnSite.Model.Cook cook = new LearnSite.Model.Cook();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
try
{
InitializePageData();
LoadStudentInfo(); // 先加载学生信息
BindLastUserData(); // 再绑定上一次使用记录
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Page_Load错误: " + ex.Message);
System.Diagnostics.Debug.WriteLine("堆栈跟踪: " + ex.StackTrace);
}
}
CheckLoginStatus();
}
// 初始化基础数据
private void InitializePageData()
{
string ip = LearnSite.Common.Computer.MyIp();
lblIpAddressValue.Text = ip;
lblPcNameValue.Text = GetHostName(ip);
}
// 加载学生信息
private void LoadStudentInfo()
{
if (!LearnSite.Common.CookieHelp.IsStudentLogin())
{
Response.Redirect("~/index.aspx");
return;
}
snum.Text = cook.Snum;
sid.Text = cook.Sid.ToString();
sname.Text = Server.UrlDecode(cook.Sname);
sclass.Text = string.Format("{0}年级{1}班", cook.Sgrade, cook.Sclass);
}
// 获取主机名
private string GetHostName(string ip)
{
LearnSite.BLL.Computers cbll = new LearnSite.BLL.Computers();
LearnSite.Model.Computers cmodel = cbll.GetModelByIp(ip);
return (cmodel != null) ? cmodel.Pmachine : "未知主机";
}
// 绑定上一次使用记录
private void BindLastUserData()
{
string ip = lblIpAddressValue.Text;
int currentSid = 0;
// 安全地获取当前学生ID
if (!int.TryParse(sid.Text, out currentSid))
{
lname.Text = "学生信息异常,无法查询上机记录";
lname.ForeColor = System.Drawing.Color.Orange;
return;
}
try
{
bool foundRecord = false;
// 使用单个连接执行查询
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServer"].ConnectionString))
{
conn.Open();
// 优先从CheckRecords表查询该电脑的上一次检查记录
using (SqlCommand cmd = new SqlCommand(
@"SELECT TOP 1 xuehao, sname, ClassName FROM CheckRecords
WHERE IpAddress=@Ip AND snid<>@CurrentSid
ORDER BY Id DESC", conn))
{
cmd.Parameters.AddWithValue("@Ip", ip);
cmd.Parameters.AddWithValue("@CurrentSid", currentSid);
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
lname.Text = string.Format("{0}({1})班 {2}",
reader["xuehao"],
reader["ClassName"],
reader["sname"]);
lname.ForeColor = System.Drawing.Color.Red;
foundRecord = true;
}
}
}
// 如果CheckRecords表没有记录,从Signin表查询
if (!foundRecord)
{
using (SqlCommand cmd = new SqlCommand(
@"SELECT TOP 1 qgrade, qclass, qname FROM Signin
WHERE qip=@Ip AND (qsid IS NULL OR qsid<>@CurrentSid)
ORDER BY qdate DESC", conn))
{
cmd.Parameters.AddWithValue("@Ip", ip);
cmd.Parameters.AddWithValue("@CurrentSid", currentSid);
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
lname.Text = string.Format("{0}({1})班 {2}",
reader["qgrade"],
reader["qclass"],
reader["qname"]);
lname.ForeColor = System.Drawing.Color.Red;
foundRecord = true;
}
}
}
}
}
// 如果都没有记录(第一次使用该电脑)
if (!foundRecord)
{
lname.Text = "本机首次使用,无上机记录";
lname.ForeColor = System.Drawing.Color.Green;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("BindLastUserData错误: " + ex.Message);
lname.Text = "查询上机记录时发生错误";
lname.ForeColor = System.Drawing.Color.Orange;
}
}
// 表单提交
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (rbNormal.Checked && !HasAnyIssues())
{
// 选择"正常"且没有勾选任何问题项,直接跳转
SafeRedirect();
}
else if (SaveFormData())
{
SetSubmissionCookie();
SafeRedirect();
}
else
{
Response.Write("<script>alert('提交失败,请重试!')</script>");
}
}
// 检查是否有任何问题项被选中
private bool HasAnyIssues()
{
return CheckListSelected(cblHygiene) ||
CheckListSelected(cblEquipmentArrangement) ||
CheckListSelected(cblDeviceIssues);
}
// 检查CheckBoxList是否至少选择一项
private bool CheckListSelected(CheckBoxList list)
{
foreach (ListItem item in list.Items)
{
if (item.Selected) return true;
}
return false;
}
// 保存数据
private bool SaveFormData()
{
// 验证必要字段
if (string.IsNullOrEmpty(sclass.Text) || string.IsNullOrEmpty(sname.Text) || string.IsNullOrEmpty(snum.Text))
{
System.Diagnostics.Debug.WriteLine("保存失败:学生信息不完整");
Response.Write("<script>alert('学生信息不完整,请重新登录!')</script>");
return false;
}
string sql = @"INSERT INTO CheckRecords (
PcName, IpAddress, ClassName,
HasRubbish, DrawerClean, EquipmentArranged, ChairAdjusted,
KeyboardMouseDamaged, CableUnplugged, PeripheralUnplugged, ScreenMarked,
snid, xuehao, sname, Suser, SubmitTime
) VALUES (
@PcName, @Ip, @Class,
@Rubbish, @Drawer, @Equipment, @Chair,
@KeyboardDamaged, @CableUnplugged, @PeripheralUnplugged, @ScreenMarked,
@snid, @xuehao, @sname, @lname, GETDATE()
)";
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServer"].ConnectionString))
{
SqlCommand cmd = new SqlCommand(sql, conn);
AddParameters(cmd);
try
{
conn.Open();
int rowsAffected = cmd.ExecuteNonQuery();
System.Diagnostics.Debug.WriteLine("保存成功,影响行数: " + rowsAffected);
return true;
}
catch (SqlException sqlEx)
{
// 记录详细的 SQL 错误信息
System.Diagnostics.Debug.WriteLine("SQL错误: " + sqlEx.Message);
System.Diagnostics.Debug.WriteLine("错误号: " + sqlEx.Number);
System.Diagnostics.Debug.WriteLine("SQL: " + sql);
string errorMsg = "提交失败";
if (sqlEx.Number == 208) // 表或视图不存在
{
errorMsg = "系统表不存在,请联系管理员";
}
else if (sqlEx.Number == 207) // 列名无效
{
errorMsg = "数据库结构异常,请联系管理员";
}
else if (sqlEx.Number == 547) // 外键约束
{
errorMsg = "数据关联错误,请重试";
}
Response.Write("<script>alert('" + errorMsg + "!')</script>");
return false;
}
catch (Exception ex)
{
// 记录其他错误
System.Diagnostics.Debug.WriteLine("保存检查记录失败: " + ex.Message);
System.Diagnostics.Debug.WriteLine("SQL: " + sql);
Response.Write("<script>alert('提交失败,请重试!')</script>");
return false;
}
}
}
// 添加参数
private void AddParameters(SqlCommand cmd)
{
cmd.Parameters.AddWithValue("@PcName", lblPcNameValue.Text);
cmd.Parameters.AddWithValue("@Ip", lblIpAddressValue.Text);
cmd.Parameters.AddWithValue("@Class", sclass.Text);
// 绑定检查项参数
cmd.Parameters.AddWithValue("@Rubbish", cblHygiene.Items[0].Selected);
cmd.Parameters.AddWithValue("@Drawer", cblHygiene.Items[1].Selected);
cmd.Parameters.AddWithValue("@Equipment", cblEquipmentArrangement.Items[0].Selected);
cmd.Parameters.AddWithValue("@Chair", cblEquipmentArrangement.Items[1].Selected);
cmd.Parameters.AddWithValue("@KeyboardDamaged", cblDeviceIssues.Items[0].Selected);
cmd.Parameters.AddWithValue("@CableUnplugged", cblDeviceIssues.Items[1].Selected);
cmd.Parameters.AddWithValue("@PeripheralUnplugged", cblDeviceIssues.Items[2].Selected);
cmd.Parameters.AddWithValue("@ScreenMarked", cblDeviceIssues.Items[3].Selected);
// 学生信息参数
cmd.Parameters.AddWithValue("@snid", sid.Text);
cmd.Parameters.AddWithValue("@xuehao", snum.Text);
cmd.Parameters.AddWithValue("@sname", sname.Text);
// 处理上一次使用的学生信息:如果是"本机首次使用,无上机记录",则保存空字符串
string lastUser = lname.Text;
if (lastUser.Contains("本机首次使用") || lastUser.Contains("无上机记录"))
{
lastUser = "";
}
cmd.Parameters.AddWithValue("@lname", lastUser);
}
// 安全跳转
private void SafeRedirect()
{
string targetUrl = GetSafeRedirectUrl();
Response.Redirect(string.IsNullOrEmpty(targetUrl) ?
"~/student/myinfo.aspx" : targetUrl);
}
// 获取安全URL
private string GetSafeRedirectUrl()
{
string encodedUrl = Request.QueryString["url"];
if (string.IsNullOrEmpty(encodedUrl)) return null;
string decodedUrl = HttpUtility.UrlDecode(encodedUrl);
return IsValidLocalUrl(decodedUrl) ? decodedUrl : null;
}
// 验证本地URL
private bool IsValidLocalUrl(string url)
{
if (Uri.IsWellFormedUriString(url, UriKind.Relative)) return true;
Uri absoluteUri;
if (Uri.TryCreate(url, UriKind.Absolute, out absoluteUri))
{
return string.Equals(
absoluteUri.Authority,
Request.Url.Authority,
StringComparison.OrdinalIgnoreCase
);
}
return false;
}
// 设置提交Cookie
private void SetSubmissionCookie()
{
HttpCookie cookie = new HttpCookie("HasSubmittedForm", DateTime.Now.ToString());
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
}
// 其他按钮事件
protected void btnjump_Click(object sender, EventArgs e)
{
SafeRedirect();
}
protected void BtnExit_Click(object sender, EventArgs e)
{
LearnSite.Common.CookieHelp.ClearStudentCookies();
Session.Abandon();
Response.Redirect("~/index.aspx");
}
// 检查登录状态(已被LoadStudentInfo中的检查替代,此方法仅作防御性检查)
private void CheckLoginStatus()
{
if (!LearnSite.Common.CookieHelp.IsStudentLogin())
{
LearnSite.Common.CookieHelp.JudgeStudentCookies();
}
}
}