// pages/master/poster/poster.js
const util = require("../../../utils/util");
const api = require('../../../api/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
		btshow:true,
    showMoments: false,
    rid: '',
    urlLink: '',
    shareQr: '',
    shareType: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
		console.log(options);
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    if (options.rid) {
      this.setData({
        rid: options.rid
      });
		}
		if(options.shareType&&options.shareType==2){//朋友圈处理
			this.setData({
				btshow:false,
				shareQr:options.shareQr
			})
			wx.navigateTo({
				url: `/pages/upgrade/joinMaster/joinMaster?rid=${options.rid}&shareType=${options.shareType}`
			})
			wx.hideLoading();
			return;
		}

    wx.showShareMenu({
        withShareTicket: true,
        menus: ['shareAppMessage', 'shareTimeline']
      }),
      util.request(api.zzyPoster, {}, "POST").then((res) => {
        if (res.errno == 0) {
          this.setData({
            shareQr: res.data.shareQr,
            urlLink: res.data.urlLink
          })
          setTimeout(() => {
            wx.hideLoading()
          }, 500)

        }
      })
  },
  onShareAppMessage() {
    let rid = this.data.rid;
    const promise = new Promise(resolve => util.request(api.recordZzyShare, {
      userId: rid,
      shareType: this.data.shareType
    }, 'GET').then(res => {
      if (res.errno === 0) {
        resolve({
          title: '鲸致生活，欢迎您的加入！',
          path: `/pages/upgrade/joinMaster/joinMaster?rid=${this.data.rid}&shareType=${this.data.shareType}`,
          imageUrl: this.data.shareQr
        })
      }
    }));

    return {
      promise
    }


  },
  showMonents() {
    this.setData({
      showMoments: true
    })
  },
  noShowMonents() {
    this.setData({
      showMoments: false
    })
  },
  shareTowx() {},
  onShareTimeline: function () {
    this.setData({
      shareType: 2
    })
    let rid = this.data.rid;
    const promise = new Promise(resolve => util.request(api.recordZzyShare, {
      userId: rid,
      shareType: this.data.shareType
    }, 'GET').then(res => {
      if (res.errno === 0) {
        resolve()
      }
    }));

    return {
			title: '鲸致生活，欢迎您的加入！',
			// path: `/pages/upgrade/joinMaster/joinMaster?rid=${this.data.rid}&shareType=${this.data.shareType}`,
			query:'rid='+this.data.rid+'&shareType='+2+'&shareQr='+this.data.shareQr,
			imageUrl: this.data.shareQr
		}
  },

  downLoadImg() {
    wx.downloadFile({
      url: this.data.shareQr, //图片地址
      success: function (res) {
        //wx.saveImageToPhotosAlbum方法：保存图片到系统相册
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath, //图片文件路径
          success: function (data) {
            wx.showToast({
              icon: "none",
              title: '图片已保存到相册',
            })
          }
        })
      }
    })
  },
  copyLink() {
    wx.setClipboardData({
      //要复制的数据
      data: this.data.urlLink,
      success: function (res) {
        console.log("复制", res);
        wx.showToast({
          title: '链接复制成功,请前往浏览器粘贴打开！',
          icon: 'none',
          duration: 2000,
        });
      }
    });
  }


})