C#、.NET后端,B接口-微信小程序带参数二维码的生成

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

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

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

// 这是首页的 js
Page({
  onLoad: function(options) {
    // options 中的 scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
    var scene = decodeURIComponent(options.scene)
  }
})

详情可见小程序开发文档:https://mp.weixin.qq.com/debug/wxadoc/dev/api/qrcode.html
 /// <summary>
        /// B接口-微信小程序带参数二维码的生成
        /// </summary>
        /// <param name="MicroId"></param>
        /// <returns></returns>
        public static string CreateWxCode(int MicroId,int UserId,int CardLevel)
        {
            string ret = string.Empty;
            try
            {
                string DataJson = string.Empty;
                string url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=*****************";
                DataJson = "{";
                DataJson += string.Format("\"scene\":\"{0}\",", "所传参数内容1,所传参数内容2");//所要传的参数用,分看
                DataJson += string.Format("\"width\":\"{0}\",", 124);
                DataJson += string.Format("\"page\":\"{0}\",", "pages/index/index");//扫码所要跳转的地址,根路径前不要填加'/',不能携带参数(参数请放在scene字段里),如果不填写这个字段,默认跳主页面
                DataJson += "\"line_color\":{";
                DataJson += string.Format("\"r\":\"{0}\",", "0");
                DataJson += string.Format("\"g\":\"{0}\",", "0");
                DataJson += string.Format("\"b\":\"{0}\"", "0");
                DataJson += "}";
                DataJson += "}";
//DataJson的配置见小程序开发文档,B接口:https://mp.weixin.qq.com/debug/wxadoc/dev/api/qrcode.html
                ret= PostMoths(url, DataJson);
                if(ret.Length>0)
                {
                    //对图片进行存储操作,下次可直接调用你存储的图片,不用再调用接口
                }
            }
            catch (Exception e)
            { ret = e.Message; }
            return ret;//返回图片地址
        }
 //请求处理,返回二维码图片
public static string  PostMoths(string url, string param)
        {
            string strURL = url;
            System.Net.HttpWebRequest request;
            request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
            request.Method = "POST";
            request.ContentType = "application/json;charset=UTF-8";
            string paraUrlCoded = param;
            byte[] payload;
            payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
            request.ContentLength = payload.Length;
            Stream writer = request.GetRequestStream();
            writer.Write(payload, 0, payload.Length);
            writer.Close();
            System.Net.HttpWebResponse response;
            response = (System.Net.HttpWebResponse)request.GetResponse();
            System.IO.Stream s;
            s = response.GetResponseStream();//返回图片数据流
            byte[] tt = StreamToBytes(s);//将数据流转为byte[]

            //在文件名前面加上时间,以防重名
            string imgName = DateTime.Now.ToString("yyyyMMddhhmmss")+".jpg";
            //文件存储相对于当前应用目录的虚拟目录
            string path = "/image/";
            //获取相对于应用的基目录,创建目录
            string imgPath = System.AppDomain.CurrentDomain.BaseDirectory + path;     //通过此对象获取文件名
            StringHelper.CreateDirectory(imgPath);
            System.IO.File.WriteAllBytes(HttpContext.Current.Server.MapPath(path+imgName), tt);//讲byte[]存储为图片
            return "/image/" + imgName;
        }
 ///将数据流转为byte[]
        public static byte[] StreamToBytes(Stream stream)
        {
            List<byte> bytes = new List<byte>();
            int temp = stream.ReadByte();
            while (temp != -1)
            {
                bytes.Add((byte)temp);
                temp = stream.ReadByte();
            }
            return bytes.ToArray();
        }
这个是返回的二维码图片







版权声明:本文为weixin_38668828原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。