1 /// <summary> 2 /// B接口-微信小程序带参数二维码的生成 3 /// </summary> 4 /// <param name="access_token"></param> 5 /// <returns></returns> 6 public static string CreateWxCode(string access_token string roomId) 7 { 8 string ret = string.Empty; 9 try 10 { 11 string page = ConfigurationManager.AppSettings["page"].ToString(); 12 13 string DataJson = string.Empty; 14 //适用于需要的码数量极多,或仅临时使用的业务场景 15 //通过该接口生成的小程序码,永久有效,数量暂无限制。 16 string url = "wxa/getwxacodeunlimit?access_token=" + access_token; 17 DataJson = "{"; 18 DataJson += string.Format("‘scene‘:‘{0}‘" "1234567");//所要传的参数用分看 19 DataJson += string.Format("‘width‘:‘{0}‘" 200); 20 DataJson += string.Format("‘page‘:‘{0}‘" page);//扫码所要跳转的地址,根路径前不要填加‘/‘不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面 21 DataJson += "‘line_color‘:{"; 22 DataJson += string.Format("‘r‘:‘{0}‘" "0"); 23 DataJson += string.Format("‘g‘:‘{0}‘" "0"); 24 DataJson += string.Format("‘b‘:‘{0}‘" "0"); 25 DataJson += "}"; 26 DataJson += "}"; 27 28 29 string jsondata = JsonConvert.Serializeobxxxxject(DataJson); 30 //DataJson的配置见小程序开发文档,B接口:https://mp.weixin.qq.com/debug/wxadoc/dev/api/qrcode.html 31 ret = CreateWeChatQrCode(url jsondata); 32 } 33 catch (Exception e) 34 { 35 ret = e.Message; 36 } 37 return ret;//返回图片地址 38 } 39 40 41 /// <summary> 42 /// 返回二维码图片 43 /// </summary> 44 /// <param name="url"></param> 45 /// <param name="param"></param> 46 /// <returns></returns> 47 public static string CreateWeChatQrCode(string url string param) 48 { 49 string wechatapi = ConfigurationManager.AppSettings["wechatapi"].ToString(); 50 string qrcodepath = ConfigurationManager.AppSettings["qrcodepath"].ToString(); 51 string strURL = wechatapi+ url; 52 HttpWebRequest request; 53 string imgName = string.Empty; 54 string path = string.Empty; 55 try 56 { 57 request = (System.Net.HttpWebRequest)WebRequest.Create(strURL); 58 59 60 request.Method = "POST"; 61 request.ContentType = "application/json;charset=UTF-8"; 62 string paraUrlCoded = param; 63 //byte[] payload; 64 byte[] payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); 65 request.ContentLength = payload.Length; 66 Stream writer = request.GetRequestStream(); 67 writer.Write(payload 0 payload.Length); 68 writer.Close(); 69 System.Net.HttpWebResponse response; 70 response = (System.Net.HttpWebResponse)request.GetResponse(); 71 System.IO.Stream s; 72 s = response.GetResponseStream();//返回图片数据流 73 74 byte[] tt = StreamToBytes(s);//将数据流转为byte[] 75 76 77 //在文件名前面加上时间,以防重名 78 imgName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg"; 79 //文件存储相对于当前应用目录的虚拟目录 80 path = qrcodepath + "EWM/"; 81 82 83 //获取相对于应用的基目录创建目录 84 string imgPath = System.Web.Hosting.HostingEnvironment.MapPath(path);//System.AppDomain.CurrentDomain.baxxxxseDirectory + path; //通过此对象获取文件名 85 if (!Directory.Exists(imgPath)) 86 { 87 Directory.CreateDirectory(imgPath); 88 } 89 System.IO.File.WriteAllBytes(HttpContext.Current.Server.MapPath(path + imgName) tt);//讲byte[]存储为图片 90 } 91 catch (Exception ex) 92 { 93 ExceptionPolicy.HandleException(ex LZExceptionType.System 94 "生成小程序二维码失败!源:Lz.Product.LJXQJGF.BLL.Files.CreateWeChatQrCode方法出错 "); 95 } 96 return path + imgName; 97 } 98 99 100 101 102 /// <summary> 103 /// 将数据流转为byte[] 104 /// </summary> 105 /// <param name="stream"></param> 106 /// <returns></returns> 107 public static byte[] StreamToBytes(Stream stream) 108 { 109 List<byte> bytes = new List<byte>(); 110 int temp = stream.ReadByte(); 111 while (temp != -1) 112 { 113 bytes.Add((byte)temp); 114 temp = stream.ReadByte(); 115 } 116 return bytes.ToArray(); 117 }
获取access_token请参考我的另外一篇博客:https://www.cnblogs.com/qinaqina/p/11610346.html
原文地址:https://www.cnblogs.com/qinaqina/p/11610356.html
时间: 2024-10-07 21:46:44