GetMiniCode.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System.Collections.Generic;
  2. using System.Web;
  3. using System.Web.Script.Serialization;
  4. using Utils;
  5. namespace SiteCore.Handler
  6. {
  7. public class GetMiniCode : BaseHandler, IHttpHandler
  8. {
  9. string appid = "wx20432512482565e9";
  10. string appsecret = "07492bc9cbbc5b303e5e135b4dccd6f0";
  11. public void ProcessRequest(HttpContext context)
  12. {
  13. con = context;
  14. CommonHelper.PageExpires();
  15. HttpHelper http = new HttpHelper();
  16. HttpResult hResult = null;
  17. object cat = WebCache.GetCache("at");
  18. if (cat == null)
  19. {
  20. HttpItem item = new HttpItem()
  21. {
  22. URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + appsecret
  23. };
  24. hResult = http.GetHtml(item);
  25. string html = hResult.Html;
  26. JavaScriptSerializer jss = new JavaScriptSerializer();
  27. Dictionary<string, object> jObj = jss.Deserialize<Dictionary<string, object>>(html);
  28. if (!jObj.ContainsKey("errcode"))
  29. {
  30. string at = jObj["access_token"].ToString();
  31. WebCache.AddCacheWithTime("at", at, 1.8);
  32. cat = at;
  33. }
  34. }
  35. if (cat != null)
  36. {
  37. string st = GetString("st");
  38. string pdata = "";
  39. string url = "";
  40. if (st == "a")
  41. {
  42. pdata = "{\"width\":520,\"line_color\":{\"r\":\"17\",\"g\":\"123\",\"b\":\"185\"},\"path\":\"pages/index\"}";
  43. url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=";
  44. }
  45. else
  46. {
  47. pdata = "{\"width\":520,\"path\":\"pages/index\"}";
  48. url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=";
  49. }
  50. HttpItem item = new HttpItem()
  51. {
  52. URL = url + cat,
  53. Method = "POST",
  54. Postdata = pdata,
  55. //Postdata = "scene=bbbb&page=pages/home/home",
  56. ResultType = ResultType.Byte,
  57. Accept = "image/webp,image/*,*/*;q=0.8",
  58. ContentType = "application/json"
  59. };
  60. //HttpItem item = new HttpItem()
  61. //{
  62. // URL = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + cat,
  63. // Method = "POST",
  64. // Postdata = "{\"scene\":\"s" + bid + "\",\"width\":450,\"line_color\":{\"r\":\"17\",\"g\":\"123\",\"b\":\"185\"},\"path\":\"pages/home/home?bid=" + bid + "&btime=" + DateTime.Now.ToString("HHmm") + "\"}",
  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. hResult = http.GetHtml(item);
  71. if (hResult.Html.IndexOf("errcode") != -1)
  72. {
  73. return;
  74. }
  75. con.Response.ClearContent(); //需要输出图象信息 要修改HTTP头
  76. con.Response.ContentType = "image/bmp";
  77. con.Response.BinaryWrite(hResult.ResultByte);
  78. con.Response.End();
  79. }
  80. }
  81. }
  82. }