using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; namespace EShang.Common { /// /// 通用帮助类 /// public class Utils { #region 方法 -> 获取支付方式释义 /// /// GetPayMethod /// /// /// public static string GetPayMethod(int payMent) { string payMethod = ""; switch (payMent) { case 1: payMethod = "支付宝"; break; case 2: payMethod = "微信"; break; case 3: payMethod = "花呗分期"; break; default: break; } return payMethod; } #endregion #region 方法 -> 获取交易状态释义 /// /// GetStateName /// /// /// public static string GetStateName(int state) { string stateName = ""; switch (state) { case 0: stateName = "待付款"; break; case 1: stateName = "已付款"; break; case 5: stateName = "已退款"; break; case 11: stateName = "退款成功"; break; case 6: stateName = "交易关闭"; break; case 8: stateName = "退款中"; break; default: break; } return stateName; } #endregion /// /// 对字符串进行URL编码 /// /// URL字符串 /// 编码 /// public static string UrlEncode(string instr, string charset) { //return instr; if (instr == null || instr.Trim() == "") return ""; else { string res; try { res = HttpUtility.UrlEncode(instr, Encoding.GetEncoding(charset)); } catch (Exception ex) { res = HttpUtility.UrlEncode(instr, Encoding.GetEncoding("GB2312")); Console.WriteLine(ex); } return res; } } /// /// 对字符串进行URL解码 /// /// 编码的URL字符串 /// 编码 /// public static string UrlDecode(string instr, string charset) { if (instr == null || instr.Trim() == "") return ""; else { string res; try { res = HttpUtility.UrlDecode(instr, Encoding.GetEncoding(charset)); } catch (Exception ex) { res = HttpUtility.UrlDecode(instr, Encoding.GetEncoding("GB2312")); Console.WriteLine(ex); } return res; } } /// /// 取时间戳生成随即数,替换交易单号中的后10位流水号 /// /// public static UInt32 UnixStamp() { TimeSpan ts = DateTime.Now - TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); return Convert.ToUInt32(ts.TotalSeconds); } /// /// 取随机数 /// /// 随机数的长度 /// public static string BuildRandomStr(int length) { Random rand = new Random(); int num = rand.Next(); string str = num.ToString(); if (str.Length > length) { str = str.Substring(0, length); } else if (str.Length < length) { int n = length - str.Length; while (n > 0) { str.Insert(0, "0"); n--; } } return str; } /// /// 加载配置文件 /// /// public static Dictionary LoadCfg() { string cfgPath = Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.ApplicationBase) + Path.DirectorySeparatorChar + "config" + Path.DirectorySeparatorChar + "config.properties"; Dictionary cfg = new Dictionary(); using (StreamReader sr = new StreamReader(cfgPath)) { while (sr.Peek() >= 0) { string line = sr.ReadLine(); if (line.StartsWith("#")) { continue; } int startInd = line.IndexOf("="); string key = line.Substring(0, startInd); string val = line.Substring(startInd + 1, line.Length - (startInd + 1)); if (!cfg.ContainsKey(key) && !string.IsNullOrEmpty(val)) { cfg.Add(key, val); } } } return cfg; } /// /// 保存接口返回结果到文件中 /// /// 标题内容 /// 接口结果 public static void WriteFile(string title, Hashtable _param) { string resFilePath = Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.ApplicationBase) + Path.DirectorySeparatorChar + "result.txt"; if (!File.Exists(resFilePath)) { using (StreamWriter sw = new StreamWriter(resFilePath)) { sw.WriteLine("=====================" + title + "====================="); foreach (DictionaryEntry de in _param) { sw.WriteLine("key:" + de.Key.ToString() + " value:" + de.Value.ToString()); } } } else { using (StreamWriter sw = File.AppendText(resFilePath)) { sw.WriteLine("=====================" + title + "====================="); foreach (DictionaryEntry de in _param) { sw.WriteLine("key:" + de.Key.ToString() + " value:" + de.Value.ToString()); } } } } /// /// 生成32位随机数 /// /// public static string Random() { char[] constant = {'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','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; StringBuilder sb = new StringBuilder(32); Random rd = new Random(); for (int i = 0; i < 32; i++) { sb.Append(constant[rd.Next(62)]); } return sb.ToString(); } /// /// 生成16位订单号 by hyf 2016年2月16日17:48:43 /// /// public static string Nmrandom() { string rm = ""; Random ra = new Random(); for (int i = 0; i < 16; i++) { rm += ra.Next(0, 9).ToString(); } return rm; } /// /// 将Hashtable参数传为XML /// /// /// public static string ToXml(Hashtable _params) { StringBuilder sb = new StringBuilder(""); foreach (DictionaryEntry de in _params) { string key = de.Key.ToString(); sb.Append("<").Append(key).Append(">"); } return sb.Append("").ToString(); } } }