// pages/public/cashCoupon/index.js
const util = require('../../../utils/util.js');
const app = getApp();
Page({
  data: {
    getSuccess: -1,
    getImgUrl: '',
    channelId: '',
    failContent: ''
  },
  onLoad(options) {
  },
  onShow() {
    let pages = getCurrentPages();
    let currentPages = pages[pages.length - 1];
    let options = currentPages.options;
    console.log(options)

    if (app.globalData.userInfo) {
      // 微信分享链接进入
      if (!util.isEmpty(options.channel)) {
        this.setData({ channelId: options.channel })
      }
      // 小程序码进入  eg：channel%3D14
      if (!util.isEmpty(options.scene)) {
        let scene = decodeURIComponent(options.scene).split('=');
        if (scene[0] == 'channel') {
          this.setData({ channelId: scene[1] })
        }
      }

      this.getVoucher()
    } else {
      // 静默登录
      let that = this
      util.silentLogin().then(res => {
        // 微信分享链接进入
        if (!util.isEmpty(options.channel)) {
          that.setData({ channelId: options.channel })
        }
        // 小程序码进入  eg：channel%3D14
        if (!util.isEmpty(options.scene)) {
          let scene = decodeURIComponent(options.scene).split('=');
          if (scene[0] == 'channel') {
            that.setData({ channelId: scene[1] })
          }
        }
        that.getVoucher()
      })
    }
  },
  // 领取现金券
  getVoucher() {
    let that = this;
    util.ajax({
      func: "voucher/new_user/receive",
      data: {
        channelId: this.data.channelId,
        type: 'wxNewUser',
        phone: app.globalData.userInfo.phoneNum
      }
    }, function (res) {
      if (res.code == 0) {
        that.setData({ getImgUrl: 'https://img.bbztx.com/image_test/upload/thumbs/20220930/logo/1664519389257009593.jpg', getSuccess: 1 })
      } else {
        that.setData({ getImgUrl: 'https://img.bbztx.com/image_test/upload/thumbs/20220930/logo/1664519439365071430.jpg', getSuccess: 0, failContent: res.reason })
      }
    });
  },
  onShareAppMessage() {
    let that = this;
    console.log('/pages/public/cashCoupon/index?channel=' + this.data.channelId)
    return {
      title: '价值158元新人优惠券免费领',
      path: '/pages/public/cashCoupon/index?channel=' + this.data.channelId,
      imageUrl: 'https://img.bbztx.com/image_test/upload/thumbs/20220906/logo/1662445015137076027.png'
    }
  }
})