Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
F
FmTakeaway
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
shangshang.dai
FmTakeaway
Commits
19e6d9c7
Commit
19e6d9c7
authored
May 31, 2017
by
ss.dai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1
parent
ba094b22
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
145 additions
and
12 deletions
+145
-12
fmPlugin/fmPlugin.cpp
+128
-11
fmPlugin/fmPlugin.h
+17
-1
No files found.
fmPlugin/fmPlugin.cpp
View file @
19e6d9c7
...
...
@@ -15,6 +15,119 @@
#include <QJsonArray>
#include "DTools/configManger.h"
// HTTP
bool
HTTP
::
get
(
const
QNetworkRequest
&
request
,
QByteArray
&
target
,
const
int
&
timeout
)
{
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
](
const
QNetworkReply
::
NetworkError
&
error
)
{
error_
=
error
.
errorString
();
eventLoop
.
exit
(
false
);
},
[
&
failFlag
,
&
eventLoop
]()
{
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
;
timer
->
setSingleShot
(
true
);
QObject
::
connect
(
timer
,
&
QTimer
::
timeout
,
[
timer
,
onTimeout
]()
{
onTimeout
();
timer
->
deleteLater
();
}
);
timer
->
start
(
timeout
);
}
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
);
}
);
}
FmPlugin
&
FmPlugin
::
GetInstance
()
{
static
FmPlugin
fmPlugin
;
...
...
@@ -118,22 +231,26 @@ bool FmPlugin::DoOrderEntry(const OrderObject *orderObject, const QString &cashi
QSettings
set
(
inipath
,
QSettings
::
IniFormat
);
QUrl
url
;
url
=
set
.
value
(
"HdServer/url"
).
toUrl
();
qDebug
()
<<
"url"
<<
url
;
QNetworkAccessManager
manger
;
HTTP
http
;
QByteArray
recvArray
;
QNetworkRequest
qRequset
;
qRequset
.
setUrl
(
url
);
qRequset
.
setRawHeader
(
"Content-Type"
,
"application/json;charset=utf-8"
);
qRequset
.
setRawHeader
(
"Accept"
,
"application/json;charset=utf-8"
);
error
=
_GetOrderEntryData
(
orderObject
);
QNetworkReply
*
reply
=
manger
.
post
(
qRequset
,
_GetOrderEntryData
(
orderObject
));
QEventLoop
eventLoop
;
QObject
::
connect
(
&
manger
,
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
(
10000
,
&
eventLoop
,
&
QEventLoop
::
quit
);
eventLoop
.
exec
();
reply
->
deleteLater
();
if
(
http
.
post
(
qRequset
,
_GetOrderEntryData
(
orderObject
),
recvArray
,
15000
))
{
}
else
{
}
if
(
reply
->
error
()
!=
QNetworkReply
::
NoError
)
{
error
=
reply
->
errorString
();
...
...
fmPlugin/fmPlugin.h
View file @
19e6d9c7
...
...
@@ -7,8 +7,24 @@
#include "Model/stockObject.h"
#include <QtSql/QSqlDatabase>
#include <QDateTime>
#include <QNetworkAccessManager>
class
PluginWorker
;
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
);
private
:
QNetworkAccessManager
manage_
;
QString
error_
;
};
class
FMPLUGINSHARED_EXPORT
FmPlugin
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment