将用户增删改操作记录到日志中,修改了日志表脚本

This commit is contained in:
lq_avenue 2016-11-04 15:22:44 +08:00
parent cd0b914aa0
commit 20b92010cb
10 changed files with 172 additions and 12 deletions

View File

@ -128,10 +128,12 @@
<Content Include="Content\fonts\glyphicons-halflings-regular.svg" />
<Content Include="Content\images\success.png" />
<Content Include="Content\js\common-scripts.js" />
<Content Include="Content\js\jQuery.cookie.js" />
<Content Include="Content\js\jquery.dcjqaccordion.2.7.js" />
<Content Include="Content\js\jquery.nicescroll.min.js" />
<Content Include="Content\js\jquery.scrollTo.js" />
<Content Include="Content\js\jquery.scrollTo.min.js" />
<Content Include="Content\js\log.js" />
<Content Include="Content\js\longbow.dataentity.js" />
<Content Include="Global.asax" />
<Content Include="Content\fonts\glyphicons-halflings-regular.woff2" />

View File

@ -144,7 +144,7 @@
type: 'DELETE',
success: function (result) {
if ($.isFunction(options.success)) options.success('del', options);
if (result) setTimeout(function () { swal("成功!", "删除数据", "success"); $(options.bootstrapTable).bootstrapTable('refresh'); }, 100);
if (result) setTimeout(function () { swal("成功!", "删除数据", "success"); $(options.bootstrapTable).bootstrapTable('refresh'); saveLog(options, "DELETE"); }, 100);
else setTimeout(function () { swal("失败", "删除数据", "error"); }, 200);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
@ -183,6 +183,7 @@
}
}
if (options.modal.constructor === String) $('#' + options.modal).modal("hide");
saveLog(options, "POST");
swal("成功", "保存数据", "success");
}
else {

View File

@ -0,0 +1,89 @@
/*jslint browser: true */ /*global jQuery: true */
/**
* jQuery Cookie plugin
*
* Copyright (c) 2010 Klaus Hartl (stilbuero.de)
* Dual licensed under the MIT and GPL licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*/
// TODO JsDoc
/**
* Create a cookie with the given key and value and other optional parameters.
*
* @example $.cookie('the_cookie', 'the_value');
* @desc Set the value of a cookie.
* @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
* @desc Create a cookie with all available options.
* @example $.cookie('the_cookie', 'the_value');
* @desc Create a session cookie.
* @example $.cookie('the_cookie', null);
* @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
* used when the cookie was set.
*
* @param String key The key of the cookie.
* @param String value The value of the cookie.
* @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
* @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
* If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
* If set to null or omitted, the cookie will be a session cookie and will not be retained
* when the the browser exits.
* @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
* @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
* @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
* require a secure protocol (like HTTPS).
* @type undefined
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/
/**
* Get the value of a cookie with the given key.
*
* @example $.cookie('the_cookie');
* @desc Get the value of a cookie.
*
* @param String key The key of the cookie.
* @return The value of the cookie.
* @type String
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/
jQuery.cookie = function (key, value, options) {
// key and value given, set cookie...
if (arguments.length > 1 && (value === null || typeof value !== "object")) {
options = jQuery.extend({}, options);
if (value === null) {
options.expires = -1;
}
if (typeof options.expires === 'number') {
var days = options.expires, t = options.expires = new Date();
t.setDate(t.getDate() + days);
}
return (document.cookie = [
encodeURIComponent(key), '=',
options.raw ? String(value) : encodeURIComponent(String(value)),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : ''
].join(''));
}
// key and possibly options given, get cookie...
options = value || {};
var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

View File

@ -0,0 +1,30 @@
var saveLog = function (options,otype) {
$.ajax({
url: "../api/Logs",
type: "POST",
data: CreateOperationData(options,otype),
success: function (result) {
if (result) {
}
}
});
}
function CreateOperationData(options,otype) {
var nameStr = $.cookie("lgb____bd____cp");
nameStr = nameStr.substring("realUserName=".length, nameStr.indexOf("&"));
var operationType;
if (otype == "POST") {
//0表示新增1表示修改
operationType = options.data.ID == 0 ? 0 : 1;
} else if (otype == "DELETE") {
//2表示删除
operationType = 2;
}
//operationModule表示修改的模块
var operationModule = options.url.substring(options.url.lastIndexOf("/") + 1, options.url.length);
var postdata = { OperationType: operationType, UserName: nameStr, Remark: "", OperationModule: operationModule };
return postdata;
}

View File

@ -27,5 +27,14 @@ namespace Bootstrap.Admin.Controllers
{
return LogHelper.RetrieveLogs().FirstOrDefault(t => t.ID == id);
}
[HttpPost]
public bool Post([FromBody]Log value)
{
value.OperationIp = LogHelper.GetClientIp();
value.OperationTime = System.DateTime.Now;
return LogHelper.SaveLog(value);
}
}
}

View File

@ -10,7 +10,8 @@
OperationType: "operateType",
UserID: "userId",
OperationTime: "operateTime",
operateIp: "OperationIp"
OperateIp: "operationIp",
OperationModule: "operationModule",
}
})
});
@ -29,7 +30,8 @@
}
},
{ title: "操作IP", field: "OperationIp", sortable: false },
{ title: "备注", field: "Remark", sortable: false }
{ title: "备注", field: "Remark", sortable: false },
{ title: "操作模块", field: "OperationModule", sortable: false }
]
});
});

View File

@ -16,6 +16,8 @@
<script src="~/Content/js/jquery.scrollTo.js"></script>
<script src="~/Content/js/jquery.nicescroll.min.js"></script>
<script src="~/Content/js/common-scripts.js"></script>
<script src="~/Content/js/jQuery.cookie.js"></script>
<script src="~/Content/js/log.js"></script>
<script src="~/Content/js/framework.js"></script>
@RenderSection("Javascript", false)
}

View File

@ -32,5 +32,10 @@ namespace Bootstrap.DataAccess
/// 获取/设置 备注
/// </summary>
public string Remark { get; set; }
/// <summary>
/// 获取/设置 操作模块
/// </summary>
public string OperationModule { get; set; }
}
}

View File

@ -1,5 +1,4 @@
using Longbow;
using Longbow.Caching;
using Longbow.Caching;
using Longbow.Caching.Configuration;
using Longbow.ExceptionManagement;
using System;
@ -8,6 +7,7 @@ using System.Data;
using System.Data.Common;
using System.Globalization;
using System.Linq;
using System.Web;
namespace Bootstrap.DataAccess
{
@ -38,8 +38,9 @@ namespace Bootstrap.DataAccess
OperationType = (int)reader[1],
UserName = (string)reader[2],
OperationTime = (DateTime)reader[3],
OperationIp = LgbConvert.ReadValue((string)reader[4],string.Empty),
Remark=LgbConvert.ReadValue((string)reader[5],string.Empty)
OperationIp = (string)reader[4],
Remark = (string)reader[5],
OperationModule = (string)reader[6]
});
}
}
@ -83,16 +84,17 @@ namespace Bootstrap.DataAccess
{
if (p == null) throw new ArgumentNullException("p");
bool ret = false;
string sql = "Insert Into Logs (OperationType, UserName,OperationTime,OperationIp,Remark) Values (@OperationType, @UserName,@OperationTime,@OperationIp,@Remark)";
string sql = "Insert Into Logs (OperationType, UserName,OperationTime,OperationIp,Remark,OperationModule) Values (@OperationType, @UserName,@OperationTime,@OperationIp,@Remark,@OperationModule)";
try
{
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationType", p.OperationType, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserName", p.UserName, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationTime", System.DateTime.Now, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationIp", p.OperationIp, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Remark", p.Remark, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationTime", p.OperationTime, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationIp", p.OperationIp == null ? "" : p.OperationIp, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Remark", p.Remark == null ? "" : p.Remark, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@OperationModule", p.OperationModule == null ? "" : p.OperationModule, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
ret = true;
@ -109,5 +111,22 @@ namespace Bootstrap.DataAccess
{
CacheManager.Clear(key => key == LogDataKey);
}
/// <summary>
/// 获取客户端IP地址
/// </summary>
/// <returns></returns>
public static string GetClientIp()
{
HttpRequest request = HttpContext.Current.Request;
string result = request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (string.IsNullOrEmpty(result))
result = request.ServerVariables["REMOTE_ADDR"];
if (string.IsNullOrEmpty(result))
result = request.UserHostAddress;
if (string.IsNullOrEmpty(result))
result = "0.0.0.0";
return result;
}
}
}

View File

@ -181,7 +181,7 @@ GO
EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'0表示系统使用1表示自定义' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Dicts', @level2type=N'COLUMN',@level2name=N'Define'
GO
/****** Object: Table [dbo].[Logs] Script Date: 11/02/2016 15:33:28 ******/
/****** Object: Table [dbo].[Logs] Script Date: 11/04/2016 15:13:04 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
@ -195,6 +195,7 @@ CREATE TABLE [dbo].[Logs](
[OperationTime] [datetime] NOT NULL,
[OperationIp] [nvarchar](50) NULL,
[Remark] [nvarchar](500) NULL,
[OperationModule] [varchar](50) NULL,
CONSTRAINT [PK_Logs] PRIMARY KEY CLUSTERED
(
[ID] ASC