小程序采坑记(六) 分享到朋友圈

小程序目前没有转发到朋友圈的功能,所以只能是保存到用户的相册,让用户自主发朋友圈。

小程序采坑记(六) 分享到朋友圈_第1张图片

点击分享到朋友圈的提示按钮,弹出需要保存到用户相册的图片,这个图片是后台提供的网络图片。

  // 分享到朋友圈
  shareFriendsCircle: function() {
    var that = this;
    that.setData({
      flag: true,
    })
    wx.showToast({
      title: '图片生成中',
      icon:'loading'
    })
    wx.request({
      url: '你的后台地址',
      method: 'post',
      data: {
   	这个地方是你给后台传的参数
      },
      success: function (res) {
        if (res.data.rtnCode == '000000') {
         
          that.setData({
            url: ,  //这个是后台返回的图片url,然后把这个url存起来,方面页面显示调用。
            canvasHidden: false
          })
          wx.hideToast()
        }  
      }
    }) 
  },

点击保存图片的方法,需要注意的就是保存到相册需要用户授权。

  toPhotosAlbum: function (e) {
    var that = this;
    wx.getSetting({
      success: res => {
        if (!res.authSetting['scope.writePhotosAlbum']) {   //用户没有授权的情况
          wx.authorize({  //弹出授权框,引导用户进行授权
            scope: 'scope.writePhotosAlbum',
            success: function () {   //用户同意授权后调用downImg方法
              that.downImg()
            }
          })
        } else {   //用户已经授权的情况
          that.downImg()
        }
      },
      fail: function () {  //授权失败,引导用户在设置中打开同意授权开关
        wx.showModal({
          content: '检测到您没打开权限,是否去设置打开?',
          confirmText: "确认",
          cancelText: "取消",
          success: function (res) {
            //点击“确认”时打开设置页面
            if (res.confirm) {
              wx.openSetting({
                success: (res) => { }
              })
            }
          }
        })
      }
    })

  },
  downImg: function () {
    var that = this
    wx.downloadFile({    //因为上面获取的图片路径是网络路径,微信接口wx.saveImageToPhotoAlbum是不支持网络路径的,所以,用这个接口把网络路径保存为本地的临时路径。
      url: that.data.url,
      success: function (res) {
        console.log(res)
        wx.showLoading({
          title: '正在保存'
        })
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (res) {
            wx.hideLoading()
            wx.showModal({
              title: '存图成功',
              content: '图片成功保存到相册了,去发圈噻~',
              showCancel: false,
              confirmText: '好哒',
              confirmColor: '#72B9C3',
              success: function () {
                that.setData({
                  flag: falses
                })
              }
            })
          },
          fail: function (res) {
            wx.showToast({
              title: '保存失败',
              icon: 'none',
              duration: 1500
            })
          }
        })
      },
      fail: function () {
        wx.showToast({
          title: '图片获取失败',
          icon: 'none',
          duration: 1500
        })
      }
    })
  },

你可能感兴趣的:(小程序)