// pages/share/share.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    codeImg: 'https://image.baidu.com/search/detail?ct=503316480&z=0&ipn=d&word=%E4%BA%8C%E7%BB%B4%E7%A0%81&step_word=&hs=0&pn=0&spn=0&di=19620&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=0&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=undefined&cs=65199140%2C1406017826&os=695039919%2C3289081284&simid=0%2C0&adpicid=0&lpn=0&ln=1845&fr=&fmq=1607396518663_R&fm=&ic=undefined&s=undefined&hd=undefined&latest=undefined&copyright=undefined&se=&sme=&tab=0&width=undefined&height=undefined&face=undefined&ist=&jit=&cg=&bdtype=11&oriquery=&objurl=http%3A%2F%2Fremote.yunnan.cn%2Fqr%2Fphp%2Fqr.php%3Fd%3Dhttp%3A%2F%2Fnews.yunnan.cn%2Fsystem%2F2020%2F12%2F08%2F031165250.shtml&fromurl=ippr_z2C%24qAzdH3FAzdH3Fgjof_z%26e3By7ggwg_z%26e3BvgAzdH3Ffyfpj4AzdH3FdadaAzdH3F8dAzdH3FabAzdH3Fan88mcdca_z%26e3Bfip4s&gsm=1&rpstart=0&rpnum=0&islist=&querylist=&force=undefined'
  },
  save() {
    wx.getSetting({
      success: (res) => {
        console.log(res)
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
              console.log(res, "我点击同意啦")
              wx.showLoading({
                title: '保存中...'
              })
              let imgSrc = this.data.codeImg;
              wx.downloadFile({
                url: imgSrc,
                success: function (res) {
                  console.log(res, '我下载完成啦');
                  //图片保存到本地
                  wx.saveImageToPhotosAlbum({
                    filePath: res.tempFilePath,
                    success: function (data) {
                      console.log(data, '我保存成功啦')
                      wx.hideLoading();
                      wx.showToast({
                        title: '保存成功',
                        icon: 'success',
                        duration: 2000
                      })
                    },
                    fail: function (err) {
                      console.log(err, '我保存失败啦')
                      wx.hideLoading();
                      if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
                        console.log("当初用户拒绝，再次发起授权")
                        wx.openSetting({
                          success(settingdata) {
                            console.log(settingdata)
                            if (settingdata.authSetting['scope.writePhotosAlbum']) {
                              console.log('获取权限成功，给出再次点击图片保存到相册的提示。')
                            } else {
                              console.log('获取权限失败，给出不给权限就无法正常使用的提示')
                            }
                          }
                        })
                      }
                    },
                    complete(res) {
                      console.log(res);
                    }
                  })
                },
                complete(res) {
                  wx.hideLoading()
                }
              })
            }
          })
        } else {
          wx.showLoading({
            title: '保存中...'
          })
          console.log(this.data.classInfo.codeImg)
          let imgSrc = this.data.classInfo.codeImg
          wx.downloadFile({
            url: imgSrc,
            success: function (res) {
              console.log(res, '我下载完成啦');
              wx.hideLoading();
              //图片保存到本地
              wx.saveImageToPhotosAlbum({
                filePath: res.tempFilePath,
                success: function (data) {
                  console.log(data, '我保存成功啦')
                  wx.hideLoading();
                  wx.showToast({
                    title: '保存成功',
                    icon: 'success',
                    duration: 2000
                  })
                },
                fail: function (err) {
                  console.log(err, '我保存失败啦')
                  wx.hideLoading();
                  if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
                    console.log("当初用户拒绝，再次发起授权")
                    wx.openSetting({
                      success(settingdata) {
                        console.log(settingdata)
                        if (settingdata.authSetting['scope.writePhotosAlbum']) {
                          console.log('获取权限成功，给出再次点击图片保存到相册的提示。')
                        } else {
                          console.log('获取权限失败，给出不给权限就无法正常使用的提示')
                        }
                      }
                    })
                  }
                },
                complete(res) {
                  console.log(res);
                }
              })
            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})