redPacket.js 3.4 KB
import {
  request
} from "../../request/index.js"
const a = getApp()
var isClick = true;
Page({
  data: {
    imagesUrl: a.globalData.baseUrl,
    currentT: 1,
    way: 1,
    redPacket: [],
    myPacket: [],
    totalFen: '',
    red_id: '',
    msg: '',
    isLing: true,
    txtpopUp: false


  },
  onLoad: function (options) {

  },


  onReady: function () {

  },

  onShow: function () {
    this.getredPacket()
    this.getmyPacket()
  },
  clickNum(e) {
    this.setData({
      currentT: e.target.dataset.num,
      way: e.target.dataset.num
    })

    console.log(this.data.way, "way");
    if (e.target.dataset.num == 1) {
      this.getredPacket()
    } else {
      this.getmyPacket()
    }
  },
  allPopup(e) {

    let that = this
    let it = e.currentTarget.dataset.id
    let list = that.data.redPacket
    list.forEach((item, index) => {
      if (it == item.id) {
        if (item.txtpopUp == true) {
          list[index].txtpopUp = false
        } else {
          list[index].txtpopUp = true
        }
      }
    });
    that.setData({
      redPacket: list
    })


  },
  clickPopup(e) {
    let that = this
    let it = e.currentTarget.dataset.id
    let list = that.data.myPacket
    list.forEach((item, index) => {
      if (it == item.id) {
        if (item.is_cloose == true) {
          list[index].is_cloose = false
        } else {
          list[index].is_cloose = true
        }
      }
    });
    that.setData({
      myPacket: list
    })


  },
  async getredPacket() {
    let that = this
    try {
      const {
        data: {
          data
        }
      } = await request({
        url: 'api/red/lingRed',
      })
      // console.log(data);
      data.forEach(item => {
        item.txtpopUp = false
      });
      that.setData({
        redPacket: data
      })
    } catch (err) {
      that.setData({
        msg: err.msg
      })
      that.popMaskTest()
    }

  },
  async receive(e) {
    let that = this
    if (isClick) {
      isClick = false;
      try {
        const {
          data
        } = await request({
          url: 'api/red/ling',
          data: {
            red_id: e.currentTarget.dataset.red_id
          }
        })
        that.setData({
          msg: data.msg,
        })
        that.popSuccessTest()
        setTimeout(() => {
          that.getredPacket()
        }, 1300);
        setTimeout(function () {
          isClick = true;
        }, 3000); //一秒内不能重复点击
      } catch (err) {
        console.log(err);
        a.popTest(err.msg)
      }

    } else {
      return;
    }

  },
  async getmyPacket() {
    let that = this
    try {
      const {
        data: {
          data
        }
      } = await request({
        url: 'api/red/myRed',
      })
      // console.log(data, "api/red/myRed ");
      data.forEach(item => {
        item.is_cloose = false
      })
      that.setData({
        myPacket: data,
      })
    } catch (err) {
      console.log(err);
      a.popTest(err.msg)
    }


  },
  popMaskTest() {
    wx.showToast({
      title: this.data.msg,
      duration: 2000,
      icon: 'none',
      mask: true //是否有透明蒙层,默认为false 
      //如果有透明蒙层,弹窗的期间不能点击文档内容 
    })
  },
  popSuccessTest() {
    wx.showToast({
      title: this.data.msg,
      icon: '', //默认值是success,就算没有icon这个值,就算有其他值最终也显示success
      duration: 2000, //停留时间
    })
  },

})