using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Shared.Phone.UserCenter.Account
{
///
/// 验证新邮箱的画面
///
public class CheckNewEmailForm : EditorCommonForm
{
#region ■ 变量声明___________________________
///
/// 验证码输入控件
///
private Dictionary dicCodeControls = new Dictionary();
///
/// 错误信息控件
///
private NormalViewControl btnErrorMsg = null;
///
/// 新邮箱(防止恶意变更)
///
private string newEmail = string.Empty;
///
/// 标记是否能够校验验证码了
///
private bool canCheckCode = false;
#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 - ControlCommonResourse.BottomLineHeight, false);
txtValue.X = Application.GetRealWidth(10);
txtValue.PlaceholderText = Language.StringByID(R.MyInternationalizationString.uPleaseInputNewEmailAddress);
txtValue.PlaceholderTextColor = UserCenterColor.Current.TextGrayColor2;
frame.AddChidren(txtValue);
//线
var btnLine = new NormalViewControl(frame.Width, ControlCommonResourse.BottomLineHeight, false);
btnLine.Y = txtValue.Bottom;
btnLine.BackgroundColor = UserCenterColor.Current.ButtomLine;
frame.AddChidren(btnLine);
if (string.IsNullOrEmpty(UserCenterResourse.UserInfo.Email) == 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);
}
//初始化验证码控件
this.InitCodeControl();
//验证码错误,请重新输入
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;
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.CanClick = false;
btnOk.ButtonClickEvent += (sender, e) =>
{
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 async void SendCodeToEmail(BottomClickButton btnNext, string Email)
{
var sendCodePra = new SendCodePra();
sendCodePra.Account = Email;
bool falge = await UserCenterLogic.GetResultStatuByRequestHttps("ZigbeeUsers/LoginSendVerCode", false, sendCodePra);
if (falge == false)
{
//验证码发送失败
this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.RegisterSendVerCode_SendFail));
return;
}
//可以开始校验验证码了
this.canCheckCode = true;
//记录起这个邮箱,防止恶意变更
this.newEmail = Email;
//控件不能再次按下
btnNext.CanClick = false;
//?s后重发
string repeat = Language.StringByID(R.MyInternationalizationString.RepeatSend1);
//有效时间300秒
int waitime = 300;
btnNext.Text = waitime + "s" + repeat;
new System.Threading.Thread(() =>
{
while (this.Parent != null)
{
waitime--;
System.Threading.Thread.Sleep(1000);
if (waitime == 0)
{
this.canCheckCode = false;
Application.RunOnMainThread(() =>
{
if (btnNext != null)
{
//获取验证码
btnNext.TextID = R.MyInternationalizationString.uGetVerificationCode;
//按键可以按下
btnNext.CanClick = true;
}
});
break;
}
Application.RunOnMainThread(() =>
{
if (btnNext != null)
{
btnNext.Text = waitime + "s" + repeat;
}
});
}
})
{ IsBackground = true }.Start();
}
#endregion
#region ■ 验证码控件_________________________
///
/// 初始化验证码控件
///
private void InitCodeControl()
{
var frame = new FrameLayout();
frame.Y = Application.GetRealHeight(475);
frame.Gravity = Gravity.CenterHorizontal;
frame.Height = Application.GetRealHeight(104);
frame.Width = Application.GetRealWidth(6 * 115 + 5 * 43);
bodyFrameLayout.AddChidren(frame);
for (int i = 0; i < 6; i++)
{
var frameCode = new FrameLayout();
frameCode.X = i * Application.GetRealWidth(115 + 43);
frameCode.Width = Application.GetRealWidth(115);
frameCode.Height = Application.GetRealHeight(104);
frameCode.Radius = (uint)Application.GetMinRealAverage(6);
frameCode.BorderWidth = 1;
frameCode.BorderColor = UserCenterColor.Current.TextFrameColor;
frame.AddChidren(frameCode);
var txtCode = new TextInputControl(frameCode.Width, frameCode.Height, false);
txtCode.Name = "txtName" + i;
txtCode.TextAlignment = TextAlignment.Center;
frameCode.AddChidren(txtCode);
dicCodeControls[i] = txtCode;
//光标事件
txtCode.FoucsChanged += this.TxtCode_FoucsChangedEvent;
//值改变事件
txtCode.TextChangeEventHandler += this.TxtCode_TextChangeEvent;
}
}
#endregion
#region ■ 验证码事件_________________________
///
/// 验证码焦点变更事件
///
///
///
private void TxtCode_FoucsChangedEvent(object sender, FocusEventArgs e)
{
var txtCode = (TextInputControl)sender;
if (txtCode.Parent == null)
{
return;
}
if (e.Focus == true)
{
txtCode.Parent.BorderColor = UserCenterColor.Current.TextFrameSelectColor;
}
else
{
txtCode.Parent.BorderColor = UserCenterColor.Current.TextFrameColor;
}
}
///
/// 验证码值改变事件
///
///
///
private void TxtCode_TextChangeEvent(object sender, string textValue)
{
if (btnErrorMsg.Visible == true)
{
btnErrorMsg.Visible = false;
}
if (textValue == string.Empty)
{
return;
}
//这个是自己复制粘贴过来的
if (textValue.Length == 6)
{
dicCodeControls[5].Foucs = true;
for (int i = 0; i < 6; i++)
{
//先移除事件
dicCodeControls[i].TextChangeEventHandler -= TxtCode_TextChangeEvent;
//赋值
dicCodeControls[i].Text = textValue[i].ToString();
dicCodeControls[i].TextChangeEventHandler += TxtCode_TextChangeEvent;
}
//校验验证码
this.CheckVerificationCode(textValue);
}
else
{
//光标移动
var txtCode = (TextInputControl)sender;
//只能一个值
if (textValue.Length > 1)
{
//先移除事件
txtCode.TextChangeEventHandler -= TxtCode_TextChangeEvent;
txtCode.Text = txtCode.Text.Substring(0, 1);
txtCode.TextChangeEventHandler += TxtCode_TextChangeEvent;
}
int index = Convert.ToInt32(txtCode.Name.Replace("txtName", string.Empty));
if (dicCodeControls.ContainsKey(index + 1) == true)
{
dicCodeControls[index + 1].Foucs = true;
}
else
{
//最后一位输入完成,校验验证码
string code = string.Empty;
for (int i = 0; i < 6; i++)
{
code += dicCodeControls[i].Text;
}
this.CheckVerificationCode(code);
}
}
}
#endregion
#region ■ 校验验证码_________________________
///
/// 验证验证码
///
///
private async void CheckVerificationCode(string code)
{
if (this.canCheckCode == false)
{
//验证码错误,请重新输入
this.btnErrorMsg.Visible = true;
this.btnErrorMsg.TextID = R.MyInternationalizationString.uVerificationCodeErrorInputAgain;
return;
}
var checkCodePra = new CheckCodePra();
checkCodePra.Code = code;
checkCodePra.Account = newEmail;
bool flage = await UserCenterLogic.GetResultStatuByRequestHttps("ZigbeeUsers/ValidatorCode", false, checkCodePra);
if (flage == false)
{
//验证码错误,请重新输入
this.btnErrorMsg.Visible = true;
this.btnErrorMsg.TextID = R.MyInternationalizationString.uVerificationCodeErrorInputAgain;
return;
}
//变更邮箱
this.SaveNewEmail();
}
///
/// 变更邮箱
///
private async void SaveNewEmail()
{
var pra = new SaveNewEmailPra();
pra.Account = this.newEmail;
bool flage = await UserCenterLogic.GetResultStatuByRequestHttps("ZigbeeUsers/BindAccount", false, pra);
if (flage == false)
{
//绑定邮箱失败
this.ShowMassage(ShowMsgType.Tip, Language.StringByID(R.MyInternationalizationString.uBindEmailFail));
return;
}
//成功绑定新邮箱
string msg = Language.StringByID(R.MyInternationalizationString.uBindEmailSuccess);
this.ShowMassage(ShowMsgType.Normal, msg, () =>
{
//刷新个人信息画面
this.RefreshUserInfoForm();
});
}
///
/// 刷新个人信息画面
///
private void RefreshUserInfoForm()
{
//如果修改的是账号的话,则重新登录
if (UserCenterResourse.UserInfo.Email == Shared.Common.Config.Instance.Account)
{
UserCenterLogic.ReLoginAgain(this.newEmail);
return;
}
UserCenterResourse.UserInfo.Email = 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 (UserCenterLogic.CheckEmail(Email) == false)
{
this.btnErrorMsg.Visible = true;
this.btnErrorMsg.TextID = R.MyInternationalizationString.uThisIsNotEmailType;
return false;
}
return true;
}
#endregion
#region ■ 结构体_____________________________
///
/// 发送验证码的启动参数
///
private class SendCodePra
{
///
/// 用户账号
///
public string Account = string.Empty;
///
/// 公司编号,国内使用手机短信验证码时,此字段填入0,国外手机短信验证码,此字段填入4
///
public int Company = 0;
///
/// 语言
///
public string Language = Shared.Language.CurrentLanguage;
///
/// 国家地区代码,手机号发送验证码时使用
///
public int AreaCode = 0;
}
///
/// 发送验证码的启动参数
///
private class CheckCodePra
{
///
/// 用户账号
///
public string Account = string.Empty;
///
/// 验证码
///
public string Code = "0";
///
/// 语言
///
public string Language = Shared.Language.CurrentLanguage;
///
/// 国家地区代码,手机号发送验证码时使用
///
public int AreaCode = 0;
}
///
/// 保存新邮箱的启动参数
///
private class SaveNewEmailPra
{
///
/// 新用户账号
///
public string Account = string.Empty;
}
#endregion
}
}