完成部分http功能
This commit is contained in:
@@ -16,14 +16,14 @@ void TCHttpService::apiLogin()
|
||||
QString md5Hex = pwdMd5.toHex();
|
||||
|
||||
QString urlStr;
|
||||
// if (m_enableSsl)
|
||||
if (m_enableSsl)
|
||||
urlStr = "https://" + m_domain + "/api/login";
|
||||
// else
|
||||
// urlStr = "http://" + m_domain + "/api/login";
|
||||
else
|
||||
urlStr = "http://" + m_domain + "/api/login";
|
||||
|
||||
QUrl url(urlStr);
|
||||
QJsonObject jsonObj;
|
||||
jsonObj["user"]="test1";
|
||||
jsonObj["user"]=m_userName;
|
||||
jsonObj["pwd"]=md5Hex;
|
||||
QJsonDocument jsonDoc(jsonObj);
|
||||
QByteArray jsonData = jsonDoc.toJson();
|
||||
@@ -31,15 +31,17 @@ void TCHttpService::apiLogin()
|
||||
headers.insert("Content-Type", "application/json");
|
||||
|
||||
QNetworkReply* reply = nullptr;
|
||||
// qDebug() <<url;
|
||||
// QNetworkRequest request(url);
|
||||
// reply = m_manager.post(request, jsonData);
|
||||
|
||||
// QObject::connect(reply, &QNetworkReply::finished, [this, reply]() {
|
||||
QNetworkRequest request(url);
|
||||
for (auto ite = headers.constBegin(); ite != headers.constEnd(); ite++){
|
||||
request.setRawHeader(ite.key().toUtf8(), ite.value().toUtf8());
|
||||
}
|
||||
reply = m_manager.post(request, jsonData);
|
||||
|
||||
// emit requestFinished(reply);
|
||||
// });
|
||||
Post(url, headers, {}, jsonData, 1);
|
||||
QObject::connect(reply, &QNetworkReply::finished, [this, reply]() {
|
||||
|
||||
emit requestFinished(reply, "login");
|
||||
});
|
||||
}
|
||||
|
||||
void TCHttpService::apiMyfileCount()
|
||||
@@ -52,26 +54,27 @@ void TCHttpService::apiMyfileCount()
|
||||
|
||||
QMap<QString, QString> headers;
|
||||
headers.insert("Content-Type", "application/json");
|
||||
QString bodyStr = QString("\"%1\":\"%2\",\"%3\":\"%4\"").arg("token").arg(m_token).arg("user").arg(m_userName);
|
||||
|
||||
QMap<QString, QString> params;
|
||||
params.insert("cmd", "count");
|
||||
// QNetworkReply* reply = Post(urlStr, headers, params, bodyStr.toUtf8());
|
||||
QUrl url = encodeUrl(urlStr, params);
|
||||
QJsonObject jsonObj;
|
||||
jsonObj["token"] = m_token;
|
||||
jsonObj["user"] = m_userName;
|
||||
QJsonDocument jsonDoc(jsonObj);
|
||||
QByteArray jsonData = jsonDoc.toJson();
|
||||
|
||||
// QObject::connect(reply, &QNetworkReply::finished, [this, reply]{
|
||||
QNetworkRequest request(url);
|
||||
for (auto ite = headers.constBegin(); ite != headers.constEnd(); ite++){
|
||||
request.setRawHeader(ite.key().toUtf8(), ite.value().toUtf8());
|
||||
}
|
||||
|
||||
// QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll());
|
||||
// QJsonObject jsonObj = jsonDoc.object();
|
||||
|
||||
// int code = jsonObj["code"].toInt();
|
||||
|
||||
// if (code == 0) {
|
||||
// int total = jsonObj["total"].toInt();
|
||||
// this->m_total = total;
|
||||
// }
|
||||
|
||||
// reply->deleteLater();
|
||||
// });
|
||||
QNetworkReply* reply = nullptr;
|
||||
reply = m_manager.post(request, jsonData);
|
||||
QObject::connect(reply, &QNetworkReply::finished, [this, reply](){
|
||||
|
||||
emit requestFinished(reply, "myfilecount");
|
||||
});
|
||||
}
|
||||
|
||||
void TCHttpService::apiMyfileNormal()
|
||||
@@ -87,28 +90,28 @@ void TCHttpService::apiMyfileNormal()
|
||||
|
||||
QMap<QString, QString> params;
|
||||
params.insert("cmd", "normal");
|
||||
QString bodyStr = QString("\"%1\":\"%2\",\"%3\":\"%4\",\"%5\":\"%6\",\"%7\":\"%8\"").arg("token").arg(m_token).
|
||||
arg("user").arg(m_userName).arg("count").arg(m_total).arg("start").arg(0);
|
||||
// QNetworkReply* reply = Post(QUrl(urlStr), headers, params, bodyStr.toUtf8());
|
||||
|
||||
// QObject::connect(reply, &QNetworkReply::finished, [this, reply]{
|
||||
// QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll());
|
||||
// QJsonObject jsonObj = jsonDoc.object();
|
||||
QUrl url = encodeUrl(urlStr, params);
|
||||
QNetworkRequest request(url);
|
||||
for (auto ite = headers.constBegin(); ite != headers.constEnd(); ite++){
|
||||
request.setRawHeader(ite.key().toUtf8(), ite.value().toUtf8());
|
||||
}
|
||||
|
||||
// int code = jsonObj["code"].toInt();
|
||||
// if (code == 0) {
|
||||
// int count = jsonObj["count"].toInt();
|
||||
// if (count != 0) {
|
||||
// QJsonArray array = jsonObj["files"].toArray();
|
||||
// for (const QJsonValue& value : array) {
|
||||
// QJsonObject item = value.toObject();
|
||||
// updateFileMap(item);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
QJsonObject jsonObj;
|
||||
jsonObj["token"] = m_token;
|
||||
jsonObj["user"] = m_userName;
|
||||
jsonObj["count"] = m_total;
|
||||
jsonObj["start"] = 0;
|
||||
|
||||
// reply->deleteLater();
|
||||
// });
|
||||
QJsonDocument jsonDoc(jsonObj);
|
||||
QByteArray jsonData = jsonDoc.toJson();
|
||||
|
||||
QNetworkReply* reply = nullptr;
|
||||
reply = m_manager.post(request, jsonData);
|
||||
QObject::connect(reply, &QNetworkReply::finished, [this, reply](){
|
||||
|
||||
emit requestFinished(reply, "myfilenormal");
|
||||
});
|
||||
}
|
||||
|
||||
void TCHttpService::apiMd5(const QString& filePath)
|
||||
@@ -137,20 +140,7 @@ void TCHttpService::apiMd5(const QString& filePath)
|
||||
QString bodyStr = QString("\"%1\":\"%2\",\"%3\":\"%4\",\"%5\":\"%6\",\"%7\":\"%8\"").arg("token").arg(m_token).
|
||||
arg("md5").arg(md5Str).arg("filename").arg(QFileInfo(file).fileName()).arg("user").arg(m_userName);
|
||||
file.close();
|
||||
// QNetworkReply* reply = Post(urlStr, headers, {}, bodyStr.toUtf8());
|
||||
|
||||
// QObject::connect(reply, &QNetworkReply::finished, [this, reply, filePath, md5Str]{
|
||||
// QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll());
|
||||
// QJsonObject jsonObj = jsonDoc.object();
|
||||
// int code = jsonObj["code"].toInt();
|
||||
// switch (code) {
|
||||
// case 0:
|
||||
// break;
|
||||
// case 1:
|
||||
// apiUpload(filePath, md5Str);
|
||||
// break;
|
||||
// }
|
||||
// });
|
||||
}
|
||||
|
||||
void TCHttpService::apiUpload(const QString &filePath, const QString &md5Str)
|
||||
@@ -236,18 +226,6 @@ void TCHttpService::apiSharePicShare(const QString &fileName, const QString &md5
|
||||
|
||||
params.insert("cmd", "share");
|
||||
|
||||
// QNetworkReply* reply = Post(QUrl(urlStr), headers, params, bodyStr.toUtf8());
|
||||
|
||||
// QObject::connect(reply, &QNetworkReply::finished, [this, reply]{
|
||||
// QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll());
|
||||
// QJsonObject jsonObj = jsonDoc.object();
|
||||
|
||||
// int code = jsonObj["code"].toInt();
|
||||
// if (code == 0) {
|
||||
// emit signal_shareSuc();
|
||||
// }
|
||||
// reply->deleteLater();
|
||||
// });
|
||||
}
|
||||
|
||||
void TCHttpService::setConfiguration(QString userName, QString firstPwd, QString domain)
|
||||
@@ -256,9 +234,35 @@ void TCHttpService::setConfiguration(QString userName, QString firstPwd, QString
|
||||
this->m_domain = domain;
|
||||
this->m_userName = userName;
|
||||
this->m_firstPwd = firstPwd;
|
||||
connect(this, &TCHttpService::requestFinished, [](QNetworkReply* reply){
|
||||
qDebug() << "readall";
|
||||
qDebug() <<reply->readAll();
|
||||
connect(this, &TCHttpService::requestFinished, [this](QNetworkReply* reply, QString api){
|
||||
QByteArray rawData = reply->readAll();
|
||||
if (api == "login") {
|
||||
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(rawData);
|
||||
if (jsonDoc.isEmpty())
|
||||
return;
|
||||
QJsonObject jsonObj = jsonDoc.object();
|
||||
int code = jsonObj["code"].toInt();
|
||||
if (code == 0) {
|
||||
this->m_token = jsonObj["token"].toString();
|
||||
qDebug() << this->m_token;
|
||||
}
|
||||
}
|
||||
if (api == "myfilecount") {
|
||||
QJsonDocument jsonDoc = QJsonDocument::fromJson(rawData);
|
||||
if (jsonDoc.isEmpty())
|
||||
return;
|
||||
QJsonObject jsonObj = jsonDoc.object();
|
||||
int code = jsonObj["code"].toInt();
|
||||
|
||||
if (code == 0) {
|
||||
int total = jsonObj["total"].toInt();
|
||||
this->m_total = total;
|
||||
}
|
||||
}
|
||||
if (api == "myfilenormal") {
|
||||
|
||||
}
|
||||
reply->deleteLater();
|
||||
});
|
||||
apiLogin();
|
||||
@@ -270,68 +274,19 @@ void TCHttpService::setSsl(bool enable)
|
||||
this->m_enableSsl = enable;
|
||||
}
|
||||
|
||||
QNetworkReply *TCHttpService::Get(const QUrl &url, const QMap<QString, QString> headers, const QMap<QString, QString> params)
|
||||
QUrl TCHttpService::encodeUrl(QString urlStr, QMap<QString, QString> params)
|
||||
{
|
||||
QUrlQuery query;
|
||||
for (auto ite = params.constBegin(); ite != params.constEnd(); ite++) {
|
||||
for(auto ite = params.constBegin(); ite != params.constEnd(); ite++){
|
||||
query.addQueryItem(ite.key(), ite.value());
|
||||
}
|
||||
|
||||
QUrl requestUrl = url;
|
||||
requestUrl.setQuery(query);
|
||||
QUrl url(urlStr);
|
||||
url.setQuery(query);
|
||||
|
||||
QNetworkRequest request(requestUrl);
|
||||
|
||||
for (auto ite = headers.constBegin(); ite != headers.constEnd(); ite++) {
|
||||
request.setRawHeader(ite.key().toUtf8(), ite.value().toUtf8());
|
||||
}
|
||||
|
||||
QNetworkReply* reply = m_manager.get(request);
|
||||
|
||||
return reply;
|
||||
return url;
|
||||
}
|
||||
|
||||
QNetworkReply *TCHttpService::Post(const QUrl &url, const QMap<QString, QString> headers, const QMap<QString, QString> params, const QByteArray body)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
QByteArray TCHttpService::Post(QUrl url, QMap<QString, QString> headers, QMap<QString, QString> params, QByteArray body, int i)
|
||||
{
|
||||
QUrlQuery query;
|
||||
for (auto ite = params.constBegin(); ite != params.constEnd(); ite++) {
|
||||
query.addQueryItem(ite.key(), ite.value());
|
||||
}
|
||||
|
||||
QUrl requestUrl = url;
|
||||
requestUrl.setQuery(query);
|
||||
qDebug() << requestUrl;
|
||||
QNetworkRequest request(url);
|
||||
|
||||
// for (auto ite = headers.constBegin(); ite != headers.constEnd(); ite++) {
|
||||
// request.setRawHeader(ite.key().toUtf8(), ite.value().toUtf8());
|
||||
// }
|
||||
qDebug() << 1;
|
||||
QJsonObject jsonObj;
|
||||
jsonObj["user"]="test1";
|
||||
jsonObj["pwd"]="e10adc3949ba59abbe56e057f20f883e";
|
||||
QJsonDocument jsonDoc(jsonObj);
|
||||
QByteArray jsonData = jsonDoc.toJson();
|
||||
qDebug() << 2;
|
||||
QNetworkReply* reply = nullptr;
|
||||
qDebug() << 3;
|
||||
reply = m_manager.post(request, jsonData);
|
||||
qDebug() << 4;
|
||||
QObject::connect(reply, &QNetworkReply::finished, [this, reply](){
|
||||
// QByteArray respData = reply->readAll();
|
||||
// qDebug() <<respData;
|
||||
// return respData;
|
||||
// reply->deleteLater();
|
||||
qDebug() << "emit requestFinished";
|
||||
// emit requestFinished(reply);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
void TCHttpService::updateFileMap(QJsonObject jsonObj)
|
||||
{
|
||||
|
||||
@@ -32,20 +32,14 @@ public:
|
||||
|
||||
private:
|
||||
|
||||
QNetworkReply* Get(const QUrl& url, const QMap<QString, QString> headers = {},
|
||||
const QMap<QString, QString> params = {});
|
||||
QNetworkReply* Post(const QUrl& url, const QMap<QString, QString> headers = {},
|
||||
const QMap<QString, QString> params = {}, const QByteArray body =QByteArray());
|
||||
|
||||
QByteArray Post(QUrl url, QMap<QString, QString> headers = {},
|
||||
QMap<QString, QString> params = {}, QByteArray body =QByteArray(), int i = 1);
|
||||
QUrl encodeUrl(QString url, QMap<QString, QString> params);
|
||||
void updateFileMap(QJsonObject jsonObj);
|
||||
signals:
|
||||
void signal_loginSuc();
|
||||
void signal_uploadSuc();
|
||||
void signal_shareSuc();
|
||||
|
||||
void requestFinished(QNetworkReply* reply);
|
||||
void requestFinished(QNetworkReply* reply, QString api);
|
||||
private:
|
||||
explicit TCHttpService(QObject *parent = nullptr);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user