小程序唯一凭证/// 小程序唯一凭证密钥/// <_小程序码图片">
当前位置:   article > 正文

生成微信小程序二维码图片_小程序码图片

小程序码图片

ASP.NET 语言实现

1、生成微信小程序二维码,需要获取微信小程序Access_Token。代码如下,先获取Access_Token

  1. /// <summary>
  2. /// 获取微信小程序Access_Token
  3. /// </summary>
  4. /// <param name="appId">小程序唯一凭证</param>
  5. /// <param name="appSecret">小程序唯一凭证密钥</param>
  6. /// <returns></returns>
  7. public static string GetAccessToken(string appId, string appSecret)
  8. {
  9. string access_token = string.Empty;
  10. try
  11. {
  12. string token_url = $"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appId}&secret={appSecret}";
  13. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(token_url);
  14. //请求方式
  15. myRequest.Method = "GET";
  16. HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  17. Stream stream = myResponse.GetResponseStream();
  18. XmlDictionaryReader xmlReader = JsonReaderWriterFactory.CreateJsonReader(stream, XmlDictionaryReaderQuotas.Max);
  19. xmlReader.Read();
  20. string xml = xmlReader.ReadOuterXml();
  21. myResponse.Close();
  22. stream.Close();
  23. stream.Dispose();
  24. XmlDocument doc = new XmlDocument();
  25. doc.LoadXml(xml);
  26. XmlElement rootElement = doc.DocumentElement;
  27. access_token = rootElement.SelectSingleNode("access_token").InnerText.Trim();
  28. return access_token;
  29. }
  30. catch (Exception ex)
  31. {
  32. throw ex;
  33. }
  34. }

2、生成微信小程序二维码,无限次数 

  1. /// <summary>
  2. /// 生成微信小程序二维码
  3. /// </summary>
  4. /// <param name="picPath"></param>
  5. /// <param name="access_token"></param>
  6. /// <param name="paramData"></param>
  7. /// <param name="page"></param>
  8. /// <param name="width"></param>
  9. public static void GetWeChatCodeLimit(string picPath, string access_token, string paramData = null, string page = null, int width = 430)
  10. {
  11. try
  12. {
  13. //这个路径生成的二维码是无限次数,scene参数最大32
  14. string url = $"https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={access_token}";
  15. Dictionary<string, object> dic = new Dictionary<string, object>();
  16. if (string.IsNullOrWhiteSpace(paramData))
  17. {
  18. paramData = "id=123";
  19. }
  20. dic.Add("scene", paramData); //必填,有不能为空,随便填好了
  21. if (!string.IsNullOrWhiteSpace(page))
  22. {
  23. dic.Add("path", page); //注意这里要用 path,而不是用page,官网有问题
  24. }
  25. dic.Add("width", width);
  26. string json = JsonHelper.GetJSON<Dictionary<string, object>>(dic);
  27. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
  28. myRequest.Method = "POST";
  29. myRequest.ContentType = "application/json;charset=UTF-8";
  30. byte[] payload = Encoding.UTF8.GetBytes(json);
  31. myRequest.ContentLength = payload.Length;
  32. Stream writer = myRequest.GetRequestStream();
  33. writer.Write(payload, 0, payload.Length);
  34. writer.Close();
  35. HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  36. Stream stream = myResponse.GetResponseStream();//返回图片数据流
  37. byte[] byteArray = StreamToBytes(stream);//将数据流转为byte[]
  38. if (byteArray.Length > 1000)
  39. {
  40. File.WriteAllBytes(picPath, byteArray);
  41. }
  42. else
  43. {
  44. //转字符串--报错
  45. string str = Encoding.Default.GetString(byteArray);
  46. throw new Exception(str);
  47. }
  48. myResponse.Close();
  49. stream.Close();
  50. stream.Dispose();
  51. }
  52. catch (Exception ex)
  53. {
  54. throw ex;
  55. }
  56. }

 3、生成微信小程序二维码,有限次数

  1. /// <summary>
  2. /// 生成微信小程序二维码
  3. /// </summary>
  4. /// <param name="picPath"></param>
  5. /// <param name="access_token"></param>
  6. /// <param name="paramData"></param>
  7. /// <param name="page"></param>
  8. /// <param name="width"></param>
  9. public static void CreateWeChatQrCode(string picPath, string access_token, string paramData = null, string page = null, int width = 430)
  10. {
  11. try
  12. {
  13. //这个路径有限次数,1000000个,要注意使用
  14. //string url = $"https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token={access_token}";
  15. //这个生成是小程序码,上面是二维码
  16. string url = $"https://api.weixin.qq.com/wxa/getwxacode?access_token={access_token}";
  17. if (!string.IsNullOrWhiteSpace(paramData))
  18. {
  19. page += "?" + paramData;
  20. }
  21. Dictionary<string, object> dic = new Dictionary<string, object>();
  22. dic.Add("path", page); //必填
  23. dic.Add("width", width);
  24. string json = JsonHelper.GetJSON<Dictionary<string, object>>(dic);
  25. HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
  26. myRequest.Method = "POST";
  27. myRequest.ContentType = "application/json;charset=UTF-8";
  28. byte[] payload = Encoding.UTF8.GetBytes(json);
  29. myRequest.ContentLength = payload.Length;
  30. Stream writer = myRequest.GetRequestStream();
  31. writer.Write(payload, 0, payload.Length);
  32. writer.Close();
  33. HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
  34. Stream stream = myResponse.GetResponseStream();//返回图片数据流
  35. byte[] byteArray = StreamToBytes(stream);//将数据流转为byte[]
  36. if (byteArray.Length > 1000)
  37. {
  38. File.WriteAllBytes(picPath, byteArray);
  39. }
  40. else
  41. {
  42. //转字符串
  43. string str = Encoding.Default.GetString(byteArray);
  44. throw new Exception(str);
  45. }
  46. myResponse.Close();
  47. stream.Close();
  48. stream.Dispose();
  49. }
  50. catch (Exception ex)
  51. {
  52. throw ex;
  53. }
  54. }
  1. /// <summary>
  2. /// 将数据流转为byte[]
  3. /// </summary>
  4. /// <param name="stream"></param>
  5. /// <returns></returns>
  6. public static byte[] StreamToBytes(Stream stream)
  7. {
  8. List<byte> bytes = new List<byte>();
  9. int temp = stream.ReadByte();
  10. while (temp != -1)
  11. {
  12. bytes.Add((byte)temp);
  13. temp = stream.ReadByte();
  14. }
  15. return bytes.ToArray();
  16. }

小程序端实现

https://www.jianshu.com/p/0522d455d492

Java实现

PHP实现

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/312331
推荐阅读
相关标签
  

闽ICP备14008679号