559 lines
14 KiB
C++
559 lines
14 KiB
C++
//---------------------------------------------------------------------------
|
|
#include <vcl.h>
|
|
#include <pcre.h>
|
|
#pragma hdrstop
|
|
|
|
#include "GlobalUnit.h"
|
|
#include "LoginFormUnit.h"
|
|
#include "MessageFormUnit.h"
|
|
#include "VerifyCodeFormUnit.h"
|
|
//---------------------------------------------------------------------------
|
|
#pragma package(smart_init)
|
|
#pragma link "PNGButton"
|
|
#pragma resource "*.dfm"
|
|
TLoginForm *LoginForm;
|
|
|
|
const char * g_pPattern[5] = { "^[_a-zA-Z0-9\\xA1-\\xFE\\.-]{3,16}$",
|
|
"^[_a-zA-Z0-9\\.-]+@([_a-zA-Z0-9]+\\.)+[a-zA-Z0-9]{2,3}$",
|
|
"^[\\x21-\\x7E]{3,16}$",
|
|
"^[\\x21-\\x7E]{6,16}$",
|
|
"^$|^1(3\\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\\d|9[0-35-9])\\d{8}$"
|
|
};
|
|
//---------------------------------------------------------------------------
|
|
__fastcall TLoginForm::TLoginForm(TComponent* Owner)
|
|
: TForm(Owner)
|
|
{
|
|
m_bRegister = false;
|
|
m_bAgree = true;
|
|
|
|
isEmailOrMobile = 0;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::FormCreate(TObject *Sender)
|
|
{
|
|
m_pImgCheck = new TBitmap();
|
|
m_pImgCheck->LoadFromResourceName(int(HInstance), L"AutoLoginCheck");
|
|
|
|
//绘制圆角窗体
|
|
HRGN hRgn = CreateRoundRectRgn( 0, 0, Width+1, Height+2, 10, 10 );
|
|
SetWindowRgn( Handle, hRgn, false );
|
|
DeleteObject( hRgn );
|
|
|
|
char strURL[256];
|
|
f_CS_GetWebPageURL( URL_REGCODE, strURL );
|
|
RESTClient->BaseURL = String(strURL);
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::FormDestroy(TObject *Sender)
|
|
{
|
|
if( m_pImgCheck )
|
|
delete m_pImgCheck;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::AutoCheckPaint(TObject *Sender)
|
|
{
|
|
//绘制Check状态
|
|
int x = 0;
|
|
if( !m_bRegister && !m_bAutoLog ){
|
|
x = -m_pImgCheck->Width/2;
|
|
}
|
|
|
|
m_pbAutoCheck->Canvas->Draw(x, 0, m_pImgCheck);
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::AutoCheckClick(TObject *Sender)
|
|
{
|
|
//切换Check状态
|
|
if( !m_bRegister ){
|
|
g_bAutoLogin = m_bAutoLog = !m_bAutoLog;
|
|
m_pbAutoCheck->Invalidate();
|
|
}
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::GoClick(TObject *Sender)
|
|
{
|
|
if( !m_bRegister ){
|
|
//检查输入
|
|
m_editLogUsername->Text = m_editLogUsername->Text.Trim();
|
|
m_editLogPassword->Text = m_editLogPassword->Text.Trim();
|
|
|
|
if( m_editLogUsername->Text.IsEmpty() ){
|
|
m_editLogUsername->SetFocus();
|
|
}
|
|
else if( m_editLogPassword->Text.IsEmpty() ){
|
|
m_editLogPassword->SetFocus();
|
|
}
|
|
else{
|
|
bool bCheckOk = true;
|
|
AnsiString astrUN = m_editLogUsername->Text;
|
|
if( !CheckInput( astrUN.c_str(), 0 ) && !CheckInput( astrUN.c_str(), 1 ) ){
|
|
m_editLogUsername->Text = "";
|
|
m_editLogUsername->SetFocus();
|
|
bCheckOk = false;
|
|
}
|
|
else{
|
|
AnsiString astrPW = m_editLogPassword->Text;
|
|
if( !CheckInput( astrPW.c_str(), 2 ) ){
|
|
m_editLogPassword->Text = "";
|
|
m_editLogPassword->SetFocus();
|
|
bCheckOk = false;
|
|
}
|
|
}
|
|
|
|
if( bCheckOk ){
|
|
m_strUsername = m_editLogUsername->Text;
|
|
m_strPassword = m_editLogPassword->Text;
|
|
ModalResult = mrOk;
|
|
|
|
string strUN = AnsiString( m_strUsername ).c_str();
|
|
string strPW = AnsiString( m_strPassword ).c_str();
|
|
|
|
SetAutoLogin( m_bAutoLog, strUN.c_str(), strPW.c_str() );
|
|
}
|
|
}
|
|
}
|
|
else{
|
|
//检查输入
|
|
m_editRegUsername->Text = m_editRegUsername->Text.Trim();
|
|
m_editRegPassword->Text = m_editRegPassword->Text.Trim();
|
|
m_editVerify->Text = m_editVerify->Text.Trim();
|
|
m_editConfirm->Text = m_editConfirm->Text.Trim();
|
|
|
|
if( m_editRegUsername->Text.IsEmpty() ){
|
|
m_editRegUsername->SetFocus();
|
|
}
|
|
else if( m_editVerify->Text.IsEmpty() ){
|
|
m_editVerify->SetFocus();
|
|
}
|
|
else if( m_editRegPassword->Text.IsEmpty() ){
|
|
m_editRegPassword->SetFocus();
|
|
}
|
|
else if( m_editConfirm->Text.IsEmpty() ){
|
|
m_editConfirm->SetFocus();
|
|
}
|
|
else{
|
|
AnsiString astrPW = m_editRegPassword->Text;
|
|
if(!CheckInput(astrPW.c_str(), 3)){
|
|
m_editRegPassword->Text = "";
|
|
m_editRegPassword->SetFocus();
|
|
|
|
return;
|
|
}
|
|
|
|
if(m_editRegPassword->Text.Compare(m_editConfirm->Text)){
|
|
m_editConfirm->Text = "";
|
|
m_editConfirm->SetFocus();
|
|
|
|
return;
|
|
}
|
|
|
|
AnsiString astrUN = m_editRegUsername->Text;
|
|
|
|
if( CheckInput(astrUN.c_str(), 1) ){
|
|
isEmailOrMobile = 1;
|
|
}
|
|
else if( CheckInput(astrUN.c_str(), 4) ){
|
|
isEmailOrMobile = 2;
|
|
}
|
|
else{
|
|
m_editRegUsername->Text = "";
|
|
m_editRegUsername->SetFocus();
|
|
|
|
return;
|
|
}
|
|
|
|
this->Enabled = false;
|
|
Screen->Cursor = crHourGlass;
|
|
AnsiString strVerify = m_editVerify->Text;
|
|
|
|
RESTRequest->Resource = "verifyRegCode";
|
|
RESTRequest->Params->Clear();
|
|
RESTRequest->Params->AddItem("username", astrUN);
|
|
RESTRequest->Params->AddItem("verifyCode", strVerify);
|
|
|
|
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"){
|
|
String msg;
|
|
if(code=="A0213")
|
|
msg = "注册验证码已过期";
|
|
else if(code=="A0214")
|
|
msg = "注册验证码错误";
|
|
else
|
|
msg = ((TJSONString*)jValue->FindValue("msg"))->Value();
|
|
|
|
MessageForm->ShowMessage( msg, MB_ICONERROR | MB_OK );
|
|
|
|
return;
|
|
}
|
|
}
|
|
else{
|
|
MessageForm->ShowMessage( RESTResponse->StatusText, MB_ICONERROR | MB_OK );
|
|
return;
|
|
}
|
|
|
|
m_strUsername = m_editRegUsername->Text;
|
|
m_strPassword = m_editRegPassword->Text;
|
|
ModalResult = mrOk;
|
|
}
|
|
}
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::FormShow(TObject *Sender)
|
|
{
|
|
if( !m_bRegister ){
|
|
Height = 173;
|
|
|
|
m_editLogUsername->Text = m_strUsername;
|
|
m_editLogPassword->Text = m_strPassword;
|
|
|
|
if( m_editLogUsername->Text.IsEmpty() ){
|
|
m_editLogUsername->SetFocus();
|
|
}
|
|
else{
|
|
m_editLogPassword->SetFocus();
|
|
}
|
|
}
|
|
else{
|
|
Height = 219;
|
|
|
|
m_editRegUsername->Text = "";
|
|
m_editRegPassword->Text = "";
|
|
m_editConfirm->Text = "";
|
|
m_editVerify->Text = "";
|
|
|
|
if( m_editRegUsername->Text.IsEmpty() ){
|
|
m_editRegUsername->SetFocus();
|
|
}
|
|
else if(m_editVerify->Text.IsEmpty()){
|
|
m_editVerify->SetFocus();
|
|
}
|
|
}
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
|
|
{
|
|
switch( Key ){
|
|
case VK_RETURN:
|
|
if( !m_bRegister && m_btnLogGo->Enabled
|
|
|| m_bRegister && m_btnRegGo->Enabled )
|
|
GoClick( NULL );
|
|
break;
|
|
|
|
case VK_ESCAPE:
|
|
ModalResult = mrCancel;
|
|
break;
|
|
}
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::SwitchClick(TObject *Sender)
|
|
{
|
|
if(Sender==(TObject*)m_btnRegister){
|
|
m_bRegister = true;
|
|
|
|
m_panLogin->Visible = false;
|
|
m_panRegister->Visible = true;
|
|
Height = 219;
|
|
|
|
|
|
if( m_editRegUsername->Text.IsEmpty() ){
|
|
m_editRegUsername->SetFocus();
|
|
}
|
|
else if( m_editVerify->Text.IsEmpty() ){
|
|
m_editVerify->SetFocus();
|
|
}
|
|
else if(m_editRegPassword->Text.IsEmpty() ){
|
|
m_editRegPassword->SetFocus();
|
|
}
|
|
else{
|
|
m_editConfirm->SetFocus();
|
|
}
|
|
}
|
|
else{
|
|
m_bRegister = false;
|
|
m_panLogin->Visible = true;
|
|
m_panRegister->Visible = false;
|
|
Height = 173;
|
|
|
|
if( m_editLogUsername->Text.IsEmpty() ){
|
|
m_editLogUsername->SetFocus();
|
|
}
|
|
else{
|
|
m_editLogPassword->SetFocus();
|
|
}
|
|
}
|
|
|
|
Invalidate();
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
bool TLoginForm::CheckInput( const char * pText, int type )
|
|
{
|
|
bool bRet = false;
|
|
pcre * pPcre = NULL;
|
|
int nOffset = -1;
|
|
const char * pPattern = g_pPattern[type];
|
|
const char * pErrMsg = NULL;
|
|
if( NULL == (pPcre = pcre_compile(pPattern, 0, &pErrMsg, &nOffset, NULL)) ){
|
|
printf("pcre_compile ErrMsg=%s\n", pErrMsg);
|
|
}
|
|
else{
|
|
if( pcre_exec(pPcre, NULL, pText, strlen(pText), 0, NULL, 0) < 0 ){
|
|
printf("%s doesn't match %s\n", pText, pPattern);
|
|
}
|
|
else{
|
|
printf("%s matches %s\n", pText, pPattern);
|
|
bRet = true;
|
|
}
|
|
|
|
pcre_free(pPcre);
|
|
}
|
|
|
|
return bRet;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::UsernameChange(TObject *Sender)
|
|
{
|
|
TransparentEditUnit::TEdit* pEdit = (TransparentEditUnit::TEdit*)Sender;
|
|
AnsiString strName = pEdit->Text.Trim();
|
|
string strTmp = GBToUTF8(strName.c_str());
|
|
pEdit->OnChange = NULL;
|
|
|
|
while( strTmp.length()>pEdit->MaxLength ){
|
|
pEdit->Text = pEdit->Text.SubString( 0, pEdit->Text.Length()-1 );
|
|
strName = pEdit->Text.Trim();
|
|
strTmp = GBToUTF8(strName.c_str());
|
|
}
|
|
|
|
pEdit->SelStart = pEdit->Text.Length();
|
|
pEdit->OnChange = UsernameChange;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::UrlLinkMouseEnter(TObject *Sender)
|
|
{
|
|
TLabel* pL = (TLabel*)Sender;
|
|
pL->Font->Color = clHighlight;
|
|
pL->Font->Style = pL->Font->Style<<fsUnderline;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::UrlLinkMouseLeave(TObject *Sender)
|
|
{
|
|
TLabel* pL = (TLabel*)Sender;
|
|
pL->Font->Color = clBlue;
|
|
pL->Font->Style = pL->Font->Style>>fsUnderline;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::UrlLinkClick(TObject *Sender)
|
|
{
|
|
TLabel* pL = (TLabel*)Sender;
|
|
|
|
char strURL[256];
|
|
f_CS_GetWebPageURL( pL->Tag, strURL );
|
|
ShellExecute(Handle, NULL, ((String)strURL).c_str(), NULL, NULL, SW_SHOWNORMAL);
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::LogHideClick(TObject *Sender)
|
|
{
|
|
AutoCheckClick(NULL);
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::RegCheckClick(TObject *Sender)
|
|
{
|
|
//切换Check状态
|
|
if( m_bRegister ){
|
|
m_bAgree = !m_bAgree;
|
|
m_btnRegGo->Enabled = m_bAgree;
|
|
m_pbRegCheck->Invalidate();
|
|
}
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::RegChcekPaint(TObject *Sender)
|
|
{
|
|
//绘制Check状态
|
|
int x = 0;
|
|
if( m_bRegister && !m_bAgree ){
|
|
x = -m_pImgCheck->Width/2;
|
|
}
|
|
|
|
m_pbRegCheck->Canvas->Draw(x, 0, m_pImgCheck);
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::OnCDTimer(int countDown)
|
|
{
|
|
|
|
if( countDown==0 ){
|
|
m_lblSendTip->Caption = "重新发送";
|
|
m_lblSendTip->Enabled = true;
|
|
m_btnSend->Enabled = true;
|
|
}
|
|
else{
|
|
m_lblSendTip->Caption = String(countDown) + "秒";
|
|
m_lblSendTip->Enabled = false;
|
|
m_btnSend->Enabled = false;
|
|
}
|
|
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::SendClick(TObject *Sender)
|
|
{
|
|
m_editRegUsername->Text = m_editRegUsername->Text.Trim();
|
|
if(m_editRegUsername->Text.IsEmpty()){
|
|
m_editRegUsername->SetFocus();
|
|
return;
|
|
}
|
|
|
|
AnsiString astrUN = m_editRegUsername->Text;
|
|
if( !CheckInput(astrUN.c_str(), 1) && !CheckInput(astrUN.c_str(), 4 )){
|
|
m_editRegUsername->Text = "";
|
|
m_editRegUsername->SetFocus();
|
|
return;
|
|
}
|
|
|
|
String sKey, sCode;
|
|
TVerifyCodeForm* pForm = new TVerifyCodeForm(NULL, RESTClient->BaseURL);
|
|
if( pForm->ShowModal() == mrOk){
|
|
sKey = pForm->GetKey();
|
|
sCode = pForm->GetCode();
|
|
}
|
|
delete pForm;
|
|
|
|
if(sKey.Length()==0 || sCode.Length()==0)
|
|
return;
|
|
|
|
this->Enabled = false;
|
|
Screen->Cursor = crHourGlass;
|
|
|
|
RESTRequest->Resource = "sendRegCode";
|
|
RESTRequest->Params->Clear();
|
|
RESTRequest->Params->AddItem("contact", astrUN);
|
|
RESTRequest->Params->AddItem("captchaKey", sKey);
|
|
RESTRequest->Params->AddItem("captchaCode", sCode);
|
|
|
|
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"){
|
|
m_lblSendTip->Caption = String(CODE_INTERVAL) + "秒";
|
|
// m_lblSendTip->Font->Color = 0x009DA7B0;//"$001E394F";
|
|
m_lblSendTip->Enabled = false;
|
|
m_btnSend->Enabled = false;
|
|
|
|
PostMessage(Application->MainFormHandle, UM_STARTCDTIMER, 0, 0);
|
|
MessageForm->ShowMessage("已向 " + astrUN + " 发送注册验证码\n(15分钟内有效,请勿重复提交!)", MB_OK | MB_ICONINFORMATION);
|
|
}
|
|
else{
|
|
String msg;
|
|
if(code=="A0213")
|
|
msg = "人机验证码已过期";
|
|
else if(code=="A0214")
|
|
msg = "人机验证码错误";
|
|
else
|
|
msg = ((TJSONString*)jValue->FindValue("msg"))->Value();
|
|
|
|
MessageForm->ShowMessage( msg, MB_ICONERROR | MB_OK );
|
|
}
|
|
}
|
|
else{
|
|
MessageForm->ShowMessage( RESTResponse->StatusText, MB_ICONERROR | MB_OK );
|
|
}
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::ResetPassClick(TObject *Sender)
|
|
{
|
|
char strURL[256];
|
|
f_CS_GetWebPageURL( URL_RESETPASS, strURL );
|
|
ShellExecute(Handle, NULL, ((String)strURL).c_str(), NULL, NULL, SW_SHOWNORMAL);
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::RegHideClick(TObject *Sender)
|
|
{
|
|
RegCheckClick(NULL);
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::SendTipClick(TObject *Sender)
|
|
{
|
|
SendClick(NULL);
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::SendTipMouseEnter(TObject *Sender)
|
|
{
|
|
m_btnSend->Up = true;;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::SendMouseLeave(TObject *Sender)
|
|
{
|
|
m_btnSend->Up = false;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
void __fastcall TLoginForm::BkgdMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
|
|
{
|
|
//点击顶部拖动
|
|
if (X > 10 && X < Width - 24 && Y < 22){
|
|
ReleaseCapture();
|
|
Perform(WM_NCLBUTTONDOWN,HTCAPTION, 0);
|
|
}
|
|
else{
|
|
MouseDown(Button, Shift, X, Y);
|
|
}
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|