admin
2020-06-17 9d3d08ba960fc739498b0648d57eaf2c50a40fd1
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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
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 WindowsFormsApp1.entity;
using WindowsFormsApp1.utils;
using WindowsFormsApp1.utils.ui;
 
namespace WindowsFormsApp1
{
    public delegate void LoginSuccessDelegate(UserInfo userInfo);
 
    public partial class LoginNew : Form
    {
       private  LoginSuccessDelegate loginSuccess;
        public LoginNew(LoginSuccessDelegate loginSuccess)
        {
            InitializeComponent();
            pictureBox1_Click(null, null);
            this.loginSuccess = loginSuccess;
        }
 
        private void Form6_Load(object sender, EventArgs e)
        {
 
        }
 
        //验证
        private bool verify() {
            if (StringUtil.isEmpty(this.textBox1.Text))
            {
                MessageBox.Show("请输入邮箱");
                return false;
            }
 
            if (StringUtil.isEmpty(this.textBox3.Text))
            {
                MessageBox.Show("请输入验证码");
                return false;
            }
 
            return true;
        }
 
        //登录
        private void login_Click(object sender, EventArgs e)
        {
 
            if (!verify())
                return;
 
            ApiUtil.Login(this.textBox1.Text.Trim(), this.textBox3.Text.Trim(),this.textBox4.Text, (String result) =>
            {
                JObject obj = JObject.Parse(result);
                if (obj["code"] != null && Convert.ToInt32(obj["code"]) == 0)
                {
                    MessageBox.Show("登录成功!");
                    this.BeginInvoke(new LoginSuccessDelegate((UserInfo user1) =>
                    {
                        this.Close();
                    }));
 
                    UserInfo user=(UserInfo) JsonConvert.DeserializeObject(obj["data"].ToString());
                    if (loginSuccess != null)
                        loginSuccess(user);
                }
                else
                {
                    MessageBox.Show(obj["msg"].ToString());
                }
 
            });
        }
 
        delegate void SendVerifyCodeDelegate();
 
        private void startTimer() {
            this.timer1.Start();
        }
 
 
        //发送短信验证码
        private void sendverifycode_Click(object sender, EventArgs e)
        {
 
            if (StringUtil.isEmpty(this.textBox2.Text))
            {
                MessageBox.Show("请输入图片验证码");
                return;
            }
 
            if (StringUtil.isEmpty(this.textBox1.Text)) {
                MessageBox.Show("请输入邮箱");
                return;
            }
            ApiUtil.SendVerifyCode(this.textBox1.Text.Trim(), this.textBox2.Text.Trim(), (String result) =>
            {
                JObject obj=    JObject.Parse(result);
                if (obj["code"] != null && Convert.ToInt32(obj["code"]) == 0)
                {
                    time = 60;
                    this.BeginInvoke(new SendVerifyCodeDelegate(startTimer));
                    MessageBox.Show("验证码发送成功!");
                }
                else {
                    MessageBox.Show(obj["msg"].ToString());
                }
 
            }); 
 
 
        }
        //刷新验证码
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            ImageUtil.DisplayImage(this.pictureBox1, ApiUtil.BASE_URL + "user/imgcode?t_" + DateTime.Now);
 
        }
 
        int time = 60;
 
        private void timer1_Tick(object sender, EventArgs e)
        {
 
            if (time <= 0)
            {
                this.button1.Text = "发送验证码";
                this.button1.Enabled = true;
                this.timer1.Stop();
                this.timer1=   new System.Windows.Forms.Timer(this.components);
                this.timer1.Interval = 1000;
                this.timer1.Tick += timer1_Tick;
            }
            else
           {
               time--;
               this.button1.Text = (time + "s后重发");
                this.button1.Enabled = false;
            }
        }
 
        private void LoginNew_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.timer1.Stop();
        }
    }
}