var app = getApp()
Page({
  data: {
    erweima:'',
    poster:"",
    img:app.globalData.imgUrl
  },
  onLoad: function (options) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
      mask:true
    })
    app.request('/user/getInviteWechatQrcode', '', 'GET').then(res => {
      console.log(res)
      if (res.statusCode == 200) {
        that.setData({erweima:res.data.path})
      }
    })
    app.request('/user/getInvitePoster', '', 'GET').then(res => {
      console.log(res)
      if (res.statusCode == 200) {
        that.setData({poster:res.data.path})
      }
      wx.hideLoading()
    })
  },
  // previewImage:function(e){
  //   var that = this;
  //   var arr = []
  //   arr = arr.concat(that.data.poster)
  //   console.log(arr)
  //   wx.previewImage({
  //     current: that.data.poster,//当前显示图片的http链接 
  //     urls: arr, //要预览的图片
  //   })
  //   wx.getImageInfo({
  //     src: that.data.poster[0],
  //     success(res){
  //       console.log(res)
  //     }
  //   })
  // },
  saveImg(e){
    // let url = e.currentTarget.dataset.url;
    //用户需要授权
    var that = this;
    wx.getSetting({
      success: (res) => {
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success:()=> {
              // 同意授权
              this.saveImg1(that.data.poster);
            },
            fail: (res) =>{
              wx.showModal({
                title: '保存失败',
                content: '请开启访问手机相册权限',
                success(res) {
                    wx.openSetting()
                }
            })
            }
          })
        }else{
          // 已经授权了
          this.saveImg1(that.data.poster);
        }
      },
      fail: (res) =>{
        console.log(res)
      }
    })   
  },
  saveImg1(url){
    wx.getImageInfo({
      src: url,
      success:(res)=> {
        let path = res.path;
        wx.saveImageToPhotosAlbum({
          filePath:path,
          success:(res)=> { 
            console.log(res);
            wx.showToast({
              title: '保存成功发给朋友吧~',
              icon:'none'
            })
          },
          fail:(res)=>{
            console.log(res);
            wx.showToast({
              title: '保存失败',
              icon:'none'
            })
          }
        })
      },
      fail:(res)=> {
        // this.saveImg()
      }
    })
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {

  },
  onReachBottom: function () {

  }
})