lixiaojun
2025-02-13 bfd1b73be85fd66ee37031eadcd4d09e7dafb52f
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
using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Yw;
using Yw.WinFrmUI;
using Timer = System.Windows.Forms.Timer;
 
namespace HStation.Desktop
{
    public partial class SmsLoginCtrl : DevExpress.XtraEditors.XtraUserControl, ILogin
    {
        public SmsLoginCtrl()
        {
            InitializeComponent();
            this.layoutControl1.SetupLayoutControl();
        }
 
        /// <summary>
        /// 登录开始事件
        /// </summary>
        public event Action LoginStartEvent;
 
        /// <summary>
        /// 登录取消事件
        /// </summary>
        public event Action LoginCancelEvent;
 
        /// <summary>
        /// 登录完成事件
        /// </summary>
        public event Action<Yw.Dto.UserLoginOutput> LoginEndEvet;
 
        private bool _isInitialized = false;//初始化
        private const string _template = "hzkw_sms_template";//微信登录模板
        private const string _software = "HStation_XHS_DESKTOP";//软件编码
 
        /// <summary>
        /// 初始化
        /// </summary>
        public void Initial()
        {
            if (_isInitialized)
            {
                return;
            }
            _isInitialized = true;
        }
 
        //发送验证码
        private async void btnSendCode_Click(object sender, EventArgs e)
        {
            this.dxErrorProvider1.ClearErrors();
            var mobile = this.txtMobileNumber.Text.Trim();
            if (!Yw.Service.Auth.MobileNumberHelper.Verify(mobile))
            {
                this.dxErrorProvider1.SetError(this.txtMobileNumber, "请输入正确手机号码");
                return;
            }
 
            var bol = await BLLFactory<Yw.BLL.ToolSms>.Instance.SendCode(_template, mobile, _software);
            if (bol)
            {
                this.labSendCode.Enabled = false;
                StartCountdown();
            }
            else
            {
                this.dxErrorProvider1.SetError(this.txtMobileNumber, "发送失败");
                this.labSendCode.Enabled = true;
            }
 
 
        }
 
        private void StartCountdown()
        {
            int countdown = 60;
            Timer timer = new Timer();
            timer.Interval = 1000; // 1秒
            timer.Tick += (s, e) =>
            {
                countdown--;
                if (countdown > 0)
                {
                    this.labSendCode.Text = $"{countdown}秒后重新发送";
                }
                else
                {
                    timer.Stop();
                    this.labSendCode.Text = "发送验证码";
                    this.labSendCode.Enabled = true;
                }
            };
            timer.Start();
        }
 
        //取消登录
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.LoginCancelEvent?.Invoke();
        }
 
        //登录
        private async void btnLogin_Click(object sender, EventArgs e)
        {
            this.dxErrorProvider1.ClearErrors();
            var mobile = this.txtMobileNumber.Text.Trim();
            if (!Yw.Service.Auth.MobileNumberHelper.Verify(mobile))
            {
                this.dxErrorProvider1.SetError(this.txtMobileNumber, "请输入正确手机号码");
                return;
            }
            var validCode = this.txtCode.Text.Trim();
            if (string.IsNullOrEmpty(validCode))
            {
                this.dxErrorProvider1.SetError(this.txtCode, "请输入验证码");
                return;
            }
            this.LoginStartEvent?.Invoke();
            var result = await BLLFactory<Yw.BLL.UserLogin>.Instance.LoginSoftwareStandardBySmsAccount(_template, mobile, _software, validCode, null);
            this.LoginEndEvet?.Invoke(result);
        }
 
 
    }
}