zhoufan лет назад: 7
Родитель
Сommit
afee9c090f

+ 24 - 23
codegit/CallCenterApi/CallCenterApi.Interface/CallCenterApi.Interface/App_Start/AuthorizeAttribute.cs

@@ -19,33 +19,34 @@ namespace CallCenterApi.Interface
19 19
         /// <param name="filterContext"></param>
20 20
         public override void OnAuthorization(AuthorizationContext filterContext)
21 21
         {
22
-            bool isAuth = false;
22
+            //bool isAuth = false;
23 23
 
24 24
             if (filterContext.RequestContext.HttpContext.Request.IsAuthenticated)
25 25
             {
26
-                var actionDescriptor = filterContext.ActionDescriptor;
27
-                var controllerDescriptor = actionDescriptor.ControllerDescriptor;
28
-                var controller = controllerDescriptor.ControllerName;
29
-                var action = actionDescriptor.ActionName;
30
-                var token = filterContext.HttpContext.Request["token"];
26
+                //var actionDescriptor = filterContext.ActionDescriptor;
27
+                //var controllerDescriptor = actionDescriptor.ControllerDescriptor;
28
+                //var controller = controllerDescriptor.ControllerName;
29
+                //var action = actionDescriptor.ActionName;
30
+                //var token = filterContext.HttpContext.Request["token"];
31
+
32
+                //var userData = CacheHelper.Get<Cache.Models.CurrentUserInfo>(token);
33
+                //var roleId = userData.F_RoleId;
34
+                //var role = new BLL.T_Sys_RoleInfo().GetModel(roleId);
35
+                //if (role != null)
36
+                //{
37
+                //    var roleFunctionList = roleFunctionBLL.DataTableToList(roleFunctionBLL.GetList(" F_RoleId=" + role.F_RoleId).Tables[0]);
38
+                //    //var str = string.Join(",", roleFunctionList.Select(x => x.F_FunctionId).ToArray());
39
+                //    var moduleFunction = new BLL.T_Sys_ModuleFunctions().GetModel(controller, action);
40
+                //    if (moduleFunction != null)
41
+                //    {
42
+                //        var single = roleFunctionList.SingleOrDefault(x => x.F_FunctionId == moduleFunction.F_FunctionId);
43
+                //        if (single != null)
44
+                //        {
45
+                //            isAuth = true;
46
+                //        }
47
+                //    }
48
+                //}
31 49
 
32
-                var userData = CacheHelper.Get<Cache.Models.CurrentUserInfo>(token);
33
-                var roleId = userData.F_RoleId;
34
-                var role = new BLL.T_Sys_RoleInfo().GetModel(roleId);
35
-                if (role != null)
36
-                {
37
-                    var roleFunctionList = roleFunctionBLL.DataTableToList(roleFunctionBLL.GetList(" F_RoleId=" + role.F_RoleId).Tables[0]);
38
-                    //var str = string.Join(",", roleFunctionList.Select(x => x.F_FunctionId).ToArray());
39
-                    var moduleFunction = new BLL.T_Sys_ModuleFunctions().GetModel(controller, action);
40
-                    if (moduleFunction != null)
41
-                    {
42
-                        var single = roleFunctionList.SingleOrDefault(x => x.F_FunctionId == moduleFunction.F_FunctionId);
43
-                        if (single != null)
44
-                        {
45
-                            isAuth = true;
46
-                        }
47
-                    }
48
-                }
49 50
             }
50 51
             else
51 52
             {

+ 3 - 0
codegit/CallCenterApi/CallCenterApi.Interface/CallCenterApi.Interface/Configs/system.config

@@ -30,4 +30,7 @@
30 30
   <add key="mbkeys" value="大区,省份,城市,姓名,电话" />
31 31
   <!-- 设置数据库列名字 -->
32 32
   <add key="dbkeys" value="F_Area,F_Province,F_City,F_Name,F_Phone" />
33
+  <!-- ================== 5:Redis配置 ================== -->
34
+  <add key="Redis_Server" value="192.168.4.18"/>
35
+  <add key="Redis_Port" value="6379"/>
33 36
 </appSettings>

+ 2 - 7
codegit/CallCenterApi/CallCenterApi.Interface/CallCenterApi.Interface/Controllers/AreaController.cs

@@ -9,6 +9,7 @@ using System.Web.Mvc;
9 9
 
10 10
 namespace CallCenterApi.Interface.Controllers
11 11
 {
12
+    [Authority]
12 13
     public class AreaController : BaseController
13 14
     {
14 15
         // GET: Area
@@ -19,7 +20,6 @@ namespace CallCenterApi.Interface.Controllers
19 20
         /// 获取区域列表
20 21
         /// </summary>
21 22
         /// <returns></returns>
22
-        [Authority]
23 23
         public ActionResult GetAreaList()
24 24
         {
25 25
             DataTable dt = areaBLL.GetList(0, "", " F_Sort").Tables[0];
@@ -30,10 +30,9 @@ namespace CallCenterApi.Interface.Controllers
30 30
         /// 获取区域列表
31 31
         /// </summary>
32 32
         /// <returns></returns>
33
-        [Authority]
34 33
         public ActionResult GetAreaListById(int id = 0)
35 34
         {
36
-            DataTable dt = areaBLL.GetList(" isnull(F_PrentId,0)='" + id + "' and F_State=0 ").Tables[0];
35
+            DataTable dt = areaBLL.GetList(" isnull(F_ParentId,0)='" + id + "' and F_State=0 ").Tables[0];
37 36
             return Success("列表加载成功", dt);
38 37
         }
39 38
 
@@ -42,7 +41,6 @@ namespace CallCenterApi.Interface.Controllers
42 41
         /// </summary>
43 42
         /// <param name="areaId"></param>
44 43
         /// <returns></returns>
45
-        [Authority]
46 44
         public ActionResult GetArea()
47 45
         {
48 46
 
@@ -60,7 +58,6 @@ namespace CallCenterApi.Interface.Controllers
60 58
         /// <param name="input"></param>
61 59
         /// <returns></returns>
62 60
         [HttpPost]
63
-        [Authority]
64 61
         public ActionResult AddArea()
65 62
         {
66 63
             int userId = CurrentUser.UserData.F_UserId;
@@ -130,7 +127,6 @@ namespace CallCenterApi.Interface.Controllers
130 127
         /// </summary>
131 128
         /// <param name="ids"></param>
132 129
         /// <returns></returns>
133
-        [Authority]
134 130
         public ActionResult DelArea(string[] ids)
135 131
         {
136 132
             if (ids == null || ids.Length <= 0)
@@ -150,7 +146,6 @@ namespace CallCenterApi.Interface.Controllers
150 146
         /// </summary>
151 147
         /// <param name="ids"></param>
152 148
         /// <returns></returns>
153
-        [Authority]
154 149
         public ActionResult DelAreas(int id)
155 150
         {
156 151
             if (id > 0)

+ 37 - 0
codegit/CallCenterApi/CallCenterApi.Interface/CallCenterApi.Interface/Controllers/HomeController.cs

@@ -250,6 +250,43 @@ namespace CallCenterApi.Interface.Controllers
250 250
             return View("Index");
251 251
         }
252 252
 
253
+        public ActionResult GetRedis(string key)
254
+        {
255
+            List<object> caches = new List<object>();
256
+            var keys = RedisHelper.GetKeyList(string.IsNullOrEmpty(key) ? "" : key);
257
+
258
+            foreach (var k in keys)
259
+            {
260
+                caches.Add(new
261
+                {
262
+                    key = k,
263
+                    value = RedisHelper.StringGet(k),
264
+                    outtime = RedisHelper.GetKeyOutTime(k)
265
+                });
266
+            }
267
+            return Success("成功", caches);
268
+        }
269
+
270
+        public ActionResult UpdateRedis(string key, string value, int second = 10)
271
+        {
272
+            List<object> caches = new List<object>();
273
+            var keys = RedisHelper.GetKeyList(string.IsNullOrEmpty(key) ? "" : key);
274
+
275
+            foreach (var k in keys)
276
+            {
277
+                RedisHelper.StringSet(k, value, new TimeSpan(0, 0, second));
278
+            }
279
+            return Success("成功");
280
+        }
281
+
282
+        public ActionResult DeleteRedis(string key)
283
+        {
284
+            List<object> caches = new List<object>();
285
+            var keys = RedisHelper.GetKeyList(string.IsNullOrEmpty(key) ? "" : key);
286
+
287
+            long n = RedisHelper.KeysDelete(keys);
288
+            return Success("成功", n);
289
+        }
253 290
 
254 291
     }
255 292
 }

+ 39 - 42
codegit/CallCenterApi/CallCenterApi.Interface/CallCenterApi.Interface/Controllers/Login/LoginController.cs

@@ -30,28 +30,28 @@ namespace CallCenterApi.Interface.Controllers
30 30
             bool appResult = LoodLoop();
31 31
             if (appResult)
32 32
             {
33
-                    Model.T_Sys_UserAccount loginUser = new BLL.T_Sys_UserAccount().LoginGetModel(login.Username ,login.Password);
34
-                    
35
-                    if (loginUser != null)
36
-                    {
33
+                Model.T_Sys_UserAccount loginUser = new BLL.T_Sys_UserAccount().LoginGetModel(login.Username, login.Password);
34
+
35
+                if (loginUser != null)
36
+                {
37 37
                     string deptcode = "";
38 38
                     Model.T_Sys_Department deptModel = new BLL.T_Sys_Department().GetModel(loginUser.F_DeptId);
39 39
                     if (deptModel != null)
40 40
                     {
41 41
                         deptcode = deptModel.F_DeptCode;
42 42
                     }
43
-                        //写入登录日志
44
-                        new CallCenterApi.BLL.T_Sys_LoginLogs().Add(new Model.T_Sys_LoginLogs()
45
-                            {
46
-                                F_LoginName = loginUser.F_UserName,
47
-                                F_LoginId = loginUser.F_UserId,
48
-                                F_Result = "登录成功",
49
-                                F_LoginIP = Common.DTRequest.GetIP(),
50
-                                F_Hostname = Common.DTRequest.GetIP(),
51
-                                F_LoginDate = DateTime.Now,
52
-                                F_Remark = "",
53
-                                F_State = 0
54
-                            });
43
+                    //写入登录日志
44
+                    new CallCenterApi.BLL.T_Sys_LoginLogs().Add(new Model.T_Sys_LoginLogs()
45
+                    {
46
+                        F_LoginName = loginUser.F_UserName,
47
+                        F_LoginId = loginUser.F_UserId,
48
+                        F_Result = "登录成功",
49
+                        F_LoginIP = Common.DTRequest.GetIP(),
50
+                        F_Hostname = Common.DTRequest.GetIP(),
51
+                        F_LoginDate = DateTime.Now,
52
+                        F_Remark = "",
53
+                        F_State = 0
54
+                    });
55 55
                     Cache.Models.CurrentUserInfo currentUserInfo = new Cache.Models.CurrentUserInfo()
56 56
                     {
57 57
                         F_UserId = loginUser.F_UserId,
@@ -64,35 +64,32 @@ namespace CallCenterApi.Interface.Controllers
64 64
                         F_DeptId = loginUser.F_DeptId,
65 65
                         F_DeptCode = deptcode
66 66
                     };
67
-                            var token = FormsPrincipal<Cache.Models.CurrentUserInfo>.GetCookieValue(currentUserInfo.F_UserCode, currentUserInfo);
68
-
69
-                            //放入缓存
70
-                            CacheHelper.Insert(token, currentUserInfo, 1440);
71
-                            return Success("登录成功", new
72
-                                    {
73
-                                        token = token
74
-                                    });
75
-
76
-                           
67
+                    var token = FormsPrincipal<Cache.Models.CurrentUserInfo>.GetCookieValue(currentUserInfo.F_UserCode, currentUserInfo);
77 68
 
69
+                    //放入redis缓存
70
+                    RedisHelper.StringSet(token, currentUserInfo.ToJson(), new TimeSpan(24, 0, 0));
78 71
 
79
-                    }
80
-                    else
72
+                    return Success("登录成功", new
81 73
                     {
82
-                        int rr = new BLL.T_Sys_LoginLogs().Add(new Model.T_Sys_LoginLogs()
83
-                        {
84
-                            F_LoginName = login.Username,
85
-                            F_LoginId = -1,
86
-                            F_Result = "登录失败:帐号-" + login.Username + ";分机号-" + login.ExtensionPhone,
87
-                            F_LoginIP = Common.DTRequest.GetIP(),
88
-                            F_Hostname = Common.DTRequest.GetIP(),
89
-                            F_LoginDate = DateTime.Now,
90
-                            F_Remark = "",
91
-                            F_State = 0
92
-                        });
93
-                        return Error("账号或密码错误,请重新登录");
74
+                        token = token
75
+                    });
76
+                }
77
+                else
78
+                {
79
+                    int rr = new BLL.T_Sys_LoginLogs().Add(new Model.T_Sys_LoginLogs()
80
+                    {
81
+                        F_LoginName = login.Username,
82
+                        F_LoginId = -1,
83
+                        F_Result = "登录失败:帐号-" + login.Username + ";分机号-" + login.ExtensionPhone,
84
+                        F_LoginIP = Common.DTRequest.GetIP(),
85
+                        F_Hostname = Common.DTRequest.GetIP(),
86
+                        F_LoginDate = DateTime.Now,
87
+                        F_Remark = "",
88
+                        F_State = 0
89
+                    });
90
+                    return Error("账号或密码错误,请重新登录");
94 91
                 }
95
-                
92
+
96 93
             }
97 94
             else
98 95
             {
@@ -104,7 +101,7 @@ namespace CallCenterApi.Interface.Controllers
104 101
         {
105 102
             if (Request.IsAuthenticated)
106 103
             {
107
-                CacheHelper.Remove(token);
104
+                RedisHelper.KeyDelete(token);
108 105
             }
109 106
             return Success("退出成功");
110 107
         }

+ 5 - 2
codegit/CallCenterApi/CallCenterApi.Interface/CallCenterApi.Interface/Global.asax.cs

@@ -1,4 +1,5 @@
1
-using CallCenterApi.Common;
1
+using CallCenter.Utility;
2
+using CallCenterApi.Common;
2 3
 using CallCenterApi.Interface.Models.Common;
3 4
 using System;
4 5
 using System.Collections.Generic;
@@ -33,7 +34,9 @@ namespace CallCenterApi.Interface
33 34
             {
34 35
                 ////获取缓存
35 36
                 //var dict = CacheHelper.Get(token);
36
-                //if (dict == null) return;
37
+                //获取redis缓存
38
+                var dict = RedisHelper.StringGet(token);
39
+                if (dict == null) return;
37 40
                 Cache.Models.CurrentUserInfo userData = null;
38 41
                 //获取FormsAuthenticationTicket对象
39 42
                 FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(token);

+ 5 - 0
codegit/CallCenterCommon/CallCenter.Utility/CallCenter.Utility.csproj

@@ -54,10 +54,14 @@
54 54
       <HintPath>..\..\packages\NPOI.2.3.0\lib\net40\NPOI.OpenXmlFormats.dll</HintPath>
55 55
       <Private>True</Private>
56 56
     </Reference>
57
+    <Reference Include="StackExchange.Redis, Version=1.2.6.0, Culture=neutral, processorArchitecture=MSIL">
58
+      <HintPath>..\..\packages\StackExchange.Redis.1.2.6\lib\net45\StackExchange.Redis.dll</HintPath>
59
+    </Reference>
57 60
     <Reference Include="System" />
58 61
     <Reference Include="System.Configuration" />
59 62
     <Reference Include="System.Core" />
60 63
     <Reference Include="System.Drawing" />
64
+    <Reference Include="System.IO.Compression" />
61 65
     <Reference Include="System.Management" />
62 66
     <Reference Include="System.Web" />
63 67
     <Reference Include="System.Xml.Linq" />
@@ -76,6 +80,7 @@
76 80
     <Compile Include="Extend\ExtLinq.cs" />
77 81
     <Compile Include="FileUp.cs" />
78 82
     <Compile Include="Linq\PredicateExtensionses.cs" />
83
+    <Compile Include="RedisHelper.cs" />
79 84
     <Compile Include="SaltAndHashHelper.cs" />
80 85
     <Compile Include="SysInformationHelper.cs" />
81 86
     <Compile Include="Time\DateTimeConvert.cs" />

+ 792 - 0
codegit/CallCenterCommon/CallCenter.Utility/RedisHelper.cs

@@ -0,0 +1,792 @@
1
+using System;
2
+using StackExchange.Redis;
3
+using System.Collections.Generic;
4
+using System.Linq;
5
+using Newtonsoft.Json;
6
+using System.Collections;
7
+
8
+namespace CallCenter.Utility
9
+{
10
+    public class RedisHelper
11
+    {
12
+        #region 
13
+        //执行顺序---静态字段---静态构造函数---构造函数
14
+        private static ConnectionMultiplexer redis;
15
+        private static IDatabase database;
16
+        static RedisHelper()
17
+        {
18
+            if (redis == null || !redis.IsConnected)
19
+            {
20
+                redis = ConnectionMultiplexer.Connect(Configs.GetValue("Redis_Server") + ":" + Configs.GetValue("Redis_Port"));
21
+                //redis = ConnectionMultiplexer.Connect("192.168.4.18, abortConnect=false");
22
+
23
+                database = redis.GetDatabase(10);
24
+            }
25
+        }
26
+
27
+        #endregion
28
+
29
+        #region redis 字符串(string)操作
30
+
31
+        /// <summary>
32
+        /// 设置指定键的值
33
+        /// </summary>
34
+        /// <param name="key"></param>
35
+        /// <param name="value"></param>
36
+        /// <returns></returns>
37
+        public static bool StringSet(string key, string value)
38
+        {
39
+            return database.StringSet(key, value);
40
+        }
41
+
42
+        /// <summary>
43
+        /// 获取指定键的值
44
+        /// </summary>
45
+        /// <param name="key"></param>
46
+        /// <returns></returns>
47
+        public static object StringGet(string key)
48
+        {
49
+            var rt = database.StringGet(key);
50
+
51
+            if (rt.IsNull)
52
+            {
53
+                return null;
54
+            }
55
+            else
56
+            {
57
+                return rt;
58
+            }
59
+        }
60
+
61
+        /// <summary>
62
+        /// 获取存储在键上的字符串的子字符串
63
+        /// </summary>
64
+        /// <param name="key"></param>
65
+        /// <param name="start"></param>
66
+        /// <param name="end"></param>
67
+        /// <returns></returns>
68
+        public static object StringGet(string key, int start, int end)
69
+        {
70
+            var rt = database.StringGetRange(key, start, end);
71
+            if (rt.IsNull)
72
+            {
73
+                return null;
74
+            }
75
+            else
76
+            {
77
+                return rt;
78
+            }
79
+        }
80
+
81
+        /// <summary>
82
+        /// 设置键的字符串值并返回其旧值
83
+        /// </summary>
84
+        /// <param name="key"></param>
85
+        /// <param name="value"></param>
86
+        /// <returns></returns>
87
+        public static object StringGetAndSet(string key, string value)
88
+        {
89
+            var rt = database.StringGetSet(key, value);
90
+            if (rt.IsNull)
91
+            {
92
+                return null;
93
+            }
94
+            else
95
+            {
96
+                return rt;
97
+            }
98
+        }
99
+
100
+        /// <summary>
101
+        /// 返回在键处存储的字符串值中偏移处的位值
102
+        /// </summary>
103
+        /// <param name="key"></param>
104
+        /// <param name="offset"></param>
105
+        /// <returns></returns>
106
+        public static bool StringGetBit(string key, long offset)
107
+        {
108
+            return database.StringGetBit(key, offset);
109
+        }
110
+
111
+        /// <summary>
112
+        /// 获取所有给定键的值
113
+        /// </summary>
114
+        /// <param name="keys"></param>
115
+        /// <returns></returns>
116
+        public static List<object> StringMultiGet(string[] keys)
117
+        {
118
+            List<object> list = new List<object>();
119
+            for (int i = 0; i < keys.Length; i++)
120
+            {
121
+                list.Add(database.StringGet(keys[i]));
122
+            }
123
+            return list;
124
+        }
125
+
126
+        /// <summary>
127
+        /// 存储在键上的字符串值中设置或清除偏移处的位
128
+        /// </summary>
129
+        /// <param name="key"></param>
130
+        /// <param name="offset"></param>
131
+        /// <param name="value"></param>
132
+        /// <returns></returns>
133
+        public static bool StringSetBit(string key, long offset)
134
+        {
135
+            return database.StringSetBit(key, offset, true);
136
+        }
137
+
138
+        /// <summary>
139
+        /// 使用键和到期时间来设置值
140
+        /// </summary>
141
+        /// <param name="key"></param>
142
+        /// <param name="value"></param>
143
+        /// <param name="expiry"></param>
144
+        /// <returns></returns>
145
+        public static bool StringSet(string key, string value, TimeSpan expiry)
146
+        {
147
+            return database.StringSet(key, value, expiry);
148
+        }
149
+
150
+        /// <summary>
151
+        /// 设置键的值,仅当键不存在时
152
+        /// </summary>
153
+        /// <param name="key"></param>
154
+        /// <param name="value"></param>
155
+        /// <returns></returns>
156
+        public static void StringSetIfAbsent(string key, string value)
157
+        {
158
+            if (database.StringGet(key) == RedisValue.Null)
159
+            {
160
+                database.StringSet(key, value);
161
+            }
162
+        }
163
+
164
+        /// <summary>
165
+        /// 在指定偏移处开始的键处覆盖字符串的一部分
166
+        /// </summary>
167
+        /// <param name="key">键值</param>
168
+        /// <param name="value">值</param>
169
+        /// <param name="offset">偏移量</param>
170
+        /// <returns></returns>
171
+        public static object StringSet(string key, long offset, string value)
172
+        {
173
+            return database.StringSetRange(key, offset, value);
174
+        }
175
+
176
+        /// <summary>
177
+        /// 获取存储在键中的值的长度
178
+        /// </summary>
179
+        /// <param name="key">键值</param>
180
+        /// <returns></returns>
181
+        public static long StringSize(string key)
182
+        {
183
+            return database.StringLength(key);
184
+        }
185
+
186
+        /// <summary>
187
+        /// 为多个键分别设置它们的值
188
+        /// </summary>
189
+        /// <param name="keys"></param>
190
+        /// <returns></returns>
191
+        public static void StringMultiSet(Dictionary<string, string> dic)
192
+        {
193
+            foreach (KeyValuePair<string, string> key in dic)
194
+            {
195
+                database.StringSet(key.Key, key.Value);
196
+            }
197
+        }
198
+
199
+        /// <summary>
200
+        /// 为多个键分别设置它们的值,仅当键不存在时
201
+        /// </summary>
202
+        /// <param name="keys">键值集合</param>
203
+        /// <returns></returns>
204
+        public static void StringMultiSetIfAbsent(Dictionary<string, string> dic)
205
+        {
206
+            foreach (KeyValuePair<string, string> key in dic)
207
+            {
208
+                //判断键值是否存在
209
+                if (database.StringGet(key.Key) == RedisValue.Null)
210
+                {
211
+                    database.StringSet(key.Key, key.Value);
212
+                }
213
+            }
214
+        }
215
+
216
+        /// <summary>
217
+        /// 将键的整数值按给定的数值增加
218
+        /// </summary>
219
+        /// <param name="key">键值</param>
220
+        /// <param name="value">给定的数值</param>
221
+        /// <returns></returns>
222
+        public static double StringIncrement(string key, double value)
223
+        {
224
+            return database.StringIncrement(key, value);
225
+        }
226
+
227
+        /// <summary>
228
+        /// 在key键对应值的右面追加值value
229
+        /// </summary>
230
+        /// <param name="key"></param>
231
+        /// <param name="value"></param>
232
+        /// <returns></returns>
233
+        public static long StringAppend(string key, string value)
234
+        {
235
+            return database.StringAppend(key, value);
236
+        }
237
+
238
+        /// <summary>
239
+        /// 删除某个键值
240
+        /// </summary>
241
+        /// <param name="key"></param>
242
+        /// <returns></returns>
243
+        public static bool StringDelete(string key)
244
+        {
245
+            return false;
246
+        }
247
+
248
+        #endregion
249
+
250
+        #region redis 哈希/散列/字典(Hash)操作
251
+
252
+        /// <summary>
253
+        /// 删除指定的哈希字段
254
+        /// </summary>
255
+        /// <param name="key"></param>
256
+        /// <param name="field"></param>
257
+        /// <returns></returns>
258
+        public static bool HashDelete(string key, string field)
259
+        {
260
+            return database.HashDelete(key, field);
261
+        }
262
+
263
+        /// <summary>
264
+        /// 判断是否存在散列字段
265
+        /// </summary>
266
+        /// <param name=""></param>
267
+        /// <param name=""></param>
268
+        /// <returns></returns>
269
+        public static bool HashHasKey(string key, string field)
270
+        {
271
+            return database.HashExists(key, field);
272
+        }
273
+
274
+        /// <summary>
275
+        /// 获取存储在指定键的哈希字段的值
276
+        /// </summary>
277
+        /// <param name="key"></param>
278
+        /// <param name="field"></param>
279
+        /// <returns></returns>
280
+        public static object HashGet(string key, string field)
281
+        {
282
+            return database.HashGet(key, field);
283
+        }
284
+
285
+        /// <summary>
286
+        /// 获取存储在指定键的哈希中的所有字段和值
287
+        /// </summary>
288
+        /// <param name="key"></param>
289
+        /// <returns></returns>
290
+        public static Dictionary<string, object> HashGetAll(string key)
291
+        {
292
+            Dictionary<string, object> dic = new Dictionary<string, object>();
293
+            var collection = database.HashGetAll(key);
294
+            foreach (var item in collection)
295
+            {
296
+                dic.Add(item.Name, item.Value);
297
+            }
298
+            return dic;
299
+        }
300
+
301
+        /// <summary>
302
+        /// 将哈希字段的浮点值按给定数值增加
303
+        /// </summary>
304
+        /// <param name="key"></param>
305
+        /// <param name="field"></param>
306
+        /// <param name="value">给定的数值</param>
307
+        /// <returns></returns>
308
+        public static double HashIncrement(string key, string field, double value)
309
+        {
310
+            return database.HashIncrement(key, field, value);
311
+        }
312
+
313
+        /// <summary>
314
+        /// 获取哈希中的所有字段
315
+        /// </summary>
316
+        /// <param name="key"></param>
317
+        /// <returns></returns>
318
+        public static string[] HashKeys(string key)
319
+        {
320
+            return database.HashKeys(key).ToStringArray();
321
+        }
322
+
323
+        /// <summary>
324
+        /// 获取散列中的字段数量
325
+        /// </summary>
326
+        /// <param name="key"></param>
327
+        /// <returns></returns>
328
+        public static long HashSize(string key)
329
+        {
330
+            return database.HashLength(key);
331
+        }
332
+
333
+        /// <summary>
334
+        /// 获取所有给定哈希字段的值
335
+        /// </summary>
336
+        /// <param name="key"></param>
337
+        /// <param name="hashKeys"></param>
338
+        /// <returns></returns>
339
+        public static List<object> HashMultiGet(string key, List<string> hashKeys)
340
+        {
341
+            List<object> result = new List<object>();
342
+            foreach (string field in hashKeys)
343
+            {
344
+                result.Add(database.HashGet(key, field));
345
+            }
346
+            return result;
347
+        }
348
+
349
+        /// <summary>
350
+        /// 为多个哈希字段分别设置它们的值
351
+        /// </summary>
352
+        /// <param name="key"></param>
353
+        /// <param name="dic"></param>
354
+        /// <returns></returns>
355
+        public static void HashPutAll(string key, Dictionary<string, string> dic)
356
+        {
357
+            List<HashEntry> list = new List<HashEntry>();
358
+            for (int i = 0; i < dic.Count; i++)
359
+            {
360
+                KeyValuePair<string, string> param = dic.ElementAt(i);
361
+                list.Add(new HashEntry(param.Key, param.Value));
362
+            }
363
+            database.HashSet(key, list.ToArray());
364
+        }
365
+
366
+        /// <summary>
367
+        /// 设置散列字段的字符串值
368
+        /// </summary>
369
+        /// <param name="key"></param>
370
+        /// <param name="field"></param>
371
+        /// <param name="value"></param>
372
+        /// <returns></returns>
373
+        public static void HashPut(string key, string field, string value)
374
+        {
375
+            database.HashSet(key, new HashEntry[] { new HashEntry(field, value) });
376
+        }
377
+
378
+        /// <summary>
379
+        /// 仅当字段不存在时,才设置散列字段的值
380
+        /// </summary>
381
+        /// <param name="key"></param>
382
+        /// <param name="fiels"></param>
383
+        /// <param name="value"></param>
384
+        /// <returns></returns>
385
+        public static void HashPutIfAbsent(string key, string field, string value)
386
+        {
387
+            if (!HashHasKey(key, field))
388
+            {
389
+                database.HashSet(key, new HashEntry[] { new HashEntry(field, value) });
390
+            }
391
+        }
392
+
393
+        /// <summary>
394
+        /// 获取哈希中的所有值
395
+        /// </summary>
396
+        /// <param name="key"></param>
397
+        /// <returns></returns>
398
+        public static string[] HashValues(string key)
399
+        {
400
+            return database.HashValues(key).ToStringArray();
401
+        }
402
+
403
+        /// <summary>
404
+        /// redis中获取指定键的值并返回对象
405
+        /// </summary>
406
+        /// <typeparam name="T"></typeparam>
407
+        /// <param name="key"></param>
408
+        /// <returns></returns>
409
+        public static T GetHashValue<T>(string key)
410
+        {
411
+            HashEntry[] array = database.HashGetAll(key);
412
+            Dictionary<string, object> dic = new Dictionary<string, object>();
413
+            for (int i = 0; i < array.Length; i++)
414
+            {
415
+                dic.Add(array[i].Name, array[i].Value);
416
+            }
417
+            if (dic.Count > 0)
418
+            {
419
+                string strJson = JsonConvert.SerializeObject(dic);
420
+                return JsonConvert.DeserializeObject<T>(strJson);
421
+            }
422
+            else
423
+            {
424
+                return default(T);
425
+            }
426
+        }
427
+
428
+        /// <summary>
429
+        /// 把指定对象存储在键值为key的redis中
430
+        /// </summary>
431
+        /// <typeparam name="T"></typeparam>
432
+        /// <param name="t"></param>
433
+        /// <param name="key"></param>
434
+        public static void SetHashValue<T>(T t, string key)
435
+        {
436
+            string strJson = JsonConvert.SerializeObject(t);
437
+            Dictionary<string, string> param = JsonConvert.DeserializeObject<Dictionary<string, string>>(strJson);
438
+            HashPutAll(key, param);
439
+        }
440
+
441
+        #endregion
442
+
443
+        #region redis 列表(List)操作
444
+
445
+        /// <summary>
446
+        /// 从左向右存压栈
447
+        /// </summary>
448
+        /// <param name="key"></param>
449
+        /// <param name="value"></param>
450
+        /// <returns></returns>
451
+        public static long ListLeftPush(string key, string value)
452
+        {
453
+            return database.ListLeftPush(key, value);
454
+        }
455
+
456
+        /// <summary>
457
+        /// 从左出栈
458
+        /// </summary>
459
+        /// <param name="key"></param>
460
+        /// <returns></returns>
461
+        public static object ListLeftPop(string key)
462
+        {
463
+            return database.ListLeftPop(key);
464
+        }
465
+
466
+        /// <summary>
467
+        /// 队/栈长
468
+        /// </summary>
469
+        /// <param name="key"></param>
470
+        /// <returns></returns>
471
+        public static long ListSize(string key)
472
+        {
473
+            return database.ListLength(key);
474
+        }
475
+
476
+        /// <summary>
477
+        /// 范围检索,返回List
478
+        /// </summary>
479
+        /// <param name="key"></param>
480
+        /// <param name="start"></param>
481
+        /// <param name="end"></param>
482
+        /// <returns></returns>
483
+        public static string[] ListRange(string key, int start, int end)
484
+        {
485
+            return database.ListRange(key, start, end).ToStringArray();
486
+        }
487
+
488
+        /// <summary>
489
+        /// 移除key中值为value的i个,返回删除的个数;如果没有这个元素则返回0 
490
+        /// </summary>
491
+        /// <param name="key"></param>
492
+        /// <param name="i"></param>
493
+        /// <param name="value"></param>
494
+        /// <returns></returns>
495
+        public static long ListRemove(string key, string value)
496
+        {
497
+            return database.ListRemove(key, value);
498
+        }
499
+
500
+        /// <summary>
501
+        /// 检索
502
+        /// </summary>
503
+        /// <param name="key"></param>
504
+        /// <param name="index"></param>
505
+        /// <returns></returns>
506
+        public static object ListIndex(string key, long index)
507
+        {
508
+            return database.ListGetByIndex(key, index);
509
+        }
510
+
511
+        /// <summary>
512
+        /// 赋值
513
+        /// </summary>
514
+        /// <param name="key"></param>
515
+        /// <param name="index"></param>
516
+        /// <param name="value"></param>
517
+        /// <returns></returns>
518
+        public static void ListSet(string key, int index, string value)
519
+        {
520
+            database.ListSetByIndex(key, index, value);
521
+        }
522
+
523
+        /// <summary>
524
+        /// 裁剪,删除除了[start,end]以外的所有元素 
525
+        /// </summary>
526
+        /// <param name="key"></param>
527
+        /// <param name="start"></param>
528
+        /// <param name="end"></param>
529
+        /// <returns></returns>
530
+        public static void ListTrim(string key, int start, int end)
531
+        {
532
+            database.ListTrim(key, start, end);
533
+        }
534
+
535
+        /// <summary>
536
+        /// 将源key的队列的右边的一个值删除,然后塞入目标key的队列的左边,返回这个值
537
+        /// </summary>
538
+        /// <param name="sourceKey"></param>
539
+        /// <param name="destinationKey"></param>
540
+        /// <returns></returns>
541
+        public static object ListRightPopAndLeftPush(string sourceKey, string destinationKey)
542
+        {
543
+            return database.ListRightPopLeftPush(sourceKey, destinationKey);
544
+        }
545
+
546
+        #endregion
547
+
548
+        #region redis 集合(Set)操作
549
+
550
+        /// <summary>
551
+        /// 集合添加元素
552
+        /// </summary>
553
+        /// <param name="key"></param>
554
+        /// <param name="value"></param>
555
+        public static void SetAdd(string key, string value)
556
+        {
557
+            database.SetAdd(key, value);
558
+        }
559
+
560
+        /// <summary>
561
+        /// 集合组合操作
562
+        /// </summary>
563
+        /// <param name="point">操作标示:0--并集;1--交集;2--差集</param>
564
+        /// <param name="firstKey">第一个集合的键值</param>
565
+        /// <param name="secondKey">第二个集合的键值</param>
566
+        public static string[] SetCombine(int point, string firstKey, string secondKey)
567
+        {
568
+            RedisValue[] array;
569
+            switch (point)
570
+            {
571
+                case 0:
572
+                    array = database.SetCombine(SetOperation.Union, firstKey, secondKey);
573
+                    break;
574
+                case 1:
575
+                    array = database.SetCombine(SetOperation.Intersect, firstKey, secondKey);
576
+                    break;
577
+                case 2:
578
+                    array = database.SetCombine(SetOperation.Difference, firstKey, secondKey);
579
+                    break;
580
+                default:
581
+                    array = new RedisValue[0];
582
+                    break;
583
+            }
584
+            return array.ToStringArray();
585
+        }
586
+
587
+        /// <summary>
588
+        /// 
589
+        /// </summary>
590
+        /// <param name="key"></param>
591
+        /// <param name="value"></param>
592
+        /// <returns></returns>
593
+        public static bool SetContains(string key, string value)
594
+        {
595
+            return database.SetContains(key, value);
596
+        }
597
+
598
+        /// <summary>
599
+        /// 返回对应键值集合的长度
600
+        /// </summary>
601
+        /// <param name="key"></param>
602
+        /// <returns></returns>
603
+        public static long SetLength(string key)
604
+        {
605
+            return database.SetLength(key);
606
+        }
607
+
608
+        /// <summary>
609
+        /// 根据键值返回集合中所有的value
610
+        /// </summary>
611
+        /// <param name="key"></param>
612
+        /// <returns></returns>
613
+        public static string[] SetMembers(string key)
614
+        {
615
+            return database.SetMembers(key).ToStringArray();
616
+        }
617
+
618
+        /// <summary>
619
+        /// 将成员从源集移动到目标集
620
+        /// </summary>
621
+        /// <param name="sourceKey">源集key</param>
622
+        /// <param name="destinationKey">目标集key</param>
623
+        /// <param name="value"></param>
624
+        public static bool SetMove(string sourceKey, string destinationKey, string value)
625
+        {
626
+            return database.SetMove(sourceKey, destinationKey, value);
627
+        }
628
+
629
+        /// <summary>
630
+        /// 移除集合中指定键值随机元素
631
+        /// </summary>
632
+        /// <param name="key"></param>
633
+        public static string SetPop(string key)
634
+        {
635
+            return database.SetPop(key);
636
+        }
637
+
638
+        /// <summary>
639
+        /// 返回集合中指定键值随机元素
640
+        /// </summary>
641
+        /// <param name="key"></param>
642
+        /// <returns></returns>
643
+        public static string SetRandomMember(string key)
644
+        {
645
+            return database.SetRandomMember(key);
646
+        }
647
+
648
+        /// <summary>
649
+        /// 
650
+        /// </summary>
651
+        /// <param name="key"></param>
652
+        /// <param name="count"></param>
653
+        public static string[] SetRandomMembers(string key, long count)
654
+        {
655
+            return database.SetRandomMembers(key, count).ToStringArray();
656
+        }
657
+
658
+        /// <summary>
659
+        /// 移除集合中指定key值和value
660
+        /// </summary>
661
+        /// <param name="key"></param>
662
+        /// <param name="value"></param>
663
+        public static void SetRemove(string key, string value)
664
+        {
665
+            database.SetRemove(key, value);
666
+        }
667
+
668
+        /// <summary>
669
+        /// 
670
+        /// </summary>
671
+        /// <param name="key"></param>
672
+        public static void SetScan(string key)
673
+        {
674
+            database.SetScan(key);
675
+        }
676
+
677
+        #endregion
678
+
679
+        #region redis 有序集合(sorted set)操作
680
+
681
+        public static void Method(string key, string value, double score)
682
+        {
683
+            database.SortedSetAdd(key, new SortedSetEntry[] { new SortedSetEntry(value, score) });
684
+        }
685
+
686
+        #endregion
687
+
688
+        #region redis 键(Key)操作
689
+
690
+        /// <summary>
691
+        /// 获取 Key
692
+        /// </summary>
693
+        /// <param name="redisKey"></param>
694
+        /// <returns></returns>
695
+        //public static IEnumerable<RedisKey> GetKeyList(string redisKey)
696
+        //{
697
+        //    var server = redis.GetServer(Configs.GetValue("Redis_Server"), Configs.GetValue("Redis_Port"));
698
+        //    return server.Keys(pattern: "*"+ redisKey + "*");
699
+        //}
700
+        public static List<string> GetKeyList(string redisKey)
701
+        {
702
+            var server = redis.GetServer(Configs.GetValue("Redis_Server"), Int32.Parse(Configs.GetValue("Redis_Port")));
703
+            List<string> keylist = new List<string>();
704
+            var redisenum = server.Keys(pattern: "*" + redisKey + "*");
705
+            foreach (var r in redisenum.ToList())
706
+            {
707
+                keylist.Add(r.ToString());
708
+            }
709
+            return keylist;
710
+        }
711
+
712
+        /// <summary>
713
+        /// 移除指定 Key
714
+        /// </summary>
715
+        /// <param name="redisKey"></param>
716
+        /// <returns></returns>
717
+        public static bool KeyDelete(string redisKey)
718
+        {
719
+            return database.KeyDelete(redisKey);
720
+        }
721
+
722
+        /// <summary>
723
+        /// 移除指定 Key
724
+        /// </summary>
725
+        /// <param name="redisKey"></param>
726
+        /// <returns></returns>
727
+        //public static long KeysDelete(IEnumerable<RedisKey> redisKeys)
728
+        //{
729
+        //    return database.KeyDelete(redisKeys.ToArray());
730
+        //}
731
+        public static long KeysDelete(List<string> redisKeys)
732
+        {
733
+            int n = 0;
734
+            foreach (var r in redisKeys)
735
+            {
736
+                if (database.KeyDelete(r))
737
+                {
738
+                    n++;
739
+                }
740
+            }
741
+            return n;
742
+        }
743
+
744
+        /// <summary>
745
+        /// 校验 Key 是否存在
746
+        /// </summary>
747
+        /// <param name="redisKey"></param>
748
+        /// <returns></returns>
749
+        public static bool KeyExists(string redisKey)
750
+        {
751
+            return database.KeyExists(redisKey);
752
+        }
753
+
754
+        /// <summary>
755
+        /// 重命名 Key
756
+        /// </summary>
757
+        /// <param name="redisKey"></param>
758
+        /// <param name="redisNewKey"></param>
759
+        /// <returns></returns>
760
+        public static bool KeyRename(string redisKey, string redisNewKey)
761
+        {
762
+            return database.KeyRename(redisKey, redisNewKey);
763
+        }
764
+
765
+        /// <summary>
766
+        /// 设置 Key 的时间
767
+        /// </summary>
768
+        /// <param name="redisKey"></param>
769
+        /// <param name="expiry"></param>
770
+        /// <returns></returns>
771
+        public static bool KeyExpire(string redisKey, TimeSpan? expiry)
772
+        {
773
+            return database.KeyExpire(redisKey, expiry);
774
+        }
775
+
776
+        #endregion
777
+
778
+        /// <summary>
779
+        /// 获取key过期时间
780
+        /// </summary>
781
+        /// <param name="redisKey"></param>
782
+        /// <returns></returns>
783
+        public static string GetKeyOutTime(string redisKey)
784
+        {
785
+            var server = redis.GetServer(Configs.GetValue("Redis_Server"), Int32.Parse(Configs.GetValue("Redis_Port")));
786
+            var timeNow = server.Time().ToUniversalTime();
787
+            var time = database.KeyTimeToLive(redisKey);
788
+            var expire = time == null ? (DateTime?)null : timeNow.Add(time.Value); //返回UTC时间。
789
+            return expire.Value.AddHours(8).ToString("yyyy-MM-dd HH:mm:ss");
790
+        }
791
+    }
792
+}

+ 1 - 0
codegit/CallCenterCommon/CallCenter.Utility/packages.config

@@ -4,4 +4,5 @@
4 4
   <package id="Newtonsoft.Json" version="10.0.2" targetFramework="net45" />
5 5
   <package id="NPOI" version="2.3.0" targetFramework="net45" />
6 6
   <package id="SharpZipLib" version="0.86.0" targetFramework="net45" />
7
+  <package id="StackExchange.Redis" version="1.2.6" targetFramework="net45" />
7 8
 </packages>