RoadFlow2.1 临时演示

Tools.cs 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. using System.Web;
  7. namespace RoadFlow.Utility
  8. {
  9. public class Tools
  10. {
  11. [Obsolete("请用RoadFlow.Utility.DateTimeNew.Now", true)]
  12. public static DateTime DateTime
  13. {
  14. get
  15. {
  16. return DateTimeNew.Now;
  17. }
  18. }
  19. public static System.IO.MemoryStream GetValidateImg(out string code, string bgImg = "/Images/vcodebg.png")
  20. {
  21. code = GetValidateCode();
  22. Random rnd = new Random();
  23. System.Drawing.Bitmap img = new System.Drawing.Bitmap((int)Math.Ceiling((code.Length * 17.2)), 28);
  24. System.Drawing.Image bg = System.Drawing.Bitmap.FromFile(HttpContext.Current.Server.MapPath(bgImg));
  25. System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(img);
  26. System.Drawing.Font font = new System.Drawing.Font("Arial", 16, (System.Drawing.FontStyle.Regular | System.Drawing.FontStyle.Italic));
  27. System.Drawing.Font fontbg = new System.Drawing.Font("Arial", 16, (System.Drawing.FontStyle.Regular | System.Drawing.FontStyle.Italic));
  28. System.Drawing.Drawing2D.LinearGradientBrush brush = new System.Drawing.Drawing2D.LinearGradientBrush(new System.Drawing.Rectangle(0, 0, img.Width, img.Height), System.Drawing.Color.Blue, System.Drawing.Color.DarkRed, 1.2f, true);
  29. g.DrawImage(bg, 0, 0, new System.Drawing.Rectangle(rnd.Next(bg.Width - img.Width), rnd.Next(bg.Height - img.Height), img.Width, img.Height), System.Drawing.GraphicsUnit.Pixel);
  30. g.DrawString(code, fontbg, System.Drawing.Brushes.White, 0, 1);
  31. g.DrawString(code, font, System.Drawing.Brushes.Green, 0, 1);//字颜色
  32. //画图片的背景噪音线
  33. int x = img.Width;
  34. int y1 = rnd.Next(5, img.Height);
  35. int y2 = rnd.Next(5, img.Height);
  36. g.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Green, 2), 1, y1, x - 2, y2);
  37. g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Transparent), 0, 10, img.Width - 1, img.Height - 1);
  38. System.IO.MemoryStream ms = new System.IO.MemoryStream();
  39. img.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
  40. return ms;
  41. }
  42. private static string GetValidateCode()
  43. { //产生五位的随机字符串
  44. int number;
  45. char code;
  46. string checkCode = String.Empty;
  47. System.Random random = new Random();
  48. for (int i = 0; i < 4; i++)
  49. {
  50. number = random.Next();
  51. if (number % 2 == 0)
  52. code = (char)('0' + (char)(number % 10));
  53. else if (number % 3 == 0)
  54. code = (char)('a' + (char)(number % 26));
  55. else
  56. code = (char)('A' + (char)(number % 26));
  57. checkCode += code == '0' || code == 'O' ? "x" : code.ToString();
  58. }
  59. return checkCode;
  60. }
  61. /// <summary>
  62. /// 获取远程浏览器端 IP 地址
  63. /// </summary>
  64. /// <returns>返回 IPv4 地址</returns>
  65. public static string GetIPAddress()
  66. {
  67. string userHostAddress = HttpContext.Current.Request.UserHostAddress;
  68. if (userHostAddress.IsNullOrEmpty())
  69. {
  70. userHostAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
  71. }
  72. return userHostAddress;
  73. }
  74. /// <summary>
  75. /// 得到用户浏览器类型
  76. /// </summary>
  77. /// <returns></returns>
  78. public static string GetBrowse()
  79. {
  80. return System.Web.HttpContext.Current.Request.Browser.Type;
  81. }
  82. /// <summary>
  83. /// 获取浏览器端操作系统名称
  84. /// </summary>
  85. /// <returns></returns>
  86. public static string GetOSName()
  87. {
  88. string osVersion = System.Web.HttpContext.Current.Request.Browser.Platform;
  89. string userAgent = System.Web.HttpContext.Current.Request.UserAgent;
  90. if (userAgent.Contains("NT 6.3"))
  91. {
  92. osVersion = "Windows8.1";
  93. }
  94. else if (userAgent.Contains("NT 6.2"))
  95. {
  96. osVersion = "Windows8";
  97. }
  98. else if (userAgent.Contains("NT 6.1"))
  99. {
  100. osVersion = "Windows7";
  101. }
  102. else if (userAgent.Contains("NT 6.0"))
  103. {
  104. osVersion = "WindowsVista";
  105. }
  106. else if (userAgent.Contains("NT 5.2"))
  107. {
  108. osVersion = "WindowsServer2003";
  109. }
  110. else if (userAgent.Contains("NT 5.1"))
  111. {
  112. osVersion = "WindowsXP";
  113. }
  114. else if (userAgent.Contains("NT 5"))
  115. {
  116. osVersion = "Windows2000";
  117. }
  118. else if (userAgent.Contains("NT 4"))
  119. {
  120. osVersion = "WindowsNT4.0";
  121. }
  122. else if (userAgent.Contains("Me"))
  123. {
  124. osVersion = "WindowsMe";
  125. }
  126. else if (userAgent.Contains("98"))
  127. {
  128. osVersion = "Windows98";
  129. }
  130. else if (userAgent.Contains("95"))
  131. {
  132. osVersion = "Windows95";
  133. }
  134. else if (userAgent.Contains("Mac"))
  135. {
  136. osVersion = "Mac";
  137. }
  138. else if (userAgent.Contains("Unix"))
  139. {
  140. osVersion = "UNIX";
  141. }
  142. else if (userAgent.Contains("Linux"))
  143. {
  144. osVersion = "Linux";
  145. }
  146. else if (userAgent.Contains("SunOS"))
  147. {
  148. osVersion = "SunOS";
  149. }
  150. return osVersion;
  151. }
  152. /// <summary>
  153. /// 得到分页HTML
  154. /// </summary>
  155. /// <param name="recordCount">记录总数</param>
  156. /// <param name="pageSize">每页条数</param>
  157. /// <param name="pageNumber">当前页</param>
  158. /// <param name="queryString">查询字符串</param>
  159. /// <returns></returns>
  160. public static string GetPagerHtml(long recordCount, int pageSize, int pageNumber, string queryString)
  161. {
  162. //得到共有多少页
  163. long PageCount = recordCount <= 0 ? 1 : recordCount % pageSize == 0 ? recordCount / pageSize : recordCount / pageSize + 1;
  164. long pNumber = pageNumber;
  165. if (pNumber < 1)
  166. {
  167. pNumber = 1;
  168. }
  169. else if (pNumber > PageCount)
  170. {
  171. pNumber = PageCount;
  172. }
  173. //如果只有一页则返回空分页字符串
  174. if (PageCount <= 1)
  175. {
  176. return "";
  177. }
  178. StringBuilder ReturnPagerString = new StringBuilder(1500);
  179. string JsFunctionName = string.Empty;
  180. //构造分页字符串
  181. int DisplaySize = 10;//中间显示的页数
  182. ReturnPagerString.Append("<div>");
  183. ReturnPagerString.Append("<span style='margin-right:15px;'>共 " + recordCount.ToString() + " 条 每页 <input type='text' id='tnt_count' title='输入数字可改变每页显示条数' class='pagertxt' onchange=\"javascript:_toPage_" + JsFunctionName + "(" + pNumber.ToString() + ",this.value);\" value='" + pageSize.ToString() + "' /> 条 ");
  184. ReturnPagerString.Append("转到 <input type='text' id='paernumbertext' title='输入数字可跳转页' value=\"" + pNumber.ToString() + "\" onchange=\"javascript:_toPage_" + JsFunctionName + "(this.value," + pageSize.ToString() + ");\" class='pagertxt'/> 页</span>");
  185. if (pNumber > 1)
  186. ReturnPagerString.Append("<a class=\"pager\" href=\"javascript:_toPage_" + JsFunctionName + "(" + (pNumber - 1).ToString() + "," + pageSize.ToString() + ");\"><span class=\"pagerarrow\">«</span></a>");
  187. //添加第一页
  188. if (pNumber >= DisplaySize / 2 + 3)
  189. ReturnPagerString.Append("<a class=\"pager\" href=\"javascript:_toPage_" + JsFunctionName + "(1," + pageSize.ToString() + ");\">1…</a>");
  190. else
  191. ReturnPagerString.Append("<a class=\"" + (1 == pNumber ? "pagercurrent" : "pager") + "\" href=\"javascript:_toPage_" + JsFunctionName + "(1," + pageSize.ToString() + ");\">1</a>");
  192. //添加中间数字
  193. long star = pNumber - DisplaySize / 2;
  194. long end = pNumber + DisplaySize / 2;
  195. if (star < 2)
  196. {
  197. end += 2 - star;
  198. star = 2;
  199. }
  200. if (end > PageCount - 1)
  201. {
  202. star -= end - (PageCount - 1);
  203. end = PageCount - 1;
  204. }
  205. if (star < 2)
  206. star = 2;
  207. for (long i = star; i <= end; i++)
  208. ReturnPagerString.Append("<a class=\"" + (i == pNumber ? "pagercurrent" : "pager") + "\" href=\"javascript:_toPage_" + JsFunctionName + "(" + i.ToString() + "," + pageSize.ToString() + ");\">" + i.ToString() + "</a>");
  209. //添加最后页
  210. if (pNumber <= PageCount - (DisplaySize / 2))
  211. ReturnPagerString.Append("<a class=\"pager\" href=\"javascript:_toPage_" + JsFunctionName + "(" + PageCount.ToString() + "," + pageSize.ToString() + ");\">…" + PageCount.ToString() + "</a>");
  212. else if (PageCount > 1)
  213. ReturnPagerString.Append("<a class=\"" + (PageCount == pNumber ? "pagercurrent" : "pager") + "\" href=\"javascript:_toPage_" + JsFunctionName + "(" + PageCount.ToString() + "," + pageSize.ToString() + ");\">" + PageCount.ToString() + "</a>");
  214. if (pNumber < PageCount)
  215. ReturnPagerString.Append("<a class=\"pager\" href=\"javascript:_toPage_" + JsFunctionName + "(" + (pNumber + 1).ToString() + "," + pageSize.ToString() + ");\"><span class=\"pagerarrow\">»</span></a>");
  216. ReturnPagerString.Append("</div>");
  217. //构造分页JS函数
  218. ReturnPagerString.Append("<script type=\"text/javascript\" lanuage=\"javascript\">");
  219. ReturnPagerString.Append("function _toPage_" + JsFunctionName + "(page,size){");
  220. ReturnPagerString.Append("var par=\"" + queryString + "\";");
  221. ReturnPagerString.Append("window.location=\"?pagenumber=\"+page+\"&pagesize=\"+size+par;");
  222. ReturnPagerString.Append("}");
  223. ReturnPagerString.Append("</script>");
  224. return ReturnPagerString.ToString();
  225. }
  226. /// <summary>
  227. /// 得到页尺寸
  228. /// </summary>
  229. /// <returns></returns>
  230. public static int GetPageSize()
  231. {
  232. string size = System.Web.HttpContext.Current.Request["pagesize"] ?? "15";
  233. int size1;
  234. return size.IsInt(out size1) ? size1 : 15;
  235. }
  236. /// <summary>
  237. /// 得到页号
  238. /// </summary>
  239. /// <returns></returns>
  240. public static int GetPageNumber()
  241. {
  242. string number = System.Web.HttpContext.Current.Request["pagenumber"] ?? "1";
  243. int number1;
  244. return number.IsInt(out number1) ? number1 : 1;
  245. }
  246. /// <summary>
  247. /// 得到列表项
  248. /// </summary>
  249. /// <param name="list">列表, 标题,值</param>
  250. /// <param name="value">默认值</param>
  251. /// <param name="showEmpty">是不显示空选项</param>
  252. /// <param name="emptyTitle">空选项显示标题</param>
  253. /// <returns></returns>
  254. public static System.Web.UI.WebControls.ListItem[] GetListItems(IList<string[]> list, string value, bool showEmpty = false, string emptyTitle = "")
  255. {
  256. List<System.Web.UI.WebControls.ListItem> items = new List<System.Web.UI.WebControls.ListItem>();
  257. if (showEmpty)
  258. {
  259. items.Add(new System.Web.UI.WebControls.ListItem(emptyTitle, ""));
  260. }
  261. foreach (var li in list)
  262. {
  263. if (li.Length < 2)
  264. {
  265. continue;
  266. }
  267. var item = new System.Web.UI.WebControls.ListItem(li[0], li[1]);
  268. item.Selected = !value.IsNullOrEmpty() && value == li[1] && !items.Exists(p => p.Selected);
  269. items.Add(item);
  270. }
  271. return items.ToArray();
  272. }
  273. public static System.Web.UI.WebControls.ListItem[] GetListItems(IList<string> list, string value, bool showEmpty = false, string emptyTitle = "")
  274. {
  275. List<string[]> newList = new List<string[]>();
  276. foreach (string str in list)
  277. {
  278. newList.Add(new string[] { str, str });
  279. }
  280. return GetListItems(newList, value, showEmpty, emptyTitle);
  281. }
  282. /// <summary>
  283. /// 将服务器控件列表项转换为select列表项
  284. /// </summary>
  285. /// <param name="items"></param>
  286. /// <returns></returns>
  287. public static string GetOptionsString(System.Web.UI.WebControls.ListItem[] items)
  288. {
  289. StringBuilder options = new StringBuilder(items.Length * 50);
  290. foreach (var item in items)
  291. {
  292. options.AppendFormat("<option value=\"{0}\" {1}>", item.Value.Replace("\"","'"), item.Selected ? "selected=\"selected\"" : "");
  293. options.Append(item.Text);
  294. options.Append("</option>");
  295. }
  296. return options.ToString();
  297. }
  298. /// <summary>
  299. /// 将服务器控件列表项转换为Checkbox项
  300. /// </summary>
  301. /// <param name="items"></param>
  302. /// <returns></returns>
  303. public static string GetCheckBoxString(System.Web.UI.WebControls.ListItem[] items, string name, string[] values, string otherAttr = "")
  304. {
  305. StringBuilder options = new StringBuilder(items.Length * 50);
  306. foreach (var item in items)
  307. {
  308. string tempid = Guid.NewGuid().ToString("N");
  309. options.AppendFormat("<input type=\"checkbox\" value=\"{0}\" {1} id=\"{2}\" name=\"{3}\" {4} style=\"vertical-align:middle\" />",
  310. item.Value.Replace("\"", "'"),
  311. values != null && values.Contains(item.Value) ? "checked=\"checked\"" : "",
  312. string.Format("{0}_{1}", name, tempid),
  313. name,
  314. otherAttr
  315. );
  316. options.AppendFormat("<label style=\"vertical-align:middle;margin-right:2px;\" for=\"{0}\">", string.Format("{0}_{1}", name, tempid));
  317. options.Append(item.Text);
  318. options.Append("</label>");
  319. }
  320. return options.ToString();
  321. }
  322. /// <summary>
  323. /// 将服务器控件列表项转换为Radio项
  324. /// </summary>
  325. /// <param name="items"></param>
  326. /// <returns></returns>
  327. public static string GetRadioString(System.Web.UI.WebControls.ListItem[] items, string name, string otherAttr = "")
  328. {
  329. StringBuilder options = new StringBuilder(items.Length * 50);
  330. foreach (var item in items)
  331. {
  332. string tempid = Guid.NewGuid().ToString("N");
  333. options.AppendFormat("<input type=\"radio\" value=\"{0}\" {1} id=\"{2}\" name=\"{3}\" {4} style=\"vertical-align:middle\" />",
  334. item.Value.Replace("\"", "'"),
  335. item.Selected ? "checked=\"checked\"" : "",
  336. string.Format("{0}_{1}", name, tempid),
  337. name,
  338. otherAttr
  339. );
  340. options.AppendFormat("<label style=\"vertical-align:middle;margin-right:2px;\" for=\"{0}\">", string.Format("{0}_{1}", name, tempid));
  341. options.Append(item.Text);
  342. options.Append("</label>");
  343. }
  344. return options.ToString();
  345. }
  346. /// <summary>
  347. /// 得到是否选择项
  348. /// </summary>
  349. /// <param name="value"></param>
  350. /// <param name="showEmpty"></param>
  351. /// <param name="emptyString"></param>
  352. /// <returns></returns>
  353. public static System.Web.UI.WebControls.ListItem[] GetYesNoListItems(string value, bool showEmpty = false, string emptyString = "")
  354. {
  355. List<string[]> list = new List<string[]>();
  356. list.Add(new string[] { "是", "1" });
  357. list.Add(new string[] { "否", "0" });
  358. return GetListItems(list, value, showEmpty, emptyString);
  359. }
  360. /// <summary>
  361. /// 得到sql语句in里的字符串
  362. /// </summary>
  363. /// <param name="str">字符串</param>
  364. /// <returns></returns>
  365. public static string GetSqlInString(string str, bool isSingleQuotes = true, string split = ",")
  366. {
  367. string[] strArray = str.Split(new string[] { split }, StringSplitOptions.RemoveEmptyEntries);
  368. return GetSqlInString(strArray, isSingleQuotes);
  369. }
  370. /// <summary>
  371. /// 得到sql语句in里的字符串
  372. /// </summary>
  373. /// <typeparam name="T"></typeparam>
  374. /// <param name="strArray"></param>
  375. /// <param name="isSingleQuotes">是否加单引号,字符串要加,数字不加</param>
  376. /// <returns></returns>
  377. public static string GetSqlInString<T>(T[] strArray, bool isSingleQuotes = true)
  378. {
  379. StringBuilder inStr = new StringBuilder(strArray.Length * 40);
  380. foreach (var s in strArray)
  381. {
  382. if (s.ToString().IsNullOrEmpty())
  383. {
  384. continue;
  385. }
  386. if (isSingleQuotes)
  387. {
  388. inStr.Append("'");
  389. }
  390. inStr.Append(s.ToString().Trim());
  391. if (isSingleQuotes)
  392. {
  393. inStr.Append("'");
  394. }
  395. inStr.Append(",");
  396. }
  397. return inStr.ToString().TrimEnd(',');
  398. }
  399. /// <summary>
  400. /// 产生不重复随机数
  401. /// </summary>
  402. /// <param name="count">共产生多少随机数</param>
  403. /// <param name="minValue">最小值</param>
  404. /// <param name="maxValue">最大值</param>
  405. /// <returns>int[]数组</returns>
  406. public static int[] GetRandomNum(int count, int minValue, int maxValue)
  407. {
  408. Random rnd = new Random(Guid.NewGuid().GetHashCode());
  409. int length = maxValue - minValue + 1;
  410. byte[] keys = new byte[length];
  411. rnd.NextBytes(keys);
  412. int[] items = new int[length];
  413. for (int i = 0; i < length; i++)
  414. {
  415. items[i] = i + minValue;
  416. }
  417. Array.Sort(keys, items);
  418. int[] result = new int[count];
  419. Array.Copy(items, result, count);
  420. return result;
  421. }
  422. /// <summary>
  423. /// 产生随机字符串
  424. /// </summary>
  425. /// <returns>字符串位数</returns>
  426. public static string GetRandomString(int length = 5)
  427. {
  428. int number;
  429. char code;
  430. string checkCode = String.Empty;
  431. System.Random random = new Random(Guid.NewGuid().GetHashCode());
  432. for (int i = 0; i < length + 1; i++)
  433. {
  434. number = random.Next();
  435. if (number % 2 == 0)
  436. code = (char)('0' + (char)(number % 10));
  437. else
  438. code = (char)('A' + (char)(number % 26));
  439. checkCode += code.ToString();
  440. }
  441. return checkCode;
  442. }
  443. /// <summary>
  444. /// 产生随机字母
  445. /// </summary>
  446. /// <returns>字符串位数</returns>
  447. public static string GetRandomLetter(int length = 2)
  448. {
  449. int number;
  450. char code;
  451. string checkCode = String.Empty;
  452. System.Random random = new Random(Guid.NewGuid().GetHashCode());
  453. for (int i = 0; i < length; i++)
  454. {
  455. number = random.Next();
  456. code = (char)('A' + (char)(number % 26));
  457. checkCode += code.ToString();
  458. }
  459. return checkCode;
  460. }
  461. /// <summary>
  462. /// 得到一个文件的大小(单位KB)
  463. /// </summary>
  464. /// <returns></returns>
  465. public static string GetFileSize(string file)
  466. {
  467. if (!System.IO.File.Exists(file))
  468. {
  469. return "";
  470. }
  471. System.IO.FileInfo fi = new System.IO.FileInfo(file);
  472. return (fi.Length/1000).ToString("###,###");
  473. }
  474. public static string DataTableToJsonString(System.Data.DataTable dt)
  475. {
  476. LitJson.JsonData json = new LitJson.JsonData();
  477. foreach(System.Data.DataRow dr in dt.Rows)
  478. {
  479. LitJson.JsonData drJson = new LitJson.JsonData();
  480. for (int i = 0; i < dt.Columns.Count; i++)
  481. {
  482. LitJson.JsonData drJson1 = new LitJson.JsonData();
  483. drJson1 = dr[i].ToString();
  484. drJson.Add(drJson1);
  485. }
  486. json.Add(drJson);
  487. }
  488. return json.ToJson();
  489. }
  490. }
  491. }