From 189b13927a9ae2f45506b8f968d4ff122f32a3df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=8A=B2=E9=B9=8F?= Date: Sun, 25 Apr 2021 16:44:39 +0800 Subject: [PATCH] add network plugin --- doric-Qt/doric/DoricRegistry.cpp | 2 + doric-Qt/doric/doric.pro | 2 + doric-Qt/doric/plugin/DoricNetworkPlugin.cpp | 41 ++++++++++++++++++++ doric-Qt/doric/plugin/DoricNetworkPlugin.h | 25 ++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 doric-Qt/doric/plugin/DoricNetworkPlugin.cpp create mode 100644 doric-Qt/doric/plugin/DoricNetworkPlugin.h diff --git a/doric-Qt/doric/DoricRegistry.cpp b/doric-Qt/doric/DoricRegistry.cpp index ed38af43..acabd580 100644 --- a/doric-Qt/doric/DoricRegistry.cpp +++ b/doric-Qt/doric/DoricRegistry.cpp @@ -1,6 +1,7 @@ #include "DoricRegistry.h" #include "plugin/DoricModalPlugin.h" +#include "plugin/DoricNetworkPlugin.h" #include "plugin/DoricPopoverPlugin.h" #include "plugin/DoricShaderPlugin.h" @@ -16,6 +17,7 @@ DoricRegistry::DoricRegistry() { registerNativePlugin("shader"); registerNativePlugin("modal"); registerNativePlugin("popover"); + registerNativePlugin("network"); registerViewNode("Root"); registerViewNode("Stack"); diff --git a/doric-Qt/doric/doric.pro b/doric-Qt/doric/doric.pro index 2a708de5..bacd9ad8 100644 --- a/doric-Qt/doric/doric.pro +++ b/doric-Qt/doric/doric.pro @@ -35,6 +35,7 @@ SOURCES += \ engine/v8/V8Executor.cpp \ main.cpp \ plugin/DoricModalPlugin.cpp \ + plugin/DoricNetworkPlugin.cpp \ plugin/DoricPopoverPlugin.cpp \ plugin/DoricShaderPlugin.cpp \ shader/DoricGroupNode.cpp \ @@ -108,6 +109,7 @@ HEADERS += \ engine/v8/V8Executor.h \ plugin/DoricModalPlugin.h \ plugin/DoricNativePlugin.h \ + plugin/DoricNetworkPlugin.h \ plugin/DoricPopoverPlugin.h \ plugin/DoricShaderPlugin.h \ shader/DoricGroupNode.h \ diff --git a/doric-Qt/doric/plugin/DoricNetworkPlugin.cpp b/doric-Qt/doric/plugin/DoricNetworkPlugin.cpp new file mode 100644 index 00000000..0487e65e --- /dev/null +++ b/doric-Qt/doric/plugin/DoricNetworkPlugin.cpp @@ -0,0 +1,41 @@ +#include "DoricNetworkPlugin.h" + +#include +#include + +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(); +} diff --git a/doric-Qt/doric/plugin/DoricNetworkPlugin.h b/doric-Qt/doric/plugin/DoricNetworkPlugin.h new file mode 100644 index 00000000..f10b5c8e --- /dev/null +++ b/doric-Qt/doric/plugin/DoricNetworkPlugin.h @@ -0,0 +1,25 @@ +#ifndef DORICNETWORKPLUGIN_H +#define DORICNETWORKPLUGIN_H + +#include "DoricNativePlugin.h" + +#include +#include +#include +#include + +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