| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- 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<string, object> jObj = jss.Deserialize<Dictionary<string, object>>(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("<script type=\"text/javascript\">window.location.href='http://qr.topscan.com/api.php?w=400&text=" + txt + "';</script>");
- // }
- //}
- /// <summary>
- /// 生成二维码图片
- /// </summary>
- /// <param name="strMessage">要生成二维码的字符串</param>
- /// <param name="width">二维码图片宽度</param>
- /// <param name="height">二维码图片高度</param>
- /// <returns></returns>
- //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;
- //}
- }
- }
|