Skip to content

Commit

Permalink
refeator: 封装cookie属性
Browse files Browse the repository at this point in the history
  • Loading branch information
s12mmm3 committed Dec 28, 2024
1 parent 77b405d commit 83e703f
Showing 2 changed files with 35 additions and 33 deletions.
26 changes: 10 additions & 16 deletions QCloudMusicApi/apihelper.cpp
Original file line number Diff line number Diff line change
@@ -21,18 +21,20 @@ ApiHelper::ApiHelper(QObject* parent)

void ApiHelper::beforeInvoke(QVariantMap& arg)
{
QVariantMap arg_cookie_map = Index::cookieToJson(cookie());
// Api只能处理map类型的cookie
if (arg.contains("cookie")) {
// 若传入新的cookie,替换原有的cookie
if (arg["cookie"].userType() == QMetaType::QVariantMap) {
m_cookie = arg["cookie"].toMap();
arg_cookie_map = arg["cookie"].toMap();
}
else if (arg["cookie"].userType() == QMetaType::QString) {
m_cookie = Index::cookieToJson(arg["cookie"].toString());
arg_cookie_map = Index::cookieToJson(arg["cookie"].toString());
}
set_cookie(Index::cookieObjToString(arg_cookie_map));
}
// 使用存储的cookie
arg["cookie"] = m_cookie;
// 使用存储的cookie map
arg["cookie"] = arg_cookie_map;

// 设置全局代理
if (!proxy().isEmpty() && !arg.contains("proxy")) {
@@ -47,14 +49,16 @@ void ApiHelper::beforeInvoke(QVariantMap& arg)

void ApiHelper::afterInvoke(QVariantMap& ret)
{
QVariantMap arg_cookie_map = Index::cookieToJson(cookie());
auto newCookie = Index::cookieToJson(ret.value("cookie").toString());
if (!newCookie.isEmpty()) {
m_cookie = Index::mergeMap(m_cookie, newCookie);
arg_cookie_map = Index::mergeMap(arg_cookie_map, newCookie);
}
auto token = ret.value("body").toMap()["token"].toString();
if (!token.isEmpty()) {
m_cookie["MUSIC_A"] = token;
arg_cookie_map["MUSIC_A"] = token;
}
set_cookie(Index::cookieObjToString(arg_cookie_map));
}

QVariantMap ApiHelper::invoke(QString member, QVariantMap arg)
@@ -115,16 +119,6 @@ QStringList ApiHelper::memberList()
return memberList;
}

void ApiHelper::set_cookie(QString cookie)
{
m_cookie = Index::cookieToJson(cookie);
}

QString ApiHelper::cookie()
{
return Index::cookieObjToString(m_cookie);
}

void ApiHelper::setFilterRules(const QString& rules)
{
QLoggingCategory::setFilterRules(rules);
42 changes: 25 additions & 17 deletions QCloudMusicApi/apihelper.h
Original file line number Diff line number Diff line change
@@ -16,6 +16,31 @@ class QCloudMusicApiPlugin;
class QCLOUDMUSICAPI_EXPORT ApiHelper : public NeteaseCloudMusicApi
{
Q_OBJECT
public:
/**
* @brief 封装了读取/修改方法
* proxy(): 获取代理
* set_proxy(proxy): 设置代理
* 可直接通过元对象系统修改/访问属性,不要直接修改私有属性
*/
DEFINE_VALUE(QString, proxy, "");

/**
* @brief 封装了读取/修改方法
* realIP(): 获取realIP
* set_realIP(realIP): 设置realIP
* 可直接通过元对象系统修改/访问属性,不要直接修改私有属性
*/
DEFINE_VALUE(QString, realIP, "");

/**
* @brief 封装了读取/修改方法
* cookie(): cookie
* set_cookie(cookie): 设置cookie
* 可直接通过元对象系统修改/访问属性,不要直接修改私有属性
*/
DEFINE_VALUE(QString, cookie, "");

public:
explicit ApiHelper(QObject* parent = nullptr);

@@ -34,18 +59,6 @@ class QCLOUDMUSICAPI_EXPORT ApiHelper : public NeteaseCloudMusicApi
*/
Q_INVOKABLE QVariantMap invokeUrl(QString url);

/**
* @brief 设置cookie
* @param cookie 用于API调用的cookie字符串
*/
Q_INVOKABLE void set_cookie(QString cookie);

/**
* @brief 获取cookie
* @return cookie
*/
Q_INVOKABLE QString cookie();

/**
* @brief 获取API列表
* @return API列表
@@ -86,16 +99,11 @@ class QCLOUDMUSICAPI_EXPORT ApiHelper : public NeteaseCloudMusicApi
*/
Q_INVOKABLE bool unloadPlugin(QCloudMusicApiPlugin* plugin);

public:
DEFINE_VALUE(QString, proxy, "")
DEFINE_VALUE(QString, realIP, "")

private:
void beforeInvoke(QVariantMap& arg);
void afterInvoke(QVariantMap& ret);

private:
QVariantMap m_cookie;
QStringList m_memberList;
QList<ApiPluginImpl*> m_pluginImpls;
};

0 comments on commit 83e703f

Please sign in to comment.