商丘12345 后端

HomeController.cs 13KB

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