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();
|
}
|
}
|
}
|