首页 > 代码库 > .NET开发微信小程序-生成二维码

.NET开发微信小程序-生成二维码

1.生成小程序二维码功能

直接请求相应的链接。传递相应的参数

以生成商铺的付款码为例:

 var shopsId = e.ShopsId
     //付款码的参数
     var codeModel = new function () { }
     codeModel.path = "pages/PageWxPay/PageWxPay?shopsId=" + shopsId
     codeModel.width = 430
     codeModel.auto_color = false
     codeModel.line_color = { "r": "0", "g": "0", "b": "0" }
     var data =http://www.mamicode.com/ {
       shopsID: shopsId,
       data: JSON.stringify(codeModel)
     }
     console.log(data)
     api.RequestApiURL("Weixin/MyPaymentCode", data, function (codeData) {
       console.log(codeData)
       var obj = codeData.data.data
       if (obj.Key == "0") {
         that.setData({
           payCodeUrl: app.globalData.apiurl + obj.Value
         })
         wx.hideLoading()
       }
       else {
         wx.showToast({ title: obj.Value })
       }
     })

后台代码处理

 private static object obj = new object();
        /// <summary>
        /// 创建二维码
        /// 接口A: 适用于需要的码数量较少的业务场景 接口地址:
        /// 接口B:适用于需要的码数量极多,或仅临时使用的业务场景
        /// 接口C:适用于需要的码数量较少的业务场景
        /// </summary>
        /// <param name="data">前台传递的数据</param>
        /// <param name="path">图片存储位置</param>
        /// <param name="toKen"></param>
        /// <returns></returns>
        public static bool CreateWxaqrCode(Utils.QrCodeType nType, string data, string path, string toKen, out string ExcaptionMassage)
        {
            ExcaptionMassage = "";
            bool msg = false;
            string url = string.Empty;
            switch (nType)
            {
                case Utils.QrCodeType.A:
                    url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={0}";
                    break;
                case Utils.QrCodeType.B:
                    url = "http://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={0}";
                    break;
                case Utils.QrCodeType.C:
                    url = "https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token={0}";
                    break;

            }
            url = string.Format(url, toKen);
            lock (obj)
            {
                //判断当前用户是否生成二微码
                if (!System.IO.File.Exists(path))
                {
                    try
                    {
                        //获取数据流
                        Stream str = Request.PostMoths(url, data);

                        byte[] by = Utils.StreamToBytes(str);

                        Utils.PreservationCodeImage(path, by);
                        //保存该文件
                        msg = true;
                    }
                    catch(Exception e)
                    {
                        ExcaptionMassage= e.Message;
                        msg = false;//出现异常
                    }
                }
            }
            return msg;
        }

注:PostMoths方法在小程序基础配置里面有

StreamToBytes方法和PreservationCodeImage方法在支付里面有

 

.NET开发微信小程序-生成二维码