-
Notifications
You must be signed in to change notification settings - Fork 1
/
messenger.cpp
140 lines (114 loc) · 4.09 KB
/
messenger.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
#include "messenger.h"
Messenger::Messenger(QObject *parent) : QObject(parent)
{
manager = new QNetworkAccessManager();
}
Messenger::~Messenger()
{
manager->deleteLater();
}
void Messenger::postON(QJsonObject json, QString uri)
{
QJsonDocument document;
document.setObject(json);
QByteArray data = document.toJson(QJsonDocument::Indented);
QNetworkRequest* request = new QNetworkRequest;
request->setHeader(QNetworkRequest::UserAgentHeader, config.UserAgent);
request->setHeader(QNetworkRequest::ContentTypeHeader, "application/json"); //上面语句固定这么写,要不然会报错“contest—type is missing”
QString url = QString("http://") + config.ip + ':' + QString::number(config.serverPort) + uri;
request->setUrl(QUrl(url));
QNetworkReply* reply=manager->post(*request, data);
connect(reply,&QNetworkReply::finished,this,&Messenger::finishPost);
delete request;
request = nullptr;
}
void Messenger::changeOneStatus(int orderID, QString toStatus)
{
QJsonObject json;
QJsonObject orderInf;
QJsonObject userInf;
userInf.insert("username", thisUser.username);
userInf.insert("password", thisUser.password);
QJsonArray theOrders; //订单序列
QJsonObject aOrder; //单个订单信息
aOrder.insert("id",QJsonValue(orderID)); //填充单个订单的ID
theOrders.append(QJsonValue(aOrder)); //将该订单添加到订单序列
orderInf.insert("theOrders",QJsonValue(theOrders)); //将订单序列添加到订单信息
orderInf.insert("isSame",QJsonValue(true)); //所有订单所要转换到的状态相同
orderInf.insert("status", QJsonValue(toStatus)); //同一状态
json.insert("userInformation", QJsonValue(userInf));
json.insert("orderInformation", QJsonValue(orderInf));
postON(json, QString("/changestatus")); //uri
}
void Messenger::finishOneOrder(int orderID)
{
QJsonObject json;
QJsonObject orderInf;
QJsonObject userInf;
userInf.insert("username", thisUser.username);
userInf.insert("password", thisUser.password);
orderInf.insert("id", QJsonValue(orderID));
json.insert("userInformation", QJsonValue(userInf));
json.insert("orderInformation", QJsonValue(orderInf));
postON(json, QString("/finishorder")); //uri
}
void Messenger::finishResOrder(int orderID)
{
QJsonObject json;
QJsonObject orderInf;
QJsonObject userInf;
userInf.insert("username", thisUser.username);
userInf.insert("password", thisUser.password);
orderInf.insert("id", QJsonValue(orderID));
json.insert("userInformation", QJsonValue(userInf));
json.insert("orderInformation", QJsonValue(orderInf));
postON(json, QString("/finishorder")); //uri
}
void Messenger::newOrder(QJsonObject json)
{
postON(json, QString("/new/forOrder"));
}
void Messenger::changeOrder(QJsonObject json)
{
postON(json, QString("/change/forOrder"));
}
void Messenger::newResOrder(QJsonObject json)
{
postON(json, QString("/new/forItemOrder"));
}
void Messenger::changeResOrder(QJsonObject json)
{
postON(json, QString("/change/forItemOrder"));
}
void Messenger::changeUser(QJsonObject json)
{
postON(json, QString("/user/Change"));
}
void Messenger::addnewUser(QVector<User> theUsers)
{
QJsonObject json;
QJsonObject userInf;
userInf.insert("username", thisUser.username);
userInf.insert("password", thisUser.password);
QJsonArray theApps; //用户序列
for(User forOne : theUsers)
{
QJsonObject appInf;
appInf.insert("username",forOne.username);
appInf.insert("truename",forOne.truename);
appInf.insert("workshop",forOne.workshop);
int isUse=forOne.isUseful ? 1 : 0;
appInf.insert("isUseful",isUse);
appInf.insert("identity",forOne.identity);
theApps.append(QJsonValue(appInf));
}
json.insert("userInformation", QJsonValue(userInf));
json.insert("applicantInformation", QJsonValue(theApps));
postON(json, QString("/user/AddNew")); //uri
}
//槽函数:
void Messenger::finishPost()
{
QNetworkReply* reply=qobject_cast<QNetworkReply *>(sender());
emit gotResponse(reply);
}