const app = require('../../utils/util.js');
const tool = require('../../utils/tool.js');
Page({
  data: {
    codeImg: '../../images/ewm.png',
    bj: '../../images/ewmbj.png'
  },
  save() {
    console.log('我调取save了')
    wx.saveImageToPhotosAlbum({
      filePath: this.data.imagePath,
      success(res) {
        wx.showToast({
          title: '图片保存成功！',
          icon: 'none'
        })
      },
      fail: (res) => {
        console.log(res)
        if (res.errMsg === "saveImageToPhotosAlbum:fail auth deny" || res.errMsg === "saveImageToPhotosAlbum:fail:auth denied") {
          this.doAuth()
        }
      }
    })
  },
  // 获取授权
  doAuth() {
    wx.showModal({
      title: '获取授权',
      content: '您是否同意重新授权保存图片',
      cancelText: '不同意',
      confirmText: '同意',
      confirmColor: '#21c0ae',
      success: function (res) {
        if (res.confirm) { // 点击确认
          wx.openSetting({
            success(settingdata) {
              if (settingdata.authSetting["scope.writePhotosAlbum"]) {
                console.log("获取权限成功，再次点击图片保存到相册")
              } else {
                console.log("获取权限失败")
              }
            },
            fail: function (res) {
              console.log(res)
            }
          })
        }
      }
    })
  },
  getEwm() {
      tool.isFinishInfo().then(result2 => {
          wx.request({
            url: app.globalData.publicUrl + '/wx/mpuser/wxeef6678b92b6c856/qrCode',
            method: "get",
            data: {
              memberId:result2.id
            },
            success: (res) => {
              console.log(res)
                this.draw(res.data);
            },
            fail() {
              wx.hideLoading();
            }
          })
      }).catch(err => {
        // wx.showLoading();
        wx.request({
          url: app.globalData.publicUrl + '/wx/mpuser/wxeef6678b92b6c856/qrCode',
          method: "get",
          data: {
            memberId:0
          },
          success: (res) => {
            console.log(res)
              this.draw(res.data);
          },
          fail() {
            wx.hideLoading();
          }
        })
      })

  },
  draw(qrcode) {
    wx.downloadFile({
      url: qrcode,
      success: (res) => {
        const ctx = wx.createCanvasContext('myCanvasId');
        ctx.drawImage(this.data.bj, 10, 0, 355, 295); //里面的参数无非就是图片放置的位置即图片的横纵坐标，图片的宽高
        ctx.drawImage(res.tempFilePath, 106, 124, 148, 148);
        ctx.draw();
        setTimeout(() => {
          wx.canvasToTempFilePath({
            canvasId: 'myCanvasId',
            success: (res) => {
              var tempFilePath = res.tempFilePath;
              this.setData({
                imagePath: tempFilePath
              })
              wx.hideLoading();
            },
            fail: (res) => {
              console.log(res);
              wx.hideLoading();
            }
          });
        }, 200);
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  // getInfos() {
  //   wx.request({
  //       url: app.globalData.publicUrl + '/wx/member/' + this.data.openid + '/info',
  //       method: "get",
  //       success: (res) => {
  //         console.log(res, '我是获取的信息')
  //         if (res.data.code == 0) {
  //           // this.setData({
  //           //   myInfo: res.data.info
  //           // })
  //           this.getEwm(res.data.info.id);
  //         }
  //       }
  //   })
  // },
  onLoad: function (options) {
    wx.showLoading();
    tool.openidStatus().then(result => {
      this.setData({
        openid: result[0],
        sessionkey: result[1]
      })
      this.getEwm();
    })
  }
})