GetBarcode.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Web.Script.Serialization;
  5. using Utils;
  6. namespace SiteCore.Handler
  7. {
  8. public class GetBarcode : BaseHandler, IHttpHandler
  9. {
  10. string appid = "wx0819ba57901308c0";
  11. string appsecret = "d9b31c6be01fc55f082a770a4462ef5c";
  12. public void ProcessRequest(HttpContext context)
  13. {
  14. con = context;
  15. CommonHelper.PageExpires();
  16. HttpHelper http = new HttpHelper();
  17. HttpResult hResult = null;
  18. int bid = GetInt("bid");
  19. int ismet = GetInt("ismet");
  20. string name = GetString("name");
  21. //string file = WeiXinPicPath + "\\" + bid + ".bmp";
  22. //object bf = WebCache.GetCache("wx_" + bid);
  23. //if (bf != null && File.Exists(bf.ToString()))//如果己存在5分钟内的二维码
  24. //{
  25. // Bitmap bmp = new Bitmap(file);
  26. // MemoryStream ms = new MemoryStream();
  27. // bmp.Save(ms, ImageFormat.Bmp);
  28. // con.Response.ClearContent(); //需要输出图象信息 要修改HTTP头
  29. // con.Response.ContentType = "image/bmp";
  30. // con.Response.BinaryWrite(ms.ToArray());
  31. // if (ms != null) ms.Dispose();
  32. // con.Response.End();
  33. // return;
  34. //}
  35. object cat = WebCache.GetCache("at");
  36. if (cat == null)
  37. {
  38. HttpItem item = new HttpItem()
  39. {
  40. URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + appsecret
  41. };
  42. hResult = http.GetHtml(item);
  43. string html = hResult.Html;
  44. JavaScriptSerializer jss = new JavaScriptSerializer();
  45. Dictionary<string, object> jObj = jss.Deserialize<Dictionary<string, object>>(html);
  46. if (!jObj.ContainsKey("errcode"))
  47. {
  48. string at = jObj["access_token"].ToString();
  49. WebCache.AddCacheWithTime("at", at, 1.8);
  50. cat = at;
  51. }
  52. }
  53. if (cat != null)
  54. {
  55. string dtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  56. string parms = SecurityHelper.UrlEncoding(bid + "_" + dtime + "_" + ismet);
  57. parms = parms.Replace("=", "@");
  58. HttpItem item = new HttpItem()
  59. {
  60. URL = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + cat,
  61. Method = "POST",
  62. Postdata = "{\"width\":500,\"line_color\":{\"r\":\"17\",\"g\":\"123\",\"b\":\"185\"},\"path\":\"pages/sign/sign?p=" + parms + "|" + dtime + "\"}",
  63. //Postdata = "scene=bbbb&page=pages/home/home",
  64. ResultType = ResultType.Byte,
  65. Accept = "image/webp,image/*,*/*;q=0.8",
  66. ContentType = "application/json"
  67. };
  68. hResult = http.GetHtml(item);
  69. if (hResult.Html.IndexOf("errcode") != -1)
  70. {
  71. return;
  72. }
  73. //using (MemoryStream ms = new MemoryStream(hResult.ResultByte))
  74. //{
  75. // Bitmap bm = new Bitmap(ms);
  76. // bm.Save(WeiXinPicPath + "\\" + bid + ".bmp", ImageFormat.Bmp);
  77. // WebCache.AddCacheWithTime("wx_" + bid, file, 3);//3个小时变一次
  78. //}
  79. con.Response.ClearContent(); //需要输出图象信息 要修改HTTP头
  80. con.Response.ContentType = "image/bmp";
  81. con.Response.BinaryWrite(hResult.ResultByte);
  82. con.Response.End();
  83. }
  84. }
  85. }
  86. }