-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign.cpp
91 lines (81 loc) · 2.94 KB
/
sign.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
#include "sign.h"
#include "ui_sign.h"
#include "settings.h"
#include "clientinfo.h"
#include <QMessageBox>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QDesktopWidget>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QProcessEnvironment>
Sign::Sign(QWidget *parent) :
QDialog(parent),ui(new Ui::Sign)
{
ui->setupUi(this);
if(QProcessEnvironment::systemEnvironment().value("OS")!=QString("Windows_NT"))
{
QRect applicationGeometry=QApplication::desktop()->availableGeometry();
this->setFixedSize(applicationGeometry.size());
}
}
Sign::~Sign()
{
delete ui;
}
QByteArray Sign::signUp(const QString &username,const QString &password,const QString &email)
{
QNetworkRequest request(QUrl(Settings::SignPage));
request.setRawHeader("Content-Type","application/x-www-form-urlencoded");
// request.setRawHeader("Accept","text/html,application/xhtml+xml,"
// "application/xml;q=0.9,image/webp,*/*;q=0.8");
// request.setRawHeader("Accept-Encoding","gzip,deflate,sdch");
// request.setRawHeader("User-Agent","Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36"
// " (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36");
QByteArray postData;
postData.append("username=").append(username).append("&password=")
.append(QCryptographicHash::hash(password.toLocal8Bit(),QCryptographicHash::Md5))
.append("&email=").append(email);
postData=QUrl(postData).toEncoded();
qDebug(postData.constData());
QNetworkReply *reply=NAM.post(request,postData);
void (QNetworkReply:: *E)(QNetworkReply::NetworkError)=&QNetworkReply::error;
QMetaObject::Connection b=connect(reply,E,[&reply](QNetworkReply::NetworkError err){
qDebug() << static_cast<int>(err);
qDebug() << reply->errorString();
QMessageBox ErrMsg;
ErrMsg.setText(reply->errorString());
ErrMsg.exec();
});
QEventLoop loop;
QMetaObject::Connection a=connect(&NAM,&QNetworkAccessManager::finished,[&loop](){loop.exit();});
loop.exec();
NAM.disconnect(a);
reply->disconnect(b);
QByteArray response=reply->readAll();
reply->deleteLater();
return response;
}
void Sign::on_acceptButton_clicked()
{
QString username=ui->usernameEdit->text(),
password=ui->passwordEdit->text(),
confirmpass=ui->confirmPasswordEdit->text(),
email=ui->emailEdit->text();
if(password!=confirmpass)
{
QMessageBox::warning(this,"Error","密码输入错误");
return;
}
QByteArray response=signUp(username,password,email);
QJsonObject JObj=QJsonDocument::fromJson(response).object();
if(JObj["status"].toString()!="ok")
{
QMessageBox::warning(this,"注册失败",JObj["msg"].toString());
return;
}
QMessageBox::information(this,"注册成功",JObj["msg"].toString());
emit succeed();
accept();
}