美文网首页
vuecli3使用 微信jweixin.js 2019-09

vuecli3使用 微信jweixin.js 2019-09

作者: Kawing_Zhang | 来源:发表于2019-11-18 17:01 被阅读0次

不要用npm 引入,看了网上的教程,一点用都没有,坑死个人。
应在public文件夹里面的index.html中直接引用以下就行啦。

<script src="http://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>

然后就可以直接在对应页面写逻辑,需要注意的是,向后台请求appId、timestamp、nonceStr、signature的时候,用的url一定要是当前页面根路径。还需要注意顺序,wx.config--->wx.checkJsApi--->wx.error--->wx.ready

 //点击分享添加页面
shareAddClient() {
      this.getWeixinConfigData();
      this.showShareDialog = true;
    },
 getWeixinConfigData() {
      const self = this;
      const shareUrl = window.location.href.split("#")[0];//取当前页面的根路径,vue的路径是会有#的
      const url = urlConfig.getAccessInfo;
      const param = {
        url: shareUrl
      };
      const success = function(response) {
        const data = response.data;
        if (data.status == 200) {
          self.appId = data.data.appId;
          self.timestamp = data.data.timestamp;
          self.nonceStr = data.data.nonceStr;
          self.signature = data.data.signature;
          self.weixinConfig();
        } else {
          self.$toast.fail("请检查网络");
        }
      };
      const failed = function(error) {
        self.$toast.fail("请检查网络");
      };
      Http.http(url, param, success, failed);
    },
    //设置微信分享内容
    weixinConfig() {
      var shareLink =
        "http://baidu.com/bancassuranceVisit/questionnaire/index.html?salesId=" +
        this.salesId;
      //需在用户可能点击分享按钮前就先调用
      wx.config({
        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
        appId: this.appId, // 必填,公众号的唯一标识
        timestamp: this.timestamp, // 必填,生成签名的时间戳
        nonceStr: this.nonceStr, // 必填,生成签名的随机串
        signature: this.signature, // 必填,签名
        jsApiList: ["updateAppMessageShareData", "updateTimelineShareData"] // 必填,需要使用的JS接口列表
      });
      wx.checkJsApi({
        jsApiList: ["updateAppMessageShareData", "updateTimelineShareData"], // 需要检测的JS接口列表,所有JS接口列表见附录2,
        success: function(res) {
        }
      });
      wx.error(function(res) {
      });
      wx.ready(function() {
        
        // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
        wx.updateAppMessageShareData({
          title: "啦啦啦阿里邀请您补充信息,快来点击输入吧。", // 分享标题
          desc: '啦啦啦阿拉啦啦啦', // 分享描述
          link: shareLink, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
          imgUrl: "https://i.loli.net/2019/11/15/lalalaalal.png", // 分享图标
          success: function() {
          },
          cancel: function(err) {
          },
          fail: function(e) {
          }
        });
        wx.updateTimelineShareData({
          title: "啦啦啦阿里邀请您补充信息,快来点击输入吧。", // 分享标题
          desc: '啦啦啦阿拉啦啦啦', // 分享描述
          link: shareLink, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
          imgUrl: "https://i.loli.net/2019/11/15/lalalala.png", // 分享图标
          success: function() {
          },
          cancel: function(err) {  
          },
          fail: function(e) {
          }
        });
      });

    },
   
   

相关文章

网友评论

      本文标题:vuecli3使用 微信jweixin.js 2019-09

      本文链接:https://www.haomeiwen.com/subject/vslvictx.html