using System; using System.Collections.Generic; using System.Web; using System.Web.Script.Serialization; using Utils; namespace SiteCore.Handler { public class GetBarcode : BaseHandler, IHttpHandler { string appid = "wx0819ba57901308c0"; string appsecret = "d9b31c6be01fc55f082a770a4462ef5c"; public void ProcessRequest(HttpContext context) { con = context; CommonHelper.PageExpires(); HttpHelper http = new HttpHelper(); HttpResult hResult = null; int bid = GetInt("bid"); int ismet = GetInt("ismet"); string name = GetString("name"); //string file = WeiXinPicPath + "\\" + bid + ".bmp"; //object bf = WebCache.GetCache("wx_" + bid); //if (bf != null && File.Exists(bf.ToString()))//如果己存在5分钟内的二维码 //{ // Bitmap bmp = new Bitmap(file); // MemoryStream ms = new MemoryStream(); // bmp.Save(ms, ImageFormat.Bmp); // con.Response.ClearContent(); //需要输出图象信息 要修改HTTP头 // con.Response.ContentType = "image/bmp"; // con.Response.BinaryWrite(ms.ToArray()); // if (ms != null) ms.Dispose(); // con.Response.End(); // return; //} object cat = WebCache.GetCache("at"); if (cat == null) { HttpItem item = new HttpItem() { URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + appsecret }; hResult = http.GetHtml(item); string html = hResult.Html; JavaScriptSerializer jss = new JavaScriptSerializer(); Dictionary jObj = jss.Deserialize>(html); if (!jObj.ContainsKey("errcode")) { string at = jObj["access_token"].ToString(); WebCache.AddCacheWithTime("at", at, 1.8); cat = at; } } if (cat != null) { string dtime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); string parms = SecurityHelper.UrlEncoding(bid + "_" + dtime + "_" + ismet); parms = parms.Replace("=", "@"); HttpItem item = new HttpItem() { URL = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + cat, Method = "POST", Postdata = "{\"width\":500,\"line_color\":{\"r\":\"17\",\"g\":\"123\",\"b\":\"185\"},\"path\":\"pages/sign/sign?p=" + parms + "|" + dtime + "\"}", //Postdata = "scene=bbbb&page=pages/home/home", ResultType = ResultType.Byte, Accept = "image/webp,image/*,*/*;q=0.8", ContentType = "application/json" }; hResult = http.GetHtml(item); if (hResult.Html.IndexOf("errcode") != -1) { return; } //using (MemoryStream ms = new MemoryStream(hResult.ResultByte)) //{ // Bitmap bm = new Bitmap(ms); // bm.Save(WeiXinPicPath + "\\" + bid + ".bmp", ImageFormat.Bmp); // WebCache.AddCacheWithTime("wx_" + bid, file, 3);//3个小时变一次 //} con.Response.ClearContent(); //需要输出图象信息 要修改HTTP头 con.Response.ContentType = "image/bmp"; con.Response.BinaryWrite(hResult.ResultByte); con.Response.End(); } } } }