user_coupon.js 4.12 KB
var e = getApp(),os = e.globalData.setting;
var utils = require('../../../../utils/util.js');
var regeneratorRuntime = require('../../../../utils/runtime.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    judge:0,
    quan_list:null,
    iurl:os.imghost,
    h_pic:null,
    GradeId:null,
    FormId:null,
    now:'',
  },
  clik_coupon:function(){
     this.setData({
         judge:1
     })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     console.log("onLoad");
     console.log(options);
     var h_pic=options.img;

     if(h_pic.indexOf("http")==-1){
         h_pic= os.imghost+h_pic;
     }

     var FormId=options.FormId;
     var GradeId=options.gradeId;
     var now=utils.gettimestamp();
     this.setData({h_pic:h_pic,GradeId:GradeId,FormId:FormId,now:now});
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow:async function () {
    var th = this,q_list=null;

    //--获取列表--
    await getApp().request.promiseGet("/api/weshop/users/grade/wechat/cash/page", {
      data:{ storeId:os.stoid,FormId:th.data.FormId,GradeId:th.data.GradeId,userId:getApp().globalData.user_id}
    }).then(res => {
      q_list = res.data.data.pageData;
    })
    //取分类最后一级
    for(var i in q_list){
        var name=q_list[i].UseObjectName;
        var arr=name.split("\\")
        q_list[i].UseObjectName=arr[arr.length-1];
        q_list[i].Remark =  q_list[i].Remark.replace(/\n/g, "\n")
    }
    th.setData({quan_list:q_list});

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  //获取券
  get_quan:function (e) {
      var th=this,index=e.currentTarget.dataset.ind;
      var item = this.data.quan_list[index];
      var can_get = item.ObtainTimes;
      var Obtain = item.Obtain;
      var Id = item.Id;

      if (!Obtain || Obtain == undefined) Obtain = 0;

      getApp().request.post("/api/weshop/users/grade/cash/insert",{
      data:{
          privilegeId:Id,
          storeId:os.stoid,
          userId:getApp().globalData.user_id,
      },
      success:function (e) {
        if(e.data.code==0){
           var txt="quan_list["+index+"].ObtainTimes"
           var obj={};obj[txt]=can_get-1;

           var text = "quan_list[" + index + "].Obtain"; Obtain++;
           obj[text] = Obtain;
           th.setData(obj);
           getApp().my_warnning("您已成功领取该券",1,th);
        }
      }
    })
  },

  //点击核销券
  get_out:function (e) {
    var qt_txt = this.selectComponent("#pop_txt"); //组件的id
    var ob={title:"温馨提示",content:"当前优惠券已领取,点击二维码可立即使用"};
    qt_txt.open(ob);

  },

  clik_coupon:function (e) {
    var ind=e.currentTarget.dataset.ind;
    var is_open=this.data.quan_list[ind].is_open;
    if(is_open==1) is_open=0;
    else  is_open=1;

    var txt="quan_list["+ind+"].is_open"
    var obj={};obj[txt]=is_open;
    this.setData(obj);
  },

  //--显示核销券--
  show_quan:function (e) {
   var ind=e.currentTarget.dataset.ind;
   var item=this.data.quan_list[ind];
   var pId=this.data.quan_list[ind].Id;
   var th=this;
   var validay = item.Validay;
   var now = e.currentTarget.dataset.now;
  
   getApp().request.get("/api/weshop/users/grade/cash/code/get",{
       data:{
           storeId:os.stoid,
           privilegeId:pId,
           userId:getApp().globalData.user_id,
       },
       success:function (e) {
           console.log(e);
           console.log('/code/get');
           var  name= e.data.data.UseObjectName;

           //--获取成功的时候--
           if(e.data.code==0){
               var no=e.data.data.CashRepNo;
               var qc_com = th.selectComponent("#qc_com"); //组件的id
               var obj={val:no,content:"请将二维码展示给核销员,抵券更快捷",now: now, validay: validay, name: name,is_quan:1};
               qc_com.open(obj)

           }else{
               var qt_txt = this.selectComponent("#pop_txt"); //组件的id
               var ob={title:"温馨提示",content:"您暂时还没有该券"};
               qt_txt.open(ob);
           }

       }
   })
   

  }

})