
var app = getApp()
var that=null
var sopenFrame=null
var ewmtitle=null
var img1=""
var mCanvas,mCanvas0,mCanvas1
Page( {
  data: {
    avatarUrl: "../../images/profile.png",
    YQRcodeUrl: "",
    QRcodeUrl: "",
    minQRcodeUrl: "",
    NCName:"您还没有登录，请先登录！", 
    loadingHidden:false,
    shareImgSrc:"",
    yqm:"",
    savetitle:"保存二维码到相册",
    ifdownload:0,
    selindex:0,
    tempFilePath:""

  },

  onLoad: function() {
      that = this
     
      that.rdata(0)
     
    },onPullDownRefresh: function () {
      //在当前页面显示导航条加载动画
       wx.showNavigationBarLoading(); 
      //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
      wx.showLoading({
          title: '刷新中...',
      })
      that.rdata(1)
  },rdata: function(t) {
    
      var userId = wx.getStorageSync("userId") 
      var LoginId = wx.getStorageSync("LoginId")
      wx.request({
        url: app.globalData.apiurl + "/xapiajax.ashx", 
        data: { 
          action:"getmytgm",
           userId:(!userId?"":userId),
           LoginId:(!LoginId?"":LoginId)
        },
        method:"GET",
        dataType:"json", 
        success (res) {  
          if(res.data.status==1){
                that.setData({
                    QRcodeUrl:res.data.QRcode,
                    YQRcodeUrl:res.data.QRcode,
                    minQRcodeUrl:res.data.QRcode,
                    yqm:res.data.yqm
               })
               
               
               if(res.data.QRcode!=""){
                 try{
                 
                   /*wx.downloadFile({
                        url: res.data.QRcode,
                        success (res0) {
                             if (res0.statusCode === 200) {
                                 img1=res0.tempFilePath
                                 that.setData({
                                     QRcodeUrl:img1,
                                     ifdownload:1
                                 })*/
                                 img1=res.data.QRcode
                                 that.setData({
                                  ifdownload:1
                              })
                                  wx.createSelectorQuery().select('#myCanvas')
                                  .fields({ node: true, size: true })
                                  .exec((res) => {
                                      let textCanvas = res[0].node;  // 重点1
                                      mCanvas=textCanvas
                                      let textCtx = textCanvas.getContext('2d');  // 重点2
                                      const dpr = wx.getSystemInfoSync().pixelRatio
                                      textCanvas.width = res[0].width * dpr
                                      textCanvas.height = res[0].height * dpr
                                      textCtx.scale(dpr, dpr)
                                      textCtx.clearRect(0,0,textCanvas.width,textCanvas.height)
                                      textCtx.beginPath(); 
                                      //绘制背景
                                      //textCtx.fillStyle = '#fff'
                                      //textCtx.clearRect(0,0,textCanvas.width,textCanvas.height)
                                      //textCtx.fillRect(0, 0, textCanvas.width, textCanvas.height)
                                      const bg = textCanvas.createImage();
                                      bg.src = '../../images/2wm_bg.png';
                                      bg.onload = () => {
                                            textCtx.drawImage(bg, 0, 0, 300, 533)
                                            const bg0 = textCanvas.createImage();
                                      bg0.src = img1;
                                      bg0.onload = () => {
                                       
                                            textCtx.drawImage(bg0, 110, 380, 90, 90) 
                                      } 
                                      }
                                      
                   
                                     
                                
               
                  
             })
             that.setData({
              loadingHidden: true 
         })
            
                             //}
                        //}
                   //})
                  }catch(err){
                    wx.showToast({
                      title: err,
                      icon: "none",
                      duration: 5000
                    })
                  }
              }
              
          }else{
            if(res.data.msg=="您未登录"||res.data.msg=="参数错误"){
              wx.removeStorageSync('userId')
              wx.removeStorageSync('LoginId')
              that.rdata(0) 
           }
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 3000
             })
          }
          if(t==1){
            //隐藏loading 提示框
            wx.hideLoading();
            //隐藏导航条加载动画
            wx.hideNavigationBarLoading();
            //停止下拉刷新
            wx.stopPullDownRefresh();
          }
          }
      })
    },onredata: function(e) {
      that.rdata(0)
    },swiperchange: function(e) {
      that.setData({
        selindex:e.detail.current
      })
    },onsavepic: function(e) {
      that.setData({
        savetitle:"保存中，请稍候..."
      })
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      wx.canvasToTempFilePath({
        canvasId: "myCanvas"+(that.data.selindex==0?"":(that.data.selindex==1?"0":"1")),
        destWidth: 405,
        destHeight: 720,
        canvas: (that.data.selindex==0?mCanvas:(that.data.selindex==1?mCanvas0:mCanvas1)),
        success: function(res0) {
          // 获取图片路径
          const tempFilePath = res0.tempFilePath;
          
          wx.saveImageToPhotosAlbum({
            filePath: tempFilePath,
            success(res) {
              console.log(res);
              wx.showModal({
                title: '图片保存成功',
                content: '图片成功保存到相册了，去发圈噻~',
                showCancel: false,
                confirmText: '好哒',
                confirmColor: '#72B9C3',
                success: function (res0) {
                  if (res0.confirm) {
                  
                  }
                  that.setData({
                    QRcodeHidden: true,
                    savetitle:"保存二维码到相册"
                  })
                }
              })
            }
          })
         
         
         
        },
        fail: function(err) {
          wx.showToast({
            title:  err,
            icon: "none",
            duration: 10000
          })
        }
      })
    
    },onsavepic0: function() {
     
      /*wx.saveImageToPhotosAlbum({
        filePath: img1,
        success(res) {
          console.log(res);
          wx.showModal({
            title: '图片保存成功',
            content: '图片成功保存到相册了，去发圈噻~',
            showCancel: false,
            confirmText: '好哒',
            confirmColor: '#72B9C3',
            success: function (res0) {
              if (res0.confirm) {
              
              }
              that.setData({
                QRcodeHidden: true,
                savetitle:"保存二维码到相册"
              })
            }
          })
        }
      })*/
    },gotourl: function(e) {
      var index = e.currentTarget.dataset.id
      var userId = wx.getStorageSync("userId")
      var LoginId = wx.getStorageSync("LoginId")
      if(userId&&LoginId){
          if(index==0){
              that.onQRcode()
          }else{
              wx.navigateTo({  
                  url: that.data.userListInfo[index].url
              })
          }
    }else{
        wx.navigateTo({  
           url: "../login/index"
        })   
    }
    },gouserinfo: function(e) {
       var userId = wx.getStorageSync("userId")
       var LoginId = wx.getStorageSync("LoginId")
      
       if(userId&&LoginId){
            wx.navigateTo({  
                url: "../myto/index"
            })
        }else{
          wx.navigateTo({  
             url: "../login/index"
          })   
       }
    },
    ontreturn:function() {
         wx.navigateBack({})
    },
    onQRcode:function() {
             that.setData({
              QRcodeHidden:false
             })
     },closeQRcode:function(e) {
        that.setData({
             QRcodeHidden: true
        })
    },inQRcode:function(e) {
      
    },onregister:function(e) {
      
       wx.navigateTo({  
          url: "../register/index?tguid=" + that.data.yqm
       })  

    },
    oncopy:function(e) {
      var font = e.currentTarget.dataset.font
      wx.setClipboardData({
        data: font,
        success: function (res) { 
          wx.showToast({
            title: "复制成功"
          })
        }
      })
     }
})