|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.AspNetCore.Mvc; |
| 6 | +using NETCore.DapperKit.Web.ViewModels; |
| 7 | +using NETCore.DapperKit.Web.Model; |
| 8 | + |
| 9 | +// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 |
| 10 | + |
| 11 | +namespace NETCore.DapperKit.Web.Controllers |
| 12 | +{ |
| 13 | + public class AccountController : Controller |
| 14 | + { |
| 15 | + |
| 16 | + private readonly IDapperContext _DapperContext; |
| 17 | + |
| 18 | + public AccountController(IDapperContext context) |
| 19 | + { |
| 20 | + _DapperContext = context; |
| 21 | + } |
| 22 | + |
| 23 | + // GET: /<controller>/ |
| 24 | + public IActionResult Index() |
| 25 | + { |
| 26 | + return View(); |
| 27 | + } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// 登录方法 |
| 31 | + /// </summary> |
| 32 | + /// <param name="model">UserViewModel实体信息</param> |
| 33 | + /// <param name="returnUrl">返回Url地址</param> |
| 34 | + /// <returns></returns> |
| 35 | + [HttpPost] |
| 36 | + [ValidateAntiForgeryToken] |
| 37 | + public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null) |
| 38 | + { |
| 39 | + ViewData["ReturnUrl"] = returnUrl; |
| 40 | + if (!ModelState.IsValid) return View(model); |
| 41 | + |
| 42 | + try |
| 43 | + { |
| 44 | + var users = await _DapperContext.DbSet<SysUser>() |
| 45 | + .Select<SysUserRole>((u, r) => new SysUser() { Id = u.Id, IsAdmin = u.IsAdmin, LoginName = u.LoginName, LoginPwd = u.LoginPwd, CreateTime = u.CreateTime, UserName = u.UserName, UserRoleNo = u.UserRoleNo, RoleName = r.Name }) |
| 46 | + .Join<SysUserRole>((u, r) => u.UserRoleNo == r.No) |
| 47 | + .Where(m => m.LoginName == model.Account && m.LoginPwd == model.Password) |
| 48 | + .ToListAsync<SysUser>(); |
| 49 | + |
| 50 | + |
| 51 | + if (users != null && users.First().LoginName == model.Account) |
| 52 | + { |
| 53 | + return RedirectToAction("Index", "Home"); |
| 54 | + } |
| 55 | + } |
| 56 | + catch (Exception ex) |
| 57 | + { |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + return RedirectToAction("Index"); |
| 62 | + } |
| 63 | + |
| 64 | + } |
| 65 | +} |
0 commit comments