微信H5网页调用扫一扫功能(vue)

// 扫一扫按钮
<button type="button" @click="onScan">扫一扫</button>
// 引入weixin-js-sdk
import wx from 'weixin-js-sdk';
//点击上传扫描二维码
onScan() {
  this.getCofig();
  const that = this;
  wx.ready(function() {
    wx.checkJsApi({
      // 需要使用的JS接口列表,在这里只需要用到scanQRCode
      jsApiList: ['scanQRCode'],
      success: function(res1) {
        if (res1.checkResult.scanQRCode) {
          // 当scanQRCode可使用时
          wx.scanQRCode({
            needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
            scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码,默认二者都有
            success: function(res2) {
              let result = res2.resultStr;
              console.log(res2.resultStr, '扫描的结果~');
              window.location.href = result;
              // 也可以对扫描结果处理过之后再使用
              // 比如可以这样使用:
              // window.location.href = result.split('?')[0] + '/detail?' + result.split('?')[1]
            },
            error: function(response) {
              that.$toast(response);
            },
          });
        }
      },
    });
  });
},
getCofig() {
  const that = this;
  let url = '';
  let ua = navigator.userAgent.toLowerCase();
  if (/iphone|ipad|ipod/.test(ua)) {
    url = window.location.href.split('#')[0];
  } else if (/android/.test(ua)) {
    url = window.location.href;
  }
  console.log(window.location.href, '这个是获取当前扫描的完整url~');
  // GetWeixinScan 后端提供
  let WxAuthUrl = GetWeixinScan + '?url=' + url;
  this.$get(WxAuthUrl, {}, header)
    .then((res) => {
      if (res.data.code == 0) {
        // console.log(res.data.data, '这个是获取调用扫描返回的参数?~~');
        //微信的配置~~
        that.wxConfig(
          res.data.data.appId,
          res.data.data.timestamp,
          res.data.data.nonceStr,
          res.data.data.signature
        );
      } else {
        that.$toast(res.data.message);
      }
    })
    .catch(function(error) {
      console.log(error);
    });
},
//wx.config的配置
wxConfig(appId, timestamp, nonceStr, signature) {
  wx.config({
    debug: false, // 开启调试模式,
    appId: appId, // 必填,企业号的唯一标识
    timestamp: timestamp, // 必填,生成签名的时间戳
    nonceStr: nonceStr, // 必填,生成签名的随机串
    signature: signature, // 必填,签名
    jsApiList: ['scanQRCode', 'checkJsApi'], // 必填,需要使用的JS接口列表
  });
  wx.error(function(res) {
    alert('出错了:' + res.errMsg); //wx.config配置错误,会弹出窗口哪里错误,然后根据微信文档查询即可。
  });
},

文档:
微信扫一扫官方文档
附录2-所有JS接口列表
微信扫一扫错误返回码

要在Vue H5调用微信分享,首先需要引入微信官方的JS-SDK库。在Vue项目的index.html文件中,可以在<head>标签内添加如下代码: ```html <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script> ``` 然后,在Vue组件的created生命周期钩子函数中,使用Vue的axios或者其他网络请求库向后端请求获取微信配置信息。 ```javascript created() { this.fetchWechatConfig(); }, methods: { async fetchWechatConfig() { const response = await axios.get('/api/wechat/config'); // 向后端请求微信配置信息 const { appId, timestamp, nonceStr, signature } = response.data; // 将配置信息存储到全局变量中 this.$store.commit('setWechatConfig', { appId, timestamp, nonceStr, signature }); this.initWechatSDK(); }, initWechatSDK() { const { appId, timestamp, nonceStr, signature } = this.$store.state.wechatConfig; wx.config({ appId, timestamp, nonceStr, signature, jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'], // 配置需要使用的微信API }); // 进行微信SDK的初始化 wx.ready(() => { this.shareToWechat(); }); }, shareToWechat() { const shareData = { title: '分享标题', link: '分享链接', imgUrl: '分享图片链接', desc: '分享描述', }; // 配置分享的具体内容 wx.onMenuShareTimeline(shareData); // 分享到朋友圈 wx.onMenuShareAppMessage(shareData); // 分享给好友 }, }, ``` 上述代码中,我们通过axios库向后端请求了微信配置信息,并将其存储到Vuex的全局状态中。然后,我们使用微信JS-SDK中的`wx.config`方法进行微信验证和初始化配置。一旦初始化成功,`wx.ready`回调函数将会被触发,我们在该函数中调用`shareToWechat`方法来配置分享的具体内容。 在`shareData`对象中,我们可以自定义分享的标题、链接、图片和描述等信息,然后使用`wx.onMenuShareTimeline`方法配置分享给朋友圈的内容,使用`wx.onMenuShareAppMessage`方法配置分享给好友的内容。 最后,我们可以在Vue组件中调用`shareToWechat`方法来触发微信分享。 注意:在实际开发中,需要根据微信的API文档和后端提供的接口来进行相应的调整和配置。以上代码仅为示例,具体实现方式可能会有所不同。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值