kylin-weather/model/datarequest.cpp

189 lines
6.6 KiB
C++
Raw Normal View History

2022-06-22 18:45:06 +08:00
#include "datarequest.h"
DataRequest::DataRequest(QObject *parent) : QObject(parent)
{
m_networkManager = new QNetworkAccessManager(this);
m_timer.setSingleShot(true);
2023-03-13 13:42:58 +08:00
m_timerLAndR.setSingleShot(true);
// m_timerR.setSingleShot(true);
2022-06-22 18:45:06 +08:00
m_timerColl.setSingleShot(true);
}
DataRequest::~DataRequest()
{
m_networkManager->deleteLater();
m_reply->deleteLater();
m_replyCollect->deleteLater();
2023-03-13 13:42:58 +08:00
m_replyLAndR->deleteLater();
2022-06-22 18:45:06 +08:00
}
2023-03-13 13:42:58 +08:00
void DataRequest::dataRequestLeftAndRight(const QString &cityIdList)
{
if (cityIdList.isEmpty()) {
return;
}
//如果是搜藏城市列表加载城市
QString forecastUrl = CoreVar::SERVICEREQUEST_COLLECT;
QStringList cityList = cityIdList.split(","); // cityList最后一项为空字符
//访问要求为 : "11111"+ "22222"形式
for (int i = 0; i < cityList.size(); i++) {
if (i == cityList.size() - 1) {
forecastUrl.append(cityList.at(i));
} else {
forecastUrl.append(cityList.at(i));
forecastUrl.append("+");
}
}
QNetworkRequest request;
request.setUrl(forecastUrl);
m_replyLAndR = m_networkManager->get(request);
QEventLoop eventLoop;
connect(&m_timerLAndR, SIGNAL(timeout()), &eventLoop, SLOT(quit())); //定时器结束,循环断开
connect(m_replyLAndR, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
m_timerLAndR.start(CoreVar::DEFAULT_TIMEOUT); // 30s等待
eventLoop.exec();
//超时处理
if (!timeOut(&m_timerLAndR, m_replyLAndR, &eventLoop)) {
ErrorReact::getInstance()->networkTimeout("false"); //超时
return;
} else {
int statusCode = m_replyLAndR->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
//状态码不为200时异常
if (statusCode != 200) {
ErrorReact::getInstance()->serverFeedBack(false, QString::number(statusCode));
return;
}
//网络层错误[与目标服务器有关]
if (m_replyLAndR->error() != QNetworkReply::NoError) {
ErrorReact::getInstance()->networkFeedBack(false, m_replyLAndR->errorString());
return;
}
QByteArray dataBaC = m_replyLAndR->readAll();
m_replyLAndR->abort();
emit sigSendLAndRData(dataBaC);
}
return;
}
2022-06-22 18:45:06 +08:00
//利用连接请求网络数据
void DataRequest::dataRequest(const QString &cityId)
{
2022-11-30 15:09:46 +08:00
qDebug() << " ##### Function Positioning ##### 利用连接请求网络数据 ##### " <<__FILE__<< ","<<__FUNCTION__<<","<<__LINE__;
2022-06-22 18:45:06 +08:00
if (cityId.isEmpty()) {
return;
}
QString forecastUrl = CoreVar::SERVICEREQUEST.arg(cityId);
QNetworkRequest request;
request.setUrl(forecastUrl);
m_reply = m_networkManager->get(request);
QEventLoop eventLoop;
connect(&m_timer, SIGNAL(timeout()), &eventLoop, SLOT(quit())); //定时器结束,循环断开
connect(m_reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
m_timer.start(CoreVar::DEFAULT_TIMEOUT); // 30s等待
eventLoop.exec();
//超时处理
if (!timeOut(&m_timer, m_reply, &eventLoop)) {
2022-11-30 15:09:46 +08:00
qDebug() << " ##### Function Positioning ##### 超时处理 ##### " <<__FILE__<< ","<<__FUNCTION__<<","<<__LINE__;
2022-06-22 18:45:06 +08:00
ErrorReact::getInstance()->networkTimeout("true"); //超时
return;
} else {
//数据处理
dataReply();
}
}
//处理返回的网络数据
void DataRequest::dataReply()
{
2022-11-30 15:09:46 +08:00
qDebug() << " ##### Function Positioning ##### 处理返回的网络数据 ##### " <<__FILE__<< ","<<__FUNCTION__<<","<<__LINE__;
2022-06-22 18:45:06 +08:00
// QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
int statusCode = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
//状态码不为200时异常
if (statusCode != 200) {
ErrorReact::getInstance()->serverFeedBack(false, QString::number(statusCode));
return;
}
//网络层错误[与目标服务器有关]
if (m_reply->error() != QNetworkReply::NoError) {
ErrorReact::getInstance()->networkFeedBack(false, m_reply->errorString());
return;
}
//一切正常,进行数据解析
QByteArray dataBa = m_reply->readAll();
m_reply->abort();
emit sendData(dataBa);
}
2023-03-13 13:42:58 +08:00
2022-06-22 18:45:06 +08:00
//请求收藏城市数据
void DataRequest::dataRequestCollect(const QString &cityIdList)
{
if (cityIdList.isEmpty()) {
return;
}
//如果是搜藏城市列表加载城市
QString forecastUrl = CoreVar::SERVICEREQUEST_COLLECT;
QStringList cityList = cityIdList.split(","); // cityList最后一项为空字符
//访问要求为 : "11111"+ "22222"形式
for (int i = 0; i < cityList.size(); i++) {
if (i == cityList.size() - 1) {
forecastUrl.append(cityList.at(i));
} else {
forecastUrl.append(cityList.at(i));
forecastUrl.append("+");
}
}
QNetworkRequest request;
request.setUrl(forecastUrl);
m_replyCollect = m_networkManager->get(request);
QEventLoop eventLoop;
connect(&m_timerColl, SIGNAL(timeout()), &eventLoop, SLOT(quit())); //定时器结束,循环断开
connect(m_replyCollect, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
m_timerColl.start(CoreVar::DEFAULT_TIMEOUT); // 30s等待
eventLoop.exec();
//超时处理
if (!timeOut(&m_timerColl, m_replyCollect, &eventLoop)) {
ErrorReact::getInstance()->networkTimeout("false"); //超时
return;
} else {
//数据处理
dataReplyCollect();
}
}
//处理收藏城市数据返回
void DataRequest::dataReplyCollect()
{
int statusCode = m_replyCollect->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
//状态码不为200时异常
if (statusCode != 200) {
ErrorReact::getInstance()->serverFeedBack(false, QString::number(statusCode));
return;
}
//网络层错误[与目标服务器有关]
if (m_replyCollect->error() != QNetworkReply::NoError) {
ErrorReact::getInstance()->networkFeedBack(false, m_replyCollect->errorString());
return;
}
QByteArray dataBaC = m_replyCollect->readAll();
m_replyCollect->abort();
emit sendCollData(dataBaC);
}
bool DataRequest::timeOut(QTimer *timer, QNetworkReply *reply, QEventLoop *eventloop)
{
//超时处理
if (timer->isActive()) {
//在设定的时间内没有超时,定时器关闭
timer->stop();
return true;
} else {
if (reply == nullptr) {
return false;
}
//超时-断开访问连接
disconnect(reply, &QNetworkReply::finished, eventloop, &QEventLoop::quit);
reply->abort();
return false;
}
}