Skip to content

Commit

Permalink
feat: 增加二维码登录测试
Browse files Browse the repository at this point in the history
  • Loading branch information
s12mmm3 committed Feb 26, 2024
1 parent 6a8ef68 commit b5c8f95
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "QCloudMusicApi/openssl-cmake"]
path = QCloudMusicApi/openssl-cmake
url = https://github.com/janbar/openssl-cmake.git
[submodule "Test/libqrencode"]
path = Test/libqrencode
url = https://github.com/fukuchi/libqrencode.git
22 changes: 22 additions & 0 deletions ApiServer/tabcommon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef TABCOMMON_H
#define TABCOMMON_H

#include <QWidget>

namespace Ui {
class TabCommon;
}

class TabCommon : public QWidget
{
Q_OBJECT

public:
explicit TabCommon(QWidget *parent = nullptr);
~TabCommon();

private:
Ui::TabCommon *ui;
};

#endif // TABCOMMON_H
13 changes: 9 additions & 4 deletions Test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,25 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets Concurrent)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets Network Concurrent)

set(WITH_TOOLS OFF)
add_subdirectory(
${CMAKE_CURRENT_SOURCE_DIR}/libqrencode
)

FILE(GLOB HEADERS ./*.h)
FILE(GLOB SOURCES ./*.cpp)
FILE(GLOB UIS ./*.ui)
FILE(GLOB QRCS ./*.qrc)
FILE(GLOB COMMONWIDGET ./commonwidget/*)
FILE(GLOB CUSTOMWIDGET ./customwidget/*)
set(PROJECT_SOURCES
${HEADERS}
${SOURCES}
${UIS}
${QRCS}
${COMMONWIDGET}
${CUSTOMWIDGET}
)

include_directories (./commonwidget)
include_directories (./customwidget)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(${PROJECT_NAME}
Expand All @@ -50,7 +55,7 @@ else()
endif()
endif()

target_link_libraries(${PROJECT_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Network Qt${QT_VERSION_MAJOR}::Concurrent QCloudMusicApi)
target_link_libraries(${PROJECT_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Network Qt${QT_VERSION_MAJOR}::Concurrent QCloudMusicApi qrencode)

# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1.
# If you are developing for iOS or macOS you should consider setting an
Expand Down
95 changes: 95 additions & 0 deletions Test/customwidget/tablogin_qr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include "tablogin_qr.h"
#include "ui_tablogin_qr.h"

#include "../../QCloudMusicApi/module.h"

#include "../libqrencode/qrencode.h"

#include <QJsonDocument>
#include <QLabel>
#include <QPainter>

TabLogin_qr::TabLogin_qr(QWidget *parent) :
QWidget(parent),
ui(new Ui::TabLogin_qr)
{
ui->setupUi(this);
}

TabLogin_qr::~TabLogin_qr()
{
delete ui;
}
QPixmap TabLogin_qr::generateQRCode(QString strUrl, qint32 temp_width, qint32 temp_height, int offset) {
QPixmap mainmap;
if (strUrl.isEmpty()) {
return mainmap;
}
// 调用 qrencode 的 API,生成 QR 码的位图数组
QRcode *qrcode = QRcode_encodeString(strUrl.toStdString().c_str(), 2, QR_ECLEVEL_Q, QR_MODE_8, 1);
// 获取 QR 码的宽度
qint32 qrcode_width = qrcode->width > 0 ? qrcode->width : 1;
// 计算缩放比例
double scale_x = (double)temp_width / (double)qrcode_width;
double scale_y = (double)temp_height / (double)qrcode_width;

QImage mainimg = QImage(temp_width + offset * 2, temp_height + offset * 2, QImage::Format_ARGB32);
QPainter painter(&mainimg);
// 设置背景色为白色
QColor background(Qt::white);
painter.setBrush(background);
painter.setPen(Qt::NoPen);
painter.drawRect(offset, offset, temp_width, temp_height);
// 设置前景色为黑色
QColor foreground(Qt::black);
painter.setBrush(foreground);
// 遍历 QR 码的位图数组,绘制黑色的小方块
for (qint32 y = 0; y < qrcode_width; y++) {
for (qint32 x = 0; x < qrcode_width; x++) {
unsigned char b = qrcode->data[y * qrcode_width + x];
if (b & 0x01) {
QRectF r(offset + x * scale_x, offset + y * scale_y, scale_x, scale_y);
painter.drawRects(&r, 1);
}
}
}
mainmap = QPixmap::fromImage(mainimg);
return mainmap;
}

QVariantMap TabLogin_qr::invoke(const QString funName, const QVariantMap arg) {
NeteaseCloudMusicApi api;
QVariantMap ret;
QMetaObject::invokeMethod(&api, funName.toUtf8()
, Qt::DirectConnection
, Q_RETURN_ARG(QVariantMap, ret)
, Q_ARG(QVariantMap, arg));
return ret;
}

void TabLogin_qr::on_pushButton_login_qr_create_clicked()
{
QVariantMap ret = invoke("login_qr_key", {});
unikey = ret["body"].toMap()["data"].toMap()["unikey"].toString();
if (unikey.isEmpty()) {
return;
}
ret = invoke("login_qr_create",
{
{ "key", unikey }
});
QString qrurl = ret["body"].toMap()["data"].toMap()["qrurl"].toString();
int size = qMin(ui->label->width(), ui->label->height());
ui->label->setPixmap(generateQRCode(qrurl, size, size, 0));
}


void TabLogin_qr::on_pushButton_login_qr_check_clicked()
{
QVariantMap ret = invoke("login_qr_check",
{
{ "key", unikey }
});
ui->textEdit_ret->setText(QJsonDocument::fromVariant(ret).toJson(QJsonDocument::Indented));
}

42 changes: 42 additions & 0 deletions Test/customwidget/tablogin_qr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef TABLOGIN_QR_H
#define TABLOGIN_QR_H

#include <QWidget>

QT_BEGIN_NAMESPACE
namespace Ui {
class TabLogin_qr;
}
QT_END_NAMESPACE

class TabLogin_qr : public QWidget
{
Q_OBJECT

public:
explicit TabLogin_qr(QWidget *parent = nullptr);
~TabLogin_qr();

private:

/**
* @brief 反射调用API中的方法
* @param funName 函数名称
* @param arg 参数
* @return QVariantMap 返回的数据
*/
QVariantMap invoke(const QString funName, const QVariantMap arg);

QPixmap generateQRCode(QString strUrl, qint32 temp_width, qint32 temp_height, int offset);

private slots:
void on_pushButton_login_qr_create_clicked();

void on_pushButton_login_qr_check_clicked();

private:
Ui::TabLogin_qr *ui;
QString unikey;
};

#endif // TABLOGIN_QR_H
70 changes: 70 additions & 0 deletions Test/customwidget/tablogin_qr.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>TabLogin_qr</class>
<widget class="QWidget" name="TabLogin_qr">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>399</width>
<height>365</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Result</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QTextEdit" name="textEdit_ret"/>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Arg</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>QR Code</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_login_qr_check">
<property name="text">
<string>login_qr_check</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton_login_qr_create">
<property name="text">
<string>login_qr_key login_qr_create</string>
</property>
<property name="autoRepeat">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
10 changes: 10 additions & 0 deletions Test/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
<string>login_cellphone</string>
</attribute>
</widget>
<widget class="TabLogin_qr" name="tabLogin_qr">
<attribute name="title">
<string>login_qr</string>
</attribute>
</widget>
</widget>
</item>
</layout>
Expand Down Expand Up @@ -98,6 +103,11 @@
<extends>QWidget</extends>
<header>tabLogin_cellphone.h</header>
</customwidget>
<customwidget>
<class>TabLogin_qr</class>
<extends>QWidget</extends>
<header>tabLogin_qr.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
Expand Down

0 comments on commit b5c8f95

Please sign in to comment.