add network plugin

This commit is contained in:
王劲鹏 2021-04-25 16:44:39 +08:00 committed by osborn
parent a910f61c2a
commit 189b13927a
4 changed files with 70 additions and 0 deletions

View File

@ -1,6 +1,7 @@
#include "DoricRegistry.h" #include "DoricRegistry.h"
#include "plugin/DoricModalPlugin.h" #include "plugin/DoricModalPlugin.h"
#include "plugin/DoricNetworkPlugin.h"
#include "plugin/DoricPopoverPlugin.h" #include "plugin/DoricPopoverPlugin.h"
#include "plugin/DoricShaderPlugin.h" #include "plugin/DoricShaderPlugin.h"
@ -16,6 +17,7 @@ DoricRegistry::DoricRegistry() {
registerNativePlugin<DoricShaderPlugin>("shader"); registerNativePlugin<DoricShaderPlugin>("shader");
registerNativePlugin<DoricModalPlugin>("modal"); registerNativePlugin<DoricModalPlugin>("modal");
registerNativePlugin<DoricPopoverPlugin>("popover"); registerNativePlugin<DoricPopoverPlugin>("popover");
registerNativePlugin<DoricNetworkPlugin>("network");
registerViewNode<DoricRootNode>("Root"); registerViewNode<DoricRootNode>("Root");
registerViewNode<DoricStackNode>("Stack"); registerViewNode<DoricStackNode>("Stack");

View File

@ -35,6 +35,7 @@ SOURCES += \
engine/v8/V8Executor.cpp \ engine/v8/V8Executor.cpp \
main.cpp \ main.cpp \
plugin/DoricModalPlugin.cpp \ plugin/DoricModalPlugin.cpp \
plugin/DoricNetworkPlugin.cpp \
plugin/DoricPopoverPlugin.cpp \ plugin/DoricPopoverPlugin.cpp \
plugin/DoricShaderPlugin.cpp \ plugin/DoricShaderPlugin.cpp \
shader/DoricGroupNode.cpp \ shader/DoricGroupNode.cpp \
@ -108,6 +109,7 @@ HEADERS += \
engine/v8/V8Executor.h \ engine/v8/V8Executor.h \
plugin/DoricModalPlugin.h \ plugin/DoricModalPlugin.h \
plugin/DoricNativePlugin.h \ plugin/DoricNativePlugin.h \
plugin/DoricNetworkPlugin.h \
plugin/DoricPopoverPlugin.h \ plugin/DoricPopoverPlugin.h \
plugin/DoricShaderPlugin.h \ plugin/DoricShaderPlugin.h \
shader/DoricGroupNode.h \ shader/DoricGroupNode.h \

View File

@ -0,0 +1,41 @@
#include "DoricNetworkPlugin.h"
#include <QJsonDocument>
#include <QJsonObject>
void DoricNetworkPlugin::request(QString jsValueString, QString callbackId) {
QJsonDocument document = QJsonDocument::fromJson(jsValueString.toUtf8());
QJsonValue jsValue = document.object();
QString url = jsValue["url"].toString();
QString method = jsValue["method"].toString();
QJsonValue headerVal = jsValue["headers"];
QJsonValue dataVal = jsValue["data"];
QJsonValue timeoutVal = jsValue["timeout"];
if (headerVal.isObject()) {
foreach (QString key, headerVal.toObject().keys()) {
httpRequest.setRawHeader(key.toUtf8(),
headerVal[key].toString().toUtf8());
}
}
QObject::connect(&networkAccessManager, SIGNAL(finished(QNetworkReply *)),
this, SLOT(networkRequestFinished(QNetworkReply *)));
if (method == "get") {
httpRequest.setUrl(QUrl(url));
networkAccessManager.get(httpRequest);
}
}
void DoricNetworkPlugin::networkRequestFinished(QNetworkReply *reply) {
int statusCode =
reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (reply->error() == QNetworkReply::NoError) {
} else {
}
reply->deleteLater();
}

View File

@ -0,0 +1,25 @@
#ifndef DORICNETWORKPLUGIN_H
#define DORICNETWORKPLUGIN_H
#include "DoricNativePlugin.h"
#include <QSettings>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QNetworkAccessManager>
class DoricNetworkPlugin : public DoricNativePlugin {
Q_OBJECT
public:
using DoricNativePlugin::DoricNativePlugin;
Q_INVOKABLE void request(QString jsValueString, QString callbackId);
private:
QNetworkRequest httpRequest;
QNetworkAccessManager networkAccessManager;
public slots:
void networkRequestFinished(QNetworkReply *reply);
};
#endif // DORICNETWORKPLUGIN_H