This repository was archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapirouter.cpp
More file actions
48 lines (40 loc) · 1.28 KB
/
apirouter.cpp
File metadata and controls
48 lines (40 loc) · 1.28 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
#include "apirouter.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QDebug>
ApiRouter::ApiRouter(Worker* worker)
{
m_worker = worker;
connect(m_worker, &Worker::taskFinished, this, &ApiRouter::onTaskFinished);
}
void ApiRouter::requestFromClient(QString strParameter) {
QJsonParseError json_error;
QJsonDocument jsonDoc = QJsonDocument::fromJson(strParameter.toLocal8Bit().data(), &json_error);
if(json_error.error != QJsonParseError::NoError)
{
qDebug() << "json parse error!";
throw QString("json parse error!");
}
QJsonObject jsonObj = jsonDoc.object();
m_worker->addTask(jsonObj);
}
void ApiRouter::onTaskFinished(QJsonObject response)
{
QString responseJson = QString(QJsonDocument(response).toJson(QJsonDocument::Compact));
QString msg = response["msg"].toString();
emit responseFromServer(responseJson);
emit receiveMsgFromClient(msg);
}
void ApiRouter::sendMsgToClient(QString strParameter)
{
emit receiveMsgFromServer(strParameter);
}
QJsonObject ApiRouter::formatResponse(QJsonObject taskInfo, QString msg)
{
QJsonObject response;
response.insert("id", taskInfo["id"]);
response.insert("data", msg);
response.insert("msg", taskInfo["data"]);
return response;
}