Skip to content

Commit

Permalink
Merge pull request #23 from s12mmm3/dev_apiHelper
Browse files Browse the repository at this point in the history
Dev api helper
  • Loading branch information
s12mmm3 authored Mar 30, 2024
2 parents 0b6289f + 347128c commit 59e5575
Show file tree
Hide file tree
Showing 21 changed files with 301 additions and 163 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- qt_version: 6.2.4
qt_arch: clang_64
# 6.6.0
- qt_version: 6.6.1
- qt_version: 6.6.2
qt_arch: clang_64
modules: 'qthttpserver qtwebsockets'
env:
Expand All @@ -34,7 +34,7 @@ jobs:
assume: --release
qt_target: 'desktop'
qt_host: 'mac'
archiveName: 'QCloudMusicApi-${{ github.ref_name }}-${{ matrix.qt_version }}-${{ matrix.qt_arch }}'
archiveName: 'QCloudMusicApi-${{ matrix.qt_version }}-${{ matrix.qt_arch }}'
# 步骤
steps:
# 安装Qt
Expand All @@ -61,7 +61,7 @@ jobs:
- name: CMake Build
id: build
run: |
cmake -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} -DCMAKE_INSTALL_PREFIX=${{ github.workspace }} -B ${{ env.BUILD_PATH }}
cmake -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} -DCMAKE_INSTALL_PREFIX=${{ env.BUILD_PATH }}/${{ env.BUILD_TYPE }} -B ${{ env.BUILD_PATH }}
ls
# CMake Install
- name: CMake Install
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- qt_version: 6.2.4
qt_arch: gcc_64
# 6.6.0
- qt_version: 6.6.1
- qt_version: 6.6.2
qt_arch: gcc_64
modules: 'qthttpserver qtwebsockets'
env:
Expand All @@ -34,7 +34,7 @@ jobs:
assume: --release
qt_target: 'desktop'
qt_host: 'linux'
archiveName: 'QCloudMusicApi-${{ github.ref_name }}-${{ matrix.qt_version }}-${{ matrix.qt_arch }}'
archiveName: 'QCloudMusicApi-${{ matrix.qt_version }}-${{ matrix.qt_arch }}'
# 步骤
steps:
# 安装Qt
Expand All @@ -61,7 +61,7 @@ jobs:
- name: CMake Build
id: build
run: |
cmake -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} -DCMAKE_INSTALL_PREFIX=${{ github.workspace }} -B ${{ env.BUILD_PATH }}
cmake -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} -DCMAKE_INSTALL_PREFIX=${{ env.BUILD_PATH }}/${{ env.BUILD_TYPE }} -B ${{ env.BUILD_PATH }}
ls
# CMake Install
Expand Down
25 changes: 13 additions & 12 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
qt_arch: win64_msvc2019_64
arch: x64
# 6.6.0
- qt_version: 6.6.0
- qt_version: 6.6.2
qt_arch: win64_msvc2019_64
arch: x64
modules: 'qthttpserver qtwebsockets'
Expand All @@ -46,7 +46,7 @@ jobs:
qt_target: 'desktop'
qt_host: 'windows'
# 压缩包名称
archiveName: 'QCloudMusicApi-${{ github.ref_name }}-${{ matrix.qt_version }}-${{ matrix.qt_arch }}.zip'
archiveName: 'QCloudMusicApi-${{ matrix.qt_version }}-${{ matrix.qt_arch }}'
# 步骤
steps:
# 安装Qt
Expand All @@ -73,7 +73,7 @@ jobs:
id: build
shell: cmd
run: |
cmake -A ${{ matrix.arch }} -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} -DCMAKE_INSTALL_PREFIX=${{ github.workspace }} -B ${{ env.BUILD_PATH }}
cmake -A ${{ matrix.arch }} -DCMAKE_BUILD_TYPE=${{ env.BUILD_TYPE }} -DCMAKE_INSTALL_PREFIX=${{ env.BUILD_PATH }}/${{ env.BUILD_TYPE }} -B ${{ env.BUILD_PATH }}
ls
# CMake Install
- name: CMake Install
Expand All @@ -88,19 +88,20 @@ jobs:
- name: Package
id: package
shell: pwsh
env:
path: ${{ github.workspace }}/${{ env.BUILD_PATH }}/${{ env.BUILD_TYPE }}
continue-on-error: true
run: |
cd ${{ github.workspace }}/${{ env.BUILD_PATH }}
ls
Tree ${{ env.BUILD_TYPE }} /F
# Tree ${{ env.path }} /F
# 拷贝依赖
windeployqt ${{ env.assume }} --qmldir . --no-translations --compiler-runtime ${{ env.BUILD_TYPE }}\bin\QCloudMusicApi.dll
windeployqt ${{ env.assume }} --qmldir . --no-translations --compiler-runtime ${{ env.BUILD_TYPE }}\bin\Test.exe
windeployqt ${{ env.assume }} --qmldir . --no-translations --compiler-runtime ${{ env.BUILD_TYPE }}\bin\ApiServer.exe
windeployqt ${{ env.assume }} --qmldir . --no-translations --compiler-runtime ${{ env.path }}/bin/QCloudMusicApi.dll
windeployqt ${{ env.assume }} --qmldir . --no-translations --compiler-runtime ${{ env.path }}/bin/Test.exe
windeployqt ${{ env.assume }} --qmldir . --no-translations --compiler-runtime ${{ env.path }}/bin/ApiServer.exe
# 打包zip
Compress-Archive -Path ${{ env.BUILD_TYPE }}\* ${{ env.archiveName }}
Tree ${{ env.BUILD_TYPE }} /F
ls
Compress-Archive -Path ${{ env.path }}/* ${{ env.archiveName }}
# Tree ${{ env.path }} /F
# tag 查询github-Release
# 上传artifacts
- uses: actions/upload-artifact@v4
Expand Down
5 changes: 1 addition & 4 deletions QCloudMusicApi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ if(BUILD_SHARED_LIBS)
target_compile_definitions(${PROJECT_NAME} PRIVATE BUILD_SHARED_LIBS)
endif()

set(PUBLIC_HEADERS
module.h
)

if(MUSICAPI_SETOUTPUTPATH)
FILE(GLOB PUBLIC_HEADERS ./*.h)
install(FILES ${PUBLIC_HEADERS} DESTINATION ${CMAKE_HEADER_OUTPUT_DIRECTORY})
endif()
82 changes: 82 additions & 0 deletions QCloudMusicApi/apihelper.cpp
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));
}
33 changes: 33 additions & 0 deletions QCloudMusicApi/apihelper.h
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
53 changes: 53 additions & 0 deletions QCloudMusicApi/global.h
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
21 changes: 2 additions & 19 deletions QCloudMusicApi/module.h
Original file line number Diff line number Diff line change
@@ -1,26 +1,9 @@
#ifndef MODULE_H
#define MODULE_H

#include <QVariantMap>
#include "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
#include <QVariantMap>

class QCLOUDMUSICAPI_EXPORT NeteaseCloudMusicApi: public QObject {
Q_OBJECT
Expand Down
12 changes: 12 additions & 0 deletions QCloudMusicApi/util/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ extern "C" {
}

using namespace QCloudMusicApiProject;

const QString Crypto::iv = QStringLiteral("0102030405060708");
const QString Crypto::presetKey = QStringLiteral("0CoJUm6Qyw8W8jud");
const QString Crypto::linuxapiKey = QStringLiteral("rFgB&h#%2?^eDg:Q");
const QString Crypto::base62 = QStringLiteral("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
const QString Crypto::publicKey = QStringLiteral(
"-----BEGIN PUBLIC KEY-----\n"
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDgtQn2JZ34ZC28NWYpAUd98iZ37BUrX/aKzmFbt7clFSs6sXqHauqKWqdtLkF2KexO40H1YTX8z2lSgBBOAxLsvaklV8k4cBFK9snQXE9/DDaFt6Rr7iVZMldczhC0JNgTz+SHXT6CBHuX3e9SdB1Ua44oncaTWz7OBGLbCiK45wIDAQAB"
"\n-----END PUBLIC KEY-----"
);

const QString Crypto::eapiKey = QStringLiteral("e82ckenh8dichen8");
/**
* @brief 使用AES算法加密数据的函数
* @param plainText 明文数据
Expand Down
18 changes: 7 additions & 11 deletions QCloudMusicApi/util/crypto.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
#ifndef CRYPTO_H
#define CRYPTO_H

#include "util_global.h"
#include "../global.h"

#include <QByteArray>
#include <QVariantMap>

namespace QCloudMusicApiProject {
class QCLOUDMUSICAPI_EXPORT Crypto {
public:
inline const QString static iv = QStringLiteral("0102030405060708");
inline const QString static presetKey = QStringLiteral("0CoJUm6Qyw8W8jud");
inline const QString static linuxapiKey = QStringLiteral("rFgB&h#%2?^eDg:Q");
inline const QString static base62 = QStringLiteral("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
inline const QString static publicKey = QStringLiteral(
"-----BEGIN PUBLIC KEY-----\n"
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDgtQn2JZ34ZC28NWYpAUd98iZ37BUrX/aKzmFbt7clFSs6sXqHauqKWqdtLkF2KexO40H1YTX8z2lSgBBOAxLsvaklV8k4cBFK9snQXE9/DDaFt6Rr7iVZMldczhC0JNgTz+SHXT6CBHuX3e9SdB1Ua44oncaTWz7OBGLbCiK45wIDAQAB"
"\n-----END PUBLIC KEY-----"
);
inline const QString static eapiKey = QStringLiteral("e82ckenh8dichen8");
static const QString iv;
static const QString presetKey;
static const QString linuxapiKey;
static const QString base62;
static const QString publicKey;
static const QString eapiKey;

static QByteArray aesEncrypt (const QByteArray &plainText, const QString mode, const QByteArray &key, const QByteArray &iv, QString format = "base64");

Expand Down
2 changes: 1 addition & 1 deletion QCloudMusicApi/util/index.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef INDEX_H
#define INDEX_H

#include "util_global.h"
#include "../global.h"

#include <QNetworkCookie>
#include <QVariantMap>
Expand Down
2 changes: 1 addition & 1 deletion QCloudMusicApi/util/request.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <QNetworkAccessManager>
#include <QVariantMap>

#include "util_global.h"
#include "../global.h"

namespace QCloudMusicApiProject {
class QCLOUDMUSICAPI_EXPORT Request {
Expand Down
Loading

0 comments on commit 59e5575

Please sign in to comment.