-
Notifications
You must be signed in to change notification settings - Fork 1
/
userdialog.cpp
166 lines (150 loc) · 4.72 KB
/
userdialog.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
#include "userdialog.h"
#include "ui_userdialog.h"
UserDialog::UserDialog(QWidget *parent) :
QWidget(parent),
ui(new Ui::UserDialog)
{
ui->setupUi(this);
setAttribute(Qt::WA_QuitOnClose, false);
setAttribute(Qt::WA_DeleteOnClose, true);
ui->comboBox_isUse->setView(new QListView());
ui->comboBox_identity->setView(new QListView());
messenger=new Messenger();
connect(messenger,&Messenger::gotResponse,this,&UserDialog::afterConfirmReply);
setAddNewUser();
allFinish=false;
}
UserDialog::~UserDialog()
{
delete ui;
}
/*--------类型设置---------*/
void UserDialog::setChangeUser(User user)
{
forOne=user;
setTable(forOne);
ui->usernameEdit->setReadOnly(true);
dowhat=ONCHANGEUSER;
QString title=QString("修改用户 id:")+QString::number(forOne.id);
setWindowTitle(title);
}
void UserDialog::setAddNewUser()
{
forOne.id=0;
forOne.identity=config.userIdentityList[0];
forOne.username="";
forOne.truename="";
forOne.workshop="";
forOne.isUseful=true;
forOne.password=nullptr;
setTable(forOne);
dowhat=ONADDNEWUSER;
setWindowTitle(QString("新增用户"));
}
/*--------类型设置end-------*/
/*--------界面设置---------*/
void UserDialog::setTable(User theOne)
{
ui->usernameEdit->setText(theOne.username);
ui->truenameEdit->setText(theOne.truename);
ui->workshopEdit->setText(theOne.workshop);
ui->rePass_check->setChecked(false);
QStringList sl;
sl<<QString("不可用")<<QString("可用");
ui->comboBox_isUse->clear();
ui->comboBox_isUse->addItems(sl);
ui->comboBox_isUse->setCurrentIndex(theOne.isUseful);
ui->comboBox_identity->clear();
ui->comboBox_identity->addItems(config.userCNIdentityList);
int i=0;
for(QString s : config.userIdentityList)
{
if(theOne.identity==s)
{
ui->comboBox_identity->setCurrentIndex(i);
break;
}
i++;
}
}
void UserDialog::closeEvent(QCloseEvent* event)
{
if(!allFinish)
{
QMessageBox::StandardButton re=QMessageBox::question(nullptr,QString("确定?"),QString("用户修改尚未保存提交\n\t确认退出修改?"));
if(re==QMessageBox::Yes)
this->close();
else
event->ignore();
}
}
/*--------界面设置end-------*/
/*------------按钮响应---------*/
void UserDialog::on_button_cancel_clicked()
{
this->close();
}
void UserDialog::on_button_confirm_clicked()
{
forOne.username=ui->usernameEdit->text();
forOne.truename=ui->truenameEdit->text();
forOne.workshop=ui->workshopEdit->text();
if(ui->rePass_check->isChecked())
forOne.password=QString("000000");
else
forOne.password=nullptr;
forOne.isUseful=ui->comboBox_isUse->currentIndex();
int i=ui->comboBox_identity->currentIndex();
forOne.identity=config.userIdentityList[i];
if(!userCheck(forOne))
{
QMessageBox::warning(nullptr,QString("警告"),QString("信息有误,请检查后提交"));
return ;
}
if(dowhat==ONCHANGEUSER)
{
QJsonObject json;
QJsonObject appInf;
QJsonObject userInf;
userInf.insert("username", thisUser.username);
userInf.insert("password", thisUser.password);
appInf.insert("id",forOne.id);
appInf.insert("truename",forOne.truename);
appInf.insert("workshop",forOne.workshop);
if(forOne.password!=nullptr)
{
appInf.insert("password",toSHA256(forOne.password));
}
int isUse=forOne.isUseful ? 1 : 0;
appInf.insert("isUseful",isUse);
appInf.insert("identity",forOne.identity);
json.insert("userInformation", QJsonValue(userInf));
json.insert("applicantInformation", QJsonValue(appInf));
messenger->changeUser(json);
}
else if(dowhat==ONADDNEWUSER)
{
QVector<User> tmpU;
tmpU.push_back(forOne);
messenger->addnewUser(tmpU);
}
ui->button_confirm->setDisabled(true);
}
void UserDialog::afterConfirmReply(QNetworkReply* reply)
{
if (reply->error() == QNetworkReply::NoError) {
if (dowhat == ONADDNEWUSER)
QMessageBox::information(nullptr, QString("完成"), QString("新增用户成功"));
else if(dowhat == ONCHANGEUSER)
QMessageBox::information(nullptr, QString("完成"), QString("修改用户成功"));
reply->deleteLater();
allFinish=true;
emit finishAll();
this->close();
} else {
QMessageBox::warning(nullptr, QString("错误"), QString("失败!\n请检查是否有名字重复用户或请稍后重试"));
reply->deleteLater();
ui->button_confirm->setDisabled(false);
}
}
/*------------按钮响应end-------*/