DHelper.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Web.Security;
  7. using System.Security.Cryptography;
  8. using System.IO;
  9. using System.Web;
  10. using System.Drawing;
  11. using System.Drawing.Drawing2D;
  12. using System.Drawing.Imaging;
  13. using System.Reflection;
  14. using System.Data;
  15. public static class DHelper
  16. {
  17. #region 常用处理
  18. /// <summary>
  19. /// 检查DataTable 是否有数据行
  20. /// </summary>
  21. /// <param name="dt">DataTable</param>
  22. /// <returns></returns>
  23. public static bool IsHaveRows(this System.Data.DataTable dt)
  24. {
  25. if (dt != null && dt.Rows.Count > 0)
  26. return true;
  27. return false;
  28. }
  29. /// <summary>
  30. /// 对象是否为NULL
  31. /// </summary>
  32. /// <param name="str">对象</param>
  33. /// <returns>是否为NULL</returns>
  34. public static bool IsNull(this object obj)
  35. {
  36. if ((Object.Equals(obj, null)) || (Object.Equals(obj, DBNull.Value)) || (obj == null))
  37. {
  38. return true;
  39. }
  40. if (obj is DateTime && obj.ToDateTime().Equals(DateTime.MinValue))
  41. {
  42. return true;
  43. }
  44. return false;
  45. }
  46. /// <summary>
  47. /// 是否包含str字符有true
  48. /// </summary>
  49. /// <param name="strs"></param>
  50. /// <param name="str"></param>
  51. /// <returns></returns>
  52. public static bool IsIndexOf(this string strs, string str)
  53. {
  54. if (strs.ToLower().IndexOf(str.ToLower()) != -1)
  55. return true;
  56. return false;
  57. }
  58. /// <summary>
  59. /// 字符串是否为空true是空
  60. /// </summary>
  61. /// <param name="str">字符串</param>
  62. /// <returns>是否为空</returns>
  63. public static bool IsEmpty(this string str)
  64. {
  65. if (IsNull(str))
  66. {
  67. return true;
  68. }
  69. if (str.Equals(String.Empty))
  70. {
  71. return true;
  72. }
  73. return false;
  74. }
  75. /// <summary>
  76. /// 对象不为NULL
  77. /// </summary>
  78. /// <param name="str">字符串</param>
  79. /// <returns>返回字符串</returns>
  80. public static string IfNull(this object str)
  81. {
  82. if (str == null || Object.Equals(str, null))
  83. {
  84. return String.Empty;
  85. }
  86. return str.ToString();
  87. }
  88. /// <summary>
  89. /// 对象不为NULL
  90. /// </summary>
  91. /// <param name="str">字符串</param>
  92. /// <returns>返回字符串</returns>
  93. public static bool IfNotNull(this object str)
  94. {
  95. if ((Object.Equals(str, null)) || (Object.Equals(str, DBNull.Value)) || (str == null))
  96. {
  97. return false;
  98. }
  99. return true;
  100. }
  101. /// <summary>
  102. /// 有值?(与IsNullOrEmpty相反)
  103. /// </summary>
  104. /// <returns></returns>
  105. public static bool IsValuable(this IEnumerable<object> thisValue)
  106. {
  107. if (thisValue == null || thisValue.Count() == 0) return false;
  108. return true;
  109. }
  110. /// <summary>
  111. /// 字符串不为NULL
  112. /// </summary>
  113. /// <param name="str">要验证字符串</param>
  114. /// <param name="val">为空返回字符串</param>
  115. /// <returns>返回字符串</returns>
  116. public static string IfNull(this string str, string retStr)
  117. {
  118. if (str == null || Object.Equals(str, null))
  119. {
  120. return retStr;
  121. }
  122. return str;
  123. }
  124. /// <summary>
  125. /// 字符串数组转换成数字
  126. /// </summary>
  127. /// <param name="strs">字符串</param>
  128. /// <returns>数字</returns>
  129. public static int[] StrsToInts(this string[] strs)
  130. {
  131. int[] ints = new int[strs.Length];
  132. for (int i = 0; i < strs.Length; i++)
  133. {
  134. ints[i] = Convert.ToInt32(strs[i]);
  135. }
  136. return ints;
  137. }
  138. /// <summary>
  139. /// 取得自定义长度的字符串
  140. /// </summary>
  141. /// <param name="str">字符串</param>
  142. /// <param name="lenght">长度</param>
  143. /// <returns>截取后字符串</returns>
  144. public static string CutChar(this string str, int lenght)
  145. {
  146. if (str.Length > lenght)
  147. {
  148. return str.Substring(0, lenght) + DString.OMITTED;
  149. }
  150. else
  151. {
  152. return str;
  153. }
  154. }
  155. /// <summary>
  156. /// 取得自定义长度的字符串 中文123456789
  157. /// </summary>
  158. /// <param name="str">字符串</param>
  159. /// <param name="lenght">长度</param>
  160. /// <returns>截取后字符串</returns>
  161. public static string CutCNChar(this string str, int lenght)
  162. {
  163. string ret = String.Empty;
  164. if (str.Length > lenght)
  165. {
  166. int tempnum1 = 0;
  167. int tempnum2 = 0;
  168. byte[] byitem = System.Text.ASCIIEncoding.ASCII.GetBytes(str);
  169. for (int i = 0; i < str.Length; i++)
  170. {
  171. if ((int)byitem[i] != 63)
  172. {
  173. tempnum1++;
  174. }
  175. else
  176. {
  177. tempnum2++;
  178. }
  179. if (tempnum2 * 2 + tempnum1 >= lenght * 2)
  180. {
  181. break;
  182. }
  183. }
  184. ret = str.Substring(0, tempnum2 + tempnum1) + DString.OMITTED;
  185. }
  186. else
  187. {
  188. ret = str;
  189. }
  190. return ret;
  191. }
  192. /// <summary>
  193. /// 返回字符串真实长度 1个汉字长度为2
  194. /// </summary>
  195. /// <param name="str">字符串</param>
  196. /// <returns>实际字符串长度</returns>
  197. public static int GetCharLength(this string str)
  198. {
  199. return Encoding.Default.GetBytes(str).Length;
  200. }
  201. /// <summary>
  202. /// 检测是否有Sql危险字符
  203. /// </summary>
  204. /// <param name="str">要判断字符串</param>
  205. /// <returns>判断结果</returns>
  206. public static bool IsSafeSqlString(this string str)
  207. {
  208. return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
  209. }
  210. public static bool IsSafety(this string str)
  211. {
  212. string text1 = Regex.Replace(str.Replace("%20", " "), @"\s", " ");
  213. string text2 = "select |insert |delete from |count\\(|drop table|update |truncate |asc\\(|mid\\(|char\\(|xp_cmdshell|exec master|net localgroup administrators|:|net user|\"|\\'| or ";
  214. return !Regex.IsMatch(text1, text2, RegexOptions.IgnoreCase);
  215. }
  216. /// <summary>
  217. /// 替换字符串
  218. /// </summary>
  219. /// <param name="Str">要替换的字符型</param>
  220. /// <returns>替换后的字符串</returns>
  221. public static string ReplaceW(this string str)
  222. {
  223. return Regex.Replace(str, DString.W, String.Empty).ToString();
  224. }
  225. /// <summary>
  226. /// SHA1加密字符串
  227. /// </summary>
  228. /// <param name="str">要加密的字符串</param>
  229. /// <returns>加密后的字符串</returns>
  230. public static string EncryptSHA1(this string str)
  231. {
  232. if (!str.IsEmpty())
  233. {
  234. return FormsAuthentication.HashPasswordForStoringInConfigFile(ReplaceW(str), DConfig.SHA1);
  235. }
  236. return str;
  237. }
  238. /// <summary>
  239. /// MD5加密字符串
  240. /// </summary>
  241. /// <param name="str">要加密的字符串</param>
  242. /// <returns>加密后的字符串</returns>
  243. public static string EncryptMD5(this string str)
  244. {
  245. if (!str.IsEmpty())
  246. {
  247. return FormsAuthentication.HashPasswordForStoringInConfigFile(ReplaceW(str), DConfig.MD5);
  248. }
  249. return str;
  250. }
  251. /// <summary>
  252. /// DES加密字符串
  253. /// </summary>
  254. /// <param name="encryptString">待加密的字符串</param>
  255. /// <param name="encryptKey">加密密钥,要求为8位</param>
  256. /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>
  257. public static string EncryptDES(this string encryptString, string encryptKey)
  258. {
  259. try
  260. {
  261. byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
  262. byte[] rgbIV = DConfig.KEYS;
  263. byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
  264. DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();
  265. MemoryStream mStream = new MemoryStream();
  266. CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  267. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  268. cStream.FlushFinalBlock();
  269. return Convert.ToBase64String(mStream.ToArray());
  270. }
  271. catch (Exception ex)
  272. {
  273. // Log.WriteLog(DMessage.ERR, ex);
  274. }
  275. return encryptString;
  276. }
  277. /// <summary>
  278. /// DES解密字符串
  279. /// </summary>
  280. /// <param name="decryptString">待解密的字符串</param>
  281. /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
  282. /// <returns>解密成功返回解密后的字符串,失败返源串</returns>
  283. public static string DecryptDES(this string decryptString, string decryptKey)
  284. {
  285. try
  286. {
  287. byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
  288. byte[] rgbIV = DConfig.KEYS;
  289. byte[] inputByteArray = Convert.FromBase64String(decryptString);
  290. DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
  291. MemoryStream mStream = new MemoryStream();
  292. CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
  293. cStream.Write(inputByteArray, 0, inputByteArray.Length);
  294. cStream.FlushFinalBlock();
  295. return Encoding.UTF8.GetString(mStream.ToArray());
  296. }
  297. catch (Exception ex)
  298. {
  299. ////DLog.WriteLog(DMessage.ERR, ex);
  300. }
  301. return decryptString;
  302. }
  303. /// <summary>
  304. /// 生成随机字符串
  305. /// </summary>
  306. /// <param name="length">字符串长度</param>
  307. /// <returns>随机字符串</returns>
  308. public static string RandChar(int length)
  309. {
  310. string strSep = DString.COMMA;
  311. char[] chrSep = strSep.ToCharArray();
  312. //因1与l不容易分清楚,所以去除
  313. //因0与O不容易分清楚,所以去除
  314. string strChar = DString.CHAR;
  315. string[] aryChar = strChar.Split(chrSep, strChar.Length);
  316. string strRandom = string.Empty;
  317. Random Rnd = new Random();
  318. //生成随机字符串
  319. for (int i = 0; i < length; i++)
  320. {
  321. strRandom += aryChar[Rnd.Next(aryChar.Length)];
  322. }
  323. return strRandom;
  324. }
  325. /// <summary>
  326. /// 生成随机数字字符串
  327. /// </summary>
  328. /// <param name="length">字符串长度</param>
  329. /// <returns>随机字符串</returns>
  330. public static string RandNum(int length)
  331. {
  332. string strSep = DString.COMMA;
  333. char[] chrSep = strSep.ToCharArray();
  334. //因1与l不容易分清楚,所以去除
  335. //因0与O不容易分清楚,所以去除
  336. string strChar = DString.NUM;
  337. string[] aryChar = strChar.Split(chrSep, strChar.Length);
  338. string strRandom = string.Empty;
  339. Random rnd = new Random();
  340. //生成随机字符串
  341. for (int i = 0; i < length; i++)
  342. {
  343. strRandom += aryChar[rnd.Next(aryChar.Length)];
  344. }
  345. return strRandom;
  346. }
  347. /// <summary>
  348. /// 生成随机汉子字符串
  349. /// </summary>
  350. /// <param name="length">字符串长度</param>
  351. /// <returns>随机字符串</returns>
  352. public static string RandCN(int length)
  353. {
  354. string strSep = DString.COMMA;
  355. char[] chrSep = strSep.ToCharArray();
  356. string strChar = DString.CN;
  357. //定义一个字符串数组储存汉字编码的组成元素
  358. string[] rBase = strChar.Split(chrSep, strChar.Length);
  359. Random rnd = new Random();
  360. //定义一个object数组用来
  361. object[] bytes = new object[length];
  362. /*每循环一次产生一个含两个元素的十六进制字节数组,并将其放入bject数组中
  363. 每个汉字有四个区位码组成
  364. 区位码第1位和区位码第2位作为字节数组第一个元素
  365. 区位码第3位和区位码第4位作为字节数组第二个元素
  366. */
  367. for (int i = 0; i < length; i++)
  368. {
  369. //区位码第1位
  370. int r1 = rnd.Next(11, 14);
  371. string str_r1 = rBase[r1].Trim();
  372. //区位码第2位
  373. rnd = new Random(r1 * unchecked((int)DateTime.Now.Ticks) + i);//更换随机数发生器的种子避免产生重复值
  374. int r2;
  375. if (r1 == 13)
  376. {
  377. r2 = rnd.Next(0, 7);
  378. }
  379. else
  380. {
  381. r2 = rnd.Next(0, 16);
  382. }
  383. string str_r2 = rBase[r2].Trim();
  384. //区位码第3位
  385. rnd = new Random(r2 * unchecked((int)DateTime.Now.Ticks) + i);
  386. int r3 = rnd.Next(10, 16);
  387. string str_r3 = rBase[r3].Trim();
  388. //区位码第4位
  389. rnd = new Random(r3 * unchecked((int)DateTime.Now.Ticks) + i);
  390. int r4;
  391. if (r3 == 10)
  392. {
  393. r4 = rnd.Next(1, 16);
  394. }
  395. else if (r3 == 15)
  396. {
  397. r4 = rnd.Next(0, 15);
  398. }
  399. else
  400. {
  401. r4 = rnd.Next(0, 16);
  402. }
  403. string str_r4 = rBase[r4].Trim();
  404. //定义两个字节变量存储产生的随机汉字区位码
  405. byte byte1 = Convert.ToByte(str_r1 + str_r2, 16);
  406. byte byte2 = Convert.ToByte(str_r3 + str_r4, 16);
  407. //将两个字节变量存储在字节数组中
  408. byte[] str_r = new byte[] { byte1, byte2 };
  409. //将产生的一个汉字的字节数组放入object数组中
  410. bytes.SetValue(str_r, i);
  411. }
  412. Encoding gb = Encoding.GetEncoding(DString.GBK);
  413. string[] str = new string[length];
  414. string chars = String.Empty;
  415. for (int i = 0; i < length; i++)
  416. {
  417. str[i] = gb.GetString((byte[])Convert.ChangeType(bytes[i], typeof(byte[])));
  418. chars = chars + str[i];
  419. }
  420. return chars;
  421. }
  422. /// <summary>
  423. /// 生成随机文件名
  424. /// </summary>
  425. /// <param name="filename">文件名</param>
  426. /// <returns>随机文件名</returns>
  427. public static string RandFileName()
  428. {
  429. return DObject.DATE_TIME + RandChar(5);
  430. }
  431. /// <summary>
  432. /// 生成随机订单号
  433. /// </summary>
  434. /// <returns>随机文件名</returns>
  435. public static string RandOrder()
  436. {
  437. return DObject.DATE_TIME + RandNum(8);
  438. }
  439. /// <summary>
  440. /// 生成小文件名
  441. /// </summary>
  442. /// <returns>原文件名</returns>
  443. public static string GetMin(this string filename)
  444. {
  445. return filename.Substring(0, filename.LastIndexOf(DString.POINT)) + DString.MIN + filename.Substring(filename.LastIndexOf(DString.POINT), filename.Length - filename.LastIndexOf(DString.POINT));
  446. }
  447. /// <summary>
  448. /// 生成缩略图
  449. /// </summary>
  450. /// <param name="originalImagePath">源图路径(物理路径)</param>
  451. /// <param name="thumbnailPath">缩略图路径(物理路径)</param>
  452. /// <param name="width">缩略图宽度</param>
  453. /// <param name="height">缩略图高度</param>
  454. /// <param name="mode">生成缩略图的方式 3:指定高宽缩放(可能变形)2:指定宽,高按比例 1:指定高,宽按比例0:指定高宽裁减(不变形)</param>
  455. public static bool MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, int mode)
  456. {
  457. bool isSuccess = true;
  458. Image originalImage = Image.FromFile(originalImagePath);
  459. int towidth = width;
  460. int toheight = height;
  461. int x = 0;
  462. int y = 0;
  463. int ow = originalImage.Width;
  464. int oh = originalImage.Height;
  465. switch (mode)
  466. {
  467. case 3://指定高宽缩放(可能变形)
  468. break;
  469. case 2://指定宽,高按比例
  470. toheight = originalImage.Height * width / originalImage.Width;
  471. break;
  472. case 1://指定高,宽按比例
  473. towidth = originalImage.Width * height / originalImage.Height;
  474. break;
  475. case 0://指定高宽裁减(不变形)
  476. if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
  477. {
  478. oh = originalImage.Height;
  479. ow = originalImage.Height * towidth / toheight;
  480. y = 0;
  481. x = (originalImage.Width - ow) / 2;
  482. }
  483. else
  484. {
  485. ow = originalImage.Width;
  486. oh = originalImage.Width * height / towidth;
  487. x = 0;
  488. y = (originalImage.Height - oh) / 2;
  489. }
  490. break;
  491. default:
  492. break;
  493. }
  494. //新建一个bmp图片
  495. Image bitmap = new Bitmap(towidth, toheight);
  496. //新建一个画板
  497. Graphics g = Graphics.FromImage(bitmap);
  498. //设置高质量插值法
  499. g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  500. //设置高质量,低速度呈现平滑程度
  501. g.SmoothingMode = SmoothingMode.HighQuality;
  502. //清空画布并以透明背景色填充
  503. g.Clear(Color.White);
  504. //在指定位置并且按指定大小绘制原图片的指定部分
  505. g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
  506. new Rectangle(x, y, ow, oh),
  507. GraphicsUnit.Pixel);
  508. try
  509. {
  510. //以jpg格式保存缩略图
  511. bitmap.Save(thumbnailPath, ImageFormat.Jpeg);
  512. }
  513. catch (System.Exception ex)
  514. {
  515. isSuccess = false;
  516. ////DLog.WriteLog(DMessage.ERR, ex);
  517. }
  518. finally
  519. {
  520. originalImage.Dispose();
  521. bitmap.Dispose();
  522. g.Dispose();
  523. }
  524. return isSuccess;
  525. }
  526. /// <summary>
  527. /// 根据属性名提取属性值
  528. /// </summary>
  529. /// <typeparam name="T">类型</typeparam>
  530. /// <param name="obj">要提取的对象</param>
  531. /// <param name="name">属性名</param>
  532. /// <returns>提取出的属性值</returns>
  533. public static object GetPropertyByName<T>(T obj, string name)
  534. {
  535. try
  536. {
  537. Type t = obj.GetType();
  538. PropertyInfo pi = t.GetProperty(name);
  539. return pi.GetValue(obj, null);
  540. }
  541. catch (System.Exception ex)
  542. {
  543. //DLog.WriteLog(DMessage.ERR, ex);
  544. }
  545. return new Object();
  546. }
  547. /// <summary>
  548. /// 根据属性名设置值
  549. /// </summary>
  550. /// <typeparam name="T">类型</typeparam>
  551. /// <param name="obj">要设置的对象</param>
  552. /// <param name="name">属性名</param>
  553. /// <param name="val">属性值</param>
  554. /// <returns>设置值后的对象</returns>
  555. public static T SetPropertyByName<T>(T obj, string name, object val)
  556. {
  557. try
  558. {
  559. Type t = obj.GetType();
  560. PropertyInfo pi = t.GetProperty(name);
  561. pi.SetValue(obj, val, null);
  562. }
  563. catch (System.Exception ex)
  564. {
  565. //DLog.WriteLog(DMessage.ERR, ex);
  566. }
  567. return obj;
  568. }
  569. #endregion
  570. #region 网页
  571. #region 常用
  572. /// <summary>
  573. /// 提取网址
  574. /// </summary>
  575. /// <param name="url">地址</param>
  576. /// <returns>真实地址</returns>
  577. public static string GetResolvedUrl(this string url)
  578. {
  579. return DObject.CURRECT_PAGE.ResolveUrl(url);
  580. }
  581. #endregion
  582. #region 页面跳转
  583. /// <summary>
  584. /// 页面出错
  585. /// </summary>
  586. public static void GoError()
  587. {
  588. DObject.CURRECT_PAGE.Response.Redirect(DConfig.PAGE_ERROR);
  589. }
  590. /// <summary>
  591. /// 无访问权限
  592. /// </summary>
  593. public static void GoNoPower()
  594. {
  595. DObject.CURRECT_PAGE.Response.Redirect(DConfig.PAGE_NOACCESS);
  596. }
  597. /// <summary>
  598. /// 找不到页面
  599. /// </summary>
  600. public static void GoNoFound()
  601. {
  602. DObject.CURRECT_PAGE.Response.Redirect(DConfig.PAGE_NOFOUND);
  603. }
  604. /// <summary>
  605. /// 网站维护中
  606. /// </summary>
  607. public static void GoMaintenance()
  608. {
  609. DObject.CURRECT_PAGE.Response.Redirect(DConfig.PAGE_MAINTENANCE);
  610. }
  611. #endregion
  612. #region COOKIE
  613. /// <summary>
  614. /// 创建COOKIE对象并赋Value值,修改COOKIE的Value值也用此方法,因为对COOKIE修改必须重新设Expires
  615. /// </summary>
  616. /// <param name="strCookieName">COOKIE对象名</param>
  617. /// <param name="iExpires">COOKIE对象有效时间(秒数),0一周内有效,1表示永久有效,大于等于2表示具体有效秒数</param>
  618. /// <param name="strValue">COOKIE对象Value值</param>
  619. public static void SetCookie(string strCookieName, int iExpires, string strValue)
  620. {
  621. HttpCookie objCookie = new HttpCookie(strCookieName);
  622. objCookie.Value = UrlEncode(strValue.Trim());
  623. if (iExpires >= 0)
  624. {
  625. if (iExpires.Equals(0))
  626. {
  627. objCookie.Expires = DateTime.Now.AddDays(7);
  628. }
  629. else if (iExpires.Equals(1))
  630. {
  631. objCookie.Expires = DateTime.MaxValue;
  632. }
  633. else
  634. {
  635. objCookie.Expires = DateTime.Now.AddSeconds(iExpires);
  636. }
  637. }
  638. DObject.CURRECT_PAGE.Response.Cookies.Add(objCookie);
  639. }
  640. /// <summary>
  641. /// 读取Cookie某个对象的Value值,返回Value值,如果对象本就不存在,则返回字符串"CookieNonexistence"
  642. /// </summary>
  643. /// <param name="strCookieName">Cookie对象名称</param>
  644. /// <returns>Value值,如果对象本就不存在,则返回字符串""</returns>
  645. public static string GetCookie(string strCookieName)
  646. {
  647. if (DObject.CURRECT_PAGE.Request.Cookies[strCookieName].IsNull())
  648. {
  649. return String.Empty;
  650. }
  651. else
  652. {
  653. return UrlDecode(DObject.CURRECT_PAGE.Request.Cookies[strCookieName].Value);
  654. }
  655. }
  656. /// <summary>
  657. /// URL解码
  658. /// </summary>
  659. /// <param name="str">要解码的字符串</param>
  660. /// <returns>解码后的字符串</returns>
  661. public static string UrlDecode(this string str)
  662. {
  663. try
  664. {
  665. return HttpUtility.UrlDecode(str);
  666. }
  667. catch (Exception ex)
  668. {
  669. //DLog.WriteLog(DMessage.ERR, ex);
  670. }
  671. return str;
  672. }
  673. /// <summary>
  674. /// URL编码
  675. /// </summary>
  676. /// <param name="str">要编码的字符串</param>
  677. /// <returns>编码后的字符串</returns>
  678. public static string UrlEncode(this string str)
  679. {
  680. try
  681. {
  682. return HttpUtility.UrlEncode(str);
  683. }
  684. catch (Exception ex)
  685. {
  686. //DLog.WriteLog(DMessage.ERR, ex);
  687. }
  688. return str;
  689. }
  690. /// <summary>
  691. /// HTML解码
  692. /// </summary>
  693. /// <param name="str">要解码的字符串</param>
  694. /// <returns>解码后的字符串</returns>
  695. public static string HtmlDecode(this string str)
  696. {
  697. try
  698. {
  699. return HttpUtility.HtmlDecode(str);
  700. }
  701. catch (Exception ex)
  702. {
  703. //DLog.WriteLog(DMessage.ERR, ex);
  704. }
  705. return str;
  706. }
  707. /// <summary>
  708. /// HTML编码
  709. /// </summary>
  710. /// <param name="str">要编码的字符串</param>
  711. /// <returns>编码后的字符串</returns>
  712. public static string HtmlEncode(this string str)
  713. {
  714. try
  715. {
  716. return HttpUtility.HtmlEncode(str);
  717. }
  718. catch (Exception ex)
  719. {
  720. //DLog.WriteLog(DMessage.ERR, ex);
  721. }
  722. return str;
  723. }
  724. #endregion
  725. #region Session
  726. /// <summary>
  727. /// 读取session值
  728. /// </summary>
  729. /// <param name="name">名称</param>
  730. /// <returns>值</returns>
  731. public static object GetSession(string name)
  732. {
  733. object Str_Value = null;
  734. try
  735. {
  736. Str_Value = DObject.CURRECT_PAGE.Session[name];
  737. }
  738. catch (Exception ex)
  739. {
  740. Str_Value = null;
  741. //DLog.WriteLog(DMessage.ERR, ex);
  742. }
  743. return Str_Value;
  744. }
  745. /// <summary>
  746. /// 读取session值
  747. /// </summary>
  748. /// <param name="name">名称</param>
  749. /// <returns>值</returns>
  750. public static string GetStrSession(string name)
  751. {
  752. string Str_Value = null;
  753. try
  754. {
  755. Str_Value = DObject.CURRECT_PAGE.Session[name].ToString();
  756. }
  757. catch (Exception ex)
  758. {
  759. Str_Value = String.Empty;
  760. //DLog.WriteLog(DMessage.ERR, ex);
  761. }
  762. return Str_Value;
  763. }
  764. /// <summary>
  765. /// 设置session值
  766. /// </summary>
  767. /// <param name="name">名称</param>
  768. /// <param name="pObj_Value">值</param>
  769. public static void SetSession(string name, object value)
  770. {
  771. try
  772. {
  773. DObject.CURRECT_PAGE.Session[name] = value;
  774. }
  775. catch (Exception ex)
  776. {
  777. //DLog.WriteLog(DMessage.ERR, ex);
  778. }
  779. }
  780. #endregion
  781. #region HTML
  782. /// <summary>
  783. /// 读取提交值
  784. /// </summary>
  785. /// <param name="name">名称</param>
  786. /// <returns>提交值</returns>
  787. public static string GetQueryString(string name)
  788. {
  789. if (DObject.CURRECT_PAGE.Request[name].IsNull())
  790. {
  791. return String.Empty;
  792. }
  793. return DObject.CURRECT_PAGE.Request[name];
  794. }
  795. /// <summary>
  796. /// 取得当前URL
  797. /// </summary>
  798. /// <returns>当前URL</returns>
  799. public static string GetPath()
  800. {
  801. string strPath = String.Format(DString.STRING_URL,
  802. HttpContext.Current.Request.ServerVariables[DString.STRING_HOST],
  803. HttpContext.Current.Request.ServerVariables[DString.STRING_PATH_INFO],
  804. HttpContext.Current.Request.ServerVariables[DString.STRING_QUERY_STRING]);
  805. if (strPath.IndexOf(DString.QM) > 0)
  806. {
  807. strPath = strPath.Substring(0, strPath.IndexOf(DString.QM));
  808. }
  809. return strPath;
  810. }
  811. /// <summary>
  812. /// Aspx 转换到 Html
  813. /// </summary>
  814. /// <param name="url">地址</param>
  815. /// <returns>字符串</returns>
  816. public static string AspxToHtml(this string url)
  817. {
  818. //判断路径是否为空
  819. if (!url.IsEmpty())
  820. {
  821. //分割路径
  822. string[] temp = url.Split(DString.CHAR_QM);
  823. //路径不合法
  824. if (temp.Length != 1 && temp.Length != 2)
  825. {
  826. return url;
  827. }
  828. //获取路径后缀
  829. string ext = Path.GetExtension(temp[0]);
  830. //后缀不为ASPX直接返回
  831. if (!(ext.Equals(DString.STRING_ASPX, StringComparison.OrdinalIgnoreCase)))
  832. {
  833. return url;
  834. }
  835. //截取.aspx中前面的内容
  836. int offset = temp[0].LastIndexOf(DString.CHAR_POINT);
  837. string resource = temp[0].Substring(0, offset);
  838. //路径不带参数时
  839. if (temp.Length == 1 || string.IsNullOrEmpty(temp[1]))
  840. {
  841. return string.Format(DString.STRING_HTML0, resource);
  842. }
  843. string aurl = temp[1].Replace(DString.CHAR_EQ, DString.CHAR_UNDERLINE);
  844. //路径带参数时
  845. return string.Format(DString.STRING_HTML01, resource, aurl.Replace(DString.AMP, DString.UNDERLINE_T));
  846. }
  847. return String.Empty;
  848. }
  849. /// <summary>
  850. /// Html 转换到 Aspx
  851. /// </summary>
  852. /// <param name="url">地址</param>
  853. /// <returns>字符串</returns>
  854. public static string HtmlToAspx(this string url)
  855. {
  856. //判断路径是否为空
  857. if (!url.IsEmpty())
  858. {
  859. string ext = Path.GetExtension(url);
  860. //路径不为HTML
  861. if (!(ext.Equals(DString.STRING_HTML, StringComparison.OrdinalIgnoreCase)))
  862. {
  863. return url;
  864. }
  865. string[] temp = url.Split(new String[] { DString.UNDERLINE_TH, DString.POINT }, StringSplitOptions.RemoveEmptyEntries);
  866. if (temp.Length == 2)
  867. {
  868. return string.Format(DString.STRING_ASPX0, temp[0]);
  869. }
  870. if (temp.Length == 3)
  871. {
  872. string aurl = temp[1].Replace(DString.UNDERLINE_T, DString.AMP);
  873. return String.Format(DString.STRING_ASPX01, temp[0], aurl.Replace(DString.CHAR_UNDERLINE, DString.CHAR_EQ));
  874. }
  875. }
  876. return String.Empty;
  877. }
  878. /// <summary>
  879. /// 过滤HTML代码
  880. /// </summary>
  881. /// <param name="fieldValue">要过滤的数据</param>
  882. /// <returns>过滤后的数据</returns>
  883. public static string FilterHTML(this string str)
  884. {
  885. Regex regexScript = new Regex(DString.STRING_SCRIPT, RegexOptions.IgnoreCase);
  886. Regex regexHref = new Regex(DString.STRING_HREFSCRIPT, RegexOptions.IgnoreCase);
  887. Regex regexOn = new Regex(DString.HTML_EVEN, RegexOptions.IgnoreCase);
  888. Regex regexIframe = new Regex(DString.STRING_IFRAME, RegexOptions.IgnoreCase);
  889. Regex regexFrameset = new Regex(DString.HTML_IFRAMESET, RegexOptions.IgnoreCase);
  890. string html = str.Trim();
  891. html = regexScript.Replace(html, String.Empty); //过滤<script></script>标记
  892. html = regexHref.Replace(html, String.Empty); //过滤href=javascript: (<A>) 属性
  893. html = regexOn.Replace(html, DString.HTML_EVEN_R); //过滤其它控件的on事件
  894. html = regexIframe.Replace(html, String.Empty); //过滤iframe
  895. html = regexFrameset.Replace(html, String.Empty); //过滤frameset
  896. return html;
  897. }
  898. #endregion
  899. #endregion
  900. #region 类型转换
  901. /// <summary>
  902. /// 转换成INT
  903. /// </summary>
  904. /// <param name="obj">要转换的对象</param>
  905. /// <returns>转换结果</returns>
  906. public static int ToInt32(this object obj)
  907. {
  908. try
  909. {
  910. if (obj.IsNull())
  911. {
  912. return 0;
  913. }
  914. int ret = Convert.ToInt32(obj);
  915. return ret;
  916. }
  917. catch (Exception ex)
  918. {
  919. //DLog.WriteLog(DMessage.ERR, ex);
  920. }
  921. return 0;
  922. }
  923. /// <summary>
  924. /// 转换成DataTable
  925. /// </summary>
  926. /// <param name="obj"></param>
  927. /// <returns></returns>
  928. public static DataTable ToDataTable(this object obj)
  929. {
  930. try
  931. {
  932. if (obj.IsNull())
  933. {
  934. return new DataTable();
  935. }
  936. DataTable ret = obj as DataTable;
  937. return ret;
  938. }
  939. catch (Exception ex)
  940. {
  941. //DLog.WriteLog(DMessage.ERR, ex);
  942. }
  943. return new DataTable();
  944. }
  945. /// <summary>
  946. /// 转换成List集合
  947. /// </summary>
  948. /// <typeparam name="T"></typeparam>
  949. /// <param name="obj"></param>
  950. /// <returns></returns>
  951. public static List<T> ToModList<T>(this object obj) where T : new()
  952. {
  953. try
  954. {
  955. if (obj.IsNull())
  956. {
  957. return new List<T>();
  958. }
  959. List<T> ret = obj as List<T>;
  960. return ret;
  961. }
  962. catch (Exception ex)
  963. {
  964. //DLog.WriteLog(DMessage.ERR, ex);
  965. }
  966. return new List<T>();
  967. }
  968. /// <summary>
  969. /// 转换成List集合
  970. /// </summary>
  971. /// <typeparam name="T"></typeparam>
  972. /// <param name="obj"></param>
  973. /// <returns></returns>
  974. public static T ToMod<T>(this object obj) where T : new()
  975. {
  976. try
  977. {
  978. if (obj.IsNull())
  979. {
  980. return new T();
  981. }
  982. T ret = (T)obj;
  983. return ret;
  984. }
  985. catch (Exception ex)
  986. {
  987. //DLog.WriteLog(DMessage.ERR, ex);
  988. }
  989. return new T();
  990. }
  991. /// <summary>
  992. /// 转换成INT
  993. /// </summary>
  994. /// <param name="obj">要转换的对象</param>
  995. /// <returns>转换结果</returns>
  996. public static int ToInt32(this object obj, int defValue)
  997. {
  998. try
  999. {
  1000. if (obj.IsNull())
  1001. {
  1002. return defValue;
  1003. }
  1004. int ret = Convert.ToInt32(obj);
  1005. return ret;
  1006. }
  1007. catch (Exception ex)
  1008. {
  1009. //DLog.WriteLog(DMessage.ERR, ex);
  1010. }
  1011. return defValue;
  1012. }
  1013. /// <summary>
  1014. /// 转换成INT
  1015. /// </summary>
  1016. /// <param name="obj">要转换的对象</param>
  1017. /// <returns>转换结果</returns>
  1018. public static decimal ToDec(this object obj)
  1019. {
  1020. try
  1021. {
  1022. if (obj.IsNull())
  1023. {
  1024. return 0;
  1025. }
  1026. decimal ret = Convert.ToDecimal(obj);
  1027. return ret;
  1028. }
  1029. catch (Exception ex)
  1030. {
  1031. //DLog.WriteLog(DMessage.ERR, ex);
  1032. }
  1033. return 0;
  1034. }
  1035. /// <summary>
  1036. /// 转换成INT
  1037. /// </summary>
  1038. /// <param name="obj">要转换的对象</param>
  1039. /// <returns>转换结果</returns>
  1040. public static decimal ToDec(this object obj, decimal defValue)
  1041. {
  1042. try
  1043. {
  1044. if (obj.IsNull())
  1045. {
  1046. return defValue;
  1047. }
  1048. decimal ret = Convert.ToDecimal(obj);
  1049. return ret;
  1050. }
  1051. catch (Exception ex)
  1052. {
  1053. //DLog.WriteLog(DMessage.ERR, ex);
  1054. }
  1055. return defValue;
  1056. }
  1057. /// <summary>
  1058. /// 转换成BOOL
  1059. /// </summary>
  1060. /// <param name="obj">要转换的对象</param>
  1061. /// <returns>转换结果</returns>
  1062. public static double ToDou(this object obj)
  1063. {
  1064. try
  1065. {
  1066. if (obj.IsNull())
  1067. {
  1068. return 0;
  1069. }
  1070. double ret = Convert.ToDouble(obj);
  1071. return ret;
  1072. }
  1073. catch (Exception ex)
  1074. {
  1075. //DLog.WriteLog(DMessage.ERR, ex);
  1076. }
  1077. return 0;
  1078. }
  1079. public static float ToFloat(this object obj, int defValue)
  1080. {
  1081. try
  1082. {
  1083. if (obj.IsNull())
  1084. {
  1085. return defValue;
  1086. }
  1087. float ret = Convert.ToInt32(obj);
  1088. return ret;
  1089. }
  1090. catch (Exception ex)
  1091. {
  1092. //DLog.WriteLog(DMessage.ERR, ex);
  1093. }
  1094. return defValue;
  1095. }
  1096. /// <summary>
  1097. /// 转换成BOOL
  1098. /// </summary>
  1099. /// <param name="obj">要转换的对象</param>
  1100. /// <returns>转换结果</returns>
  1101. public static double ToDou(this object obj, double defValue)
  1102. {
  1103. try
  1104. {
  1105. if (obj.IsNull())
  1106. {
  1107. return defValue;
  1108. }
  1109. double ret = Convert.ToDouble(obj);
  1110. return ret;
  1111. }
  1112. catch (Exception ex)
  1113. {
  1114. //DLog.WriteLog(DMessage.ERR, ex);
  1115. }
  1116. return defValue;
  1117. }
  1118. /// <summary>
  1119. /// 转换成BOOL
  1120. /// </summary>
  1121. /// <param name="obj">要转换的对象</param>
  1122. /// <returns>转换结果</returns>
  1123. public static bool ToBool(this object obj)
  1124. {
  1125. try
  1126. {
  1127. if (obj.IsNull())
  1128. {
  1129. return false;
  1130. }
  1131. bool ret = Convert.ToBoolean(obj);
  1132. return ret;
  1133. }
  1134. catch (Exception ex)
  1135. {
  1136. //DLog.WriteLog(DMessage.ERR, ex);
  1137. }
  1138. return false;
  1139. }
  1140. /// <summary>
  1141. /// 转换成BOOL
  1142. /// </summary>
  1143. /// <param name="obj">要转换的对象</param>
  1144. /// <returns>转换结果</returns>
  1145. public static DateTime ToDateTime(this object obj)
  1146. {
  1147. try
  1148. {
  1149. DateTime ret = Convert.ToDateTime(obj);
  1150. return ret;
  1151. }
  1152. catch (Exception ex)
  1153. {
  1154. //DLog.WriteLog(DMessage.ERR, ex);
  1155. }
  1156. return DObject.EMPTY_DATE_TIME;
  1157. }
  1158. public static string ToDateTimeStr(this DateTime obj)
  1159. {
  1160. try
  1161. {
  1162. return obj.ToString("yyyy-MM-dd");
  1163. }
  1164. catch (Exception ex)
  1165. {
  1166. //DLog.WriteLog(DMessage.ERR, ex);
  1167. }
  1168. return "";
  1169. }
  1170. #endregion
  1171. }