Commit ad44247b by ss.dai

1

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