小程序文章转发到企业微信朋友圈——自己记录可能不适合每个人

1.先在app.js里判断是不是企业微信登录
调用wx.qy.login拿到返回的code,然后调用企业微信接口,我这边是跟后台要的,主要是拿到sessionKey和userid,然后将这两个值在放在app里面

**  // 企业微信
  getQyLogin:function(){
    var this_ = this
    wx.qy.login({
      success: function(res) {
        if (res.code) {
          console.log('res.code',res.code)
          //发起网络请求
          var postData = {
            code:res.code
          }
          this_.requestQueryLoading(api.port.这里跟后台要接口, postData, '', 'GET', function (res) {
            console.log('res',res.data)
            if(res.meta.success){
              this_.globalData.sessionKey = res.data.sessionKey
              this_.globalData.userid = res.data.userid
            }else{
              wx.showToast({
                title: res.meta.message,
                icon:'none',
                duration:2000,
              })
            }
          })
        } else {
          console.log('登录失败!' + res.errMsg)
        }
      }
    });
  },
**

2.由与我平时不是企业微信看这个小程序,我就是在使用这个方法的时候掉用这个方法,如果你需要的话,可以在app.js里直接使用这个方法

goLinkqypyq(e) {
      var that = this;
      app.getQyLogin();   //企业微信登录方法
      var item = e.currentTarget.dataset.item   //传过来的文章详情
      setTimeout(() => {
        if (app.globalData.sessionKey != null && app.globalData.userid != null) {
          wx.qy.checkSession({   //校验用户当前 session_key 是否有效
            success: function (res) {
              that.transmit(item)   //调用转发到企业微信朋友圈方法
            },
            fail: function () { // session_key 已经失效,需要重新执行登录流程
              app.getQyLogin(); //重新登录
            }
          })
        }
      }, 2000)
    },

3.这里是转发到企业微信的方法,因为我可能是多图上传到朋友圈所有我在前面定义了一下

   // 企业微信朋友圈
    transmit(item) {
      var that = this
      wx.showLoading({
        title: '加载中~',
      })
      if (that.data.typeLike == 'down') { 
        let imgList = [];   //定义空数组
        item.picList.map((item) => { //item.picList 为图片mediaId的数组
          let msgObj = {
            msgtype: "image", // 消息类型,必填
            image: {
              imgUrl: item.mainUrl + item.picUrl // 图片的素材地址
            },
          };
          imgList.push(msgObj)
        })
        wx.qy.shareToExternalMoments({   //具有客户联系权限的企业成员,可通过该接口将文本内容和附件传递到客户朋友圈。当前暂仅支持在企业微信内调用。**
          text: {
            content: item.summary, // 文本内容
          },
          attachments: imgList,
          success(res){
            wx.hideLoading()
          },
          fail(res) {
              wx.showToast({
                title: '转发失败',
                icon:'none',
                duration:2000
              })
          }
        }, 
        );
    },
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值