B接口-微信小程序带参数二维码的生成 - 绅士狼

B接口-微信小程序带参数二维码的生成

接口B:适用于需要的码数量极多,或仅临时使用的业务场景

接口地址:https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN

注意:通过该接口生成的小程序码,永久有效,数量暂无限制。用户扫描该码进入小程序后,开发者需在对应页面获取的码中 scene 字段的值,再做处理逻辑。使用如下代码可以获取到二维码中的 scene 字段的值。调试阶段可以使用开发工具的条件编译自定义参数 scene=xxxx 进行模拟,开发工具模拟时的 scene 的参数值需要进行 urlencode

 1 // 这是首页的 js
 2 
 3 Page({
 4 
 5   onLoad: function(options) {
 6 
 7     // options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
 8 
 9     var scene = decodeURIComponent(options.scene)
10 
11   }
12 
13 })

详情可见小程序开发文档:https://mp.weixin.qq.com/debug/wxadoc/dev/api/qrcode.html

后台 C#

 1  /// <summary>
 2 
 3         /// B接口-微信小程序带参数二维码的生成
 4 
 5         /// </summary>
 6 
 7         /// <param name="MicroId"></param>
 8 
 9         /// <returns></returns>
10 
11         public static string CreateWxCode(int MicroId,int UserId,int CardLevel)
12 
13         {
14 
15             string ret = string.Empty;
16 
17             try
18 
19             {
20 
21                 string DataJson = string.Empty;
22 
23                 string url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=*****************";
24 
25                 DataJson = "{";
26 
27                 DataJson += string.Format("\"scene\":\"{0}\",", "所传参数内容1,所传参数内容2");//所要传的参数用,分看
28 
29                 DataJson += string.Format("\"width\":\"{0}\",", 124);
30 
31                 DataJson += string.Format("\"page\":\"{0}\",", "pages/index/index");//扫码所要跳转的地址,根路径前不要填加\'/\',不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面
32 
33                 DataJson += "\"line_color\":{";
34 
35                 DataJson += string.Format("\"r\":\"{0}\",", "0");
36 
37                 DataJson += string.Format("\"g\":\"{0}\",", "0");
38 
39                 DataJson += string.Format("\"b\":\"{0}\"", "0");
40 
41                 DataJson += "}";
42 
43                 DataJson += "}";
44 
45 //DataJson的配置见小程序开发文档,B接口:https://mp.weixin.qq.com/debug/wxadoc/dev/api/qrcode.html
46 
47                 ret= PostMoths(url, DataJson);
48 
49                 if(ret.Length>0)
50 
51                 {
52 
53                     //对图片进行存储操作,下次可直接调用你存储的图片,不用再调用接口
54 
55                 }
56 
57             }
58 
59             catch (Exception e)
60 
61             { ret = e.Message; }
62 
63             return ret;//返回图片地址
64 
65         }
 1  //请求处理,返回二维码图片
 2 
 3 public static string  PostMoths(string url, string param)
 4 
 5         {
 6 
 7             string strURL = url;
 8 
 9             System.Net.HttpWebRequest request;
10 
11             request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
12 
13             request.Method = "POST";
14 
15             request.ContentType = "application/json;charset=UTF-8";
16 
17             string paraUrlCoded = param;
18 
19             byte[] payload;
20 
21             payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
22 
23             request.ContentLength = payload.Length;
24 
25             Stream writer = request.GetRequestStream();
26 
27             writer.Write(payload, 0, payload.Length);
28 
29             writer.Close();
30 
31             System.Net.HttpWebResponse response;
32 
33             response = (System.Net.HttpWebResponse)request.GetResponse();
34 
35             System.IO.Stream s;
36 
37             s = response.GetResponseStream();//返回图片数据流
38 
39             byte[] tt = StreamToBytes(s);//将数据流转为byte[]
40 
41  
42 
43             //在文件名前面加上时间,以防重名
44 
45             string imgName = DateTime.Now.ToString("yyyyMMddhhmmss")+".jpg";
46 
47             //文件存储相对于当前应用目录的虚拟目录
48 
49             string path = "/image/";
50 
51             //获取相对于应用的基目录,创建目录
52 
53             string imgPath = System.AppDomain.CurrentDomain.BaseDirectory + path;     //通过此对象获取文件名
54 
55             StringHelper.CreateDirectory(imgPath);
56 
57             System.IO.File.WriteAllBytes(HttpContext.Current.Server.MapPath(path+imgName), tt);//讲byte[]存储为图片
58 
59             return "/image/" + imgName;
60 
61         }
62 
63  ///将数据流转为byte[]
64 
65         public static byte[] StreamToBytes(Stream stream)
66 
67         {
68 
69             List<byte> bytes = new List<byte>();
70 
71             int temp = stream.ReadByte();
72 
73             while (temp != -1)
74 
75             {
76 
77                 bytes.Add((byte)temp);
78 
79                 temp = stream.ReadByte();
80 
81             }
82 
83             return bytes.ToArray();
84 
85         }