黄学彪
2020-12-16 0d9f64668fd7350d6a21fd157e32009a96d98134
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Shared.Phone.UserCenter.Password
{
    /// <summary>
    /// 验证新手机的画面
    /// </summary>
    public class CheckNewPhoneForm : EditorCommonForm
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 错误信息控件
        /// </summary>
        private NormalViewControl btnErrorMsg = null;
        /// <summary>
        /// 标记是否能够校验验证码了
        /// </summary>
        private bool canCheckCode = false;
        /// <summary>
        /// 新手机(防止恶意变更)
        /// </summary>
        private string newPhone = string.Empty;
        /// <summary>
        /// 能否发送验证码
        /// </summary>
        private bool canSendCode = true;
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// 画面显示(底层会固定调用此方法,借以完成画面创建)
        /// </summary>
        public void ShowForm()
        {
            //设置头部信息
            base.SetTitleText(Language.StringByID(R.MyInternationalizationString.uEditorPhoneNumber));
 
            //初始化中部信息
            this.InitMiddleFrame();
        }
 
        /// <summary>
        /// 初始化中部信息
        /// </summary>
        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 btnArea = new NormalViewControl(127, 58, true);
            btnArea.Text = "+86";
            btnArea.TextAlignment = TextAlignment.Center;
            btnArea.TextColor = UserCenterColor.Current.TextGrayColor3;
            btnArea.Gravity = Gravity.CenterVertical;
            frame.AddChidren(btnArea);
            //目前只针对+86
            //btnArea.ButtonClickEvent += (sender, e) =>
            //{
            //    var form = new AreaCodeSelectForm();
            //    form.AddForm();
            //    form.FinishSelectEvent += (code) =>
            //    {
            //        btnArea.Text = "+" + code;
            //    };
            //};
 
            //输入框
            var txtValue = new TextInputControl(Application.GetRealWidth(510), frame.Height - HdlControlResourse.BottomLineHeight, false);
            txtValue.X = btnArea.Right + Application.GetRealWidth(35);
            txtValue.PlaceholderText = Language.StringByID(R.MyInternationalizationString.uPleaseInputNewPhoneNumber);
            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.UserPhone) == 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.uAuthenticationSuccessAndBindNewPhone;
                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.SaveNewPhoneNumber(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.IsBold = true;
            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.oldBackgroundColor = UserCenterColor.Current.ClickButtonDefultColor;
            btnOk.CanClick = false;
            btnOk.ButtonClickEvent += (sender, e) =>
            {
                //清空输入的值
                btnCodeControl.ClearInputValue();
 
                this.btnErrorMsg.Visible = false;
                string areaCode = btnArea.Text.Substring(1);
                //检测手机号码
                if (this.CheckPhoneNumber(areaCode, txtValue.Text.Trim()) == false)
                {
                    return;
                }
                //发送验证码到手机
                this.SendCodeToPhone(btnOk, areaCode, txtValue.Text.Trim());
            };
 
            txtValue.TextChangeEventHandler += (sender, value) =>
            {
                if (value == string.Empty)
                {
                    //按键不可用
                    btnOk.CanClick = false;
                }
                else if (btnOk.CanClick == false)
                {
                    //按键可用
                    btnOk.CanClick = true;
                }
            };
        }
 
        #endregion
 
        #region ■ 发送验证码到手机___________________
 
        /// <summary>
        /// 发送验证码到手机
        /// </summary>
        private void SendCodeToPhone(BottomClickButton btnNext,string areaCode, string phoneNum)
        {
            var errorMsg = HdlAccountLogic.Current.SendVeriCodeToPhone(areaCode, phoneNum, VerCodeType.A绑定);
            if (errorMsg != null)
            {
                //显示错误
                this.btnErrorMsg.Visible = true;
                this.btnErrorMsg.Text = errorMsg;
                return;
            }
            //不能再发送验证码
            this.canSendCode = false;
            //可以开始校验验证码了
            this.canCheckCode = true;
            //记录起这个手机,防止恶意变更
            this.newPhone = phoneNum;
            //控件不能再次按下
            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);
                }
                Application.RunOnMainThread(() =>
                {
                    if (btnNext != null)
                    {
                        //获取验证码
                        btnNext.TextID = R.MyInternationalizationString.uGetVerificationCode;
                        //按键可以按下
                        btnNext.CanClick = true;
                    }
                });
            });
        }
 
        #endregion
 
        #region ■ 变更手机___________________________
 
        /// <summary>
        /// 变更手机号
        /// </summary>
        private void SaveNewPhoneNumber(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.newPhone, code);
            if (errorMsg != null)
            {
                this.ShowMassage(ShowMsgType.Tip, errorMsg);
                //可以重新发送验证码
                this.canSendCode = true;
                return;
            }
 
            //成功绑定新手机号
            string msg = Language.StringByID(R.MyInternationalizationString.uBindPhoneNumberSuccess);
            this.ShowMassage(ShowMsgType.Normal, msg, () =>
            {
                //刷新个人信息画面
                this.RefreshUserInfoForm();
            });
        }
 
        /// <summary>
        /// 刷新个人信息画面
        /// </summary>
        private void RefreshUserInfoForm()
        {
            //如果修改的是账号的话,则重新登录
            if (HdlUserCenterResourse.UserInfo.UserPhone == Shared.Common.Config.Instance.Account)
            {
                HdlAccountLogic.Current.ReLoginAgain(newPhone);
                return;
            }
            HdlUserCenterResourse.UserInfo.UserPhone = newPhone;
            this.CloseForm();
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 检测手机号
        /// </summary>
        private bool CheckPhoneNumber(string areaCode, string phoneNum)
        {
            //输入为空
            if (phoneNum == string.Empty)
            {
                //请输入新手机号码
                this.btnErrorMsg.Visible = true;
                this.btnErrorMsg.TextID = R.MyInternationalizationString.uPleaseInputNewPhoneNumber;
                return false;
            }
 
            //检测手机号格式
            if (HdlCheckLogic.Current.CheckPhoneNumber(phoneNum, areaCode) == false)
            {
                //这不是一个有效的手机号
                this.btnErrorMsg.Visible = true;
                this.btnErrorMsg.TextID = R.MyInternationalizationString.uThisIsNotPhoneNumberType;
                return false;
            }
            return true;
        }
 
        #endregion
    }
}