using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Shared.Phone.UserCenter.Password
{
///
/// 验证新邮箱的画面
///
public class CheckNewEmailForm : EditorCommonForm
{
#region ■ 变量声明___________________________
///
/// 错误信息控件
///
private NormalViewControl btnErrorMsg = null;
///
/// 新邮箱(防止恶意变更)
///
private string newEmail = string.Empty;
///
/// 标记是否能够校验验证码了
///
private bool canCheckCode = false;
///
/// 能否发送验证码
///
private bool canSendCode = true;
#endregion
#region ■ 初始化_____________________________
///
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
///
public void ShowForm()
{
//设置头部信息
base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uEditorEmail));
//初始化中部信息
this.InitMiddleFrame();
}
///
/// 初始化中部信息
///
private void InitMiddleFrame()
{
//清空bodyFrame
this.ClearBodyFrame();
var frame = new FrameLayout();
frame.Y = Application.GetRealHeight(161);
frame.Gravity = Gravity.CenterHorizontal;
frame.Width = Application.GetRealWidth(688);
frame.Height = Application.GetRealHeight(127);
bodyFrameLayout.AddChidren(frame);
//输入框
var txtValue = new TextInputControl(frame.Width - Application.GetRealWidth(20), frame.Height - HdlControlResourse.BottomLineHeight, false);
txtValue.X = Application.GetRealWidth(10);
txtValue.PlaceholderText = Language.StringByID(R.MyInternationalizationString.uPleaseInputNewEmailAddress);
frame.AddChidren(txtValue);
//线
var btnLine = new NormalViewControl(frame.Width, HdlControlResourse.BottomLineHeight, false);
btnLine.Y = txtValue.Bottom;
btnLine.BackgroundColor = UserCenterColor.Current.ButtomLine;
frame.AddChidren(btnLine);
//联动线的状态
txtValue.btnLine = btnLine;
if (string.IsNullOrEmpty(HdlUserCenterResourse.UserInfo.UserEmail) == false)
{
//确认身份成功,请绑定新邮箱
var btnMsg1 = new NormalViewControl(800, 49, false);
btnMsg1.X = Application.GetRealWidth(196);
btnMsg1.Y = frame.Bottom + Application.GetRealHeight(40);
btnMsg1.TextSize = 12;
btnMsg1.TextColor = UserCenterColor.Current.TextGrayColor1;
btnMsg1.TextID = R.MyInternationalizationString.uAuthenticationSuccessAndBindNewEmail;
bodyFrameLayout.AddChidren(btnMsg1);
}
//初始化验证码控件
var btnCodeControl = new VerificationCodeControl(6);
btnCodeControl.Y = Application.GetRealHeight(475);
bodyFrameLayout.AddChidren(btnCodeControl);
btnCodeControl.InitControl();
btnCodeControl.TxtCodeChangeEvent += (sender, e) =>
{
if (btnErrorMsg.Visible == true)
{
btnErrorMsg.Visible = false;
}
};
btnCodeControl.FinishInputEvent += (value) =>
{
//绑定新邮箱
this.SaveNewEmail(btnCodeControl, value);
};
//验证码错误,请重新输入
this.btnErrorMsg = new NormalViewControl(bodyFrameLayout.Width, Application.GetRealHeight(58), false);
btnErrorMsg.Y = Application.GetRealHeight(677);
btnErrorMsg.TextAlignment = TextAlignment.Center;
btnErrorMsg.TextColor = 0xfff75858;
btnErrorMsg.TextID = R.MyInternationalizationString.uVerificationCodeErrorInputAgain;
btnErrorMsg.IsBold = true;
bodyFrameLayout.AddChidren(btnErrorMsg);
btnErrorMsg.Visible = false;
var btnOk = new BottomClickButton(688);
btnOk.Y = Application.GetRealHeight(792);
btnOk.TextID = R.MyInternationalizationString.uGetVerificationCode;
bodyFrameLayout.AddChidren(btnOk);
btnOk.oldBackgroundColor = UserCenterColor.Current.ClickButtonDefultColor;
btnOk.CanClick = false;
btnOk.ButtonClickEvent += (sender, e) =>
{
//清空输入的值
btnCodeControl.ClearInputValue();
this.btnErrorMsg.Visible = false;
//检测邮箱的地址
if (this.CheckEmail(txtValue.Text.Trim()) == false)
{
return;
}
//发送验证码到邮箱
this.SendCodeToEmail(btnOk, txtValue.Text.Trim());
};
txtValue.TextChangeEventHandler += (sender, value) =>
{
if (value == string.Empty)
{
//按键不可用
btnOk.CanClick = false;
}
else if (btnOk.CanClick == false)
{
//按键可用
btnOk.CanClick = true;
}
};
}
#endregion
#region ■ 发送验证码到邮箱___________________
///
/// 发送验证码到邮箱
///
private void SendCodeToEmail(BottomClickButton btnNext, string Email)
{
var errorMsg = HdlAccountLogic.Current.SendVeriCodeToEmail(Email, VerCodeType.A绑定);
if (errorMsg != null)
{
//显示错误
this.btnErrorMsg.Visible = true;
this.btnErrorMsg.Text = errorMsg;
return;
}
//不能再发送验证码
this.canSendCode = false;
//可以开始校验验证码了
this.canCheckCode = true;
//记录起这个邮箱,防止恶意变更
this.newEmail = Email;
//控件不能再次按下
btnNext.CanClick = false;
//?s后重发
string repeat = Language.StringByID(R.MyInternationalizationString.RepeatSend1);
//有效时间60秒
int waitime = 60;
btnNext.Text = waitime + "s" + repeat;
HdlThreadLogic.Current.RunThread(() =>
{
while (this.Parent != null && this.canSendCode == false)
{
waitime--;
System.Threading.Thread.Sleep(1000);
if (waitime == 0)
{
break;
}
HdlThreadLogic.Current.RunMain(() =>
{
if (btnNext != null)
{
btnNext.Text = waitime + "s" + repeat;
}
}, ShowErrorMode.NO);
}
HdlThreadLogic.Current.RunMain(() =>
{
if (btnNext != null)
{
//获取验证码
btnNext.TextID = R.MyInternationalizationString.uGetVerificationCode;
//按键可以按下
btnNext.CanClick = true;
}
});
});
}
#endregion
#region ■ 变更邮箱___________________________
///
/// 变更邮箱
///
private void SaveNewEmail(VerificationCodeControl btnCodeControl, string code)
{
if (this.canCheckCode == false)
{
//验证码错误,请重新输入
this.btnErrorMsg.Visible = true;
this.btnErrorMsg.TextID = R.MyInternationalizationString.uVerificationCodeErrorInputAgain;
//清空输入值
btnCodeControl.ClearInputValue();
return;
}
//执行绑定
var errorMsg = HdlAccountLogic.Current.BindAccount(this.newEmail, code);
if (errorMsg != null)
{
this.ShowMassage(ShowMsgType.Tip, errorMsg);
//可以重新发送验证码
this.canSendCode = true;
return;
}
//成功绑定新邮箱
string msg = Language.StringByID(R.MyInternationalizationString.uBindEmailSuccess);
this.ShowMassage(ShowMsgType.Normal, msg, () =>
{
//刷新个人信息画面
this.RefreshUserInfoForm();
});
}
///
/// 刷新个人信息画面
///
private void RefreshUserInfoForm()
{
//如果修改的是账号的话,则重新登录
if (HdlUserCenterResourse.UserInfo.UserEmail == Shared.Common.Config.Instance.Account)
{
HdlAccountLogic.Current.ReLoginAgain(this.newEmail);
return;
}
HdlUserCenterResourse.UserInfo.UserEmail = this.newEmail;
this.CloseForm();
}
#endregion
#region ■ 一般方法___________________________
///
/// 检查邮箱
///
///
///
private bool CheckEmail(string Email)
{
//输入为空
if (Email == string.Empty)
{
this.btnErrorMsg.Visible = true;
this.btnErrorMsg.TextID = R.MyInternationalizationString.uPleaseInputNewEmailAddress;
return false;
}
//检测邮箱格式
if (HdlCheckLogic.Current.CheckEmail(Email) == false)
{
this.btnErrorMsg.Visible = true;
this.btnErrorMsg.TextID = R.MyInternationalizationString.uThisIsNotEmailType;
return false;
}
return true;
}
#endregion
}
}