vue单页面引用 weixin-js-sdk 以及授权页面

根据文档

main.js中

npm install weixin-js-sdk
import wx from ‘weixin-js-sdk’;
Vue.prototype.$wx = wx; //写不写这个 在组件中引用wx的时候都是undefined

解决方法:
在需要的页面 直接 import wx from ‘weixin-js-sdk’;

但是像一些分享功能需要每个页面都要用这种方式就不可取了
我的做法:直接在public静态文件夹中放一个微信的sdk直接引入,全局都能用wx
在这里插入图片描述在这里插入图片描述
接下来得获取微信签名
传统的jq项目都是只要把当前页面给服务端就行了,vue项目因为是单页面应用,路由采用hash的模式(history这里不讨论),只要将锚点前面的传给服务器就可以了

还有一个问题

很多博客说是每个路由切换的时候都得重新config,比较麻烦,直接将获取微信签名放入index.html页面底部代码如下:

<script>
    //   // 获取微信签名
    console.log(wx)
    var _getWechatSignUrl = location.protocol + "//" + 'tstgo.u-breath.cn'+'/Wapi/Pfdevice/configJsSdk';
    axios.post(_getWechatSignUrl, {
      html:'index.html'		(这个具体传什么要跟后台商量 我这边只要index。htm就行了  有些是还要加上域名的)
    })
    .then(function (res) {
      wx.config({
          debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
          appId: res.data.data.appid, // 必填,公众号的唯一标识
          timestamp: res.data.data.time, // 必填,生成签名的时间戳
          nonceStr: res.data.data.noncestr, // 必填,生成签名的随机串
          signature: res.data.data.sha1, // 必填,签名,见附录1
          jsApiList: [ 'scanQRCode'
          ]
          // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
        });
      console.log(res.data.data);
    })
    .catch(function (error) {
      console.log(error);
    });		 
  </script>

接下来就是组件中的调用(分享功能可以写上面index.html里)

methods: {
  //扫一扫的方法
    scanning(){
      // this.weixinApi.scanCode(res=>{
      //   console.log('sao')
      // })
      console.log('12322')
      wx.ready(function() {
            wx.scanQRCode({
                needResult: 1,
                // scanType: ["qrCode","barCode"],
                success: function(res) {
                    var result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果
                    // result=result.split(",");
                    console.log(result[1])
                },
                fail: function(res) {
                    console.log(res,'222222')
                }
            });
        })
    }

  },
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3 + TypeScript项目中使用weixin-js-sdk,你需要执行以下步骤: 1. 安装weixin-js-sdk ``` npm install weixin-js-sdk ``` 2. 在组件中引入weixin-js-sdk ``` import wx from 'weixin-js-sdk'; ``` 3. 在组件mounted钩子函数中初始化JS-SDK ``` mounted() { this.initJSSDK(); } ``` 4. 编写初始化JS-SDK的方法 ``` async initJSSDK() { const res = await this.$http.get('/api/signature'); wx.config({ debug: false, appId: res.data.appId, timestamp: res.data.timestamp, nonceStr: res.data.nonceStr, signature: res.data.signature, jsApiList: ['chooseImage', 'uploadImage'] }); } ``` 上面的代码中,我们从后端获取了一个签名,然后使用wx.config方法进行JS-SDK的初始化。其中,debug表示是否开启调试模式,appId、timestamp、nonceStr、signature是签名相关的参数,jsApiList是需要使用的微信JS接口列表。 5. 在需要使用微信JS接口的地方调用相应的方法 ``` async chooseImage() { const res = await this.$http.get('/api/upload'); wx.chooseImage({ count: 1, sizeType: ['original', 'compressed'], sourceType: ['album', 'camera'], success (res) { const localIds = res.localIds; // 上传到服务器 wx.uploadImage({ localId: localIds[0], isShowProgressTips: 1, success: function (res) { const serverId = res.serverId; // 返回图片的服务器端ID // 将serverId发送到服务器进行保存等操作 } }); } }); } ``` 上面的代码中,我们在chooseImage的成功回调函数中,获取到了选择的图片的本地ID,然后使用wx.uploadImage方法将图片上传到服务器。在上传成功的回调函数中,我们可以获取到图片的serverId,然后将serverId发送到服务器进行保存等操作。 以上就是在Vue3 + TypeScript项目中使用weixin-js-sdk的步骤。需要注意的是,我们需要从后端获取签名等参数才能进行JS-SDK的初始化,因此需要与后端进行配合。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值