using System;
using System.Threading.Tasks;
namespace Shared.Phone.UserCenter.User
{
///
/// 验证新手机号的画面
///
public class CheckNewPhoneNumForm : UserCenterCommonForm
{
///
/// 重新发送的状态控件
///
private RowMostRightTextView btnStatu = null;
///
/// 信息提示控件
///
private ViewNormalControl txtMsg = null;
///
/// 下一步的控件
///
private BottomClickButton btnBottom = null;
///
/// 线程运行
///
private bool IsAction = false;
///
/// 新的手机号
///
private string newPhoneNum = string.Empty;
///
/// 地区代码
///
private int newAreaCode = 0;
///
/// 画面显示(底层会固定调用此方法,借以完成画面创建)
///
public void ShowForm()
{
//设定标题
base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uCheckPhoneNumber));
//初始化中部控件
this.InitMiddleFrame();
}
///
/// 初始化中部控件
///
private void InitMiddleFrame()
{
var rowlayout1 = new RowLayout();
rowlayout1.Height = ControlCommonResourse.ListViewRowHeight;
bodyFrameLayout.AddChidren(rowlayout1);
//新手机号码
var btnPhoneView = new RowTopBlackView(false);
btnPhoneView.TextID = R.MyInternationalizationString.uNewPhoneNumber;
rowlayout1.AddChidren(btnPhoneView);
//请输入手机号码
var txtPhone = new PhoneAreaControl();
txtPhone.Gravity = Gravity.CenterVertical;
rowlayout1.AddChidren(txtPhone);
//重置Y轴
txtPhone.Y = UserCenterLogic.GetControlChidrenYaxis(rowlayout1.Height, txtPhone.Height, UViewAlignment.Bottom) - btnPhoneView.DeviationValue;
txtPhone.InitControl(this);
var rowlayout2 = new RowLayout();
rowlayout2.Y = rowlayout1.Bottom;
rowlayout2.Height = ControlCommonResourse.ListViewRowHeight;
bodyFrameLayout.AddChidren(rowlayout2);
//验证码
var btnCode = new RowTopBlackView(false);
btnCode.TextID = R.MyInternationalizationString.uVerificationCode;
rowlayout2.AddChidren(btnCode);
//请输入验证码
var txtCode = new RowBottomEditorText();
txtCode.PlaceholderText = Language.StringByID(R.MyInternationalizationString.uPleaseInputVerificationCode);
txtCode.PlaceholderTextColor = UserCenterColor.Current.TextTipColor;
rowlayout2.AddChidren(txtCode);
//状态
btnStatu = new RowMostRightTextView();
btnStatu.TextID = R.MyInternationalizationString.uSendVerificationCode;
btnStatu.TextColor = UserCenterColor.Current.TextBlueColor;
rowlayout2.AddChidren(btnStatu);
btnStatu.ReSetYaxis(UViewAlignment.Bottom);
//提示信息
txtMsg = new ViewNormalControl(800, true);
txtMsg.X = ControlCommonResourse.XXLeft;
txtMsg.Y = rowlayout2.Bottom + Application.GetRealHeight(5);
txtMsg.TextColor = UserCenterColor.Current.Red;
bodyFrameLayout.AddChidren(txtMsg);
//验证
btnBottom = new BottomClickButton();
btnBottom.TextID = R.MyInternationalizationString.uVerification;
btnBottom.Visible = false;
bodyFrameLayout.AddChidren(btnBottom);
btnBottom.MouseUpEvent += (sender, e) =>
{
if (this.IsAction == false || txtCode.Text.Trim() == string.Empty)
{
txtMsg.TextID = R.MyInternationalizationString.uVerificationCodeError;
return;
}
//验证验证码
this.CheckVerificationCode(txtCode.Text.Trim());
};
//发送验证码的单击事件
btnStatu.MouseUpEvent += (sender, e) =>
{
//检测手机号
if (this.CheckPhoneNumber(txtPhone.AreaCode, txtPhone.PhoneNumber) == false)
{
return;
}
//发送验证码到手机
this.SendCodeToPhone(txtPhone.AreaCode, txtPhone.PhoneNumber);
};
}
///
/// 线程开启倒计时
///
private void StartTimeOutThead()
{
if (this.IsAction == true)
{
return;
}
this.IsAction = true;
int waitime = 300;
//重发
string repeat = Language.StringByID(R.MyInternationalizationString.RepeatSend1);
new System.Threading.Thread(() =>
{
while (this.IsAction == true)
{
if (waitime == 0)
{
break;
}
Application.RunOnMainThread(() =>
{
this.btnStatu.Text = repeat + "(" + waitime + "s)";
});
waitime--;
System.Threading.Thread.Sleep(1000);
}
this.IsAction = false;
Application.RunOnMainThread(() =>
{
//重新发送
this.btnStatu.Text = Language.StringByID(R.MyInternationalizationString.RepeatSend2);
});
})
{ IsBackground = true }.Start();
}
///
/// 发送验证码到手机
///
///
///
///
private async void SendCodeToPhone(string areaCode, string phoneNum)
{
var sendCodePra = new SendCodePra();
sendCodePra.Account = phoneNum;
sendCodePra.AreaCode = Convert.ToInt32(areaCode);
bool flage = await UserCenterLogic.GetResultStatuByRequestHttps("ZigbeeUsers/RegisterSendVerCode", sendCodePra);
if (flage == false)
{
return;
}
this.newPhoneNum = phoneNum;
this.newAreaCode = Convert.ToInt32(areaCode);
Application.RunOnMainThread(() =>
{
btnBottom.Visible = true;
btnStatu.Text = Language.StringByID(R.MyInternationalizationString.RepeatSend1) + " (300s)";
});
//线程开启倒计时
this.StartTimeOutThead();
}
///
/// 验证验证码
///
///
private async void CheckVerificationCode(string code)
{
var checkCodePra = new CheckCodePra();
checkCodePra.Code = code;
checkCodePra.Account = this.newPhoneNum;
checkCodePra.AreaCode = this.newAreaCode;
bool flage = await UserCenterLogic.GetResultStatuByRequestHttps("ZigbeeUsers/ValidatorCode", checkCodePra);
if (flage == false)
{
txtMsg.TextID = R.MyInternationalizationString.uVerificationCodeError;
return;
}
this.IsAction = false;
//变更手机号
this.SaveNewPhoneNumber();
}
///
/// 变更手机号
///
private async void SaveNewPhoneNumber()
{
var pra = new SaveNewPhoneNumPra();
pra.Account = this.newPhoneNum;
bool flage = await UserCenterLogic.GetResultStatuByRequestHttps("ZigbeeUsers/BindAccount", pra);
if (flage == false)
{
return;
}
//您的手机号码已经更新
string msg = Language.StringByID(R.MyInternationalizationString.uYourPhoneNumberIsRefresh);
this.ShowNormalMsg(msg, "RefreshUserInfoForm");
}
///
/// 检测手机号
///
private bool CheckPhoneNumber(string areaCode, string phoneNum)
{
if (this.IsAction == true)
{
return false;
}
//输入为空
if (phoneNum == string.Empty)
{
txtMsg.Text = Language.StringByID(R.MyInternationalizationString.uPleaseInputPhoneNumber);
return false;
}
//检测手机号格式
if (UserCenterLogic.CheckPhoneNumber(phoneNum, areaCode) == false)
{
txtMsg.Text = Language.StringByID(R.MyInternationalizationString.uThisIsNotPhoneNumberType);
return false;
}
txtMsg.Text = string.Empty;
return true;
}
///
/// 刷新个人信息画面
///
public void RefreshUserInfoForm()
{
//如果修改的是账号的话,则重新登录
if (UserCenterResourse.UserInfo.Phone == Shared.Common.Config.Instance.Account)
{
UserCenterLogic.ReLoginAgain(this.newPhoneNum);
return;
}
UserCenterResourse.UserInfo.Phone = this.newPhoneNum;
this.CloseForm();
}
///
/// 画面关闭
///
/// 是否关闭界面,false的时候,只会调用关闭函数里面的附加功能
public override void CloseForm(bool isCloseForm = true)
{
this.IsAction = false;
base.CloseForm(isCloseForm);
}
///
/// 发送验证码的启动参数
///
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 SaveNewPhoneNumPra
{
///
/// 新用户账号
///
public string Account = string.Empty;
}
}
}