forked from ccsens_hardware/qt_qcp_show
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
229 lines
7.2 KiB
229 lines
7.2 KiB
#include "httpservice.h"
|
|
#include "mainconfig.h"
|
|
|
|
HttpService::HttpService(QObject *parent) : QObject(parent)
|
|
{
|
|
mManager = new QNetworkAccessManager;
|
|
}
|
|
|
|
QByteArray HttpService::get(QUrl url,quint32 timeoutInMs)
|
|
{
|
|
qDebug() << url;
|
|
//设置超时
|
|
QTimer timer;
|
|
timer.setInterval(timeoutInMs);
|
|
timer.setSingleShot(true);
|
|
|
|
//构造并发送请求
|
|
QNetworkRequest request;
|
|
request.setUrl(url);
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));
|
|
request.setRawHeader("Authorization",QByteArray("Bearer ").append(MainConfig::token));
|
|
QNetworkReply *reply = mManager->get(request);
|
|
|
|
//超时绑定
|
|
QEventLoop loop;
|
|
QObject::connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
|
|
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
|
timer.start();
|
|
loop.exec();
|
|
|
|
//处理响应
|
|
if (!timer.isActive()) {// 超时
|
|
disconnect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
|
reply->abort();
|
|
reply->deleteLater();
|
|
qDebug() << "Timeout";
|
|
return "";
|
|
}
|
|
|
|
QByteArray result = "";
|
|
//未超时
|
|
timer.stop();
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
// 错误处理
|
|
qDebug() << "Error String : " << reply->errorString();
|
|
} else {
|
|
QVariant variant = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
|
int nStatusCode = variant.toInt();
|
|
// 根据状态码做进一步数据处理
|
|
//QByteArray bytes = pReply->readAll();
|
|
qDebug() << "Status Code : " << nStatusCode;
|
|
if(nStatusCode == 200){
|
|
result = reply->readAll();
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
QByteArray HttpService::postJson(QUrl url,const QByteArray &json, quint32 timeoutInMs)
|
|
{
|
|
qDebug() << url << "\n" << json;
|
|
//设置超时
|
|
QTimer timer;
|
|
timer.setInterval(timeoutInMs);
|
|
timer.setSingleShot(true);
|
|
|
|
//构造并发送请求
|
|
QNetworkRequest request;
|
|
request.setUrl(url);
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("application/json"));
|
|
request.setRawHeader("Authorization",QByteArray("Bearer ").append(MainConfig::token));
|
|
QNetworkReply *reply = mManager->post(request,json);
|
|
|
|
//超时绑定
|
|
QEventLoop loop;
|
|
QObject::connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
|
|
QObject::connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
|
timer.start();
|
|
loop.exec();
|
|
|
|
//处理响应
|
|
if (!timer.isActive()) {// 超时
|
|
disconnect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
|
reply->abort();
|
|
reply->deleteLater();
|
|
qDebug() << "Timeout";
|
|
return "";
|
|
}
|
|
|
|
QByteArray result = "";
|
|
//未超时
|
|
timer.stop();
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
// 错误处理
|
|
qDebug() << "Error String : " << reply->errorString();
|
|
} else {
|
|
QVariant variant = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
|
|
int nStatusCode = variant.toInt();
|
|
// 根据状态码做进一步数据处理
|
|
//QByteArray bytes = pReply->readAll();
|
|
qDebug() << "Status Code : " << nStatusCode;
|
|
if(nStatusCode == 200){
|
|
result = reply->readAll();
|
|
}
|
|
}
|
|
qDebug() << result;
|
|
return result;
|
|
}
|
|
|
|
void HttpService::login(QString username, QString password)
|
|
{
|
|
//构造参数
|
|
QString rawJson = "{\"client\":1,\"data\":{\"credential\":\"%2\",\"identifier\":\"%1\"},\"scene\":0,\"type\":3}";
|
|
QString json = rawJson.arg(username).arg(password);
|
|
|
|
//发送请求
|
|
QByteArray result = postJson(MainConfig::loginUrl,json.toUtf8());
|
|
|
|
//解析响应
|
|
QJsonParseError jerror;
|
|
QJsonDocument jdoc;
|
|
QJsonObject jobj;
|
|
jdoc = QJsonDocument::fromJson(result, &jerror);
|
|
if (jerror.error != QJsonParseError::NoError || !jdoc.isObject() || !jdoc.object().contains("code"))
|
|
{
|
|
qDebug() << QString::fromUtf8("登录请求发送失败");
|
|
return;
|
|
}
|
|
jobj = jdoc.object();
|
|
int resCode = jobj.take("code").toInt();
|
|
if(resCode == 200){
|
|
MainConfig::token = jobj.take("data").toObject().take("token").toString();
|
|
qDebug() << MainConfig::token;
|
|
}else{
|
|
qDebug() << "登录失败:" << resCode;
|
|
}
|
|
}
|
|
|
|
void HttpService::updateStatus(QString firstAidId, QString status)
|
|
{
|
|
//构造参数
|
|
QString rawJson = "{\"param\":{\"firstAidId\":\"%1\",\"status\":\"%2\"}}";
|
|
QString json = rawJson.arg(firstAidId).arg(status);
|
|
|
|
//发送请求
|
|
QByteArray result = postJson(MainConfig::serviceStatusChangedUrl,json.toUtf8());
|
|
|
|
//解析响应
|
|
QJsonParseError jerror;
|
|
QJsonDocument jdoc;
|
|
QJsonObject jobj;
|
|
jdoc = QJsonDocument::fromJson(result, &jerror);
|
|
if (jerror.error != QJsonParseError::NoError || !jdoc.isObject() || !jdoc.object().contains("code"))
|
|
{
|
|
qDebug() << QString::fromUtf8("updateStatus请求失败");
|
|
return;
|
|
}
|
|
jobj = jdoc.object();
|
|
int resCode = jobj.take("code").toInt();
|
|
if(resCode == 200){
|
|
qDebug() << "updateStatus请求成功";
|
|
}else{
|
|
qDebug() << "updateStatus失败:" << resCode;
|
|
}
|
|
}
|
|
|
|
void HttpService::sendIdcardInfo(QString idcard,QString name, QString sex, QString nation)
|
|
{
|
|
//构造参数
|
|
QString rawJson = "{\"param\":{\"idcard\":\"%1\",\"name\":\"%2\",\"sex\":\"%3\",\"nation\":\"%4\"}}";
|
|
QString json = rawJson.arg(idcard).arg(name).arg(sex).arg(nation);
|
|
|
|
//发送请求
|
|
QByteArray result = postJson(MainConfig::sendUserInfoUrl,json.toUtf8());
|
|
|
|
//解析响应
|
|
QJsonParseError jerror;
|
|
QJsonDocument jdoc;
|
|
QJsonObject jobj;
|
|
jdoc = QJsonDocument::fromJson(result, &jerror);
|
|
if (jerror.error != QJsonParseError::NoError || !jdoc.isObject() || !jdoc.object().contains("code"))
|
|
{
|
|
qDebug() << QString::fromUtf8("sendIdcardInfo请求失败");
|
|
qDebug() << jdoc;
|
|
return;
|
|
}
|
|
jobj = jdoc.object();
|
|
int resCode = jobj.take("code").toInt();
|
|
if(resCode == 200){
|
|
qDebug() << "sendIdcardInfo请求成功";
|
|
}else{
|
|
qDebug() << "sendIdcardInfo失败:" << resCode;
|
|
}
|
|
}
|
|
|
|
void HttpService::oneKeyStart(QString idcard)
|
|
{
|
|
//构造参数
|
|
QString rawJson = "{\"param\":{\"idcard\":\"%1\"}}";
|
|
QString json = rawJson.arg(idcard);
|
|
|
|
//发送请求
|
|
QByteArray result = postJson(MainConfig::oneKeyStartUrl,json.toUtf8());
|
|
|
|
//解析响应
|
|
QJsonParseError jerror;
|
|
QJsonDocument jdoc;
|
|
QJsonObject jobj;
|
|
jdoc = QJsonDocument::fromJson(result, &jerror);
|
|
if (jerror.error != QJsonParseError::NoError || !jdoc.isObject() || !jdoc.object().contains("code"))
|
|
{
|
|
qDebug() << QString::fromUtf8("oneKeyStart请求失败");
|
|
return;
|
|
}
|
|
jobj = jdoc.object();
|
|
int resCode = jobj.take("code").toInt();
|
|
if(resCode == 200){
|
|
qDebug() << "oneKeyStart请求成功";
|
|
}else{
|
|
qDebug() << "oneKeyStart失败:" << resCode;
|
|
}
|
|
}
|
|
|
|
void HttpService::onComplexOneKeyStart(QString idcard, QString name, QString sex, QString nation)
|
|
{
|
|
qDebug() << "onComplexOneKeyStart" << idcard << name << sex << nation;
|
|
sendIdcardInfo(idcard,name,sex,nation);
|
|
oneKeyStart(idcard);
|
|
}
|
|
|