-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from s12mmm3/dev_apiHelper
Dev api helper
- Loading branch information
Showing
21 changed files
with
301 additions
and
163 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#include "apihelper.h" | ||
#include "util/index.h" | ||
|
||
#include <functional> | ||
#include <QMetaMethod> | ||
|
||
using namespace QCloudMusicApiProject; | ||
ApiHelper::ApiHelper(QObject *parent) | ||
: QObject{parent} | ||
{} | ||
|
||
void ApiHelper::beforeInvoke(QVariantMap& arg) | ||
{ | ||
//Api只能处理map类型的cookie | ||
if(arg.contains("cookie")) { | ||
//如果传入新的cookie,替换原有的cookie | ||
if(arg["cookie"].userType() == QMetaType::QVariantMap) { | ||
set_cookie(arg["cookie"].toMap()); | ||
} | ||
else if(arg["cookie"].userType() == QMetaType::QString) { | ||
set_cookie(arg["cookie"].toString()); | ||
} | ||
} | ||
else { | ||
//使用存储的cookie | ||
arg["cookie"] = cookie(); | ||
} | ||
} | ||
|
||
void ApiHelper::afterInvoke(QVariantMap& ret) | ||
{ | ||
auto newCookie = Index::stringToMap(ret["cookie"].toString()); | ||
if (!newCookie.isEmpty()) { | ||
set_cookie(Index::mergeMap(cookie(), newCookie)); | ||
} | ||
auto token = ret["body"].toMap()["token"].toString(); | ||
if (!token.isEmpty()) { | ||
m_cookie["MUSIC_A"] = token; | ||
} | ||
} | ||
|
||
QVariantMap ApiHelper::invoke(QString member, QVariantMap arg) | ||
{ | ||
beforeInvoke(arg); | ||
|
||
QVariantMap ret; | ||
QMetaObject::invokeMethod(&api, member.toUtf8() | ||
, Qt::DirectConnection | ||
, Q_RETURN_ARG(QVariantMap, ret) | ||
, Q_ARG(QVariantMap, arg)); | ||
|
||
afterInvoke(ret); | ||
|
||
return ret; | ||
} | ||
|
||
QVariantMap ApiHelper::invoke(QVariantMap (NeteaseCloudMusicApi::*member)(QVariantMap), QVariantMap arg) | ||
{ | ||
beforeInvoke(arg); | ||
|
||
auto bind = std::bind(member, &api, arg); | ||
QVariantMap ret = bind(); | ||
|
||
afterInvoke(ret); | ||
|
||
return ret; | ||
} | ||
|
||
QStringList ApiHelper::memberList() | ||
{ | ||
QStringList memberList; | ||
NeteaseCloudMusicApi api; | ||
for(int i = QObject().metaObject()->methodCount(); i < api.metaObject()->methodCount(); i++) { | ||
memberList.push_back(api.metaObject()->method(i).name()); | ||
} | ||
return memberList; | ||
} | ||
|
||
void ApiHelper::set_cookie(QString cookie) | ||
{ | ||
set_cookie(Index::stringToMap(cookie)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#ifndef APIHELPER_H | ||
#define APIHELPER_H | ||
|
||
#include "module.h" | ||
|
||
#include <QObject> | ||
|
||
class QCLOUDMUSICAPI_EXPORT ApiHelper : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit ApiHelper(QObject *parent = nullptr); | ||
|
||
QVariantMap invoke(QString member, QVariantMap arg); | ||
|
||
QVariantMap invoke(QVariantMap (NeteaseCloudMusicApi::* member)(QVariantMap), QVariantMap arg); | ||
|
||
void set_cookie(QString cookie); | ||
|
||
private: | ||
void beforeInvoke(QVariantMap& arg); | ||
void afterInvoke(QVariantMap& ret); | ||
|
||
private: | ||
NeteaseCloudMusicApi api; | ||
|
||
DEFINE_VALUE(QVariantMap, cookie, {}) | ||
|
||
public: | ||
static QStringList memberList(); | ||
}; | ||
|
||
#endif // APIHELPER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#ifndef GLOBAL_H | ||
#define GLOBAL_H | ||
|
||
#if defined(_MSC_VER) || defined(WIN64) || defined(_WIN64) || defined(__WIN64__) || defined(WIN32) \ | ||
|| defined(_WIN32) || defined(__WIN32__) || defined(__NT__) | ||
#define Q_DECL_EXPORT __declspec(dllexport) | ||
#define Q_DECL_IMPORT __declspec(dllimport) | ||
#else | ||
#define Q_DECL_EXPORT __attribute__((visibility("default"))) | ||
#define Q_DECL_IMPORT __attribute__((visibility("default"))) | ||
#endif | ||
|
||
#if defined(BUILD_SHARED_LIBS) | ||
#if defined(QCLOUDMUSICAPI_LIBRARY) | ||
# define QCLOUDMUSICAPI_EXPORT Q_DECL_EXPORT | ||
#else | ||
# define QCLOUDMUSICAPI_EXPORT Q_DECL_IMPORT | ||
#endif | ||
#else | ||
# define QCLOUDMUSICAPI_EXPORT | ||
#endif | ||
|
||
|
||
//读函数名 | ||
#define READ_NAME(valueName) valueName | ||
|
||
//写函数名 | ||
#define WRITE_NAME(valueName) \ | ||
set_##valueName | ||
|
||
//变化函数名 | ||
#define NOTIFY_NAME(valueName) \ | ||
valueName##Changed | ||
|
||
//定义变量 | ||
#define DEFINE_VALUE(type, valueName, defaultValue) \ | ||
Q_PROPERTY(type valueName READ READ_NAME(valueName) WRITE WRITE_NAME(valueName) NOTIFY NOTIFY_NAME(valueName)) \ | ||
public: \ | ||
type READ_NAME(valueName)() const { return m_##valueName; } \ | ||
void WRITE_NAME(valueName)(type valueName) { \ | ||
if(valueName == m_##valueName) { \ | ||
return; \ | ||
} \ | ||
m_##valueName = valueName; \ | ||
emit NOTIFY_NAME(valueName)(); \ | ||
} \ | ||
Q_SIGNALS: \ | ||
void NOTIFY_NAME(valueName)(); \ | ||
private: \ | ||
type m_##valueName = defaultValue; \ | ||
public: \ | ||
|
||
#endif // GLOBAL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.