/// <summary>
/// 生成随机验证码
/// </summary>
public static class RandomCode
{
/// <summary>
/// 返回一个N位验证码
/// </summary>
/// <param name="N">位数</param>
/// <returns></returns>
public static string RandomCodeCommand(int N)
{
string code = "";
Random random = new Random();
for (int i = 0; i < N; i++)
{
code += random.Next(9);
}
return code;
}
}
public class SendRandomCode : ViewModelBase
{
private int _interval;//记录倒计时长
private string idCode;//在规定时间内保存验证码
private int idCodeTime;//设置验证码的有效时间(秒)
private int idCodeNum = 6;//设置验证码的位数
public void GetCode(string phoneNum)
{
//获取验证码
timerSend = new Timer(1000);
timerSend.AutoReset = true;
timerSend.Elapsed += Timer_Elapsed;
_interval = SecondNum;
timerSend.Start();
//在验证码有效期内,再次请求验证码,需要先关闭上一次的
if (timerTime != null)
{
timerTime.Close();
timerTime.Dispose();
}
//验证码的有效期
timerTime = new Timer(1000);
timerTime.AutoReset = true;
timerTime.Elapsed += TimerTime_Elapsed;
timerTime.Start();
idCodeTime = SaveTime;
IdCode = RandomCode.RandomCodeCommand(idCodeNum);
PhoneNum = phoneNum;
}
#region 获取验证码倒计时
Timer timerSend;
Timer timerTime;
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
BtnIsEnable = false;
BtnContent = "(" + (_interval--) + ")秒后再次获取验证码";
if (_interval <= -1)
{
BtnIsEnable = true;
BtnContent = "获取验证码";
timerSend.Stop();
timerSend.Dispose();
}
//throw new NotImplementedException();
}
private void TimerTime_Elapsed(object sender, ElapsedEventArgs e)
{
idCodeTime--;
if (idCodeTime <= 0)
{
IdCode = "";
timerTime.Stop();
timerTime.Dispose();
}
Console.WriteLine(IdCode);
//throw new NotImplementedException();
}
#endregion
#region 字段
//*************************************************************************************************//上线时需要修改
private int secondNum = 30;//设置倒计时长
private int saveTime = 60;//设置保存验证码时长
//*************************************************************************************************//
private string btnContent = "获取验证码";//设置获取验证码按钮显示的名称
private bool btnIsEnable = true;//设置获取验证码按钮是否可用
private string phoneNum;//记录是否是发送验证码的手机号
public int SecondNum
{
get
{
return secondNum;
}
set
{
secondNum = value;
}
}
public int SaveTime
{
get
{
return saveTime;
}
set
{
saveTime = value;
}
}
public string BtnContent
{
get
{
return btnContent;
}
set
{
btnContent = value;
RaisePropertyChanged("BtnContent");
}
}
public bool BtnIsEnable
{
get
{
return btnIsEnable;
}
set
{
btnIsEnable = value;
RaisePropertyChanged("BtnIsEnable");
}
}
public string IdCode
{
get
{
return idCode;
}
set
{
idCode = value;
RaisePropertyChanged("IdCode");
}
}
public string PhoneNum
{
get
{
return phoneNum;
}
set
{
phoneNum = value;
RaisePropertyChanged("PhoneNum");
}
}
#endregion
}
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal">
<Label Content="手机号"/>
<TextBox Text="{Binding PhoneNum}" Height="20" Width="100"/>
<Button Content="{Binding Src.BtnContent}" IsEnabled="{Binding Src.BtnIsEnable}" Command="{Binding SendCode}" Height="20" Width="120"/>
</StackPanel>
<StackPanel Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal">
<Label Content="验证码"/>
<TextBox Text="{Binding IdentifyCode}" Height="20" Width="100"/>
<Button Content="提交" Command="{Binding Submit}" Height="20" Width="120"/>
</StackPanel>
</Grid>
public class BingVM: ViewModelBase
{
#region 界面字段
private string phoneNum;//手机号
private string identifyCode;//验证码
public string PhoneNum
{
get
{
return phoneNum;
}
set
{
phoneNum = value;
RaisePropertyChanged("PhoneNum");
}
}
public string IdentifyCode
{
get
{
return identifyCode;
}
set
{
identifyCode = value;
RaisePropertyChanged("IdentifyCode");
}
}
#endregion
#region 为获取验证码按钮设置content和isEnable用的
SendRandomCode src = new SendRandomCode();
public SendRandomCode Src
{
get { return src; }
set
{
src = value;
}
}
#endregion
private RelayCommand sendCode;//获取验证码
public RelayCommand SendCode
{
get
{
return sendCode ?? (sendCode = new RelayCommand(
() =>
{
if (!string.IsNullOrEmpty(PhoneNum))
{
src.GetCode(PhoneNum);
}
else
{
MessageBox.Show("手机号不能为空!");
}
}));
}
}
private RelayCommand submit;
public RelayCommand Submit
{
get
{
return submit ?? (submit = new RelayCommand(
() =>
{
if (IdentifyCode == src.IdCode && PhoneNum == src.PhoneNum)
{
MessageBox.Show("验证成功");
}
else
{
MessageBox.Show("验证失败");
}
}));
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有