From 20b92010cb0ad0740b1f426e144773ce6dbc2787 Mon Sep 17 00:00:00 2001
From: lq_avenue <704528201@qq.com>
Date: Fri, 4 Nov 2016 15:22:44 +0800
Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E7=94=A8=E6=88=B7=E5=A2=9E=E5=88=A0?=
=?UTF-8?q?=E6=94=B9=E6=93=8D=E4=BD=9C=E8=AE=B0=E5=BD=95=E5=88=B0=E6=97=A5?=
=?UTF-8?q?=E5=BF=97=E4=B8=AD=EF=BC=8C=E4=BF=AE=E6=94=B9=E4=BA=86=E6=97=A5?=
=?UTF-8?q?=E5=BF=97=E8=A1=A8=E8=84=9A=E6=9C=AC?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
Bootstrap.Admin/Bootstrap.Admin.csproj | 2 +
Bootstrap.Admin/Content/js/framework.js | 3 +-
Bootstrap.Admin/Content/js/jQuery.cookie.js | 89 +++++++++++++++++++
Bootstrap.Admin/Content/js/log.js | 30 +++++++
Bootstrap.Admin/Controllers/LogsController.cs | 9 ++
Bootstrap.Admin/Scripts/Logs.js | 6 +-
Bootstrap.Admin/Views/Shared/_Admin.cshtml | 2 +
Bootstrap.DataAccess/Log.cs | 5 ++
Bootstrap.DataAccess/LogHelper.cs | 35 ++++++--
DatabaseScripts/Install.sql | 3 +-
10 files changed, 172 insertions(+), 12 deletions(-)
create mode 100644 Bootstrap.Admin/Content/js/jQuery.cookie.js
create mode 100644 Bootstrap.Admin/Content/js/log.js
diff --git a/Bootstrap.Admin/Bootstrap.Admin.csproj b/Bootstrap.Admin/Bootstrap.Admin.csproj
index ccc774a4..c66e0882 100644
--- a/Bootstrap.Admin/Bootstrap.Admin.csproj
+++ b/Bootstrap.Admin/Bootstrap.Admin.csproj
@@ -128,10 +128,12 @@
+
+
diff --git a/Bootstrap.Admin/Content/js/framework.js b/Bootstrap.Admin/Content/js/framework.js
index fb6f4b4c..cb802e2d 100644
--- a/Bootstrap.Admin/Content/js/framework.js
+++ b/Bootstrap.Admin/Content/js/framework.js
@@ -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 {
diff --git a/Bootstrap.Admin/Content/js/jQuery.cookie.js b/Bootstrap.Admin/Content/js/jQuery.cookie.js
new file mode 100644
index 00000000..7b3e7012
--- /dev/null
+++ b/Bootstrap.Admin/Content/js/jQuery.cookie.js
@@ -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;
+};
diff --git a/Bootstrap.Admin/Content/js/log.js b/Bootstrap.Admin/Content/js/log.js
new file mode 100644
index 00000000..b0814a62
--- /dev/null
+++ b/Bootstrap.Admin/Content/js/log.js
@@ -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;
+}
\ No newline at end of file
diff --git a/Bootstrap.Admin/Controllers/LogsController.cs b/Bootstrap.Admin/Controllers/LogsController.cs
index 394ad632..d9c88e42 100644
--- a/Bootstrap.Admin/Controllers/LogsController.cs
+++ b/Bootstrap.Admin/Controllers/LogsController.cs
@@ -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);
+ }
}
}
\ No newline at end of file
diff --git a/Bootstrap.Admin/Scripts/Logs.js b/Bootstrap.Admin/Scripts/Logs.js
index 4f2f03c7..7bc77c24 100644
--- a/Bootstrap.Admin/Scripts/Logs.js
+++ b/Bootstrap.Admin/Scripts/Logs.js
@@ -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 }
]
});
});
\ No newline at end of file
diff --git a/Bootstrap.Admin/Views/Shared/_Admin.cshtml b/Bootstrap.Admin/Views/Shared/_Admin.cshtml
index ea95dc64..75b18e01 100644
--- a/Bootstrap.Admin/Views/Shared/_Admin.cshtml
+++ b/Bootstrap.Admin/Views/Shared/_Admin.cshtml
@@ -16,6 +16,8 @@
+
+
@RenderSection("Javascript", false)
}
diff --git a/Bootstrap.DataAccess/Log.cs b/Bootstrap.DataAccess/Log.cs
index 9dc3c528..9a40bd41 100644
--- a/Bootstrap.DataAccess/Log.cs
+++ b/Bootstrap.DataAccess/Log.cs
@@ -32,5 +32,10 @@ namespace Bootstrap.DataAccess
/// 获取/设置 备注
///
public string Remark { get; set; }
+
+ ///
+ /// 获取/设置 操作模块
+ ///
+ public string OperationModule { get; set; }
}
}
diff --git a/Bootstrap.DataAccess/LogHelper.cs b/Bootstrap.DataAccess/LogHelper.cs
index a987b8aa..e5ee78c0 100644
--- a/Bootstrap.DataAccess/LogHelper.cs
+++ b/Bootstrap.DataAccess/LogHelper.cs
@@ -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);
}
+
+ ///
+ /// 获取客户端IP地址
+ ///
+ ///
+ 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;
+ }
}
}
diff --git a/DatabaseScripts/Install.sql b/DatabaseScripts/Install.sql
index 29b9f161..5dca742a 100644
--- a/DatabaseScripts/Install.sql
+++ b/DatabaseScripts/Install.sql
@@ -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