migrate to new git
This commit is contained in:
587
data/信眾APP規劃相關/加密參考/function.cs
Normal file
587
data/信眾APP規劃相關/加密參考/function.cs
Normal file
@@ -0,0 +1,587 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Web;
|
||||
using System.Web.UI;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
using System.ComponentModel;
|
||||
using System.Web.UI.WebControls;
|
||||
|
||||
namespace MyWeb
|
||||
{
|
||||
public class function : System.Web.UI.Page
|
||||
{
|
||||
public function()
|
||||
{
|
||||
//
|
||||
// TODO: 在這裡新增建構函式邏輯
|
||||
//
|
||||
}
|
||||
|
||||
public string randCode(int count)
|
||||
{
|
||||
//產生驗證碼
|
||||
Random rnd = new Random(Guid.NewGuid().GetHashCode());
|
||||
string[] code = {
|
||||
"1",
|
||||
"2",
|
||||
"3",
|
||||
"4",
|
||||
"5",
|
||||
"6",
|
||||
"7",
|
||||
"8",
|
||||
"9",
|
||||
"a",
|
||||
"b",
|
||||
"c",
|
||||
"d",
|
||||
"e",
|
||||
"f",
|
||||
"g",
|
||||
"h",
|
||||
"i",
|
||||
"j",
|
||||
"k",
|
||||
"m",
|
||||
"n",
|
||||
"p",
|
||||
"q",
|
||||
"r",
|
||||
"s",
|
||||
"t",
|
||||
"u",
|
||||
"v",
|
||||
"w",
|
||||
"x",
|
||||
"y",
|
||||
"z"
|
||||
};
|
||||
string rnd_code = null;
|
||||
//產生10碼
|
||||
for (int i = 1; i <= count; i++)
|
||||
{
|
||||
rnd_code += "" + code[rnd.Next(0, code.Length - 1)];
|
||||
}
|
||||
return rnd_code;
|
||||
}
|
||||
|
||||
public string randKey(int count)
|
||||
{
|
||||
//產生驗證碼
|
||||
Random rnd = new Random(Guid.NewGuid().GetHashCode());
|
||||
string[] code = {
|
||||
"0","1","2","3","4","5","6","7","8","9",
|
||||
"a","b","c","d","e","f","g","h","i", "j","k","l","m", "n", "o","p","q","r","s","t","u","v","w","x","y","z",
|
||||
"A","B","B","D","E","F","G","H","I", "J","K","L","M", "N", "O","P","Q","R","S","T","U","V","W","X","Y","Z",
|
||||
"+","/"
|
||||
};
|
||||
string rnd_code = null;
|
||||
//產生10碼
|
||||
for (int i = 1; i <= count; i++)
|
||||
{
|
||||
rnd_code += "" + code[rnd.Next(0, code.Length - 1)];
|
||||
}
|
||||
return rnd_code;
|
||||
}
|
||||
|
||||
public bool IsNumeric(object Expression)
|
||||
{
|
||||
bool isNum;
|
||||
double retNum;
|
||||
isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);
|
||||
return isNum;
|
||||
}
|
||||
|
||||
public bool isDate(object chkString)
|
||||
{
|
||||
DateTime dt;
|
||||
return DateTime.TryParse(Convert.ToString(chkString), out dt);
|
||||
}
|
||||
|
||||
public DateTime ValDate(object Expression)
|
||||
{
|
||||
DateTime _DateTime = Convert.ToDateTime(Expression);
|
||||
return _DateTime;
|
||||
}
|
||||
|
||||
|
||||
public int Val(object Expression)
|
||||
{
|
||||
int _Int = 0;
|
||||
if (IsNumeric(Expression))
|
||||
{
|
||||
_Int = Convert.ToInt32(Expression.ToString().Replace(",", ""));
|
||||
}
|
||||
return _Int;
|
||||
}
|
||||
|
||||
public float ValFloat(object Expression)
|
||||
{
|
||||
float _Int = 0;
|
||||
if (!isStrNull(Expression))
|
||||
{
|
||||
_Int = Convert.ToSingle(ValString(Expression).Replace(",", ""));
|
||||
}
|
||||
return _Int;
|
||||
}
|
||||
|
||||
public string ValMoney(object Expression, int DecimalLength = 2) //千分位,預設最多到小數點第2位
|
||||
{
|
||||
string format = "N0";
|
||||
string[] m = ValString(Expression).Replace(",", "").Split('.');
|
||||
if (ValString(Expression).IndexOf(".") > -1)
|
||||
{
|
||||
int ML = (m[m.Length - 1].Length > DecimalLength ? DecimalLength : m[m.Length - 1].Length);
|
||||
if (!isStrNull(m[m.Length - 1])) { format = "N" + ML.ToString(); }
|
||||
}
|
||||
return ValFloat(Expression).ToString(format);
|
||||
}
|
||||
|
||||
public string ValString(object Expression)
|
||||
{
|
||||
string _String = "";
|
||||
_String = Convert.ToString(Expression);
|
||||
return _String;
|
||||
}
|
||||
|
||||
public string ValMoneyCh(object Expression)
|
||||
{
|
||||
string price = Expression.ToString().Split('.')[0]; //去除小數點
|
||||
string i = price.Replace(",", ""); //去除千分位
|
||||
|
||||
string[] numc_arr = ("零,壹,貳,參,肆,伍,陸,柒,捌,玖").Split(',');
|
||||
string[] unic_arr = (",拾,佰,仟").Split(',');
|
||||
string[] unic1_arr = ("元整,萬,億,兆,京").Split(',');
|
||||
|
||||
int c0 = 0;
|
||||
List<string> str = new List<string>();
|
||||
do
|
||||
{
|
||||
int aa = 0;
|
||||
int c1 = 0;
|
||||
string s = "";
|
||||
//取最右邊四位數跑迴圈,不足四位就全取
|
||||
int lan = (i.Length >= 4 ? 4 : i.Length);
|
||||
int j = Convert.ToInt32(i.Substring(i.Length - lan, lan));
|
||||
while (j > 0)
|
||||
{
|
||||
int k = j % 10; //餘數
|
||||
if (k > 0) { aa = 1; s = numc_arr[k] + unic_arr[c1] + s; }
|
||||
else if (k == 0 && aa == 1) { s = "0" + s; }
|
||||
j = j / 10; //商
|
||||
c1++;
|
||||
}
|
||||
//轉成中文後丟入陣列,全部為零不加單位
|
||||
str.Add((s == "" ? "" : s + unic1_arr[c0]));
|
||||
//計算剩餘字串長度
|
||||
int count_len = i.Length - 4;
|
||||
i = (count_len > 0 ? i.Substring(0, count_len) : "");
|
||||
c0++;
|
||||
} while (!string.IsNullOrEmpty(i));
|
||||
|
||||
string chstring = "";
|
||||
while (str.Count > 0) { chstring += str[str.Count - 1]; str.Remove(str[str.Count - 1]); }
|
||||
|
||||
string pattern = "0+";
|
||||
string replacement = "零";
|
||||
Regex rgx = new Regex(pattern);
|
||||
string result = rgx.Replace(chstring, replacement);
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
public string Left(object Expression, int Length)
|
||||
{
|
||||
string str = "";
|
||||
str = ValString(Expression);
|
||||
if (Length > str.Length) { Length = str.Length; }
|
||||
str = str.Substring(0, Length);
|
||||
return str;
|
||||
}
|
||||
|
||||
public string Right(object Expression, int Length)
|
||||
{
|
||||
string str = "";
|
||||
str = ValString(Expression);
|
||||
int startIndex = str.Length - Length;
|
||||
if (startIndex < 0)
|
||||
{
|
||||
startIndex = 0;
|
||||
Length = str.Length;
|
||||
}
|
||||
str = str.Substring(startIndex, Length);
|
||||
return str;
|
||||
}
|
||||
|
||||
public bool isStrNull(object value)
|
||||
{
|
||||
return (value == null || value == DBNull.Value || Convert.ToString(value) == "" ? true : false);
|
||||
}
|
||||
|
||||
//截字
|
||||
public string cut_str(string str, int limit)
|
||||
{
|
||||
return str; //不截字,避免顏文字之類的圖在viewstate會出錯
|
||||
MatchCollection findCount;
|
||||
string Based = "[\u0080-\uFFFF]";
|
||||
//中日韓3byte以上的字符
|
||||
string tmp = null;
|
||||
int j = 0;
|
||||
for (int i = 0; i < str.Length; i++)
|
||||
{
|
||||
findCount = Regex.Matches(str.Substring(i, 1), Based, RegexOptions.Compiled);
|
||||
//找str裡面是否有Based指定的字
|
||||
if (findCount.Count == 0)
|
||||
{
|
||||
j += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
j += 2;
|
||||
//一個中文字占兩個
|
||||
}
|
||||
if (j <= limit)
|
||||
{
|
||||
tmp = tmp + str.Substring(i, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
i -= 1;
|
||||
if (i < str.Length)
|
||||
{
|
||||
if (!isStrNull(str.Substring(i, 1).Trim()))
|
||||
{
|
||||
//捨棄不完整的英文單字或數字
|
||||
int n = 0;
|
||||
for (int t = tmp.Length - 1; t >= 0; t--)
|
||||
{
|
||||
n++;
|
||||
if (Regex.Matches(tmp.Substring(t, 1), Based, RegexOptions.Compiled).Count > 0) //中文字
|
||||
{
|
||||
tmp = Left(tmp, tmp.Length - n + 1);
|
||||
break;
|
||||
}
|
||||
else if (isStrNull(tmp.Substring(t, 1).Trim()))
|
||||
{
|
||||
tmp = Left(tmp, tmp.Length - n);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
tmp = tmp + "...";
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
return tmp;
|
||||
}
|
||||
|
||||
public enum msgIcon : int
|
||||
{
|
||||
none = 0,
|
||||
success = 1,
|
||||
error = 2,
|
||||
warning = 3,
|
||||
info = 4,
|
||||
question = 5
|
||||
}
|
||||
|
||||
public void ScriptMsg(string txt, string url="", msgIcon icon= msgIcon.none)
|
||||
{
|
||||
ScriptManager.RegisterClientScriptBlock((Page)HttpContext.Current.Handler, typeof(string), "js", "msgbox('" + txt + "','" + (icon != msgIcon.none ? icon.ToString() : "") + "','" + url + "');", true);
|
||||
}
|
||||
|
||||
public void ScriptMsgTop(string txt, msgIcon icon)
|
||||
{
|
||||
ScriptManager.RegisterClientScriptBlock((Page)HttpContext.Current.Handler, typeof(string), "js", "msgtop('" + txt + "','" + (icon != msgIcon.none ? icon.ToString() : "") + "');", true);
|
||||
}
|
||||
|
||||
|
||||
public void ScriptJS(string script)
|
||||
{
|
||||
ScriptManager.RegisterClientScriptBlock((Page)HttpContext.Current.Handler, typeof(string), "js", script, true);
|
||||
}
|
||||
|
||||
public void ScriptMsg2(string txt, string url = "", msgIcon icon = msgIcon.none)
|
||||
{
|
||||
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "init", "msgbox('" + txt + "','" + (icon != msgIcon.none ? icon.ToString() : "") + "','" + url + "');", true);
|
||||
}
|
||||
|
||||
public void ScriptMsgTop2(string txt, msgIcon icon)
|
||||
{
|
||||
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "init", "msgtop('" + txt + "','" + (icon != msgIcon.none ? icon.ToString() : "") + "');", true);
|
||||
}
|
||||
|
||||
public void ScriptJS2(string script)
|
||||
{
|
||||
ScriptManager.RegisterStartupScript(Page, Page.GetType(), "init", script, true);
|
||||
}
|
||||
|
||||
public string br(string str)
|
||||
{
|
||||
return str.Replace(Convert.ToString((char)10), "<br>").Replace(Convert.ToString((char)13), "");
|
||||
}
|
||||
|
||||
|
||||
//日期格式
|
||||
public string datetype(string dtmp)
|
||||
{
|
||||
if (dtmp != null)
|
||||
{
|
||||
System.DateTime d = ValDate(dtmp);
|
||||
return d.ToString("yyyy-MM-dd");
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public string datetype(string dtmp, string format)
|
||||
{
|
||||
if (dtmp != null)
|
||||
{
|
||||
System.DateTime d = ValDate(dtmp);
|
||||
return d.ToString(format);
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
string allowReqAtt = System.Configuration.ConfigurationManager.AppSettings["allowReqAtt"].ToString();
|
||||
public bool AllowReq(string name)
|
||||
{
|
||||
if (!isStrNull(allowReqAtt))
|
||||
{
|
||||
string[] qns = allowReqAtt.Split(',');
|
||||
foreach (string qn in qns)
|
||||
if (qn.ToLower() == name.ToLower())
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
//傳回GET值並拿掉不要的,回傳格式為?xxxx=xxxx&yyyy=yyyy
|
||||
public string rtnQueryString(string noUseQuery)
|
||||
{
|
||||
string new_query = "";
|
||||
if (HttpContext.Current.Request.Url.AbsoluteUri.Split('?').Length == 2)
|
||||
{
|
||||
string[] query = HttpContext.Current.Request.Url.AbsoluteUri.Split('?')[1].Split('&');
|
||||
for (int i = 0; i < query.Length; i++)
|
||||
{
|
||||
string[] qs = query[i].Split('=');
|
||||
if (qs.Length == 2 && qs[0].ToLower() != noUseQuery.ToLower())
|
||||
{
|
||||
if (AllowReq(qs[0]))
|
||||
new_query += (new_query == "" ? "?" : "&") + qs[0] + "=" + Server.UrlEncode(Server.UrlDecode(qs[1]));
|
||||
}
|
||||
}
|
||||
}
|
||||
return new_query;
|
||||
}
|
||||
|
||||
public string rtnQueryString(Array noUseQuery)
|
||||
{
|
||||
string new_query = "";
|
||||
if (HttpContext.Current.Request.Url.AbsoluteUri.Split('?').Length == 2)
|
||||
{
|
||||
string[] query = HttpContext.Current.Request.Url.AbsoluteUri.Split('?')[1].Split('&');
|
||||
for (int i = 0; i < query.Length; i++)
|
||||
{
|
||||
string[] qs = query[i].Split('=');
|
||||
if (qs.Length == 2)
|
||||
{
|
||||
bool setAdd = true;
|
||||
foreach (string nq in noUseQuery)
|
||||
{
|
||||
if (qs[0].ToLower() == nq.ToLower())
|
||||
{
|
||||
setAdd = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (setAdd)
|
||||
{
|
||||
if (AllowReq(qs[0]))
|
||||
new_query += (new_query == "" ? "?" : "&") + qs[0] + "=" + Server.UrlEncode(Server.UrlDecode(qs[1]));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
return new_query;
|
||||
}
|
||||
|
||||
public string UrlHost()
|
||||
{
|
||||
string url = (IsHttps() ? "https://" : "http://")
|
||||
//+ HttpContext.Current.Request.Url.Host
|
||||
+ Request.ServerVariables["HTTP_HOST"]
|
||||
+ VirtualPathUtility.ToAbsolute("~/");
|
||||
return url;
|
||||
}
|
||||
|
||||
public string UrlAddr()
|
||||
{
|
||||
string url = (IsHttps() ? "https://" : "http://") + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.Url.AbsolutePath;
|
||||
return url;
|
||||
}
|
||||
|
||||
public static bool IsHttps()
|
||||
{
|
||||
return HttpContext.Current.Request.IsSecureConnection;
|
||||
}
|
||||
|
||||
#region 列舉
|
||||
|
||||
public string GetEnumsDescription(Enum value)
|
||||
{
|
||||
FieldInfo fi = value.GetType().GetField(value.ToString());
|
||||
DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
||||
return attributes.Length > 0 ? attributes[0].Description : value.ToString();
|
||||
}
|
||||
|
||||
public void InitEnumsOptions<T>(Control obj)
|
||||
{
|
||||
foreach (object value in Enum.GetValues(typeof(T)))
|
||||
if (obj is DropDownList)
|
||||
((DropDownList)obj).Items.Add(new ListItem(GetEnumsDescription((Enum)value), ((int)value).ToString()));
|
||||
else if (obj is RadioButtonList)
|
||||
((RadioButtonList)obj).Items.Add(new ListItem(GetEnumsDescription((Enum)value), ((int)value).ToString()));
|
||||
else if (obj is CheckBoxList)
|
||||
((CheckBoxList)obj).Items.Add(new ListItem(GetEnumsDescription((Enum)value), ((int)value).ToString()));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
public class WreqInfo
|
||||
{
|
||||
public string log = "";
|
||||
public string data = "";
|
||||
}
|
||||
|
||||
public enum Method { POST, GET, PUT, DELETE }
|
||||
|
||||
public WreqInfo WRequest(string URL, Method method, string POSTdata, string Referer = "", string UserAgent = "")
|
||||
{
|
||||
WreqInfo responseData = new WreqInfo();
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
HttpWebRequest hwrequest = (HttpWebRequest)WebRequest.Create(URL);
|
||||
hwrequest.Accept = "*/*";
|
||||
hwrequest.AllowAutoRedirect = true;
|
||||
hwrequest.Timeout = 5000;
|
||||
hwrequest.Method = method.ToString();
|
||||
|
||||
if (!isStrNull(UserAgent))
|
||||
hwrequest.UserAgent = UserAgent;
|
||||
else
|
||||
hwrequest.UserAgent = "http_requester/0.1";
|
||||
|
||||
if (!isStrNull(Referer))
|
||||
hwrequest.Referer = Referer;
|
||||
|
||||
if (URL.ToLower().IndexOf("https://") > -1)
|
||||
{
|
||||
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
|
||||
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
|
||||
hwrequest.ClientCertificates.Add(new System.Security.Cryptography.X509Certificates.X509Certificate());
|
||||
}
|
||||
|
||||
|
||||
if (hwrequest.Method == "POST")
|
||||
{
|
||||
hwrequest.ContentType = "application/x-www-form-urlencoded";
|
||||
//ASCIIEncoding encoding = new ASCIIEncoding();
|
||||
UTF8Encoding encoding = new UTF8Encoding();
|
||||
byte[] postByteArray = encoding.GetBytes(POSTdata);
|
||||
hwrequest.ContentLength = postByteArray.Length;
|
||||
Stream postStream = hwrequest.GetRequestStream();
|
||||
postStream.Write(postByteArray, 0, postByteArray.Length);
|
||||
postStream.Close();
|
||||
}
|
||||
HttpWebResponse hwresponse = (HttpWebResponse)hwrequest.GetResponse();
|
||||
if (hwresponse.StatusCode == HttpStatusCode.OK)
|
||||
{
|
||||
StreamReader responseStream = new StreamReader(hwresponse.GetResponseStream());
|
||||
responseData.data = responseStream.ReadToEnd();
|
||||
}
|
||||
hwresponse.Close();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
responseData.log = ex.Message;
|
||||
}
|
||||
|
||||
return responseData;
|
||||
}
|
||||
|
||||
#region 繁簡轉換
|
||||
|
||||
public static class ChineseConverter
|
||||
{
|
||||
internal const int LOCALE_SYSTEM_DEFAULT = 0x0800;
|
||||
internal const int LCMAP_SIMPLIFIED_CHINESE = 0x02000000;
|
||||
internal const int LCMAP_TRADITIONAL_CHINESE = 0x04000000;
|
||||
|
||||
[System.Runtime.InteropServices.DllImport("kernel32", CharSet = System.Runtime.InteropServices.CharSet.Auto, SetLastError = true)]
|
||||
internal static extern int LCMapString(int Locale, int dwMapFlags, string lpSrcStr, int cchSrc, [System.Runtime.InteropServices.Out] string lpDestStr, int cchDest);
|
||||
|
||||
public static string ToSimplified(string pSource)
|
||||
{
|
||||
String tTarget = new String(' ', pSource.Length);
|
||||
int tReturn = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_SIMPLIFIED_CHINESE, pSource, pSource.Length, tTarget, pSource.Length);
|
||||
return tTarget;
|
||||
}
|
||||
|
||||
public static string ToTraditional(string pSource)
|
||||
{
|
||||
String tTarget = new String(' ', pSource.Length);
|
||||
int tReturn = LCMapString(LOCALE_SYSTEM_DEFAULT, LCMAP_TRADITIONAL_CHINESE, pSource, pSource.Length, tTarget, pSource.Length);
|
||||
return tTarget;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 讀檔
|
||||
|
||||
public string ReadFileContent(string path)
|
||||
{
|
||||
string text = "";
|
||||
try
|
||||
{
|
||||
if (path.IndexOf("~/") > -1) { path = Server.MapPath(path); }
|
||||
|
||||
Stream stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
|
||||
StreamReader objReader = new StreamReader(stream);
|
||||
text = objReader.ReadToEnd();
|
||||
objReader.Close();
|
||||
objReader.Dispose();
|
||||
stream.Close();
|
||||
stream.Dispose();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
//throw;
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user