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.
 
 
 
 
 

393 lines
14 KiB

using Common.Model;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
namespace Common.Helper
{
public static class GetStatic
{
public static string GetUser()
{
var user = ReadSession("admin", "");
WriteSession("admin", user);
//WriteSession("lastActiveTS", DateTime.Now.ToString());
return user;
}
public static string ReadSession(string key, string defVal)
{
try
{
return HttpContext.Current.Session[key] == null ? defVal : HttpContext.Current.Session[key].ToString();
}
catch
{
return "";
}
}
public static void WriteSession(string key, string value)
{
HttpContext.Current.Session[key] = value;
}
public static string GetVirtualDirName()
{
return ReadWebConfig("virtualDirName");
}
public static string ReadWebConfig(string key)
{
return ReadWebConfig(key, "");
}
public static string ReadWebConfig(string key, string defValue)
{
return ConfigurationManager.AppSettings[key] ?? defValue;
}
public static string GetIp()
{
return HttpContext.Current.Request.ClientCertificate["REMOTE_ADDR"];
}
public static void WriteCookie(string key, string value)
{
if (string.IsNullOrEmpty(value.Trim()))
{
DeleteCookie(key);
return;
}
var httpCookie = new HttpCookie(key, value);
httpCookie.Expires = DateTime.Now.AddDays(1);
HttpContext.Current.Response.Cookies.Add(httpCookie);
}
public static string ReadCookie(string key, string defVal)
{
var cookie = HttpContext.Current.Request.Cookies[key];
return cookie == null ? defVal : HttpContext.Current.Server.HtmlEncode(cookie.Value);
}
public static void DeleteCookie(string key)
{
if (HttpContext.Current.Request.Cookies[key] != null)
{
var aCookie = new HttpCookie(key);
aCookie.Expires = DateTime.Now.AddDays(-1);
HttpContext.Current.Response.Cookies.Add(aCookie);
}
}
public static void SetMessage(DbResponse value)
{
HttpContext.Current.Session["message"] = value;
}
public static void SetMessage(string responseCode, string msg)
{
var dbResult = new DbResponse { ResponseCode = responseCode, Msg = msg };
SetMessage(dbResult);
}
public static DbResponse ParseDbResult(DataTable dt)
{
var res = new DbResponse();
if (dt.Rows.Count > 0)
{
res.ResponseCode = dt.Rows[0][0].ToString();
res.Msg = dt.Rows[0][1].ToString();
res.Id = dt.Rows[0][2].ToString();
if (dt.Columns.Count > 3)
{
res.Extra = dt.Rows[0][3].ToString();
}
if (dt.Columns.Count > 4)
{
res.Extra2 = dt.Rows[0][4].ToString();
}
}
return res;
}
public static void JsonResponse<T>(T obk, Page page)
{
System.Web.Script.Serialization.JavaScriptSerializer jsonData = new System.Web.Script.Serialization.JavaScriptSerializer();
string jsonString = jsonData.Serialize(obk);
page.Response.ContentType = "application/json";
page.Response.Write(jsonString);
page.Response.End();
}
public static void PrintMessage(Page page)
{
if (HttpContext.Current.Session["message"] == null)
{
//CallBackJs1(page, "Remove Message", "window.parent.RemoveMessageBox();");
return;
}
var dbResult = GetMessage();
CallBackJs(page, "Set Message", "window.parent.SetMessageBox(\"" + dbResult.Msg + "\",\"" + dbResult.ResponseCode + "\");");
HttpContext.Current.Session.Remove("message");
}
public static DbResponse GetMessage()
{
return (DbResponse)HttpContext.Current.Session["message"];
}
public static void CallBackJs(Page page, String scriptName, string functionName)
{
ScriptManager.RegisterStartupScript(page, page.GetType(), scriptName, functionName, true);
}
public static void PrintMessage(Page page, DbResponse dbResult)
{
CallBackJs(page, "Set Message", "window.parent.SetMessageBox(\"" + dbResult.Msg + "\",\"" + dbResult.ResponseCode + "\");");
}
public static void PrintSuccessMessage(Page page, string msg)
{
PrintMessage(page, "0", msg);
}
public static void PrintErrorMessage(Page page, string msg)
{
PrintMessage(page, "1", msg);
}
public static void PrintMessage(Page page, string errorCode, string msg)
{
CallBackJs(page, "Set Message", "window.parent.SetMessageBox(\"" + msg + "\",\"" + errorCode + "\");");
}
public static void AlertMessage(Page page)
{
if (HttpContext.Current.Session["message"] == null)
return;
var dbResult = GetMessage();
if (dbResult.Msg == "")
return;
CallBackJs(page, "Alert Message", "alert(\"" + FilterMessageForJs(dbResult.Msg) + "\");");
HttpContext.Current.Session.Remove("message");
}
public static String FilterMessageForJs(string strVal)
{
if (strVal.ToLower() != "null")
{
strVal = strVal.Replace("\"", "");
}
return strVal;
}
public static string GetIpAddress(HttpRequest request)
{
return request.ServerVariables["remote_addr"];
}
public static string GetUserInfo(HttpRequest request)
{
HttpBrowserCapabilities browser = request.Browser;
string str = " Browser Capabilities = Values -:::-"
+ "Type = " + browser.Type + "-:::-" //-:::-
+ "Name = " + browser.Browser + "-:::-"
+ "Version = " + browser.Version + "-:::-"
+ "Major Version = " + browser.MajorVersion + "-:::-"
+ "Minor Version = " + browser.MinorVersion + "-:::-"
+ "Platform = " + browser.Platform + "-:::-"
+ "Is Beta = " + browser.Beta + "-:::-"
+ "Is Crawler = " + browser.Crawler + "-:::-"
+ "Is AOL = " + browser.AOL + "-:::-"
+ "Is Win16 = " + browser.Win16 + "-:::-"
+ "Is Win32 = " + browser.Win32 + "-:::-"
+ "Supports Frames = " + browser.Frames + "-:::-"
+ "Supports Tables = " + browser.Tables + "-:::-"
+ "Supports Cookies = " + browser.Cookies + "-:::-"
+ "Supports VBScript = " + browser.VBScript + "-:::-"
+ "Supports JavaScript = " + browser.EcmaScriptVersion.ToString() + "-:::-"
+ "Supports Java Applets = " + browser.JavaApplets + "-:::-"
+ "Supports ActiveX Controls = " + browser.ActiveXControls + "-:::-"
+ "Supports JavaScript Version = " + browser["JavaScriptVersion"] + "-:::-"
+ "CDF = " + browser.CDF + "-:::-"
+ "IP Adress = " + request.ServerVariables["REMOTE_ADDR"] + "-:::-"
+ "User Agent = " + request.ServerVariables["HTTP_USER_AGENT"] + "-:::-"
+ "Refrerer = " + request.ServerVariables["HTTP_REFERER"] + "-:::-"
+ "Http Accept = " + request.ServerVariables["HTTP_ACCEPT"] + "-:::-"
+ "Language = " + request.ServerVariables["HTTP_ACCEPT_LANGUAGE"];
return str;
}
public static string ReadQueryString(string key, string defVal)
{
return HttpContext.Current.Request.QueryString[key] ?? defVal;
}
public static string ReadFormData(string key, string defval)
{
return HttpContext.Current.Request.Form[key] ?? defval;
}
public static void ClearSession(string key)
{
HttpContext.Current.Session[key] = "";
}
public static string GetUrlRoot()
{
return ReadWebConfig("urlRoot");
}
public static string FormatData(string data, string dataType)
{
if (string.IsNullOrEmpty(data))
return "&nbsp;";
if (data == "-")
return data;
if (dataType == "D")
{
DateTime d;
DateTime.TryParse(data, out d);
return d.Year + "-" + d.Month.ToString("00") + "-" + d.Day.ToString("00");
}
if (dataType == "DT")
{
DateTime t;
DateTime.TryParse(data, out t);
return t.Year + "-" + t.Month.ToString("00") + "-" + t.Day.ToString("00") + " " + t.Hour.ToString("00") + ":" + t.Minute.ToString("00");
}
if (dataType == "M")
{
decimal m;
decimal.TryParse(data, out m);
return m.ToString("N");
}
return data;
}
public static void AlertMessage(Page page, string msg)
{
CallBackJs1(page, "Alert Message", "alert(\"" + FilterMessageForJs(msg) + "\");");
}
public static void CallBackJs1(Page page, String scriptName, string functionName)
{
ScriptManager.RegisterStartupScript(page, page.GetType(), scriptName, functionName, true);
}
public static string GetLogoutPage()
{
return GetUrlRoot() + "/Logout.aspx";
}
public static string GetAuthenticationPage()
{
return GetUrlRoot() + "/Authentication.aspx";
}
public static string GetAgent()
{
return ReadSession("agent", "");
}
public static string DataTableToJson(DataTable table)
{
if (table == null)
return "";
var list = new List<Dictionary<string, object>>();
foreach (DataRow row in table.Rows)
{
var dict = new Dictionary<string, object>();
foreach (DataColumn col in table.Columns)
{
dict[col.ColumnName] = string.IsNullOrEmpty(row[col].ToString()) ? "" : row[col];
}
list.Add(dict);
}
var serializer = new JavaScriptSerializer();
string json = serializer.Serialize(list);
return json;
}
public static string GetUserType()
{
var userType = ReadSession("userType", "");
//WriteSession("lastActiveTS", DateTime.Now.ToString());
return userType;
}
public static string GetAgentId()
{
return ReadSession("agentId", "");
}
public static string GetCustomerFilePath()
{
return ReadWebConfig("customerDocPath");
}
public static string GetAppRoot()
{
return ReadWebConfig("root");
}
public static string GetCountryId()
{
return ReadSession("countryId", "");
}
public static string GetSettlingAgent()
{
return ReadSession("settlingAgent", "");
}
public static string GetSuperAgent()
{
return ReadSession("superAgent", "");
}
public static string GetBranch()
{
return ReadSession("branch", "");
}
public static void CallJSFunction(Page page, string functionName)
{
ScriptManager.RegisterStartupScript(page, page.GetType(), "cb", functionName, true);
}
public static string UploadSignatureImage(string imageData, string registerDate, string membershipId, string customerId)
{
string errorCode = "1";
try
{
string fileExtension = ".png";
string fileName = customerId + "_signature_" + DateTime.Now.Hour.ToString() + DateTime.Now.Millisecond.ToString() + "_" + registerDate.Replace("-", "_") + fileExtension;
string path = GetStatic.GetCustomerFilePath() + "CustomerDocument\\" + registerDate.Replace("-", "\\") + "\\" + membershipId;
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
string fName = path + "\\" + fileName;
using (FileStream fs = new FileStream(fName, FileMode.CreateNew))
{
using (BinaryWriter bw = new BinaryWriter(fs))
{
byte[] data = Convert.FromBase64String(imageData);
bw.Write(data);
bw.Close();
}
}
errorCode = fileName;
}
catch (Exception ex)
{
errorCode = "1";
}
return errorCode;
}
}
}