GetBar.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.IO;
  6. using System.Web;
  7. using System.Web.Script.Serialization;
  8. using Utils;
  9. namespace SiteCore.Handler
  10. {
  11. class GetBar : BaseHandler, IHttpHandler
  12. {
  13. string appid = "wx968c99ff1498e09f";
  14. string appsecret = "153afb0877ee60fecd7f5580c9182ee6";
  15. public void ProcessRequest(HttpContext context)
  16. {
  17. con = context;
  18. CommonHelper.PageExpires();
  19. HttpHelper http = new HttpHelper();
  20. HttpResult hResult = null;
  21. string sid = GetString("sid");
  22. string tid = GetString("tid");
  23. string file = webConfig.WeiXinPicPath + "\\" + sid + "_" + tid + ".bmp";
  24. object bf = WebCache.GetCache("wx_" + sid + "_" + tid);
  25. if (bf != null && File.Exists(bf.ToString()))//如果己存在5分钟内的二维码
  26. {
  27. Bitmap bmp = new Bitmap(file);
  28. MemoryStream ms = new MemoryStream();
  29. bmp.Save(ms, ImageFormat.Bmp);
  30. con.Response.ClearContent(); //需要输出图象信息 要修改HTTP头
  31. con.Response.ContentType = "image/bmp";
  32. con.Response.BinaryWrite(ms.ToArray());
  33. if (ms != null) ms.Dispose();
  34. con.Response.End();
  35. return;
  36. }
  37. object cat = WebCache.GetCache("at");
  38. if (cat == null)
  39. {
  40. HttpItem item = new HttpItem()
  41. {
  42. URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + appsecret
  43. };
  44. hResult = http.GetHtml(item);
  45. string html = hResult.Html;
  46. JavaScriptSerializer jss = new JavaScriptSerializer();
  47. Dictionary<string, object> jObj = jss.Deserialize<Dictionary<string, object>>(html);
  48. if (!jObj.ContainsKey("errcode"))
  49. {
  50. string at = jObj["access_token"].ToString();
  51. WebCache.AddCacheWithTime("at", at, 1.8);
  52. cat = at;
  53. }
  54. }
  55. if (cat != null)
  56. {
  57. string dtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  58. string parms = SecurityHelper.UrlEncoding(sid + "_" + dtime);
  59. parms = parms.Replace("=", "@");
  60. HttpItem item = new HttpItem()
  61. {
  62. URL = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + cat,
  63. Method = "POST",
  64. Postdata = "{\"width\":500,\"line_color\":{\"r\":\"17\",\"g\":\"123\",\"b\":\"185\"},\"path\":\"pages/index?sid=" + sid + (tid == "" ? "" : "&tid=" + tid) + "\"}",
  65. //Postdata = "scene=bbbb&page=pages/home/home",
  66. ResultType = ResultType.Byte,
  67. Accept = "image/webp,image/*,*/*;q=0.8",
  68. ContentType = "application/json"
  69. };
  70. if (tid != "") item.Postdata = "{\"width\":500,\"line_color\":{\"r\":\"17\",\"g\":\"123\",\"b\":\"185\"},\"path\":\"pages/login/login?sid=" + sid + "&tid=" + tid + "\"}";
  71. else item.Postdata = "{\"width\":500,\"line_color\":{\"r\":\"17\",\"g\":\"123\",\"b\":\"185\"},\"path\":\"pages/index?sid=" + sid + "\"}";
  72. hResult = http.GetHtml(item);
  73. if (hResult.Html.IndexOf("errcode") != -1)
  74. {
  75. return;
  76. }
  77. using (MemoryStream ms = new MemoryStream(hResult.ResultByte))
  78. {
  79. Bitmap bm = new Bitmap(ms);
  80. bm.Save(webConfig.WeiXinPicPath + "\\" + sid + ".bmp", ImageFormat.Bmp);
  81. WebCache.AddCacheWithTime("wx_" + sid, file, 3);//3个小时变一次
  82. }
  83. con.Response.ClearContent(); //需要输出图象信息 要修改HTTP头
  84. con.Response.ContentType = "image/bmp";
  85. con.Response.BinaryWrite(hResult.ResultByte);
  86. con.Response.End();
  87. }
  88. }
  89. //private void writeCode(string txt)
  90. //{
  91. // if (txt != "")
  92. // {
  93. // string[] tArr = txt.Split('|');
  94. // string rnd = CommonHelper.GetRnd(10, true, true, true, false, "");
  95. // string str = SecurityHelper.UrlEncoding(tArr[1] + "_" + rnd);
  96. // txt = tArr[0] + "|" + str;
  97. // Bitmap bmp = GetQRCodeByZXingNet(txt, 500, 400);
  98. // // 将生成的图片发回客户端
  99. // MemoryStream ms = new MemoryStream();
  100. // bmp.Save(ms, ImageFormat.Bmp);
  101. // con.Response.ClearContent(); //需要输出图象信息 要修改HTTP头
  102. // con.Response.ContentType = "image/bmp";
  103. // con.Response.BinaryWrite(ms.ToArray());
  104. // if (ms != null) ms.Dispose();
  105. // con.Response.End();
  106. // //Response.Write(txt);
  107. // //Response.Write("<script type=\"text/javascript\">window.location.href='http://qr.topscan.com/api.php?w=400&text=" + txt + "';</script>");
  108. // }
  109. //}
  110. /// <summary>
  111. /// 生成二维码图片
  112. /// </summary>
  113. /// <param name="strMessage">要生成二维码的字符串</param>
  114. /// <param name="width">二维码图片宽度</param>
  115. /// <param name="height">二维码图片高度</param>
  116. /// <returns></returns>
  117. //private Bitmap GetQRCodeByZXingNet(String strMessage, Int32 width, Int32 height)
  118. //{
  119. //Bitmap result = null;
  120. //try
  121. //{
  122. // BarcodeWriter barCodeWriter = new BarcodeWriter();
  123. // barCodeWriter.Format = BarcodeFormat.QR_CODE;
  124. // barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");
  125. // barCodeWriter.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H);
  126. // barCodeWriter.Options.Height = height;
  127. // barCodeWriter.Options.Width = width;
  128. // barCodeWriter.Options.Margin = 0;
  129. // ZXing.Common.BitMatrix bm = barCodeWriter.Encode(strMessage);
  130. // result = barCodeWriter.Write(bm);
  131. //}
  132. //catch (Exception ex)
  133. //{
  134. // //异常输出
  135. //}
  136. //return result;
  137. //}
  138. }
  139. }