<%@ WebHandler Language="C#" Class="Session" %>
|
|
using Common;
|
using Dao;
|
using Model;
|
using Newtonsoft.Json;
|
using Newtonsoft.Json.Linq;
|
using System;
|
using System.Text.RegularExpressions;
|
|
public class Session : PageHandler<SysUser>
|
{
|
private UserDao dao = new UserDao();
|
|
public override JsonPageResult ProcessRequestInternal(PageContext<SysUser> context)
|
{
|
string @string = context.GetString("action");
|
switch (@string)
|
{
|
case "login":
|
return this.Login(context);
|
case "logout":
|
return this.Logout(context);
|
case "changePassword":
|
return this.ChangePassword(context);
|
case "mobileCode":
|
return this.MobileCode(context);
|
case "mobileLogin":
|
return this.MobileLogin(context);
|
default:
|
throw new Exception("Invalid Action=" + @string);
|
}
|
}
|
|
private JsonPageResult MobileCode(PageContext<SysUser> context)
|
{
|
string @string = context.GetString("mobile");
|
if (!Regex.IsMatch(@string, "1\\d{10}"))
|
throw new ArgumentException("手机号码无效,请重新输入!");
|
string account = context.SessionObject.Account;
|
if (string.IsNullOrEmpty(account))
|
throw new ArgumentException("请重新确认登录账号!");
|
SysUser user = this.dao.GetUser(account);
|
if (@string != user.Mobile)
|
throw new ArgumentException("与账号绑定手机号码不符合!");
|
int num = new Random().Next(100000, 999999);
|
string smsProfile = "";
|
using (GwSettingDao gwSettingDao = new GwSettingDao())
|
smsProfile = JsonConvert.DeserializeObject<JObject>(gwSettingDao.GetCurrentSetting().SmsProfile).GetValue("MTURL").ToString();
|
if (string.IsNullOrEmpty(smsProfile))
|
throw new ArgumentException("未能找到短信下发地址!");
|
new MessageCenter().SubmitMessage(smsProfile, @string, num.ToString());
|
context.Session.Add("SmsCode", (object) num);
|
context.Session.Add("SmsMobile", (object) @string);
|
context.Session.Add("SmsStatus", (object) 1);
|
return new JsonPageResult(true, (object) "验证码已经下发,请注意查收!");
|
}
|
|
private JsonPageResult MobileLogin(PageContext<SysUser> context)
|
{
|
string string1 = context.GetString("mobile");
|
string string2 = context.GetString("smscode");
|
if (!Regex.IsMatch(string1, "1\\d{10}"))
|
throw new ArgumentException("手机号码无效,请重新输入!");
|
if (string.IsNullOrEmpty(string2))
|
throw new ArgumentException("短信验证码不能为空!");
|
if (string2.Length > 6)
|
throw new ArgumentException("你输入的验证码有误,请重新输入!");
|
if (!1.Equals(context.Session["SmsStatus"]))
|
throw new ArgumentException("尚未获取验证码,请获取验证码!");
|
if (!string1.Equals(context.Session["SmsMobile"]))
|
throw new ArgumentException("你的电话号码有误!");
|
if (!string2.Equals(context.Session["SmsCode"].ToString()))
|
throw new ArgumentException("验证码有误,请重新输入!");
|
context.Session.Remove("SmsCode");
|
context.Session.Remove("SmsStatus");
|
return new JsonPageResult(true, (object) "验证成功!");
|
}
|
|
private JsonPageResult ChangePassword(PageContext<SysUser> context)
|
{
|
string string1 = context.GetString("oldpassword");
|
string string2 = context.GetString("password2");
|
string string3 = context.GetString("password1");
|
if (!string.Equals(string3, string2))
|
throw new ArgumentException("两次密码不一致,请重新输入!");
|
if (string3.Length < 6)
|
throw new ArgumentException("新密码长度至少要6位!");
|
string account = context.SessionObject.Account;
|
if (string.IsNullOrEmpty(account))
|
throw new ArgumentException("获取账户信息出错,请重新登录!");
|
this.dao.GetUser(account);
|
SysUser userInfo = this.dao.GetUserInfo(context.SessionObject.UserID);
|
string str = userInfo.Password == DataHelper.MD5Hex(string1) ? userInfo.Password : string1;
|
if (!string.Equals(context.SessionObject.Password, str))
|
throw new ArgumentException("原始密码不正确,请重新输入!");
|
if (this.dao.UpdatePassword(context.SessionObject.UserID, str, DataHelper.MD5Hex(string3)))
|
context.SessionObject.Password = DataHelper.MD5Hex(string3);
|
return new JsonPageResult(true, (object) "密码更新成功!");
|
}
|
|
private JsonPageResult Login(PageContext<SysUser> context)
|
{
|
string a = context.Session["Code"] as string;
|
string string1 = context.GetString("code");
|
string string2 = context.GetString("account");
|
string string3 = context.GetString("password");
|
string str = "登录成功!";
|
int num = 2;
|
if (string.IsNullOrEmpty(string2))
|
return new JsonPageResult(false, (object) new
|
{
|
Content = "请输入账号后再登录!",
|
Tstatus = num
|
});
|
if (string.IsNullOrEmpty(string3))
|
return new JsonPageResult(false, (object) new
|
{
|
Content = "请输入密码后再登录!",
|
Tstatus = num
|
});
|
if (!string.Equals(a, string1, StringComparison.OrdinalIgnoreCase))
|
return new JsonPageResult(false, (object) new
|
{
|
Content = "验证码错误,请重新输入!",
|
Tstatus = num
|
});
|
SysUser user = new SysUser();
|
|
try
|
{
|
if (!this.dao.CheckLogin(string2, string3, user))
|
return new JsonPageResult(false, (object)new
|
{
|
Content = "用户名或密码错误!",
|
Tstatus = num
|
});
|
}
|
catch (Exception ex)
|
{
|
return new JsonPageResult(false, (object)new
|
{
|
Content = "异常:"+ex.Message,
|
Tstatus = num
|
});
|
}
|
context.SessionObject = user;
|
string clientIp = user.ClientIp;
|
if (user.IsVerification == 1 && !context.ClientIP.Equals(clientIp))
|
return new JsonPageResult(false, (object) new
|
{
|
Content = "你登录的IP异常,请短信验证登录!",
|
Tstatus = 1
|
});
|
return new JsonPageResult(true, (object) new
|
{
|
Content = str,
|
Tstatus = num
|
});
|
}
|
|
private JsonPageResult Logout(PageContext<SysUser> context)
|
{
|
context.Reset();
|
return new JsonPageResult(true, (object) "您已经成功从系统退出!");
|
}
|
}
|