-
Notifications
You must be signed in to change notification settings - Fork 1
/
UserForm.cpp
334 lines (301 loc) · 10.4 KB
/
UserForm.cpp
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
#include "UserForm.h"
#include "ui_UserForm.h"
UserForm::UserForm(QString user, QString passwd, int level, QWidget *parent) :
QWidget(parent),
ui(new Ui::UserForm)
{
ui->setupUi(this);
m_userName = user;
m_userPasswd = passwd;
m_userLevel = level;
init();
}
UserForm::~UserForm()
{
delete ui;
}
void UserForm::init()
{
// 组合框
ui->groupBox->setTitle(QString::fromLocal8Bit("当前用户信息"));
ui->groupBoxPasswdChange->setTitle(QString::fromLocal8Bit("密码修改"));
ui->groupBoxPasswdChange->setVisible(false);
ui->groupBoxUserChange->setTitle(QString::fromLocal8Bit("用户设置"));
ui->groupBoxUserChange->setVisible(false);
// 用户名和等级
ui->labelUserName->setText(m_userName);
switch(m_userLevel)
{
case UserLevel::UserLevel_High:
ui->labelLevel->setText(QString::fromLocal8Bit("高"));
break;
case UserLevel::UserLevel_Center:
ui->labelLevel->setText(QString::fromLocal8Bit("中"));
break;
case UserLevel::UserLevel_Low:
ui->labelLevel->setText(QString::fromLocal8Bit("低"));
break;
}
// 用户列表
m_modelUser = new QSqlQueryModel(this);
connect(ui->tableViewUserInfo, SIGNAL(clicked(const QModelIndex &)), this, SLOT(onClickedUserList(const QModelIndex &)));
updateModel();
// 等级组合框
ui->comboBoxLevel->addItem("1");
ui->comboBoxLevel->addItem("2");
ui->comboBoxLevel->addItem("3");
ui->comboBoxLevel->setCurrentIndex(-1);
}
void UserForm::updateModel()
{
m_modelUser->setQuery(QString("SELECT * FROM AGVDB_INFO_USER"));
m_modelUser->setHeaderData(0, Qt::Horizontal, QString::fromLocal8Bit("用户名"));
m_modelUser->setHeaderData(1, Qt::Horizontal, QString::fromLocal8Bit("密 码"));
m_modelUser->setHeaderData(2, Qt::Horizontal, QString::fromLocal8Bit("等 级"));
ui->tableViewUserInfo->setTableModel(m_modelUser);
}
void UserForm::on_pushButtonChangePasswd_clicked()
{
if(ui->groupBoxPasswdChange->isHidden())
{
ui->groupBoxPasswdChange->setVisible(true);
}
}
void UserForm::on_pushButtonChangePwdCancel_clicked()
{
ui->lineEditPwdOld->setText("");
ui->lineEditPwdNew->setText("");
ui->lineEditPwdMakesure->setText("");
ui->groupBoxPasswdChange->setVisible(false);
}
void UserForm::on_pushButtonChangePwdOk_clicked()
{
QString pwdold = ui->lineEditPwdOld->text();
QString pwdnew = ui->lineEditPwdNew->text();
QString pwdmakesure = ui->lineEditPwdMakesure->text();
if(pwdold.isEmpty() || pwdnew.isEmpty() || pwdmakesure.isEmpty())
{
MsgBoxEx *msgBox = new MsgBoxEx();
msgBox->setMsgBoxMode(QString::fromLocal8Bit("所有密码输入框都不可为空!"));
delete msgBox;
return;
}
if(pwdold != m_userPasswd)
{
MsgBoxEx *msgBox = new MsgBoxEx();
msgBox->setMsgBoxMode(QString::fromLocal8Bit("旧密码与当前密码不一致!"));
delete msgBox;
return;
}
if(pwdold == pwdnew)
{
MsgBoxEx *msgBox = new MsgBoxEx();
msgBox->setMsgBoxMode(QString::fromLocal8Bit("旧密码与新密码相同!"));
delete msgBox;
return;
}
if(pwdnew != pwdmakesure)
{
MsgBoxEx *msgBox = new MsgBoxEx();
msgBox->setMsgBoxMode(QString::fromLocal8Bit("两次输入的密码不一致!"));
delete msgBox;
return;
}
// 更新密码
QSqlQuery query;
query.prepare(QString("UPDATE AGVDB_INFO_USER SET user_passwd = '%1' WHERE user_name = '%2'").arg(pwdnew).arg(m_userName));
if(query.exec())
{
MsgBoxEx *msgBox = new MsgBoxEx();
msgBox->setMsgBoxMode(QString::fromLocal8Bit("修改密码成功!"));
delete msgBox;
m_userPasswd = pwdnew;
// 如果是最高权限的用户需要更新用户信息列表,对于其它权限的用户由于未显示用户信息列表,故不必更新
if(m_userLevel==UserLevel::UserLevel_High)
{
updateModel();
}
}
else
{
MsgBoxEx *msgBox = new MsgBoxEx();
msgBox->setMsgBoxMode(QString::fromLocal8Bit("修改密码失败!"));
delete msgBox;
}
ui->lineEditPwdOld->setText("");
ui->lineEditPwdNew->setText("");
ui->lineEditPwdMakesure->setText("");
ui->groupBoxPasswdChange->setVisible(false);
}
void UserForm::visibleUsersetGroup()
{
ui->groupBoxUserChange->setVisible(true);
}
void UserForm::on_pushButtonLogoutUser_clicked()
{
MsgBoxEx *msgBox = new MsgBoxEx();
connect(msgBox, SIGNAL(btnOkClicked()), this, SLOT(onBtnOkClickedLogout()));
connect(msgBox, SIGNAL(btnCancelClicked()), this, SLOT(onBtnCancelClickedLogout()));
msgBox->setMsgBoxMode(QString::fromLocal8Bit("此操作会将当前用户信息删除,且程序将立即退出,\n此操作不可逆,您确定注销当前用户吗?"), "", MsgBoxBtnType::MsgBoxBtnType_OkCancle);
delete msgBox;
}
void UserForm::onBtnOkClickedLogout()
{
QSqlQuery query;
query.prepare(QString("DELETE FROM AGVDB_INFO_USER WHERE user_name = '%1'").arg(m_userName));
if(query.exec())
{
MsgBoxEx *msgBox = new MsgBoxEx();
msgBox->setMsgBoxMode(QString::fromLocal8Bit("用户注销成功,程序将退出!"), 3000);
QTimer::singleShot(3000, this, SLOT(onExitProgress()));
delete msgBox;
}
else
{
MsgBoxEx *msgBox = new MsgBoxEx();
msgBox->setMsgBoxMode(QString::fromLocal8Bit("用户注销失败!"));
delete msgBox;
}
}
void UserForm::onExitProgress()
{
qApp->quit();
}
void UserForm::onBtnCancelClickedLogout(){}
void UserForm::onClickedUserList(const QModelIndex&)
{
QModelIndex index = ui->tableViewUserInfo->currentIndex();
if (index.isValid())
{
QSqlRecord record = m_modelUser->record(index.row());
QString user = record.value(0).toString();
QString passwd = record.value(1).toString();
QString level = record.value(2).toString();
ui->lineEditUserName->setText(user);
ui->lineEditUserPasswd->setText(passwd);
ui->comboBoxLevel->setCurrentText(level);
}
}
void UserForm::on_pushButtonUserAdd_clicked()
{
QString user = ui->lineEditUserName->text();
QString passwd = ui->lineEditUserPasswd->text();
QString level = ui->comboBoxLevel->currentText();
if(user.isEmpty() || passwd.isEmpty() || level.isEmpty())
{
MsgBoxEx *msgBox = new MsgBoxEx();
msgBox->setMsgBoxMode(QString::fromLocal8Bit("添加的用户信息都不可为空!"));
delete msgBox;
return;
}
// 查询添加的用户是否存在
QSqlQuery query;
query.exec(QString("SELECT * FROM AGVDB_INFO_USER WHERE user_name = '%1'").arg(user));
if(query.next())
{
MsgBoxEx *msgBox = new MsgBoxEx();
msgBox->setMsgBoxMode(QString::fromLocal8Bit("用户 <%1> 已存在!").arg(user));
delete msgBox;
return;
}
// 添加用户
query.prepare(QString("INSERT INTO AGVDB_INFO_USER (user_name, user_passwd, user_level) VALUES ('%1', '%2', %3)").arg(user).arg(passwd).arg(level));
if(query.exec())
{
MsgBoxEx *msgBox = new MsgBoxEx();
msgBox->setMsgBoxMode(QString::fromLocal8Bit("添加用户 <%1> 成功!").arg(user));
delete msgBox;
updateModel();
}
else
{
MsgBoxEx *msgBox = new MsgBoxEx();
msgBox->setMsgBoxMode(QString::fromLocal8Bit("添加用户 <%1> 失败!").arg(user));
delete msgBox;
}
}
void UserForm::on_pushButtonUserChange_clicked()
{
QString user = ui->lineEditUserName->text();
QString passwd = ui->lineEditUserPasswd->text();
QString level = ui->comboBoxLevel->currentText();
if(user.isEmpty())
{
MsgBoxEx *msgBox = new MsgBoxEx();
msgBox->setMsgBoxMode(QString::fromLocal8Bit("请选择要修改的用户!"));
delete msgBox;
return;
}
if(passwd.isEmpty() || level.isEmpty())
{
MsgBoxEx *msgBox = new MsgBoxEx();
msgBox->setMsgBoxMode(QString::fromLocal8Bit("修改的用户信息都不可为空!"));
delete msgBox;
return;
}
// 查询修改的用户是否存在
QSqlQuery query;
query.exec(QString("SELECT * FROM AGVDB_INFO_USER WHERE user_name = '%1'").arg(user));
if(!query.next())
{
MsgBoxEx *msgBox = new MsgBoxEx();
msgBox->setMsgBoxMode(QString::fromLocal8Bit("不存在将要修改的用户 <'%1'> !").arg(user));
delete msgBox;
return;
}
// 修改用户信息
query.prepare(QString("UPDATE AGVDB_INFO_USER SET user_passwd = '%1', user_level = '%2' WHERE user_name = '%3'")
.arg(passwd).arg(level).arg(user));
if(query.exec())
{
MsgBoxEx *msgBox = new MsgBoxEx();
msgBox->setMsgBoxMode(QString::fromLocal8Bit("修改用户 <%1> 信息成功!").arg(user));
delete msgBox;
updateModel();
}
else
{
MsgBoxEx *msgBox = new MsgBoxEx();
msgBox->setMsgBoxMode(QString::fromLocal8Bit("修改用户 <%1> 信息失败!").arg(user));
delete msgBox;
}
}
void UserForm::on_pushButtonUserDel_clicked()
{
QModelIndex index = ui->tableViewUserInfo->currentIndex();
if (index.isValid())
{
QSqlRecord record = m_modelUser->record(index.row());
QString user = record.value(0).toString();
// 管理员不可以删除自己,但可以注销
if(user == m_userName)
{
MsgBoxEx *msgBox = new MsgBoxEx();
msgBox->setMsgBoxMode(QString::fromLocal8Bit("最高权限用户不可删除自己,只可注销用户!"));
delete msgBox;
return;
}
// 删除用户
QSqlQuery query;
query.prepare(QString("DELETE FROM AGVDB_INFO_USER WHERE user_name = '%1'").arg(user));
if(query.exec())
{
MsgBoxEx *msgBox = new MsgBoxEx();
msgBox->setMsgBoxMode(QString::fromLocal8Bit("删除用户 [%1] 成功!").arg(user));
delete msgBox;
updateModel();
}
else
{
MsgBoxEx *msgBox = new MsgBoxEx();
msgBox->setMsgBoxMode(QString::fromLocal8Bit("删除用户 [%1] 失败!").arg(user));
delete msgBox;
}
}
else
{
MsgBoxEx *msgBox = new MsgBoxEx();
msgBox->setMsgBoxMode(QString::fromLocal8Bit("请选择要删除的用户!"));
delete msgBox;
}
}