123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- namespace OASystem.API.CMBPayBusiness
- {
- public static class Common
- {
- public static string GetSignContent(Dictionary<string, string> sortedParams)
- {
- //字典排序,并用&进行拼接,例如,b=a,a=b,拼接后变成a=b&b=a
- StringBuilder content = new StringBuilder();
- //利用linq进行字典升序的排序
- var dicSort = from objDic in sortedParams orderby objDic.Key select objDic;
- foreach (KeyValuePair<string, string> kvp in dicSort)
- {
- string key = kvp.Key;
- string value = kvp.Value;
- if (null != key && null != value)
- {
- content.Append(key + "=" + value + "&");
- }
- }
- if (content.Length != 0)
- {
- content.Remove(content.Length - 1, 1);
- }
- return content.ToString();
- }
- public static string GetSign(Dictionary<string, string> sortedParams)
- {
- //排序拼接参数
- string signContent = GetSignContent(sortedParams);
- //加签
- string sign = GmUtil.generateSmSign(signContent);
- return sign;
- }
- public static bool VerifySign(Dictionary<string, string> sortedParams)
- {
- //获取返回的sign
- string returnSign = sortedParams["sign"];
- //删除返回的sign
- sortedParams.Remove("sign");
- //排序拼接参数
- string signContent = GetSignContent(sortedParams);
- //验签
- bool flag = GmUtil.verifySmSign(signContent, returnSign);
- return flag;
- }
- public static Dictionary<string, string> PostForEntity(string url, string param, Dictionary<string, string> dic = null)
- {
- HttpWebRequest request;//仅作展示,这个方法用于发送网络请求可能有性能问题
- request = (HttpWebRequest)WebRequest.Create(url);
- // 以POST的方式提交
- request.Method = "POST";
- // 以json的方式提交
- request.ContentType = "application/json;charset=UTF-8";
- // 请求头部
- if (dic != null && dic.Count != 0)
- {
- foreach (var item in dic)
- {
- request.Headers.Add(item.Key, item.Value);
- }
- }
- byte[] payload;
- // 请求参数
- payload = Encoding.UTF8.GetBytes(param);
- request.ContentLength = payload.Length;
- string strValue = "";
- try
- {
- Stream writer = request.GetRequestStream();
- writer.Write(payload, 0, payload.Length);
- writer.Close();
- HttpWebResponse response;
- response = (HttpWebResponse)request.GetResponse();
- Stream s;
- s = response.GetResponseStream();
- string StrDate = "";
- StreamReader Reader = new StreamReader(s, Encoding.UTF8);
- while ((StrDate = Reader.ReadLine()) != null)
- {
- strValue += StrDate;
- }
- }
- catch (Exception e)
- {
- Console.WriteLine("post请求报错:" + e.Message);
- return null;
- }
- //JavaScriptSerializer jss = new JavaScriptSerializer();
- return JsonConvert.DeserializeObject<Dictionary<string, string>>(strValue);
- }
- /// <summary>
- /// 返回网络时间 --北京时间
- /// </summary>
- /// <returns></returns>
- public static DateTime GetBeijingTime()
- {
- WebRequest request = null;
- WebResponse response = null;
- WebHeaderCollection headerCollection = null;
- string datetime = string.Empty;
- try
- {
- request = WebRequest.Create("https://www.baidu.com");
- request.Timeout = 3000;
- request.Credentials = CredentialCache.DefaultCredentials;
- response = request.GetResponse();
- headerCollection = response.Headers;
- foreach (var h in headerCollection.AllKeys)
- {
- if (h == "Date")
- {
- datetime = headerCollection[h];
- break;
- }
- }
- return Convert.ToDateTime(datetime);
- }
- catch (Exception)
- {
- return DateTime.Now;
- }
- finally
- {
- if (request != null)
- {
- request.Abort();
- }
- if (response != null)
- {
- response.Close();
- }
- if (headerCollection != null)
- {
- headerCollection.Clear();
- }
- }
- }
- #region Http请求封装
- public static Dictionary<string, string> GetHeader(Dictionary<string, string> sortedParams)
- {
- //long timestamp = ConvertDateTime(System.DateTime.Now) / 10000000;
- long timestamp = ConvertDateTime(GetBeijingTime()) / 10000000;
- // 组apiSign加密Map
- Dictionary<string, string> apiSign = new Dictionary<string, string>();
- apiSign.Add("appid", BaseConfig.APPID);
- apiSign.Add("secret", BaseConfig.APP_SECRET);
- apiSign.Add("sign", sortedParams["sign"]);
- apiSign.Add("timestamp", "" + timestamp);
- // MD5加密
- string MD5Content = GetSignContent(apiSign);
- string apiSignString = GetMD5(MD5Content, "UTF-8");
- // 组request头部Map
- Dictionary<string, string> apiHeader = new Dictionary<string, string>();
- apiHeader.Add("appid", BaseConfig.APPID);
- apiHeader.Add("timestamp", "" + timestamp);
- apiHeader.Add("apisign", apiSignString);
- return apiHeader;
- }
- private static long ConvertDateTime(DateTime time)
- {
- DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1, 0, 0, 0, 0));
- return time.Ticks - startTime.Ticks;
- }
- public static string GetMD5(string data, string charset)
- {
- //MD5加密,同样的字符串在每次加密后的字符串是一样的
- byte[] bData = Encoding.GetEncoding(charset).GetBytes(data);
- MD5CryptoServiceProvider myMD5 = new MD5CryptoServiceProvider();
- bData = myMD5.ComputeHash(bData);
- StringBuilder sBuilder = new StringBuilder();
- for (int i = 0; i < bData.Length; i++)
- {
- //十六进制转成小写的英文字符
- sBuilder.Append(bData[i].ToString("x2"));
- }
- return sBuilder.ToString();
- }
- #endregion
- #region 二维码图片处理
- /// <summary>
- /// 调用此函数后使此两种图片合并,类似相册,有个
- /// 背景图,中间贴自己的目标图片
- /// </summary>
- /// <param name="imgBack">粘贴的源图片</param>
- /// <param name="destImg">粘贴的目标图片</param>
- public static Image CombinImage(Image imgBack, string destImg)
- {
- Image img = Image.FromFile(destImg); //照片图片
- if (img.Height != 65 || img.Width != 65)
- {
- img = KiResizeImage(img, 65, 65, 0);
- }
- Graphics g = Graphics.FromImage(imgBack);
- g.DrawImage(imgBack, 0, 0, imgBack.Width, imgBack.Height); //g.DrawImage(imgBack, 0, 0, 相框宽, 相框高);
- //g.FillRectangle(System.Drawing.Brushes.White, imgBack.Width / 2 - img.Width / 2 - 1, imgBack.Width / 2 - img.Width / 2 - 1,1,1);//相片四周刷一层黑色边框
- //g.DrawImage(img, 照片与相框的左边距, 照片与相框的上边距, 照片宽, 照片高);
- g.DrawImage(img, imgBack.Width / 2 - img.Width / 2, imgBack.Width / 2 - img.Width / 2, img.Width, img.Height);
- GC.Collect();
- return imgBack;
- }
- /// <summary>
- /// Resize图片
- /// </summary>
- /// <param name="bmp">原始Bitmap</param>
- /// <param name="newW">新的宽度</param>
- /// <param name="newH">新的高度</param>
- /// <param name="Mode">保留着,暂时未用</param>
- /// <returns>处理以后的图片</returns>
- public static Image KiResizeImage(Image bmp, int newW, int newH, int Mode)
- {
- try
- {
- Image b = new Bitmap(newW, newH);
- Graphics g = Graphics.FromImage(b);
- // 插值算法的质量
- g.InterpolationMode = InterpolationMode.HighQualityBicubic;
- g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
- g.Dispose();
- return b;
- }
- catch
- {
- return null;
- }
- }
- //public bool IsReusable
- //{
- // get
- // {
- // return false;
- // }
- //}
- #endregion
- #region 通知参数处理
- public static Dictionary<string, string> str2Map(string str)
- {
- Dictionary<string, string> result = new Dictionary<string, string>();
- string[] results = str.Split('&');
- if (results != null && results.Length > 0)
- {
- for (int var = 0; var < results.Length; ++var)
- {
- string pair = results[var];
- string[] kv = pair.Split('=');
- if (kv != null && kv.Length == 2)
- {
- result.Add(kv[0], kv[1]);
- }
- }
- }
- return result;
- }
- public static string decode(string str)
- {
- string result = null;
- if (str != null)
- {
- result = Uri.UnescapeDataString(str);
- }
- return result;
- }
- public static string NotifySign(string requestJsonStr)
- {
- return "";
- }
- #endregion
- }
- }
|