wzp
2021-07-19 58ec6ffd2dc6a3e490e28026dd559352678a273d
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FineAdmin.Common;
using FineAdmin.IService;
using FineAdmin.Model;
using FineAdmin.Web.Areas.SysSet.Models;
 
namespace FineAdmin.Web.Controllers
{
    public class LoginController : Controller
    {
        public IUserService UserService { get; set; }
        public ILogonLogService LogonLogService { get; set; }
        // GET: Login
        public ActionResult Index()
        {
            return View(new WebModel().GetWebInfo());
        }
        [HttpGet]
        public ActionResult GetAuthCode()
        {
            return File(new VerifyCode().GetVerifyCode(), @"image/Gif");
        }
        [HttpPost]
        public ActionResult LoginOn(string username, string password, string captcha)
        {
            LogonLogModel logEntity = new LogonLogModel();
            logEntity.LogType = DbLogType.Login.ToString();
            try
            {
                if (Session["session_verifycode"].IsEmpty() || Md5.md5(captcha.ToLower(), 16) != Session["session_verifycode"].ToString())
                {
                    throw new Exception("验证码错误");
                }
                UserModel userEntity = UserService.LoginOn(username, Md5.md5(password, 32));
                if (userEntity != null)
                {
                    if (userEntity.EnabledMark==1)
                    {
                        throw new Exception("账号被锁定,禁止登录");
                    }
                    OperatorModel operatorModel = new OperatorModel();
                    operatorModel.UserId = userEntity.Id;
                    operatorModel.Account = userEntity.Account;
                    operatorModel.RealName = userEntity.RealName;
                    operatorModel.HeadIcon = userEntity.HeadIcon;
                    operatorModel.RoleId = userEntity.RoleId;
                    operatorModel.LoginIPAddress = Net.Ip;
                    operatorModel.LoginIPAddressName = Net.GetLocation(Net.Ip);
                    OperatorProvider.Provider.AddCurrent(operatorModel);
                    logEntity.Account = userEntity.Account;
                    logEntity.RealName = userEntity.RealName;
                    logEntity.Description = "登陆成功";
                    LogonLogService.WriteDbLog(logEntity);
                    return Content(new AjaxResult { state = ResultType.success.ToString(), message = "登录成功" }.ToJson());
                }
                else
                {
                    throw new Exception("用户名或密码错误");
                }
            }
            catch (Exception ex)
            {
                logEntity.Account = username;
                logEntity.RealName = username;
                logEntity.Description = "登录失败," + ex.Message;
                LogonLogService.WriteDbLog(logEntity);
                return Content(new AjaxResult { state = ResultType.error.ToString(), message = ex.Message }.ToJson());
            }
        }
        [HttpGet]
        public ActionResult LoginOut()
        {
            LogonLogService.WriteDbLog(new LogonLogModel
            {
                LogType = DbLogType.Exit.ToString(),
                Account = OperatorProvider.Provider.GetCurrent().Account,
                RealName = OperatorProvider.Provider.GetCurrent().RealName,
                Description = "安全退出系统",
            });
            Session.Abandon();
            Session.Clear();
            OperatorProvider.Provider.RemoveCurrent();
            return RedirectToAction("Index", "Login");
        }
    }
}