diff --git a/example/qml/global/ItemsOriginal.qml b/example/qml/global/ItemsOriginal.qml index f36572eb..f96c8915 100644 --- a/example/qml/global/ItemsOriginal.qml +++ b/example/qml/global/ItemsOriginal.qml @@ -355,6 +355,12 @@ FluObject{ navigationView.push("qrc:/example/qml/page/T_Screenshot.qml") } } + FluPaneItem{ + title:"Captcha" + onTap:{ + navigationView.push("qrc:/example/qml/page/T_Captcha.qml") + } + } FluPaneItem{ title:"Chart" onTap:{ diff --git a/example/qml/page/T_Captcha.qml b/example/qml/page/T_Captcha.qml new file mode 100644 index 00000000..8ac59e75 --- /dev/null +++ b/example/qml/page/T_Captcha.qml @@ -0,0 +1,54 @@ +import QtQuick +import QtQuick.Layouts +import QtQuick.Window +import QtQuick.Controls +import FluentUI +import "qrc:///example/qml/component" + +FluScrollablePage{ + + title:"Captcha" + + + FluCaptcha{ + id:captcha + Layout.topMargin: 20 + MouseArea{ + anchors.fill: parent + cursorShape: Qt.PointingHandCursor + onClicked: { + captcha.refresh() + } + } + } + + FluButton{ + text:"Refresh" + Layout.topMargin: 20 + onClicked: { + captcha.refresh() + } + } + + RowLayout{ + spacing: 10 + Layout.topMargin: 10 + FluTextBox{ + id:text_box + placeholderText: "请输入验证码" + } + FluButton{ + text:"verify" + onClicked: { + var success = captcha.verify(text_box.text) + if(success){ + showSuccess("验证码正确") + }else{ + showError("错误验证,请重新输入") + } + } + } + } + + +} diff --git a/src/FluCaptcha.cpp b/src/FluCaptcha.cpp new file mode 100644 index 00000000..a728323e --- /dev/null +++ b/src/FluCaptcha.cpp @@ -0,0 +1,73 @@ +#include "FluCaptcha.h" +#include +#include +#include +#include +#include + +FluCaptcha::FluCaptcha(QQuickItem *parent) + : QQuickPaintedItem(parent) +{ + font(QFont("楷体",25,QFont::Bold,true)); + setWidth(180); + setHeight(80); + refresh(); +} + +void FluCaptcha::paint(QPainter* painter) +{ + painter->save(); + painter->fillRect(boundingRect().toRect(),QColor(255,255,255,255)); + QPen pen; + painter->setFont(_font); + for(int i=0;i<100;i++) + { + pen = QPen(QColor(_generaNumber(256),_generaNumber(256),_generaNumber(256))); + painter->setPen(pen); + painter->drawPoint(_generaNumber(180),_generaNumber(80)); + } + for(int i=0;i<5;i++) + { + pen = QPen(QColor(_generaNumber(256),_generaNumber(256),_generaNumber(256))); + painter->setPen(pen); + painter->drawLine(_generaNumber(180),_generaNumber(80),_generaNumber(180),_generaNumber(80)); + } + for(int i=0;i<4;i++) + { + pen = QPen(QColor(_generaNumber(255),_generaNumber(255),_generaNumber(255))); + painter->setPen(pen); + painter->drawText(15+35*i,10+_generaNumber(15),30,40,Qt::AlignCenter, QString(_code[i])); + } + painter->restore(); +} + +int FluCaptcha::_generaNumber(int number){ + return QRandomGenerator::global()->bounded(0,number); +} + +void FluCaptcha::refresh(){ + this->_code.clear(); + for(int i = 0;i < 4;++i) + { + int num = _generaNumber(3); + if(num == 0) + { + this->_code += QString::number(_generaNumber(10)); + } + else if(num == 1) + { + int temp = 'A'; + this->_code += static_cast(temp + _generaNumber(26)); + } + else if(num == 2) + { + int temp = 'a'; + this->_code += static_cast(temp + _generaNumber(26)); + } + } + update(); +} + +bool FluCaptcha::verify(const QString& code){ + return this->_code == code; +} diff --git a/src/FluCaptcha.h b/src/FluCaptcha.h new file mode 100644 index 00000000..a309d79d --- /dev/null +++ b/src/FluCaptcha.h @@ -0,0 +1,24 @@ +#ifndef FLUCAPTCHA_H +#define FLUCAPTCHA_H + +#include +#include +#include +#include "stdafx.h" + +class FluCaptcha : public QQuickPaintedItem +{ + Q_OBJECT + Q_PROPERTY_AUTO(QFont,font); + QML_NAMED_ELEMENT(FluCaptcha) +private: + int _generaNumber(int number); + QString _code; +public: + explicit FluCaptcha(QQuickItem *parent = nullptr); + void paint(QPainter* painter) override; + Q_INVOKABLE void refresh(); + Q_INVOKABLE bool verify(const QString& code); +}; + +#endif // FLUCAPTCHA_H