县级监管平台

HomeController.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. 
  2. using CallCenterApi.Common;
  3. using CallCenterApi.Interface.Controllers.Base;
  4. using CallCenterApi.Interface.Models.Common;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.Data;
  9. using System.Data.SqlTypes;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading;
  13. using System.Web;
  14. using System.Web.Caching;
  15. using System.Web.Mvc;
  16. namespace CallCenterApi.Interface.Controllers
  17. {
  18. public class HomeController : BaseController
  19. {
  20. // GET: Home
  21. public ActionResult Index()
  22. {
  23. return Success("成功");
  24. }
  25. [Authorize]
  26. public JsonResult json1()
  27. {
  28. var person = new
  29. {
  30. Name = "张三",
  31. Age = 22,
  32. Sex = "男"
  33. };
  34. return Json(person, JsonRequestBehavior.AllowGet);
  35. }
  36. public string json2()
  37. {
  38. var person = new
  39. {
  40. Name = "张三",
  41. Age = 22,
  42. Sex = "男",
  43. Date = DateTime.Now
  44. };
  45. return person.ToJson();
  46. }
  47. public string json2_()
  48. {
  49. var tt = new
  50. {
  51. exp = 20,
  52. money = 12
  53. };
  54. var person = new
  55. {
  56. Name = "张三",
  57. Age = 22,
  58. Sex = "男",
  59. other = tt,
  60. Date = DateTime.Now
  61. };
  62. return person.ToJson();
  63. }
  64. /// <summary>
  65. /// 演示 查询出来datatable的数据进行展示
  66. /// </summary>
  67. /// <returns></returns>
  68. public string json3()
  69. {
  70. DataTable tblDatas = new DataTable("Datas");
  71. DataColumn dc = null;
  72. dc = tblDatas.Columns.Add("Product", Type.GetType("System.String"));
  73. dc = tblDatas.Columns.Add("Version", Type.GetType("System.String"));
  74. dc = tblDatas.Columns.Add("Description", Type.GetType("System.String"));
  75. DataRow newRow;
  76. newRow = tblDatas.NewRow();
  77. newRow["Product"] = "大话西游";
  78. newRow["Version"] = "2.0";
  79. newRow["Description"] = "我很喜欢";
  80. tblDatas.Rows.Add(newRow);
  81. newRow = tblDatas.NewRow();
  82. newRow["Product"] = "梦幻西游";
  83. newRow["Version"] = "3.0";
  84. newRow["Description"] = "比大话更幼稚";
  85. tblDatas.Rows.Add(newRow);
  86. return tblDatas.ToJson();
  87. }
  88. public ActionResult json3_()
  89. {
  90. DataTable tblDatas = new DataTable("Datas");
  91. DataColumn dc = null;
  92. dc = tblDatas.Columns.Add("Product", Type.GetType("System.String"));
  93. dc = tblDatas.Columns.Add("Version", Type.GetType("System.String"));
  94. dc = tblDatas.Columns.Add("Description", Type.GetType("System.String"));
  95. DataRow newRow;
  96. newRow = tblDatas.NewRow();
  97. newRow["Product"] = "大话西游";
  98. newRow["Version"] = "2.0";
  99. newRow["Description"] = "我很喜欢";
  100. tblDatas.Rows.Add(newRow);
  101. newRow = tblDatas.NewRow();
  102. newRow["Product"] = "梦幻西游";
  103. newRow["Version"] = "3.0";
  104. newRow["Description"] = "比大话更幼稚";
  105. tblDatas.Rows.Add(newRow);
  106. return Success("成功", tblDatas);
  107. }
  108. public ActionResult excel()
  109. {
  110. DataTable tblDatas = new DataTable("Datas");
  111. DataColumn dc = null;
  112. dc = tblDatas.Columns.Add("Product", Type.GetType("System.String"));
  113. dc = tblDatas.Columns.Add("Version", Type.GetType("System.String"));
  114. dc = tblDatas.Columns.Add("Description", Type.GetType("System.String"));
  115. DataRow newRow;
  116. newRow = tblDatas.NewRow();
  117. newRow["Product"] = "大话西游";
  118. newRow["Version"] = "2.0";
  119. newRow["Description"] = "我很喜欢";
  120. tblDatas.Rows.Add(newRow);
  121. newRow = tblDatas.NewRow();
  122. newRow["Product"] = "梦幻西游";
  123. newRow["Version"] = "3.0";
  124. newRow["Description"] = "比大话更幼稚";
  125. tblDatas.Rows.Add(newRow);
  126. NPOIHelper npoi = new NPOIHelper();
  127. string[] s = { "列1", "列2", "列3" };
  128. if (npoi.ToExcel(tblDatas, "test", null, "D:/2.xlsx", s))
  129. {
  130. return Success("成功", tblDatas);
  131. }
  132. else
  133. {
  134. return Error("导出失败");
  135. }
  136. }
  137. public ActionResult excel1()
  138. {
  139. DataTable tblDatas = new DataTable("Datas");
  140. DataColumn dc = null;
  141. dc = tblDatas.Columns.Add("Product", Type.GetType("System.String"));
  142. dc = tblDatas.Columns.Add("Version", Type.GetType("System.String"));
  143. dc = tblDatas.Columns.Add("Description", Type.GetType("System.String"));
  144. DataRow newRow;
  145. newRow = tblDatas.NewRow();
  146. newRow["Product"] = "大话西游";
  147. newRow["Version"] = "2.0";
  148. newRow["Description"] = "我很喜欢";
  149. tblDatas.Rows.Add(newRow);
  150. newRow = tblDatas.NewRow();
  151. newRow["Product"] = "梦幻西游";
  152. newRow["Version"] = "3.0";
  153. newRow["Description"] = "比大话更幼稚";
  154. tblDatas.Rows.Add(newRow);
  155. NPOIHelper npoi = new NPOIHelper();
  156. if (npoi.ExportToExcel("test", tblDatas) == "")
  157. {
  158. return Success("成功", tblDatas);
  159. }
  160. else
  161. {
  162. return Error("导出失败");
  163. }
  164. }
  165. public ActionResult error()
  166. {
  167. return Error("不具备权限");
  168. }
  169. public string ss()
  170. {
  171. return "ss";
  172. }
  173. public string tt(string name, string pass)
  174. {
  175. return "sss " + name + pass;
  176. }
  177. /// <summary>
  178. /// 抛出HTTP 500
  179. /// </summary>
  180. /// <returns></returns>
  181. public ActionResult ThrowHttp500()
  182. {
  183. throw new HttpException(500, "服务器错误");
  184. }
  185. /// <summary>
  186. /// 抛出HTTP 404
  187. /// </summary>
  188. /// <returns></returns>
  189. public ActionResult ThrowHttp404()
  190. {
  191. throw new HttpException(404, "页面未找到");
  192. }
  193. /// <summary>
  194. /// 抛出未引用对象异常
  195. /// ,此处单独使用HandleError特性
  196. /// ,并指定异常类型及响应视图
  197. /// </summary>
  198. /// <returns></returns>
  199. [HandleError(ExceptionType = typeof(NullReferenceException), View = "CustomError")]
  200. public ActionResult ThrowNullReferenceException()
  201. {
  202. throw new NullReferenceException();
  203. }
  204. /// <summary>
  205. /// 引发输入字符串的格式不正确异常
  206. /// ,此处指定了响应的错误页面
  207. /// ,由于是不同的控制器所以要完整的相对路径
  208. /// </summary>
  209. /// <returns></returns>
  210. [HandleError(View = "~/Views/Error/CustomHttpError.cshtml")]
  211. public ActionResult ThrowFormatException()
  212. {
  213. string str = "";
  214. int count = Convert.ToInt32(str);
  215. return View("Index");
  216. }
  217. public ActionResult GetRedis(string key)
  218. {
  219. List<object> caches = new List<object>();
  220. var keys = RedisHelper.GetKeyList(string.IsNullOrEmpty(key) ? "" : key);
  221. foreach (var k in keys)
  222. {
  223. caches.Add(new
  224. {
  225. key = k,
  226. value = RedisHelper.StringGet(k),
  227. outtime = RedisHelper.GetKeyOutTime(k)
  228. });
  229. }
  230. return Success("成功", caches);
  231. }
  232. public ActionResult UpdateRedis(string key, string value, int second = 10)
  233. {
  234. List<object> caches = new List<object>();
  235. var keys = RedisHelper.GetKeyList(string.IsNullOrEmpty(key) ? "" : key);
  236. foreach (var k in keys)
  237. {
  238. RedisHelper.StringSet(k, value, new TimeSpan(0, 0, second));
  239. }
  240. return Success("成功");
  241. }
  242. public ActionResult DeleteRedis(string key)
  243. {
  244. List<object> caches = new List<object>();
  245. var keys = RedisHelper.GetKeyList(string.IsNullOrEmpty(key) ? "" : key);
  246. long n = RedisHelper.KeysDelete(keys);
  247. return Success("成功", n);
  248. }
  249. public ActionResult GetAllCache()
  250. {
  251. System.Web.Caching.Cache cache = HttpRuntime.Cache;
  252. IDictionaryEnumerator cacheEnum = cache.GetEnumerator();
  253. List<cacheobj> caches = new List<cacheobj>();
  254. StringBuilder sb = new StringBuilder();
  255. while (cacheEnum.MoveNext())
  256. {
  257. Dictionary<string, string> dic = null;
  258. try
  259. {
  260. dic = (Dictionary<string, string>)cacheEnum.Value;
  261. }
  262. catch
  263. {
  264. continue;
  265. }
  266. sb.Append($"F_UserCode={dic["F_UserCode"]}, ");
  267. sb.Append($"F_UserCode={dic["F_UserCode"]}, ");
  268. sb.Append($"F_UserName={dic["F_UserName"]} ");
  269. caches.Add(new cacheobj
  270. {
  271. key = cacheEnum.Key.ToString().Substring(0, 20),
  272. value = sb.ToString()
  273. });
  274. sb.Clear();
  275. }
  276. return Success("", new
  277. {
  278. count = caches.Count(),
  279. });
  280. }
  281. //public ActionResult MakeCache()
  282. //{
  283. // while (true)
  284. // {
  285. // Dictionary<string, string> Dic = new Dictionary<string, string>();
  286. // Dic.Add("F_UserCode", "1411");
  287. // Dic.Add("F_UserCode", "8000");
  288. // Dic.Add("F_DeptId", "1");
  289. // Dic.Add("F_UserName", "系统管理员");
  290. // Dic.Add("F_Telephone", "15537150907");
  291. // Dic.Add("F_RoleID", "1");
  292. // Dic.Add("F_SeatFlag", "1");
  293. // new CallCenterApi.BLL.T_Sys_LoginLogs().Add(new Model.T_Sys_LoginLogs()
  294. // {
  295. // F_LoginName = "8000",
  296. // F_LoginId = 1411,
  297. // F_Result = "登录成功",
  298. // F_LoginIP = Common.DTRequest.GetIP(),
  299. // F_Hostname = Common.DTRequest.GetIP(),
  300. // F_LoginDate = DateTime.Now,
  301. // F_Remark = "",
  302. // F_State = 0
  303. // });
  304. // var token = FormsPrincipal<Dictionary<string, string>>.GetCookieValue(Dic["F_UserCode"], Dic);
  305. // //放入缓存
  306. // CacheHelper.Insert(token, Dic, 2880, System.Web.Caching.CacheItemPriority.NotRemovable);
  307. // Response.Write(token.Substring(0, 20) + "</n>");
  308. // }
  309. //}
  310. public ActionResult TestCache()
  311. {
  312. int n = 0;
  313. while (n<100)
  314. {
  315. Dictionary<string, string> Dic = new Dictionary<string, string>();
  316. Dic.Add("F_UserCode", (1000 + n).ToString());
  317. Dic.Add("F_UserCode", (8000+n).ToString());
  318. Dic.Add("F_DeptId", "1");
  319. Dic.Add("F_UserName", "系统管理员" + n);
  320. Dic.Add("F_Telephone", "15537150907");
  321. Dic.Add("F_RoleID", "1");
  322. Dic.Add("F_SeatFlag", "1");
  323. var token = FormsPrincipal<Dictionary<string, string>>.GetCookieValue(Dic["F_UserCode"], Dic);
  324. CacheItemRemovedCallback cacheItemRemovedCallback = new CacheItemRemovedCallback(onRemove);
  325. //放入缓存
  326. CacheHelper.Insert(token, Dic, 1, System.Web.Caching.CacheItemPriority.NotRemovable, cacheItemRemovedCallback);
  327. n++;
  328. }
  329. return Success(n.ToString());
  330. }
  331. /// <summary>
  332. /// 移除缓存后调用
  333. /// </summary>
  334. /// <param name="key"></param>
  335. /// <param name="val"></param>
  336. /// <param name="reason"></param>
  337. public void onRemove(string key, object val, CacheItemRemovedReason reason)
  338. {
  339. var obj = new
  340. {
  341. key = key,
  342. val = val,
  343. reason = reason
  344. };
  345. Error(obj.ToJson());
  346. }
  347. }
  348. public class cacheobj
  349. {
  350. public string key { get; set; }
  351. public string value { get; set; }
  352. }
  353. }