Commit ad44247b by ss.dai

1

parent e44d14b0
......@@ -16,117 +16,39 @@
#include <QNetworkReply>
#include "DTools/configManger.h"
// HTTP
bool HTTP::get(const QNetworkRequest &request, QByteArray &target, const int &timeout)
bool HTTP::post(const QNetworkRequest &request, const QByteArray &appendData, QByteArray &target, const int &timeout, QString &error)
{
error = "";
target.clear();
QEventLoop eventLoop;
auto reply = manage_.get( request );
bool failFlag = false;
this->handle(
reply,
timeout,
[&](const QByteArray &data)
{
target = data;
eventLoop.exit(1);
},
[&](const QNetworkReply::NetworkError &)
{
eventLoop.exit(0);
},
[&]()
{
failFlag = true;
eventLoop.exit(0);
}
);
return eventLoop.exec() && !failFlag;
}
bool HTTP::post(const QNetworkRequest &request, const QByteArray &appendData, QByteArray &target, const int &timeout)
{
target.clear();
QEventLoop eventLoop;
auto reply = manage_.post( request, appendData );
bool failFlag = false;
this->handle(
reply,
timeout,
[ &target, &eventLoop ](const QByteArray &data)
{
target = data;
eventLoop.exit( true );
},
[ &eventLoop , this](const QNetworkReply::NetworkError &error)
{
this->error_ = QString::number((int)error);
eventLoop.exit( false );
},
[ &failFlag, &eventLoop, this ]()
{
this->error_ = "timeout";
failFlag = true;
eventLoop.exit( false );
}
);
return eventLoop.exec() && !failFlag;
}
void HTTP::handle(QNetworkReply *reply, const int &timeout,
const std::function<void (const QByteArray &)> &onFinished,
const std::function<void (const QNetworkReply::NetworkError &)> &onError,
const std::function<void ()> &onTimeout)
{
QTimer *timer = nullptr;
if ( timeout )
if(QNetworkAccessManager::Accessible != manage_.networkAccessible())
{
timer = new QTimer;
timer->setSingleShot(true);
QObject::connect( timer, &QTimer::timeout, [ timer, onTimeout ]()
{
onTimeout();
timer->deleteLater();
} );
timer->start( timeout );
manage_.setNetworkAccessible(QNetworkAccessManager::Accessible);
}
QObject::connect( reply, &QNetworkReply::finished, [ reply, timer, onFinished ]()
{
if ( timer )
{
timer->deleteLater();
}
onFinished( reply->readAll() );
} );
#ifndef QT_NO_SSL
if ( reply->url().toString().toLower().startsWith( "https" ) )
{
QObject::connect( reply, ( void( QNetworkReply::* )( QList< QSslError > ) )&QNetworkReply::sslErrors, [ reply ](const QList< QSslError > &errors)
{
qDebug() << "HTTP::handle: ignoreSslErrors:" << errors;
reply->ignoreSslErrors();
} );
}
#endif
QObject::connect( reply, ( void( QNetworkReply::* )( QNetworkReply::NetworkError ) )&QNetworkReply::error, [ reply, timer, onError ](const QNetworkReply::NetworkError &code)
{
if ( timer )
{
timer->deleteLater();
}
onError( code );
} );
QEventLoop eventLoop;
QNetworkReply *reply = manage_.post(request , appendData);
QObject::connect(&manage_, SIGNAL(networkAccessibleChanged(QNetworkAccessManager::NetworkAccessibility)), &eventLoop, SLOT(quit()));
QObject::connect(reply, SIGNAL(finished()), &eventLoop, SLOT(quit()));
QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), &eventLoop, SLOT(quit()));
// 加用定时器防止网络出现异常长时间不返回导致的阻塞
QTimer::singleShot(timeout, &eventLoop, &QEventLoop::quit);
eventLoop.exec();
if(reply->error() != QNetworkReply::NoError)
{
error = reply->errorString();
return false;
}
target = reply->readAll();
if(target.size() == 0)
{
error = "timeout";
return false;
}
reply->deleteLater();
return true;
}
FmPlugin &FmPlugin::GetInstance()
......@@ -257,7 +179,7 @@ bool FmPlugin::DoOrderEntry(const OrderObject *orderObject, const QString &cashi
// 查询会员号
QString memberCode("");
if(http.post(request_vip, _GetVipQueryData(orderObject->phone, QDateTime::fromTime_t(orderObject->create_time).toString("yyyy-MM-ddThh:mm:ss.zzz+0800")), recvArray, 10000))
if(http.post(request_vip, _GetVipQueryData(orderObject->phone, QDateTime::fromTime_t(orderObject->create_time).toString("yyyy-MM-ddThh:mm:ss.zzz+0800")), recvArray, 10000, error))
{
QJsonObject recvJson;
recvJson = QJsonDocument::fromJson(recvArray).object();
......@@ -266,7 +188,7 @@ bool FmPlugin::DoOrderEntry(const OrderObject *orderObject, const QString &cashi
// 写入销售单
bill_data = _GetOrderEntryData(orderObject, memberCode);
if(http.post(requset_bill, _GetOrderEntryData(orderObject, memberCode), recvArray, 20000))
if(http.post(requset_bill, _GetOrderEntryData(orderObject, memberCode), recvArray, 20000, error))
{
QJsonObject recvJson;
recvJson = QJsonDocument::fromJson(recvArray).object();
......@@ -280,7 +202,7 @@ bool FmPlugin::DoOrderEntry(const OrderObject *orderObject, const QString &cashi
// 写入配送费&服务费
fee_data = _GetFeeData(orderObject, uuid);
if(http.post(request_fee, _GetFeeData(orderObject, uuid), recvArray, 20000))
if(http.post(request_fee, _GetFeeData(orderObject, uuid), recvArray, 20000, error))
{
QJsonObject recvJson;
recvJson = QJsonDocument::fromJson(recvArray).object();
......@@ -297,11 +219,12 @@ bool FmPlugin::DoOrderEntry(const OrderObject *orderObject, const QString &cashi
return true;
}else
{
error = "配送费服务器,连接出错";
error = QString("配送费服务器,%1").arg(error);
}
}else
{
error = "销售单服务器,连接出错";
error = QString("销售单服务器,%1").arg(error);
}
return false;
......
......@@ -16,18 +16,9 @@
class HTTP
{
public:
inline QNetworkAccessManager &manage() { return manage_; }
inline QString error() { return error_; }
bool get(const QNetworkRequest &request, QByteArray &target, const int &timeout = 30000);
bool post(const QNetworkRequest &request, const QByteArray &appendData, QByteArray &target, const int &timeout);
private:
void handle(QNetworkReply *reply, const int &timeout,
const std::function< void(const QByteArray &data) > &onFinished,
const std::function< void(const QNetworkReply::NetworkError &code) > &onError,
const std::function< void() > &onTimeout);
bool post(const QNetworkRequest &request, const QByteArray &appendData, QByteArray &target, const int &timeout, QString& error);
private:
QNetworkAccessManager manage_;
QString error_;
};
class FMPLUGINSHARED_EXPORT FmPlugin
......
......@@ -25,6 +25,8 @@ FlowControl::FlowControl()
m_pullOrderSocket = NULL;
m_procOrderSocket = NULL;
netErrorIndex = 0;
QTimer::singleShot(10000,this, &FlowControl::_PostBill);
}
bool FlowControl::_GetStoreInfo()
......@@ -249,34 +251,13 @@ bool FlowControl::_PullOrder()
if(bWrite)
{
QLOG_INFO() << QString("will entry %1").arg(orderObject->order_id);
//QTimer::singleShot(1000*60*120, [orderObject, this](){
QTimer::singleShot(1000*10, [orderObject,this](){
if(orderObject->status == 6 || orderObject->status == 200)
{
QString error, bill_data, fee_data;
for(int i=0; i<5; i++)
{
QLOG_INFO() << QString("第%1次尝试写入订单%2").arg(i+1).arg(orderObject->order_id);
if(FmPlugin::GetInstance().DoOrderEntry(orderObject,"","",bill_data, fee_data,error))
{
QLOG_INFO() << QString("销售单数据%1 配送费数据%2").arg(bill_data,fee_data);
QLOG_INFO() << QString("_PullOrder DoOrderEntry successful %1 %2").arg(orderObject->order_id).arg(error);
this->_ReportBillEntryResult(orderObject->order_id, 1, QString("成功"));
break;
}else
{
QLOG_INFO() << QString("销售单数据%1 配送费数据%2").arg(bill_data,fee_data);
QLOG_INFO() << QString("_PullOrder DoOrderEntry failed %1 %2").arg(orderObject->order_id, error);
this->_ReportBillEntryResult(orderObject->order_id, 0, error);
}
QEventLoop loop;
QTimer::singleShot(1000*60*2, &loop, &QEventLoop::quit);
loop.exec();
}
}else
{
QLOG_INFO() << QString("cancle entry %1").arg(orderObject->order_id);
}
QTimer::singleShot(1000*60*120, [orderObject, this](){
//QTimer::singleShot(1000*10, [orderObject,this](){
QLOG_INFO() << QString("订单[%1]进入队列").arg(orderObject->order_id);
m_mutex.lock();
m_billList.append(orderObject);
m_mutex.unlock();
});
}else
......@@ -297,6 +278,49 @@ bool FlowControl::_PullOrder()
return result;
}
void FlowControl::_PostBill()
{
if(m_billList.isEmpty())
{
QTimer::singleShot(10000, this, &FlowControl::_PostBill);
return;
}
OrderObject* orderObject = m_billList.first();
if(orderObject->status == 6 || orderObject->status == 200)
{
QString error, bill_data, fee_data;
for(int i=0; i<5; i++)
{
QLOG_INFO() << QString("第%1次尝试写入订单%2").arg(i+1).arg(orderObject->order_id);
if(FmPlugin::GetInstance().DoOrderEntry(orderObject,"","",bill_data, fee_data,error))
{
QLOG_INFO() << QString("销售单数据%1 配送费数据%2").arg(bill_data,fee_data);
QLOG_INFO() << QString("_PullOrder DoOrderEntry successful %1 %2").arg(orderObject->order_id).arg(error);
this->_ReportBillEntryResult(orderObject->order_id, 1, QString("成功"));
m_mutex.lock();
m_billList.removeOne(orderObject);
QLOG_INFO() << QString("订单%1移出队列").arg(orderObject->order_id);
m_mutex.unlock();
break;
}else
{
QLOG_INFO() << QString("销售单数据%1 配送费数据%2").arg(bill_data,fee_data);
QLOG_INFO() << QString("_PullOrder DoOrderEntry failed %1 %2").arg(orderObject->order_id, error);
this->_ReportBillEntryResult(orderObject->order_id, 0, error);
}
QEventLoop loop;
QTimer::singleShot(1000*60, &loop, &QEventLoop::quit);
loop.exec();
}
}else
{
QLOG_INFO() << QString("cancle entry %1").arg(orderObject->order_id);
}
QTimer::singleShot(10000, this, &FlowControl::_PostBill);
}
bool FlowControl::_GetDelivers(const QString &orderId)
{
QString error;
......@@ -733,6 +757,7 @@ bool FlowControl::_RefundOrder(const QString &orderId, const QString &reason)
return result;
}
void FlowControl::onFlowStart()
{
m_timestamp = "0";
......
......@@ -12,6 +12,7 @@
#include "Model/cashierObject.h"
#include "alertForm.h"
#include <QMap>
#include <QMutex>
class FlowControl : public QObject
......@@ -51,6 +52,9 @@ private:
int netErrorIndex;
QList<OrderObject*> m_billList;
QMutex m_mutex;
signals:
/* 功能:连接数据库完成
* 参数:[1]是否成功[2]错误信息
......@@ -186,6 +190,8 @@ private slots:
QString _GetJsonStr(const QJsonObject& json);
bool _RefundOrder(const QString& orderId, const QString& reason);
void _PostBill();
public slots:
/* 功能:开启流程控制器
* 参数:NULL
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment