最近做一個項目,因為涉及到注冊,因此需要發送短信,一般發送短信都有一個倒計時的小按鈕,因此,就做了一個,在此做個記錄。
一、發送消息
沒有調用公司的短信平臺,只是模擬前臺生成一串數字,將此串數字輸出一下。
在這個部分寫了兩個類文件:一個是生成隨機數,一個是模擬發送此數字的。
1、因為生成幾位隨機數,是必須要到項目上線之前才能定的,因此,寫了一個帶參數的函數,如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
/// <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; } } |
2、模擬發送此串數字。
這個類里面用了兩個timer函數,一個是用作button的倒數顯示的,另一個是用作保存這個驗證碼時長的。
在記錄驗證碼的同時,還需要記錄發送驗證碼的手機號,以防止,用戶用第一個手機號點擊了發送驗證碼后,把手機號部分修改為其他的手機號。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
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 } |
二、xaml頁面代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< 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 > |
三、vm頁面代碼
vm頁面沒有什么特別的,就是聲明了一些字段,
特別注意的是,由于前臺的xaml頁面上的發送短信按鈕是需要倒計時的,因此button的content和isenable需要綁定到sendrandomcode這個類上,所以需要在vm下聲明一下這個類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
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( "驗證失敗" ); } })); } } } |
四、效果展示
上面是成功的效果圖。
驗證失敗的情況如下:
1、如果在發送驗證碼的過程中,把手機號修改了,填入原有的驗證碼
2、如果輸入的驗證碼不是程序輸出的驗證碼
3、時間超過了驗證碼的保存時間
bug修復:
剛才在測試的過程中發現了一個問題,由于我們做的主程序是調用模塊的dll文件生成磁貼的,而主程序的返回按鈕,不會關閉掉當前磁貼的所有線程,導致當返回再進入此磁貼時,再次點擊發送按鈕,則會再次出現一個驗證碼,解決方式很簡單:修改sendrandomcode代碼,在timer timertime;前加static,是其成為靜態的。這樣再次點擊時,就是知道線程已存在,先關閉再發送。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/ZXdeveloper/p/4831672.html