zoukankan      html  css  js  c++  java
  • .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 = {
           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方法在支付里面有

  • 相关阅读:
    【转】Django部署时为什么要用 uWSGI与 Nginx? 以及 WSGI,uwsgi等协议
    django 的 uwsgi+Nginx 部署配置
    【转】详解Django DRF框架中APIView、GenericAPIView、ViewSet区别
    python导入包 相对路径踩坑
    【转】Jmeter逻辑控制器-事务控制器的使用
    my live / PC keyboard / Thinkpad Mluti Connect Bluttooth Keyboard with Trackpoint / KT-1525 / KU-1255 / 4x30k12182
    my live / PC GPU NVIDIA Quadro P1000 / Intel UHD Graphics 630 / Dell P2418HT / chumoping
    project architecture evolution
    OS + Linux DevOps
    OS + Linux MP3
  • 原文地址:https://www.cnblogs.com/EasyLive2006/p/7978163.html
Copyright © 2011-2022 走看看