Files
AccompanyCube/VerifyCodeFormUnit.cpp
2026-06-13 00:10:11 +08:00

143 lines
3.7 KiB
C++

//---------------------------------------------------------------------------
#include <vcl.h>
#include <Vcl.Imaging.pngimage.hpp>
#include <System.NetEncoding.hpp>
#include "GlobalUnit.h"
#pragma hdrstop
#include "VerifyCodeFormUnit.h"
#include "MessageFormUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "PNGButton"
#pragma resource "*.dfm"
TVerifyCodeForm *VerifyCodeForm;
//---------------------------------------------------------------------------
__fastcall TVerifyCodeForm::TVerifyCodeForm(TComponent* Owner, String baseUrl)
: TForm(Owner)
{
for( int i=0; i<ComponentCount; i++ ){
if(Components[i]->ClassNameIs( TLabel::ClassName() ) )
((TLabel*)Components[i])->ControlStyle>>csOpaque;
}
RESTClient->BaseURL = String(baseUrl);
}
//---------------------------------------------------------------------------
void __fastcall TVerifyCodeForm::FormCreate(TObject *Sender)
{
Font->Color = RGB(60,43,23);
//绘制圆角窗体
HRGN hRgn = CreateRoundRectRgn( 0, 0, Width+1, Height+2, 10, 10 );
SetWindowRgn( Handle, hRgn, false );
DeleteObject( hRgn );
SetTransparent( this );
RefreshCode();
}
//---------------------------------------------------------------------------
void __fastcall TVerifyCodeForm::WMNCHitTest(TWMNCHitTest &Msg)
{
TForm::Dispatch(&Msg);
if( Msg.Result==HTCLIENT && Msg.YPos<Top+24 && Msg.XPos<Left+275 )
Msg.Result = HTCAPTION;
}
//---------------------------------------------------------------------------
void __fastcall TVerifyCodeForm::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
switch( Key ){
case VK_RETURN:
OkClick(NULL);
break;
case VK_ESCAPE:
ModalResult = mrCancel;
break;
}
}
//---------------------------------------------------------------------------
void __fastcall TVerifyCodeForm::RefreshCode()
{
this->Enabled = false;
Screen->Cursor = crHourGlass;
RESTRequest->Resource = "captcha";
RESTRequest->Params->Clear();
try{
RESTRequest->Execute();
}
catch (Exception &exception){
DebugPrint("exception: %s", exception.Message );
}
Screen->Cursor = crDefault;
this->Enabled = true;
if(RESTResponse->StatusCode==200){
TJSONValue *jValue = RESTResponse->JSONValue;
if(jValue==NULL)
return;
TJSONValue *jCode = jValue->FindValue("code");
if( jCode==NULL )
return;
String code = ((TJSONString*)jCode)->Value();
if(code=="00000"){
TJSONValue *jData = jValue->FindValue("data");
TJSONValue *jKey = jData->FindValue("captchaKey");
m_sKey = ((TJSONString*)jKey)->Value();
TJSONValue *jBase64 = jData->FindValue("captchaBase64");
String sBase64 = ((TJSONString*)jBase64)->Value();
sBase64 = sBase64.SubString(23,sBase64.Length()-22);
Sysutils::TBytes bytes = TNetEncoding::Base64String->DecodeStringToBytes(sBase64);
TPngImage * png = new TPngImage();
png->LoadFromStream(new TBytesStream(bytes));
m_imgCode->Picture->Assign(png);
delete png;
}
else{
MessageForm->ShowMessage( "获取验证码图片失败,请稍后重试", MB_ICONERROR | MB_OK );
return;
}
}
else{
MessageForm->ShowMessage( "访问服务器异常,请稍后重试", MB_ICONERROR | MB_OK );
return;
}
}
//---------------------------------------------------------------------------
void __fastcall TVerifyCodeForm::CodeImgClick(TObject *Sender)
{
RefreshCode();
}
//---------------------------------------------------------------------------
void __fastcall TVerifyCodeForm::OkClick(TObject *Sender)
{
m_editCode->Text = m_editCode->Text.Trim();
if(m_editCode->Text.IsEmpty()){
m_editCode->SetFocus();
return;
}
m_sCode = m_editCode->Text;
ModalResult = mrOk;
}
//---------------------------------------------------------------------------