You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.1 KiB

9 months ago
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Configuration;
  4. using System.Security.Cryptography;
  5. using System.Text;
  6. using System.Web;
  7. namespace Common.Utility
  8. {
  9. public static class GetStatic
  10. {
  11. public static string ReadQueryString(string key, string defVal)
  12. {
  13. ////string str=HttpContext.Current.Request.QueryString[key] ?? defVal;
  14. ////str = str.Replace("#", "");
  15. ////return str;
  16. return HttpContext.Current.Request.QueryString[key] ?? defVal;
  17. }
  18. public static string ComputeSha256Hash(string rawData)
  19. {
  20. // Create a SHA256
  21. using (SHA256 sha256Hash = SHA256.Create())
  22. {
  23. // ComputeHash - returns byte array
  24. byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));
  25. // Convert byte array to a string
  26. StringBuilder builder = new StringBuilder();
  27. for (int i = 0; i < bytes.Length; i++)
  28. {
  29. builder.Append(bytes[i].ToString("x2"));
  30. }
  31. return builder.ToString();
  32. }
  33. }
  34. public static string ConvertToJson(object obj)
  35. {
  36. return JsonConvert.SerializeObject(obj);
  37. }
  38. public static T ConvertToObject<T>(string input) where T : class
  39. {
  40. return JsonConvert.DeserializeObject<T>(input);
  41. }
  42. public static string GetSessionId()
  43. {
  44. return HttpContext.Current.Session.SessionID;
  45. }
  46. public static double ParseDouble(string value)
  47. {
  48. double tmp;
  49. double.TryParse(value, out tmp);
  50. return tmp;
  51. }
  52. public static long ParseLong(string value)
  53. {
  54. long tmp;
  55. long.TryParse(value, out tmp);
  56. return tmp;
  57. }
  58. public static string ObjectToJson(object obj)
  59. {
  60. return JsonConvert.SerializeObject(obj);
  61. }
  62. public static string ReadWebConfig(string key, string defVal)
  63. {
  64. try
  65. {
  66. return ConfigurationManager.AppSettings[key].ToString() == null ? defVal : ConfigurationManager.AppSettings[key].ToString();
  67. }
  68. catch
  69. {
  70. return defVal;
  71. }
  72. }
  73. public static String ShowDecimal(String strVal)
  74. {
  75. if (strVal != "")
  76. return String.Format("{0:0,0.00}", double.Parse(strVal));
  77. else
  78. return strVal;
  79. }
  80. public static String ShowTwoDecimal(String strVal)
  81. {
  82. if (strVal != "")
  83. return double.Parse(strVal).ToString("0.00");
  84. else
  85. return strVal;
  86. }
  87. public static string Truncate(this string value, int maxLength)
  88. {
  89. var returnValue = "";
  90. if (string.IsNullOrEmpty(value)) return value;
  91. returnValue = value.Length <= maxLength ? value : value.Substring(0, maxLength);
  92. return returnValue;
  93. }
  94. }
  95. }