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.

392 lines
14 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. using Common.Model;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.IO;
  7. using System.Web;
  8. using System.Web.Script.Serialization;
  9. using System.Web.UI;
  10. namespace Common.Helper
  11. {
  12. public static class GetStatic
  13. {
  14. public static string GetUser()
  15. {
  16. var user = ReadSession("admin", "");
  17. WriteSession("admin", user);
  18. //WriteSession("lastActiveTS", DateTime.Now.ToString());
  19. return user;
  20. }
  21. public static string ReadSession(string key, string defVal)
  22. {
  23. try
  24. {
  25. return HttpContext.Current.Session[key] == null ? defVal : HttpContext.Current.Session[key].ToString();
  26. }
  27. catch
  28. {
  29. return "";
  30. }
  31. }
  32. public static void WriteSession(string key, string value)
  33. {
  34. HttpContext.Current.Session[key] = value;
  35. }
  36. public static string GetVirtualDirName()
  37. {
  38. return ReadWebConfig("virtualDirName");
  39. }
  40. public static string ReadWebConfig(string key)
  41. {
  42. return ReadWebConfig(key, "");
  43. }
  44. public static string ReadWebConfig(string key, string defValue)
  45. {
  46. return ConfigurationManager.AppSettings[key] ?? defValue;
  47. }
  48. public static string GetIp()
  49. {
  50. return HttpContext.Current.Request.ClientCertificate["REMOTE_ADDR"];
  51. }
  52. public static void WriteCookie(string key, string value)
  53. {
  54. if (string.IsNullOrEmpty(value.Trim()))
  55. {
  56. DeleteCookie(key);
  57. return;
  58. }
  59. var httpCookie = new HttpCookie(key, value);
  60. httpCookie.Expires = DateTime.Now.AddDays(1);
  61. HttpContext.Current.Response.Cookies.Add(httpCookie);
  62. }
  63. public static string ReadCookie(string key, string defVal)
  64. {
  65. var cookie = HttpContext.Current.Request.Cookies[key];
  66. return cookie == null ? defVal : HttpContext.Current.Server.HtmlEncode(cookie.Value);
  67. }
  68. public static void DeleteCookie(string key)
  69. {
  70. if (HttpContext.Current.Request.Cookies[key] != null)
  71. {
  72. var aCookie = new HttpCookie(key);
  73. aCookie.Expires = DateTime.Now.AddDays(-1);
  74. HttpContext.Current.Response.Cookies.Add(aCookie);
  75. }
  76. }
  77. public static void SetMessage(DbResponse value)
  78. {
  79. HttpContext.Current.Session["message"] = value;
  80. }
  81. public static void SetMessage(string responseCode, string msg)
  82. {
  83. var dbResult = new DbResponse { ResponseCode = responseCode, Msg = msg };
  84. SetMessage(dbResult);
  85. }
  86. public static DbResponse ParseDbResult(DataTable dt)
  87. {
  88. var res = new DbResponse();
  89. if (dt.Rows.Count > 0)
  90. {
  91. res.ResponseCode = dt.Rows[0][0].ToString();
  92. res.Msg = dt.Rows[0][1].ToString();
  93. res.Id = dt.Rows[0][2].ToString();
  94. if (dt.Columns.Count > 3)
  95. {
  96. res.Extra = dt.Rows[0][3].ToString();
  97. }
  98. if (dt.Columns.Count > 4)
  99. {
  100. res.Extra2 = dt.Rows[0][4].ToString();
  101. }
  102. }
  103. return res;
  104. }
  105. public static void JsonResponse<T>(T obk, Page page)
  106. {
  107. System.Web.Script.Serialization.JavaScriptSerializer jsonData = new System.Web.Script.Serialization.JavaScriptSerializer();
  108. string jsonString = jsonData.Serialize(obk);
  109. page.Response.ContentType = "application/json";
  110. page.Response.Write(jsonString);
  111. page.Response.End();
  112. }
  113. public static void PrintMessage(Page page)
  114. {
  115. if (HttpContext.Current.Session["message"] == null)
  116. {
  117. //CallBackJs1(page, "Remove Message", "window.parent.RemoveMessageBox();");
  118. return;
  119. }
  120. var dbResult = GetMessage();
  121. CallBackJs(page, "Set Message", "window.parent.SetMessageBox(\"" + dbResult.Msg + "\",\"" + dbResult.ResponseCode + "\");");
  122. HttpContext.Current.Session.Remove("message");
  123. }
  124. public static DbResponse GetMessage()
  125. {
  126. return (DbResponse)HttpContext.Current.Session["message"];
  127. }
  128. public static void CallBackJs(Page page, String scriptName, string functionName)
  129. {
  130. ScriptManager.RegisterStartupScript(page, page.GetType(), scriptName, functionName, true);
  131. }
  132. public static void PrintMessage(Page page, DbResponse dbResult)
  133. {
  134. CallBackJs(page, "Set Message", "window.parent.SetMessageBox(\"" + dbResult.Msg + "\",\"" + dbResult.ResponseCode + "\");");
  135. }
  136. public static void PrintSuccessMessage(Page page, string msg)
  137. {
  138. PrintMessage(page, "0", msg);
  139. }
  140. public static void PrintErrorMessage(Page page, string msg)
  141. {
  142. PrintMessage(page, "1", msg);
  143. }
  144. public static void PrintMessage(Page page, string errorCode, string msg)
  145. {
  146. CallBackJs(page, "Set Message", "window.parent.SetMessageBox(\"" + msg + "\",\"" + errorCode + "\");");
  147. }
  148. public static void AlertMessage(Page page)
  149. {
  150. if (HttpContext.Current.Session["message"] == null)
  151. return;
  152. var dbResult = GetMessage();
  153. if (dbResult.Msg == "")
  154. return;
  155. CallBackJs(page, "Alert Message", "alert(\"" + FilterMessageForJs(dbResult.Msg) + "\");");
  156. HttpContext.Current.Session.Remove("message");
  157. }
  158. public static String FilterMessageForJs(string strVal)
  159. {
  160. if (strVal.ToLower() != "null")
  161. {
  162. strVal = strVal.Replace("\"", "");
  163. }
  164. return strVal;
  165. }
  166. public static string GetIpAddress(HttpRequest request)
  167. {
  168. return request.ServerVariables["remote_addr"];
  169. }
  170. public static string GetUserInfo(HttpRequest request)
  171. {
  172. HttpBrowserCapabilities browser = request.Browser;
  173. string str = " Browser Capabilities = Values -:::-"
  174. + "Type = " + browser.Type + "-:::-" //-:::-
  175. + "Name = " + browser.Browser + "-:::-"
  176. + "Version = " + browser.Version + "-:::-"
  177. + "Major Version = " + browser.MajorVersion + "-:::-"
  178. + "Minor Version = " + browser.MinorVersion + "-:::-"
  179. + "Platform = " + browser.Platform + "-:::-"
  180. + "Is Beta = " + browser.Beta + "-:::-"
  181. + "Is Crawler = " + browser.Crawler + "-:::-"
  182. + "Is AOL = " + browser.AOL + "-:::-"
  183. + "Is Win16 = " + browser.Win16 + "-:::-"
  184. + "Is Win32 = " + browser.Win32 + "-:::-"
  185. + "Supports Frames = " + browser.Frames + "-:::-"
  186. + "Supports Tables = " + browser.Tables + "-:::-"
  187. + "Supports Cookies = " + browser.Cookies + "-:::-"
  188. + "Supports VBScript = " + browser.VBScript + "-:::-"
  189. + "Supports JavaScript = " + browser.EcmaScriptVersion.ToString() + "-:::-"
  190. + "Supports Java Applets = " + browser.JavaApplets + "-:::-"
  191. + "Supports ActiveX Controls = " + browser.ActiveXControls + "-:::-"
  192. + "Supports JavaScript Version = " + browser["JavaScriptVersion"] + "-:::-"
  193. + "CDF = " + browser.CDF + "-:::-"
  194. + "IP Adress = " + request.ServerVariables["REMOTE_ADDR"] + "-:::-"
  195. + "User Agent = " + request.ServerVariables["HTTP_USER_AGENT"] + "-:::-"
  196. + "Refrerer = " + request.ServerVariables["HTTP_REFERER"] + "-:::-"
  197. + "Http Accept = " + request.ServerVariables["HTTP_ACCEPT"] + "-:::-"
  198. + "Language = " + request.ServerVariables["HTTP_ACCEPT_LANGUAGE"];
  199. return str;
  200. }
  201. public static string ReadQueryString(string key, string defVal)
  202. {
  203. return HttpContext.Current.Request.QueryString[key] ?? defVal;
  204. }
  205. public static string ReadFormData(string key, string defval)
  206. {
  207. return HttpContext.Current.Request.Form[key] ?? defval;
  208. }
  209. public static void ClearSession(string key)
  210. {
  211. HttpContext.Current.Session[key] = "";
  212. }
  213. public static string GetUrlRoot()
  214. {
  215. return ReadWebConfig("urlRoot");
  216. }
  217. public static string FormatData(string data, string dataType)
  218. {
  219. if (string.IsNullOrEmpty(data))
  220. return "&nbsp;";
  221. if (data == "-")
  222. return data;
  223. if (dataType == "D")
  224. {
  225. DateTime d;
  226. DateTime.TryParse(data, out d);
  227. return d.Year + "-" + d.Month.ToString("00") + "-" + d.Day.ToString("00");
  228. }
  229. if (dataType == "DT")
  230. {
  231. DateTime t;
  232. DateTime.TryParse(data, out t);
  233. return t.Year + "-" + t.Month.ToString("00") + "-" + t.Day.ToString("00") + " " + t.Hour.ToString("00") + ":" + t.Minute.ToString("00");
  234. }
  235. if (dataType == "M")
  236. {
  237. decimal m;
  238. decimal.TryParse(data, out m);
  239. return m.ToString("N");
  240. }
  241. return data;
  242. }
  243. public static void AlertMessage(Page page, string msg)
  244. {
  245. CallBackJs1(page, "Alert Message", "alert(\"" + FilterMessageForJs(msg) + "\");");
  246. }
  247. public static void CallBackJs1(Page page, String scriptName, string functionName)
  248. {
  249. ScriptManager.RegisterStartupScript(page, page.GetType(), scriptName, functionName, true);
  250. }
  251. public static string GetLogoutPage()
  252. {
  253. return GetUrlRoot() + "/Logout.aspx";
  254. }
  255. public static string GetAuthenticationPage()
  256. {
  257. return GetUrlRoot() + "/Authentication.aspx";
  258. }
  259. public static string GetAgent()
  260. {
  261. return ReadSession("agent", "");
  262. }
  263. public static string DataTableToJson(DataTable table)
  264. {
  265. if (table == null)
  266. return "";
  267. var list = new List<Dictionary<string, object>>();
  268. foreach (DataRow row in table.Rows)
  269. {
  270. var dict = new Dictionary<string, object>();
  271. foreach (DataColumn col in table.Columns)
  272. {
  273. dict[col.ColumnName] = string.IsNullOrEmpty(row[col].ToString()) ? "" : row[col];
  274. }
  275. list.Add(dict);
  276. }
  277. var serializer = new JavaScriptSerializer();
  278. string json = serializer.Serialize(list);
  279. return json;
  280. }
  281. public static string GetUserType()
  282. {
  283. var userType = ReadSession("userType", "");
  284. //WriteSession("lastActiveTS", DateTime.Now.ToString());
  285. return userType;
  286. }
  287. public static string GetAgentId()
  288. {
  289. return ReadSession("agentId", "");
  290. }
  291. public static string GetCustomerFilePath()
  292. {
  293. return ReadWebConfig("customerDocPath");
  294. }
  295. public static string GetAppRoot()
  296. {
  297. return ReadWebConfig("root");
  298. }
  299. public static string GetCountryId()
  300. {
  301. return ReadSession("countryId", "");
  302. }
  303. public static string GetSettlingAgent()
  304. {
  305. return ReadSession("settlingAgent", "");
  306. }
  307. public static string GetSuperAgent()
  308. {
  309. return ReadSession("superAgent", "");
  310. }
  311. public static string GetBranch()
  312. {
  313. return ReadSession("branch", "");
  314. }
  315. public static void CallJSFunction(Page page, string functionName)
  316. {
  317. ScriptManager.RegisterStartupScript(page, page.GetType(), "cb", functionName, true);
  318. }
  319. public static string UploadSignatureImage(string imageData, string registerDate, string membershipId, string customerId)
  320. {
  321. string errorCode = "1";
  322. try
  323. {
  324. string fileExtension = ".png";
  325. string fileName = customerId + "_signature_" + DateTime.Now.Hour.ToString() + DateTime.Now.Millisecond.ToString() + "_" + registerDate.Replace("-", "_") + fileExtension;
  326. string path = GetStatic.GetCustomerFilePath() + "CustomerDocument\\" + registerDate.Replace("-", "\\") + "\\" + membershipId;
  327. if (!Directory.Exists(path))
  328. Directory.CreateDirectory(path);
  329. string fName = path + "\\" + fileName;
  330. using (FileStream fs = new FileStream(fName, FileMode.CreateNew))
  331. {
  332. using (BinaryWriter bw = new BinaryWriter(fs))
  333. {
  334. byte[] data = Convert.FromBase64String(imageData);
  335. bw.Write(data);
  336. bw.Close();
  337. }
  338. }
  339. errorCode = fileName;
  340. }
  341. catch (Exception ex)
  342. {
  343. errorCode = "1";
  344. }
  345. return errorCode;
  346. }
  347. }
  348. }