yzh
2022-03-14 94879204c236edaf6ee80a176c2a781a352b2b93
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
<%@ 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) "您已经成功从系统退出!");
    }
}