重构代码:重写Notification接口返回数据,移除不必要的数据,减少网络带宽,更改用户验证接口到api/Register

This commit is contained in:
Argo-MacBookPro 2018-09-13 16:34:32 +08:00
parent 7bc98fb336
commit 0fdbaa0dbb
8 changed files with 100 additions and 105 deletions

View File

@ -1,7 +1,7 @@
using Bootstrap.DataAccess;
using Bootstrap.Security;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.Admin.Controllers
@ -13,15 +13,20 @@ namespace Bootstrap.Admin.Controllers
public class NewController : Controller
{
/// <summary>
/// 登录页面注册新用户remote validate调用
/// 通知管理页面获得所有新用户方法调用
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
[HttpGet]
[AllowAnonymous]
public bool Get(string userName)
public IEnumerable<object> Get()
{
return BootstrapUser.RetrieveUserByUserName(userName) == null && !UserHelper.RetrieveNewUsers().Any(u => u.UserName == userName);
return UserHelper.RetrieveNewUsers().Select(user => new
{
user.Id,
user.UserName,
user.DisplayName,
user.Description,
user.RegisterTime
});
}
/// <summary>
/// 登录页面注册新用户提交按钮调用

View File

@ -1,7 +1,6 @@
using Bootstrap.DataAccess;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.Admin.Controllers.Api
@ -17,39 +16,41 @@ namespace Bootstrap.Admin.Controllers.Api
/// </summary>
/// <returns></returns>
[HttpGet]
public Notifications Get()
public object Get()
{
var ret = new Notifications();
if (!User.IsInRole("Administrators")) return ret;
if (!User.IsInRole("Administrators")) return false;
// New Users
var user = UserHelper.RetrieveNewUsers();
ret.Users = user.Take(6).ToList();
ret.Users.AsParallel().ForAll(n =>
{
var ts = DateTime.Now - n.RegisterTime;
if (ts.TotalMinutes < 5) n.Period = "刚刚";
else if (ts.Days > 0) n.Period = string.Format("{0}天", ts.Days);
else if (ts.Hours > 0) n.Period = string.Format("{0}小时", ts.Hours);
else if (ts.Minutes > 0) n.Period = string.Format("{0}分钟", ts.Minutes);
});
ret.NewUsersCount = user.Count();
var usersCount = user.Count();
user = user.Take(6);
user.AsParallel().ForAll(n =>
{
var ts = DateTime.Now - n.RegisterTime;
if (ts.TotalMinutes < 5) n.Period = "刚刚";
else if (ts.Days > 0) n.Period = string.Format("{0}天", ts.Days);
else if (ts.Hours > 0) n.Period = string.Format("{0}小时", ts.Hours);
else if (ts.Minutes > 0) n.Period = string.Format("{0}分钟", ts.Minutes);
});
// Tasks
var task = TaskHelper.RetrieveTasks();
ret.Tasks = task.Take(6).ToList();
ret.TasksCount = task.Count();
var tasksCount = task.Count();
//Message
var message = MessageHelper.RetrieveMessagesHeader(User.Identity.Name);
ret.Messages = message.Take(6).ToList();
ret.Messages.AsParallel().ForAll(m => m.FromIcon = Url.Content(m.FromIcon));
ret.MessagesCount = message.Count();
var messagesCount = message.Count();
message = message.Take(6);
message.AsParallel().ForAll(m => m.FromIcon = Url.Content(m.FromIcon));
//Apps
var apps = ExceptionHelper.RetrieveExceptions().Where(n => n.ExceptionType != "Longbow.Data.DBAccessException");
ret.Apps = apps.Take(6).ToList();
ret.Apps.AsParallel().ForAll(n =>
var appExceptionsCount = apps.Count();
apps = apps.Take(6);
apps.AsParallel().ForAll(n =>
{
n.ExceptionType = n.ExceptionType.Split('.').Last();
var ts = DateTime.Now - n.LogTime;
@ -58,12 +59,13 @@ namespace Bootstrap.Admin.Controllers.Api
else if (ts.Hours > 0) n.Period = string.Format("{0}小时", ts.Hours);
else if (ts.Minutes > 0) n.Period = string.Format("{0}分钟", ts.Minutes);
});
ret.AppExceptionsCount = apps.Count();
//Dbs
var dbs = ExceptionHelper.RetrieveExceptions().Where(n => n.ExceptionType == "Longbow.Data.DBAccessException");
ret.Dbs = dbs.Take(6).ToList();
ret.Dbs.AsParallel().ForAll(n =>
var dbExceptionsCount = dbs.Count();
dbs = dbs.Take(6);
dbs.AsParallel().ForAll(n =>
{
var ts = DateTime.Now - n.LogTime;
if (ts.TotalMinutes < 5) n.Period = "刚刚";
@ -71,72 +73,20 @@ namespace Bootstrap.Admin.Controllers.Api
else if (ts.Hours > 0) n.Period = string.Format("{0}小时", ts.Hours);
else if (ts.Minutes > 0) n.Period = string.Format("{0}分钟", ts.Minutes);
});
ret.DbExceptionsCount = dbs.Count();
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
[HttpGet("{id}")]
public object Get(string id)
{
var ret = new object();
if (id == "newusers") ret = UserHelper.RetrieveNewUsers().ToList();
return ret;
}
public class Notifications
{
public Notifications()
return new
{
Users = new List<User>();
Apps = new List<Exceptions>();
Dbs = new List<Exceptions>();
Tasks = new List<Task>();
Messages = new List<Message>();
}
/// <summary>
///
/// </summary>
public List<User> Users { get; set; }
/// <summary>
///
/// </summary>
public List<Exceptions> Apps { get; set; }
/// <summary>
///
/// </summary>
public List<Exceptions> Dbs { get; set; }
/// <summary>
///
/// </summary>
public List<Task> Tasks { get; set; }
/// <summary>
///
/// </summary>
public List<Message> Messages { get; set; }
/// <summary>
/// 获得/设置 消息数量
/// </summary>
public int MessagesCount { get; set; }
/// <summary>
/// 获得/设置 新用户数量
/// </summary>
public int NewUsersCount { get; set; }
/// <summary>
/// 获取/设置 任务数量
/// </summary>
public int TasksCount { get; set; }
/// <summary>
/// 获取/设置 应用程序错误数量
/// </summary>
public int AppExceptionsCount { get; set; }
/// <summary>
/// 获取/设置 数据库错误数量
/// </summary>
public int DbExceptionsCount { get; set; }
NewUsersCount = usersCount,
TasksCount = tasksCount,
MessagesCount = messagesCount,
AppExceptionsCount = appExceptionsCount,
DbExceptionsCount = dbExceptionsCount,
Users = user.Select(i => new { i.Period, i.UserName, i.DisplayName, i.Description }),
Tasks = task.Take(6),
Messages = message,
Apps = apps.Select(n => new { n.ExceptionType, n.Message, n.Period }),
Dbs = dbs.Select(n => new { n.ErrorPage, n.Message, n.Period })
};
}
}
}

View File

@ -0,0 +1,24 @@
using Bootstrap.DataAccess;
using Bootstrap.Security;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
namespace Bootstrap.Admin.Controllers.Api
{
[AllowAnonymous]
[Route("api/[controller]")]
public class RegisterController : Controller
{
/// <summary>
/// 登录页面注册新用户remote validate调用
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
[HttpGet]
public bool Get(string userName)
{
return BootstrapUser.RetrieveUserByUserName(userName) == null && !UserHelper.RetrieveNewUsers().Any(u => u.UserName == userName);
}
}
}

View File

@ -91,7 +91,7 @@
<span class="fa fa-user-plus"></span>
</div>
</div>
<input type="text" id="userName" autocomplete="off" class="form-control" placeholder="登陆账号不可为空" minlength="4" maxlength="50" remote="api/New/" data-remote-msg="此用户已存在" data-valid="true" />
<input type="text" id="userName" autocomplete="off" class="form-control" placeholder="登陆账号不可为空" minlength="4" maxlength="50" remote="api/Register/" data-remote-msg="此用户已存在" data-valid="true" />
</div>
</div>
<div class="form-group">

View File

@ -50,7 +50,7 @@
<div class="form-row">
<div class="form-group col-sm-6">
<label class="control-label" for="userName">登陆名称</label>
<input type="text" class="form-control" id="userName" placeholder="不可为空50字以内" minlength="4" maxlength="50" remote="api/New/" data-remote-msg="此用户已存在" data-valid="true" />
<input type="text" class="form-control" id="userName" placeholder="不可为空50字以内" minlength="4" maxlength="50" remote="api/Register/" data-remote-msg="此用户已存在" data-valid="true" />
</div>
<div class="form-group col-sm-6">
<label class="control-label" for="displayName">显示名称</label>

View File

@ -20,6 +20,22 @@
return html;
};
var setBadge = function (source) {
var data = $.extend({
TasksCount: 0,
AppExceptionsCount: 0,
DbExceptionsCount: 0,
MessagesCount: 0,
NewUsersCount: 0
}, source);
$('#msgHeaderTaskBadge').text(data.TasksCount === 0 ? "" : data.TasksCount);
$('#msgHeaderUserBadge').text(data.NewUsersCount === 0 ? "" : data.NewUsersCount);
$('#msgHeaderAppBadge').text(data.AppExceptionsCount === 0 ? "" : data.AppExceptionsCount);
$('#msgHeaderDbBadge').text(data.DbExceptionsCount === 0 ? "" : data.DbExceptionsCount);
$('#msgHeaderMsgBadge').text(data.MessagesCount === 0 ? "" : data.MessagesCount);
$('#logoutNoti').text(data.NewUsersCount === 0 ? "" : data.NewUsersCount);
};
$.fn.extend({
nestMenu: function (callback) {
var $this = $(this);
@ -35,6 +51,7 @@
});
},
clearWidgetItems: function () {
setBadge(false);
this.children('.dropdown').each(function () {
$(this).children('.dropdown-menu').each(function () {
$(this).children('a').remove();
@ -48,11 +65,13 @@
$.bc({
url: Notifications.url,
callback: function (result) {
$('#logoutNoti').text(result.NewUsersCount === 0 ? "" : result.NewUsersCount);
that.clearWidgetItems();
if (!result) return;
setBadge(result);
// tasks
$('#msgHeaderTask').text(result.TasksCount);
$('#msgHeaderTaskBadge').text(result.TasksCount === 0 ? "" : result.TasksCount);
var htmlUserTemplate = '<a class="dropdown-item" href="{4}?id={3}"><span class="desc">{0}-{2}</span><span class="percent">{1}%</span></span><div class="progress progress-striped"><div class="progress-bar" role="progressbar" aria-valuenow="{1}" aria-valuemin="0" aria-valuemax="100" style="width: {1}%"><span class="sr-only">{1}% 完成</span></div></div></a>';
var html = result.Tasks.map(function (u) {
return $.format(htmlUserTemplate, u.TaskName, u.TaskProgress, u.AssignDisplayName, u.Id, $.formatUrl('Admin/Tasks'));
@ -61,7 +80,6 @@
// new users
$('#msgHeaderUser').text(result.NewUsersCount);
$('#msgHeaderUserBadge').text(result.NewUsersCount === 0 ? "" : result.NewUsersCount);
htmlUserTemplate = '<a class="dropdown-item" href="{4}"><span class="label label-success"><i class="fa fa-plus"></i></span><div title="{2}" class="content">{1}({0})</div><span class="small italic">{3}</span></a>';
html = result.Users.map(function (u) {
return $.format(htmlUserTemplate, u.UserName, u.DisplayName, u.Description, u.Period, $.formatUrl('Admin/Notifications'));
@ -70,7 +88,6 @@
// apps
$('#msgHeaderApp').text(result.AppExceptionsCount);
$('#msgHeaderAppBadge').text(result.AppExceptionsCount === 0 ? "" : result.AppExceptionsCount);
htmlUserTemplate = '<a class="dropdown-item" href="{3}"><span class="label label-warning"><i class="fa fa-bug"></i></span><div title="{1}" class="content">{0}</div><span class="small italic">{2}</span></a>';
html = result.Apps.map(function (u) {
return $.format(htmlUserTemplate, u.ExceptionType, u.Message, u.Period, $.formatUrl('Admin/Exceptions'));
@ -79,7 +96,6 @@
// dbs
$('#msgHeaderDb').text(result.DbExceptionsCount);
$('#msgHeaderDbBadge').text(result.DbExceptionsCount === 0 ? "" : result.DbExceptionsCount);
htmlUserTemplate = '<a class="dropdown-item" href="{3}"><span class="label label-danger"><i class="fa fa-bolt"></i></span><div title="{1}" class="content">{0}</div><span class="small italic">{2}</span></a>';
html = result.Dbs.map(function (u) {
return $.format(htmlUserTemplate, u.ErrorPage, u.Message, u.Period, $.formatUrl('Admin/Exceptions'));
@ -88,7 +104,6 @@
// messages
$('#msgHeaderMsg').text(result.MessagesCount);
$('#msgHeaderMsgBadge').text(result.MessagesCount === 0 ? "" : result.MessagesCount);
htmlUserTemplate = '<a class="dropdown-item" href="{6}?id={0}"><span class="photo"><img alt="avatar" src="{1}"></span><span class="subject"><span class="from">{2}</span><span class="time">{4}</span></span><span class="message" title="{5}">{3}</span></a>';
html = result.Messages.map(function (u) {
return $.format(htmlUserTemplate, u.Id, u.FromIcon, u.FromDisplayName, u.Title, u.Period, u.Content, $.formatUrl('Admin/Messages'));

View File

@ -14,7 +14,7 @@
$('#btnSubmit').on('click', function () {
$.bc({
url: 'api/New',
url: "api/New/",
data: { UserName: $('#userName').val(), Password: $('#password').val(), DisplayName: $('#displayName').val(), Description: $('#description').val() },
modal: '#dialogNew',
method: "post",

View File

@ -1,8 +1,9 @@
$(function () {
$('.site-footer').footer();
var apiUrl = "api/New/";
var $table = $('table').smartTable({
url: Notifications.url + "newusers",
url: apiUrl,
sidePagination: "client",
showToggle: false,
showRefresh: false,
@ -23,7 +24,7 @@
var id = $this.attr('data-id');
var result = $this.attr('data-result');
$.bc({
id: id, url: 'api/New/', method: "put", data: { Id: id, UserStatus: result }, title: result === "ApproveUser" ? "授权用户" : "拒绝用户",
id: id, url: apiUrl, method: "put", data: { Id: id, UserStatus: result }, title: result === "ApproveUser" ? "授权用户" : "拒绝用户",
callback: function (result) {
if (!result) return;
$table.bootstrapTable('refresh');