From 66f8c3e3139248ba1dbcbf6558e957d4efd83c92 Mon Sep 17 00:00:00 2001 From: wanggang <76527413@qq.com> Date: Thu, 9 Jan 2020 11:30:52 +0800 Subject: [PATCH] update Former-commit-id: 5f0eddcf4e1723fc03f88fd55e0c06a8d961de7b --- .../ModelStateDictionaryExtensions.cs | 22 ++++++++++- .../UserCenter/Controllers/TokenController.cs | 38 ++++++++----------- projects/WebUI/wwwroot/login.html | 26 ++++++------- projects/WebUI/wwwroot/login.js | 28 +++++++++----- projects/WebUI/wwwroot/pc.html | 2 +- projects/WebUI/wwwroot/pc.js | 9 +++-- projects/WebUI/wwwroot/pchome.js | 7 ++-- 7 files changed, 77 insertions(+), 55 deletions(-) diff --git a/projects/Infrastructure/Extensions/ModelStateDictionaryExtensions.cs b/projects/Infrastructure/Extensions/ModelStateDictionaryExtensions.cs index 0597ae4e..e4334d07 100644 --- a/projects/Infrastructure/Extensions/ModelStateDictionaryExtensions.cs +++ b/projects/Infrastructure/Extensions/ModelStateDictionaryExtensions.cs @@ -1,7 +1,9 @@ -using System.Collections.Generic; +using Microsoft.AspNetCore.Mvc.ModelBinding; +using System; +using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; -using Microsoft.AspNetCore.Mvc.ModelBinding; +using System.Linq.Expressions; namespace Infrastructure.Extensions { @@ -24,5 +26,21 @@ namespace Infrastructure.Extensions } } } + + public static object AddModelError(this ModelStateDictionary modelState, string message, string key = "", int code = 0) + { + modelState.AddModelError(key, message); + return new + { + code, + key = string.IsNullOrEmpty(key) ? key : key.Substring(0, 1).ToLower() + key.Substring(1), + message + }; + } + + public static object AddModelError(this ModelStateDictionary modelState, Expression> expression, string message, int code = 0) + { + return modelState.AddModelError(message, expression.GetPropertyName(), code); + } } } \ No newline at end of file diff --git a/projects/UserCenter/Controllers/TokenController.cs b/projects/UserCenter/Controllers/TokenController.cs index 0130cb20..31e718a4 100644 --- a/projects/UserCenter/Controllers/TokenController.cs +++ b/projects/UserCenter/Controllers/TokenController.cs @@ -5,6 +5,7 @@ using Infrastructure.Extensions; using Infrastructure.Security; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using System; using System.Linq; @@ -41,7 +42,7 @@ namespace UserCenter.Controllers var user = this._userRepo.Table().FirstOrDefault(o => o.UserName == model.UserName); if (user == null) { - ModelState.AddModelError("", "用户名或密码错误"); + return BadRequest(ModelState.AddModelError("用户名或密码错误")); } else { @@ -73,11 +74,11 @@ namespace UserCenter.Controllers if (user.AccessFailedCount >= maxAccessFailedCount) { user.LockoutEnd = DateTime.UtcNow.AddMinutes(lockoutEndMinutes); - ModelState.AddModelError(nameof(model.UserName), $"用户被锁定,请于{user.LockoutEnd.Value.ToLocalTime().ToString("HH:mm")}后重试"); + return BadRequest(ModelState.AddModelError(o => model.UserName, $"用户被锁定,请于{user.LockoutEnd.Value.ToLocalTime().ToString("HH:mm")}后重试", 1)); } else { - ModelState.AddModelError(nameof(model.UserName), $"密码错误,再错误{maxAccessFailedCount - user.AccessFailedCount}次后将锁定用户{lockoutEndMinutes}分钟"); + return BadRequest(ModelState.AddModelError(o => model.UserName, $"密码错误,再错误{maxAccessFailedCount - user.AccessFailedCount}次后将锁定用户{lockoutEndMinutes}分钟", 2)); } } this._userRepo.SaveChanges(); @@ -85,28 +86,17 @@ namespace UserCenter.Controllers } else//对未启用登录锁定的用户进行验证 { - if (user.PasswordHash == this._encryptionService.CreatePasswordHash(model.Password, user.SecurityStamp)) + if (user.PasswordHash != this._encryptionService.CreatePasswordHash(model.Password, user.SecurityStamp)) { - success = true; - } - else - { - ModelState.AddModelError("", "用户名或密码错误"); + return BadRequest(ModelState.AddModelError("用户名或密码错误")); } } } - if (success) - { - return Ok(new - { - AccessToken = Request.HttpContext.GetToken(model.UserName, _cfg, DateTime.Now.AddHours(_cfg.GetValue("AccessTokenHours", 0.5))), - RefreshToken = Request.HttpContext.GetToken(model.UserName, _cfg, DateTime.Now.AddHours(_cfg.GetValue("AccessTokenHours", 720))), - }); - } - else + return Ok(new { - return Unauthorized(ModelState); - } + AccessToken = Request.HttpContext.GetToken(model.UserName, _cfg, DateTime.Now.AddHours(_cfg.GetValue("AccessTokenHours", 0.5))), + RefreshToken = Request.HttpContext.GetToken(model.UserName, _cfg, DateTime.Now.AddHours(_cfg.GetValue("RefreshToken", 720))), + }); } catch (Exception ex) { @@ -153,7 +143,10 @@ namespace UserCenter.Controllers } var userName = User.Identity.Name; var model = this._userRepo.ReadOnlyTable() + .Include(o => o.UserRoles) + .ThenInclude(o => o.Role) .Where(o => o.UserName == userName) + .ToList() .Select(o => new { o.UserName, @@ -165,8 +158,9 @@ namespace UserCenter.Controllers o.Birthday, o.Email, o.PhoneNumber, - Roles = o.UserRoles.SelectMany(o => o.Role.Name) - }); + Roles = o.UserRoles.Select(o => o.Role.Name) + }) + .FirstOrDefault(); if (model == null) { ModelState.AddModelError("", "用户不存在"); diff --git a/projects/WebUI/wwwroot/login.html b/projects/WebUI/wwwroot/login.html index 7012cc38..21f45dac 100644 --- a/projects/WebUI/wwwroot/login.html +++ b/projects/WebUI/wwwroot/login.html @@ -5,21 +5,17 @@

登录

-
-
-
-
-
-
- -
-
-
-
-
- -
-
+
+
+
+
+ +
+
+
+
+
+
diff --git a/projects/WebUI/wwwroot/login.js b/projects/WebUI/wwwroot/login.js index 4d356f42..74c0a49c 100644 --- a/projects/WebUI/wwwroot/login.js +++ b/projects/WebUI/wwwroot/login.js @@ -25,22 +25,32 @@ methods: { submit: function () { weui.form.validate('#form', function (error) { - console.log(error); if (!error) { var loading = weui.loading('提交中...'); var form = $("#form"); var url = form.attr('action'); - $.ajax({ - url: url, - type: "POST", - data: form.serializeJSON(), - contentType: "application/json", - success: function (response) { - loading.hide(); - weui.toast(response, 3000); + var data = form.serializeJSON(); + axios.post(url, data).then(function (response) { + token = response.data.accessToken; + localStorage.setItem("accessToken", token); + refreshToken = response.data.refreshToken; + localStorage.setItem("refreshToken", refreshToken); + router.push('/'); + }).catch(function (error) { + console.log(error.response); + var data = error.response.data; + var key = error.response.data.key; + if (key) { + $("[name='" + key + "']").parents('.weui-cell').addClass('weui-cell_warn'); } + weui.topTips(data.message, { duration: 3000 }); + }).finally(function () { + loading.hide(); }); } + else { + console.log(error); + } }, this.regexp); } } diff --git a/projects/WebUI/wwwroot/pc.html b/projects/WebUI/wwwroot/pc.html index 81a0424b..d232d510 100644 --- a/projects/WebUI/wwwroot/pc.html +++ b/projects/WebUI/wwwroot/pc.html @@ -17,7 +17,7 @@
-

加载中

+

页面加载中

diff --git a/projects/WebUI/wwwroot/pc.js b/projects/WebUI/wwwroot/pc.js index 00f955b8..60944b63 100644 --- a/projects/WebUI/wwwroot/pc.js +++ b/projects/WebUI/wwwroot/pc.js @@ -30,6 +30,7 @@ const router = new VueRouter({ routes }); router.beforeEach((to, from, next) => { + $('#loadingToast').show(); if (!token && to.path !== '/login') { router.push('/login'); return; @@ -62,9 +63,12 @@ router.beforeEach((to, from, next) => { console.log('route from:'+from.path +' to:'+to.path); next(); } - }); - +router.afterEach((route, redirect) => { + Vue.nextTick(() => { + $('#loadingToast').fadeOut(100); + }) +}) /// const app = new Vue({ router, @@ -72,7 +76,6 @@ const app = new Vue({ }, mounted: function () { console.log('mounted:app'); - $('#loadingToast').fadeOut(100); }, methods: { } diff --git a/projects/WebUI/wwwroot/pchome.js b/projects/WebUI/wwwroot/pchome.js index 0ef1f635..acbe61b1 100644 --- a/projects/WebUI/wwwroot/pchome.js +++ b/projects/WebUI/wwwroot/pchome.js @@ -1,16 +1,17 @@ function pchome() { - return Vue.component('home', function (resolve, reject) { - axios.get("/home.html").then(function (response) { + return Vue.component('pchome', function (resolve, reject) { + axios.get("/pchome.html").then(function (response) { resolve({ template: response.data, data() { return { - name: 'home' + model:null }; }, mounted: function () { console.log('mounted:home'); weui.tab('#tab', { defaultIndex: 1 }); + }, methods: { }