using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Web; using System.Web.Script.Serialization; using Utils; namespace SiteCore.Handler { class GetBar : BaseHandler, IHttpHandler { string appid = "wx968c99ff1498e09f"; string appsecret = "153afb0877ee60fecd7f5580c9182ee6"; public void ProcessRequest(HttpContext context) { con = context; CommonHelper.PageExpires(); HttpHelper http = new HttpHelper(); HttpResult hResult = null; string sid = GetString("sid"); string tid = GetString("tid"); string file = webConfig.WeiXinPicPath + "\\" + sid + "_" + tid + ".bmp"; object bf = WebCache.GetCache("wx_" + sid + "_" + tid); 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(sid + "_" + dtime); 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/index?sid=" + sid + (tid == "" ? "" : "&tid=" + tid) + "\"}", //Postdata = "scene=bbbb&page=pages/home/home", ResultType = ResultType.Byte, Accept = "image/webp,image/*,*/*;q=0.8", ContentType = "application/json" }; if (tid != "") item.Postdata = "{\"width\":500,\"line_color\":{\"r\":\"17\",\"g\":\"123\",\"b\":\"185\"},\"path\":\"pages/login/login?sid=" + sid + "&tid=" + tid + "\"}"; else item.Postdata = "{\"width\":500,\"line_color\":{\"r\":\"17\",\"g\":\"123\",\"b\":\"185\"},\"path\":\"pages/index?sid=" + sid + "\"}"; 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(webConfig.WeiXinPicPath + "\\" + sid + ".bmp", ImageFormat.Bmp); WebCache.AddCacheWithTime("wx_" + sid, file, 3);//3个小时变一次 } con.Response.ClearContent(); //需要输出图象信息 要修改HTTP头 con.Response.ContentType = "image/bmp"; con.Response.BinaryWrite(hResult.ResultByte); con.Response.End(); } } //private void writeCode(string txt) //{ // if (txt != "") // { // string[] tArr = txt.Split('|'); // string rnd = CommonHelper.GetRnd(10, true, true, true, false, ""); // string str = SecurityHelper.UrlEncoding(tArr[1] + "_" + rnd); // txt = tArr[0] + "|" + str; // Bitmap bmp = GetQRCodeByZXingNet(txt, 500, 400); // // 将生成的图片发回客户端 // 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(); // //Response.Write(txt); // //Response.Write(""); // } //} /// /// 生成二维码图片 /// /// 要生成二维码的字符串 /// 二维码图片宽度 /// 二维码图片高度 /// //private Bitmap GetQRCodeByZXingNet(String strMessage, Int32 width, Int32 height) //{ //Bitmap result = null; //try //{ // BarcodeWriter barCodeWriter = new BarcodeWriter(); // barCodeWriter.Format = BarcodeFormat.QR_CODE; // barCodeWriter.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8"); // barCodeWriter.Options.Hints.Add(EncodeHintType.ERROR_CORRECTION, ZXing.QrCode.Internal.ErrorCorrectionLevel.H); // barCodeWriter.Options.Height = height; // barCodeWriter.Options.Width = width; // barCodeWriter.Options.Margin = 0; // ZXing.Common.BitMatrix bm = barCodeWriter.Encode(strMessage); // result = barCodeWriter.Write(bm); //} //catch (Exception ex) //{ // //异常输出 //} //return result; //} } }