mirror of
https://github.com/zhuzichu520/FluentUI.git
synced 2025-01-23 12:24:34 +08:00
update
This commit is contained in:
parent
c80689e376
commit
3d65bdc913
@ -25,6 +25,7 @@ Item {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Component.onCompleted: {
|
Component.onCompleted: {
|
||||||
|
FluNetwork.openLog = true
|
||||||
FluNetwork.setInterceptor(function(param){
|
FluNetwork.setInterceptor(function(param){
|
||||||
param.addHeader("Token","000000000000000000000")
|
param.addHeader("Token","000000000000000000000")
|
||||||
})
|
})
|
||||||
|
@ -25,6 +25,7 @@ Item {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Component.onCompleted: {
|
Component.onCompleted: {
|
||||||
|
FluNetwork.openLog = true
|
||||||
FluNetwork.setInterceptor(function(param){
|
FluNetwork.setInterceptor(function(param){
|
||||||
param.addHeader("Token","000000000000000000000")
|
param.addHeader("Token","000000000000000000000")
|
||||||
})
|
})
|
||||||
|
@ -126,7 +126,7 @@ void Log::setup(const QString &app)
|
|||||||
}
|
}
|
||||||
once = true;
|
once = true;
|
||||||
g_app = app;
|
g_app = app;
|
||||||
const QString logFileName = QString("%1-%2.log").arg(g_app,QString::number(QDateTime::currentMSecsSinceEpoch()));
|
const QString logFileName = QString("%1_%2.log").arg(g_app,QDateTime::currentDateTime().toString("yyyyMMdd"));
|
||||||
const QString logDirPath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation)+"/log";
|
const QString logDirPath = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation)+"/log";
|
||||||
const QDir logDir(logDirPath);
|
const QDir logDir(logDirPath);
|
||||||
if(!logDir.exists()){
|
if(!logDir.exists()){
|
||||||
|
@ -28,7 +28,6 @@ int main(int argc, char *argv[])
|
|||||||
QGuiApplication::setOrganizationName("ZhuZiChu");
|
QGuiApplication::setOrganizationName("ZhuZiChu");
|
||||||
QGuiApplication::setOrganizationDomain("https://zhuzichu520.github.io");
|
QGuiApplication::setOrganizationDomain("https://zhuzichu520.github.io");
|
||||||
QGuiApplication::setApplicationName("FluentUI");
|
QGuiApplication::setApplicationName("FluentUI");
|
||||||
QNetworkProxy::setApplicationProxy(QNetworkProxy::NoProxy);
|
|
||||||
SettingsHelper::getInstance()->init(argv);
|
SettingsHelper::getInstance()->init(argv);
|
||||||
Log::setup("example");
|
Log::setup("example");
|
||||||
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||||
|
@ -14,9 +14,9 @@
|
|||||||
FRAMELESSHELPER_USE_NAMESPACE
|
FRAMELESSHELPER_USE_NAMESPACE
|
||||||
|
|
||||||
FluApp::FluApp(QObject *parent):QObject{parent}{
|
FluApp::FluApp(QObject *parent):QObject{parent}{
|
||||||
connect(this,&FluApp::useSystemAppBarChanged,this,[=]{FramelessConfig::instance()->set(Global::Option::UseSystemAppBar,_useSystemAppBar);});
|
|
||||||
vsync(true);
|
vsync(true);
|
||||||
useSystemAppBar(false);
|
useSystemAppBar(false);
|
||||||
|
connect(this,&FluApp::useSystemAppBarChanged,this,[=]{FramelessConfig::instance()->set(Global::Option::UseSystemAppBar,_useSystemAppBar);});
|
||||||
}
|
}
|
||||||
|
|
||||||
FluApp::~FluApp(){
|
FluApp::~FluApp(){
|
||||||
|
@ -189,7 +189,7 @@ void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){
|
|||||||
QNetworkRequest request(url);
|
QNetworkRequest request(url);
|
||||||
addHeaders(&request,params->_headerMap);
|
addHeaders(&request,params->_headerMap);
|
||||||
QNetworkReply* reply;
|
QNetworkReply* reply;
|
||||||
sendRequest(&manager,request,params,reply,callable);
|
sendRequest(&manager,request,params,reply,i==0,callable);
|
||||||
if(!QPointer(qApp)){
|
if(!QPointer(qApp)){
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
reply = nullptr;
|
reply = nullptr;
|
||||||
@ -226,6 +226,7 @@ void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){
|
|||||||
}
|
}
|
||||||
callable->success(response);
|
callable->success(response);
|
||||||
}
|
}
|
||||||
|
printRequestEndLog(request,params,reply,response);
|
||||||
break;
|
break;
|
||||||
}else{
|
}else{
|
||||||
if(i == params->getRetry()-1){
|
if(i == params->getRetry()-1){
|
||||||
@ -238,6 +239,7 @@ void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){
|
|||||||
}
|
}
|
||||||
callable->error(httpStatus,reply->errorString(),response);
|
callable->error(httpStatus,reply->errorString(),response);
|
||||||
}
|
}
|
||||||
|
printRequestEndLog(request,params,reply,response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
@ -387,7 +389,15 @@ QString FluNetwork::getCacheFilePath(const QString& key){
|
|||||||
return cacheDir.absoluteFilePath(key);
|
return cacheDir.absoluteFilePath(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FluNetwork::sendRequest(QNetworkAccessManager* manager,QNetworkRequest request,NetworkParams* params,QNetworkReply*& reply,QPointer<NetworkCallable> callable){
|
QString FluNetwork::map2String(const QMap<QString, QVariant>& map){
|
||||||
|
QStringList parameters;
|
||||||
|
for (auto it = map.constBegin(); it != map.constEnd(); ++it) {
|
||||||
|
parameters << QString("%1=%2").arg(it.key(), it.value().toString());
|
||||||
|
}
|
||||||
|
return parameters.join(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
void FluNetwork::sendRequest(QNetworkAccessManager* manager,QNetworkRequest request,NetworkParams* params,QNetworkReply*& reply,bool isFirst,QPointer<NetworkCallable> callable){
|
||||||
QByteArray verb = params->method2String().toUtf8();
|
QByteArray verb = params->method2String().toUtf8();
|
||||||
switch (params->_type) {
|
switch (params->_type) {
|
||||||
case NetworkParams::TYPE_FORM:{
|
case NetworkParams::TYPE_FORM:{
|
||||||
@ -427,13 +437,12 @@ void FluNetwork::sendRequest(QNetworkAccessManager* manager,QNetworkRequest requ
|
|||||||
QString value;
|
QString value;
|
||||||
for (const auto& each : params->_paramMap.toStdMap())
|
for (const auto& each : params->_paramMap.toStdMap())
|
||||||
{
|
{
|
||||||
value += QString("%1=%2").arg(QString(QUrl::toPercentEncoding(each.first)),QString(QUrl::toPercentEncoding(each.second.toString())));
|
value += QString("%1=%2").arg(each.first,each.second.toString());
|
||||||
value += "&";
|
value += "&";
|
||||||
}
|
}
|
||||||
if(!params->_paramMap.isEmpty()){
|
if(!params->_paramMap.isEmpty()){
|
||||||
value.chop(1);
|
value.chop(1);
|
||||||
}
|
}
|
||||||
qDebug()<<value;
|
|
||||||
QByteArray data = value.toUtf8();
|
QByteArray data = value.toUtf8();
|
||||||
reply = manager->sendCustomRequest(request,verb,data);
|
reply = manager->sendCustomRequest(request,verb,data);
|
||||||
}
|
}
|
||||||
@ -473,6 +482,45 @@ void FluNetwork::sendRequest(QNetworkAccessManager* manager,QNetworkRequest requ
|
|||||||
reply = manager->sendCustomRequest(request,verb);
|
reply = manager->sendCustomRequest(request,verb);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if(isFirst){
|
||||||
|
printRequestStartLog(request,params);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FluNetwork::printRequestStartLog(QNetworkRequest request,NetworkParams* params){
|
||||||
|
if(!_openLog){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
qDebug()<<"<------"<<qUtf8Printable(request.header(QNetworkRequest::UserAgentHeader).toString())<<"Request Start ------>";
|
||||||
|
qDebug()<<qUtf8Printable(QString::fromStdString("<%1>").arg(params->method2String().toUtf8()))<<qUtf8Printable(params->_url);
|
||||||
|
auto contentType = request.header(QNetworkRequest::ContentTypeHeader).toString();
|
||||||
|
if(!contentType.isEmpty()){
|
||||||
|
qDebug()<<QString::fromStdString("<Header> %1=%2").arg("Content-Type",contentType);
|
||||||
|
}
|
||||||
|
for(const QByteArray& header:request.rawHeaderList()){
|
||||||
|
qDebug()<<QString::fromStdString("<Header> %1=%2").arg(header,request.rawHeader(header));
|
||||||
|
}
|
||||||
|
if(!params->_queryMap.isEmpty()){
|
||||||
|
qDebug()<<"<Query>"<<qUtf8Printable(map2String(params->_queryMap));
|
||||||
|
}
|
||||||
|
if(!params->_paramMap.isEmpty()){
|
||||||
|
qDebug()<<"<Param>"<<qUtf8Printable(map2String(params->_paramMap));
|
||||||
|
}
|
||||||
|
if(!params->_fileMap.isEmpty()){
|
||||||
|
qDebug()<<"<File>"<<qUtf8Printable(map2String(params->_fileMap));
|
||||||
|
}
|
||||||
|
if(!params->_body.isEmpty()){
|
||||||
|
qDebug()<<"<Body>"<<qUtf8Printable(params->_body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FluNetwork::printRequestEndLog(QNetworkRequest request,NetworkParams* params,QNetworkReply*& reply,const QString& response){
|
||||||
|
if(!_openLog){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
qDebug()<<"<------"<<qUtf8Printable(request.header(QNetworkRequest::UserAgentHeader).toString())<<"Request End ------>";
|
||||||
|
qDebug()<<qUtf8Printable(QString::fromStdString("<%1>").arg(params->method2String().toUtf8()))<<qUtf8Printable(params->_url);
|
||||||
|
qDebug()<<"<Result>"<<qUtf8Printable(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
void FluNetwork::saveResponse(QString key,QString response){
|
void FluNetwork::saveResponse(QString key,QString response){
|
||||||
@ -486,6 +534,7 @@ void FluNetwork::saveResponse(QString key,QString response){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void FluNetwork::addHeaders(QNetworkRequest* request,const QMap<QString, QVariant>& headers){
|
void FluNetwork::addHeaders(QNetworkRequest* request,const QMap<QString, QVariant>& headers){
|
||||||
|
request->setHeader(QNetworkRequest::UserAgentHeader,QString::fromStdString("Mozilla/5.0 %1/%2").arg(QGuiApplication::applicationName(),QGuiApplication::applicationVersion()));
|
||||||
QMapIterator<QString, QVariant> iter(headers);
|
QMapIterator<QString, QVariant> iter(headers);
|
||||||
while (iter.hasNext())
|
while (iter.hasNext())
|
||||||
{
|
{
|
||||||
@ -509,6 +558,7 @@ FluNetwork::FluNetwork(QObject *parent): QObject{parent}
|
|||||||
{
|
{
|
||||||
timeout(5000);
|
timeout(5000);
|
||||||
retry(3);
|
retry(3);
|
||||||
|
openLog(false);
|
||||||
cacheDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation).append(QDir::separator()).append("network"));
|
cacheDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation).append(QDir::separator()).append("network"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -94,6 +94,7 @@ class FluNetwork : public QObject
|
|||||||
Q_PROPERTY_AUTO(int,timeout)
|
Q_PROPERTY_AUTO(int,timeout)
|
||||||
Q_PROPERTY_AUTO(int,retry)
|
Q_PROPERTY_AUTO(int,retry)
|
||||||
Q_PROPERTY_AUTO(QString,cacheDir)
|
Q_PROPERTY_AUTO(QString,cacheDir)
|
||||||
|
Q_PROPERTY_AUTO(bool,openLog)
|
||||||
QML_NAMED_ELEMENT(FluNetwork)
|
QML_NAMED_ELEMENT(FluNetwork)
|
||||||
QML_SINGLETON
|
QML_SINGLETON
|
||||||
private:
|
private:
|
||||||
@ -123,13 +124,16 @@ public:
|
|||||||
void handle(NetworkParams* params,NetworkCallable* result);
|
void handle(NetworkParams* params,NetworkCallable* result);
|
||||||
void handleDownload(NetworkParams* params,NetworkCallable* result);
|
void handleDownload(NetworkParams* params,NetworkCallable* result);
|
||||||
private:
|
private:
|
||||||
void sendRequest(QNetworkAccessManager* manager,QNetworkRequest request,NetworkParams* params,QNetworkReply*& reply,QPointer<NetworkCallable> callable);
|
void sendRequest(QNetworkAccessManager* manager,QNetworkRequest request,NetworkParams* params,QNetworkReply*& reply,bool isFirst,QPointer<NetworkCallable> callable);
|
||||||
void addQueryParam(QUrl* url,const QMap<QString, QVariant>& params);
|
void addQueryParam(QUrl* url,const QMap<QString, QVariant>& params);
|
||||||
void addHeaders(QNetworkRequest* request,const QMap<QString, QVariant>& headers);
|
void addHeaders(QNetworkRequest* request,const QMap<QString, QVariant>& headers);
|
||||||
void saveResponse(QString key,QString response);
|
void saveResponse(QString key,QString response);
|
||||||
QString readCache(const QString& key);
|
QString readCache(const QString& key);
|
||||||
bool cacheExists(const QString& key);
|
bool cacheExists(const QString& key);
|
||||||
QString getCacheFilePath(const QString& key);
|
QString getCacheFilePath(const QString& key);
|
||||||
|
QString map2String(const QMap<QString, QVariant>& map);
|
||||||
|
void printRequestStartLog(QNetworkRequest request,NetworkParams* params);
|
||||||
|
void printRequestEndLog(QNetworkRequest request,NetworkParams* params,QNetworkReply*& reply,const QString& response);
|
||||||
public:
|
public:
|
||||||
QJSValue _interceptor;
|
QJSValue _interceptor;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user