vue调起微信扫一扫

vue调起微信扫一扫,两个注意的点

1、url必须是不带参的地址栏,如果传了带参数的地址url有可能会出现安卓机能调,苹果机报错或者安卓和苹果都报错

2、this指代问题在vx.ready等等方法里面此时的this指代的并不是vue实例,此时就应该在方法开头赋值一个对象为vue实例了

onScan(){

        const _this = this
        this.$axios
        .get("你的后台地址", {
          params: {
            url: location.href.split("#")[0]//你的不带参的当前页url
          }
        })
        .then(response => {
          let res = response.data;
          wx.config({
            debug: false,
            appId: res.data.jsSdkUiPackage.appId,
            timestamp: res.data.jsSdkUiPackage.timestamp,
            nonceStr: res.data.jsSdkUiPackage.nonceStr,
            signature: res.data.jsSdkUiPackage.signature,
            jsApiList: ["scanQRCode"]
          });
        }); 
        wx.error(function (res) {
            
                Dialog.alert({
                      title: "提示",
                      message: res.errMsg
                    }).then(() => {});
            
            
        });
        wx.ready(function () {
            wx.checkJsApi({
                jsApiList: ['scanQRCode'],
                success: function (res) {
 
                }
            });
            wx.scanQRCode({
                needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
                scanType: ["qrCode"], // 可以指定扫二维码还是一维码,默认二者都有
                success: function (res) {
                      var result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果
                      _this.$router.push({
                                  name: "reportDetail",
                                  query: { reportId: result }
                                });
                }
            });
        });
    }

  

原文地址:https://www.cnblogs.com/iwen1992/p/10971802.html