using CallCenterApi.Common; using CallCenterApi.Interface.Controllers.Base; using CallCenterApi.Interface.Models.Common; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Data.SqlTypes; using System.Linq; using System.Text; using System.Threading; using System.Web; using System.Web.Caching; using System.Web.Mvc; namespace CallCenterApi.Interface.Controllers { public class HomeController : BaseController { // GET: Home public ActionResult Index() { return Success("成功"); } [Authorize] public JsonResult json1() { var person = new { Name = "张三", Age = 22, Sex = "男" }; return Json(person, JsonRequestBehavior.AllowGet); } public string json2() { var person = new { Name = "张三", Age = 22, Sex = "男", Date = DateTime.Now }; return person.ToJson(); } public string json2_() { var tt = new { exp = 20, money = 12 }; var person = new { Name = "张三", Age = 22, Sex = "男", other = tt, Date = DateTime.Now }; return person.ToJson(); } /// /// 演示 查询出来datatable的数据进行展示 /// /// public string json3() { DataTable tblDatas = new DataTable("Datas"); DataColumn dc = null; dc = tblDatas.Columns.Add("Product", Type.GetType("System.String")); dc = tblDatas.Columns.Add("Version", Type.GetType("System.String")); dc = tblDatas.Columns.Add("Description", Type.GetType("System.String")); DataRow newRow; newRow = tblDatas.NewRow(); newRow["Product"] = "大话西游"; newRow["Version"] = "2.0"; newRow["Description"] = "我很喜欢"; tblDatas.Rows.Add(newRow); newRow = tblDatas.NewRow(); newRow["Product"] = "梦幻西游"; newRow["Version"] = "3.0"; newRow["Description"] = "比大话更幼稚"; tblDatas.Rows.Add(newRow); return tblDatas.ToJson(); } public ActionResult json3_() { DataTable tblDatas = new DataTable("Datas"); DataColumn dc = null; dc = tblDatas.Columns.Add("Product", Type.GetType("System.String")); dc = tblDatas.Columns.Add("Version", Type.GetType("System.String")); dc = tblDatas.Columns.Add("Description", Type.GetType("System.String")); DataRow newRow; newRow = tblDatas.NewRow(); newRow["Product"] = "大话西游"; newRow["Version"] = "2.0"; newRow["Description"] = "我很喜欢"; tblDatas.Rows.Add(newRow); newRow = tblDatas.NewRow(); newRow["Product"] = "梦幻西游"; newRow["Version"] = "3.0"; newRow["Description"] = "比大话更幼稚"; tblDatas.Rows.Add(newRow); return Success("成功", tblDatas); } public ActionResult excel() { DataTable tblDatas = new DataTable("Datas"); DataColumn dc = null; dc = tblDatas.Columns.Add("Product", Type.GetType("System.String")); dc = tblDatas.Columns.Add("Version", Type.GetType("System.String")); dc = tblDatas.Columns.Add("Description", Type.GetType("System.String")); DataRow newRow; newRow = tblDatas.NewRow(); newRow["Product"] = "大话西游"; newRow["Version"] = "2.0"; newRow["Description"] = "我很喜欢"; tblDatas.Rows.Add(newRow); newRow = tblDatas.NewRow(); newRow["Product"] = "梦幻西游"; newRow["Version"] = "3.0"; newRow["Description"] = "比大话更幼稚"; tblDatas.Rows.Add(newRow); NPOIHelper npoi = new NPOIHelper(); string[] s = { "列1", "列2", "列3" }; if (npoi.ToExcel(tblDatas, "test", null, "D:/2.xlsx", s)) { return Success("成功", tblDatas); } else { return Error("导出失败"); } } public ActionResult excel1() { DataTable tblDatas = new DataTable("Datas"); DataColumn dc = null; dc = tblDatas.Columns.Add("Product", Type.GetType("System.String")); dc = tblDatas.Columns.Add("Version", Type.GetType("System.String")); dc = tblDatas.Columns.Add("Description", Type.GetType("System.String")); DataRow newRow; newRow = tblDatas.NewRow(); newRow["Product"] = "大话西游"; newRow["Version"] = "2.0"; newRow["Description"] = "我很喜欢"; tblDatas.Rows.Add(newRow); newRow = tblDatas.NewRow(); newRow["Product"] = "梦幻西游"; newRow["Version"] = "3.0"; newRow["Description"] = "比大话更幼稚"; tblDatas.Rows.Add(newRow); NPOIHelper npoi = new NPOIHelper(); if (npoi.ExportToExcel("test", tblDatas) == "") { return Success("成功", tblDatas); } else { return Error("导出失败"); } } public ActionResult error() { return Error("不具备权限"); } public string ss() { return "ss"; } public string tt(string name, string pass) { return "sss " + name + pass; } /// /// 抛出HTTP 500 /// /// public ActionResult ThrowHttp500() { throw new HttpException(500, "服务器错误"); } /// /// 抛出HTTP 404 /// /// public ActionResult ThrowHttp404() { throw new HttpException(404, "页面未找到"); } /// /// 抛出未引用对象异常 /// ,此处单独使用HandleError特性 /// ,并指定异常类型及响应视图 /// /// [HandleError(ExceptionType = typeof(NullReferenceException), View = "CustomError")] public ActionResult ThrowNullReferenceException() { throw new NullReferenceException(); } /// /// 引发输入字符串的格式不正确异常 /// ,此处指定了响应的错误页面 /// ,由于是不同的控制器所以要完整的相对路径 /// /// [HandleError(View = "~/Views/Error/CustomHttpError.cshtml")] public ActionResult ThrowFormatException() { string str = ""; int count = Convert.ToInt32(str); return View("Index"); } public ActionResult GetRedis(string key) { List caches = new List(); var keys = RedisHelper.GetKeyList(string.IsNullOrEmpty(key) ? "" : key); foreach (var k in keys) { caches.Add(new { key = k, value = RedisHelper.StringGet(k), outtime = RedisHelper.GetKeyOutTime(k) }); } return Success("成功", caches); } public ActionResult UpdateRedis(string key, string value, int second = 10) { List caches = new List(); var keys = RedisHelper.GetKeyList(string.IsNullOrEmpty(key) ? "" : key); foreach (var k in keys) { RedisHelper.StringSet(k, value, new TimeSpan(0, 0, second)); } return Success("成功"); } public ActionResult DeleteRedis(string key) { List caches = new List(); var keys = RedisHelper.GetKeyList(string.IsNullOrEmpty(key) ? "" : key); long n = RedisHelper.KeysDelete(keys); return Success("成功", n); } public ActionResult GetAllCache() { System.Web.Caching.Cache cache = HttpRuntime.Cache; IDictionaryEnumerator cacheEnum = cache.GetEnumerator(); List caches = new List(); StringBuilder sb = new StringBuilder(); while (cacheEnum.MoveNext()) { Dictionary dic = null; try { dic = (Dictionary)cacheEnum.Value; } catch { continue; } sb.Append($"F_UserCode={dic["F_UserCode"]}, "); sb.Append($"F_UserCode={dic["F_UserCode"]}, "); sb.Append($"F_UserName={dic["F_UserName"]} "); caches.Add(new cacheobj { key = cacheEnum.Key.ToString().Substring(0, 20), value = sb.ToString() }); sb.Clear(); } return Success("", new { count = caches.Count(), }); } //public ActionResult MakeCache() //{ // while (true) // { // Dictionary Dic = new Dictionary(); // Dic.Add("F_UserCode", "1411"); // Dic.Add("F_UserCode", "8000"); // Dic.Add("F_DeptId", "1"); // Dic.Add("F_UserName", "系统管理员"); // Dic.Add("F_Telephone", "15537150907"); // Dic.Add("F_RoleID", "1"); // Dic.Add("F_SeatFlag", "1"); // new CallCenterApi.BLL.T_Sys_LoginLogs().Add(new Model.T_Sys_LoginLogs() // { // F_LoginName = "8000", // F_LoginId = 1411, // F_Result = "登录成功", // F_LoginIP = Common.DTRequest.GetIP(), // F_Hostname = Common.DTRequest.GetIP(), // F_LoginDate = DateTime.Now, // F_Remark = "", // F_State = 0 // }); // var token = FormsPrincipal>.GetCookieValue(Dic["F_UserCode"], Dic); // //放入缓存 // CacheHelper.Insert(token, Dic, 2880, System.Web.Caching.CacheItemPriority.NotRemovable); // Response.Write(token.Substring(0, 20) + ""); // } //} public ActionResult TestCache() { int n = 0; while (n<100) { Dictionary Dic = new Dictionary(); Dic.Add("F_UserCode", (1000 + n).ToString()); Dic.Add("F_UserCode", (8000+n).ToString()); Dic.Add("F_DeptId", "1"); Dic.Add("F_UserName", "系统管理员" + n); Dic.Add("F_Telephone", "15537150907"); Dic.Add("F_RoleID", "1"); Dic.Add("F_SeatFlag", "1"); var token = FormsPrincipal>.GetCookieValue(Dic["F_UserCode"], Dic); CacheItemRemovedCallback cacheItemRemovedCallback = new CacheItemRemovedCallback(onRemove); //放入缓存 CacheHelper.Insert(token, Dic, 1, System.Web.Caching.CacheItemPriority.NotRemovable, cacheItemRemovedCallback); n++; } return Success(n.ToString()); } /// /// 移除缓存后调用 /// /// /// /// public void onRemove(string key, object val, CacheItemRemovedReason reason) { var obj = new { key = key, val = val, reason = reason }; Error(obj.ToJson()); } } public class cacheobj { public string key { get; set; } public string value { get; set; } } }