plus.js 13.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
var t = getApp(),
  a = t.request,
  o = t.globalData.setting, 
  os = o,
  i = require("../../../utils/util.js"),
  ut = i,
  s = require("../../../utils/common.js"),
  w = require("../../../utils/wxParse/wxParse.js"),t_pay = require("../../../utils/pay2.js");
  var regeneratorRuntime = require('../../../utils/runtime.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentId: 0,
    url: o.imghost,
    screenWidth: 0,
    screenheight: 0,
    canvasHidden: true,
    shareImgPath: null,
    is_plusCard: "",//卡的列表
    imagePath: "",
    is_pulscardname: "",//plus会员的名字
    is_card: 0,//用于判断是否是续费页和购买页面 0不是PLUS会员
    content: "",//等级卡规则
    is_card_rule: "",//等级卡规则判断用
    is_show: 0,       //页面要加载完才能显示
    open_yq: 0,       //显示打开邀请码的弹窗
    by_index:null,    //购买卡的下班

    isyaoqingma:0,   //是否邀请码
    is_salesman:0,   //是否营业员
    inp_recommon: null,   //输入的邀请码
    inp_serviceman: null, //输入的营业员
    is_fengxiang: 0,      //是不是分享过来的
    def_recommon: null,    //分享人的手机号
    def_serviceman: null,  //分享人的营业员
  	fir_leader:0,          //分享人的ID	
  },
  swiperChange: function (e) {
    var currentId = e.detail.current;
    this.setData({
      currentId: currentId
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this,th=that;
  	var store_id=getApp().globalData.setting.stoid;
    //获取用户设备信息,屏幕宽度
    wx.getSystemInfo({
      success: res => {
        that.setData({screenWidth: res.screenWidth})
      }
    })
    var fir_leader = options.scene;
    if (fir_leader)	that.setData({fir_leader:fir_leader})
    //--先判断会员状态--
    var user_info = getApp().globalData.userInfo;
    if (user_info == null || user_info.mobile == undefined || user_info.mobile == "" || user_info.mobile == null) {
       wx.navigateTo({ url: '/pages/togoin/togoin?first_leader=' + fir_leader, })
       return false;
    }		 
    //-----------商家配置信息----- 等级卡规则,是否又开邀请码,营业员------------
    getApp().request.get("/api/weshop/storeconfig/get/" + store_id, {
      success: function (res) {
        var is_cardrule = res.data.data.cardrules;
        var switch_list= res.data.data.switch_list;
        var swi_arr=JSON.parse(switch_list);

        w.wxParse("content", "html", is_cardrule, that, 6);
        that.setData({
           is_card_rule: is_cardrule,
           isyaoqingma:parseInt(swi_arr.isyaoqingma),
           is_salesman:parseInt(swi_arr.is_staffno)
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
	var user_info = getApp().globalData.userInfo;  
	if(!user_info) return false;
	  
    var th=this,that=th,app_d=getApp().globalData;
  	var store_id=os.stoid;
    var fir_leader=this.data.fir_leader;

    if (fir_leader != null && fir_leader != undefined && fir_leader != "") {
      var r_url = "/api/weshop/plus/vip/mem/list?storeId=" + store_id + "&userId=" + fir_leader
      getApp().request.get(r_url, {
        success: function (res) {
          var is_ok = 1;
          if (res.data.code == -1) {
            getApp().my_warnning("未找到分享会员", 0, that);
            is_ok = 0;
            that.back_user();
          }
          var user_information = res.data.data[0];
          if (user_information['GradeCardID'] == null || user_information['GradeCardID'] == "" || user_information['GradeCardID'] == undefined) {
            getApp().my_warnning("分享会员不是plus会员", 0, that);
            is_ok = 0;
            that.back_user();
          }
          //--获取当前的时间--
          var now = ut.gettimestamp();
          if (user_information['ExpiryDate']) {
            var str = user_information['ExpiryDate'].replace(/-/g, '/');
            var end = new Date(str);
            end = Date.parse(end) / 1000;
            if (end < now) {
              getApp().my_warnning("分享plus会员已经过期", 0, that);
              is_ok = 0;
              that.back_user();
            }
          } else {
            getApp().my_warnning("分享plus会员已经过期", 0, that);
            is_ok = 0;
            that.back_user();
          }
          if (is_ok == 1) {
            //--保存或者更新会员信息--
            that.setData({ def_recommon: user_information.MobileTel, def_serviceman: user_information.StaffNo, is_fengxiang: 1 })
			th.save_th_plus_user();
          }
        }
      });
    } else {
      th.get_user_plus();
    }

    getApp().request.get("/api/weshop/users/get/" + os.stoid + "/" + app_d.user_id, {
      success: function (e) {
        var userInfo = e.data.data;
        if(userInfo['card_field'] && !getApp().globalData.is_card_back){   
              var u_url = "/pages/user/cardinfo/cardinfo";
              wx.reLaunch({url: u_url})        
        }
      },
    });

    this.getPlusCardType();     
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var goods_list = this.selectComponent("#goods_list"); //组件的id
    goods_list.get_list();
  },

  //---  获取卡类列表 ---
  getPlusCardType: function (e) {
    var storid = o.stoid;
    var th = this;
    getApp().request.promiseGet("/api/weshop/plus/vip/mem/bership/list?" + "storeId=" + storid, {}).then(res => {
      var plusCard = res.data.data;
      var arr = [1219, 2089, 3031];
      var new_arr = new Array();
      var parr=[];
      for (var i = 0; i < plusCard.length; i++) {
           //plusCard[i].free=arr[i];
            if(plusCard[i].IsStopBuy) {  continue; }
            if (plusCard[i].CardImg == "") plusCard[i].CardImg =  th.data.url + "/miniapp/images/plus/bg"+(i + 1)+ ".jpg?v=1";
            var item = {"fee": plusCard[i].CardFee, 'index': i}
            new_arr.push(item);
            parr.push(plusCard[i]);
      }

      //看是不是还有卡项是可以购买的
      if(new_arr && new_arr.length>0){
        //根據距離遠近排序,越近在前面,升序
        new_arr.sort(function (a, b) {
          if (a.fee < b.fee) {
            return -1;
          } else if (a.fee == b.fee) {
            return 0;
          } else {
            return 1;
          }
        });

        //里面和外面要显示统一
        switch (new_arr.length) {
          case 1:arr=[3031]; break;
          case 2: arr = [2089, 3031]; break;
          case 3: arr = [1219, 2089, 3031];break;
        }

        //---给数组添加立省多少钱---
        for (i = 0; i < parr.length; i++) {
            //判断是否停用
            for (var j = 0; j < new_arr.length; j++) {
              if (i == new_arr[j].index) {
                parr[i].free = arr[j];
                break;
              }
          }
        }
        th.setData({
          is_plusCard: parr, is_show: 1
        })

     }else{
          th.setData({is_show: 1});
          wx.showToast({
             title: "暂无可购买的等级卡项",
             icon: 'none',
             duration: 4000
          })

     }
     setTimeout(function(){
         var goods_list = th.selectComponent("#goods_list"); //组件的id
         goods_list.init();
         goods_list.get_list();
     },1000)

    })
  },

  //--开通会员---接受前端index,弹起窗口---
  Opening: function (e) {
    var th = this;
    th.data.by_index = e.currentTarget.dataset.ind;

    if (th.data.is_fengxiang) {
      th.buy_card();
    } else {
      //--如果后台都没有勾邀请码,营业员,立即购买--
      if (!th.data.isyaoqingma && !th.data.is_salesman) {th.buy_card(); return false;}
      th.setData({open_yq: 1})
    }
  },

  //--立即购买卡--
  buy_card: async function () {
    var th = this;
    var is_card = 0;
    //--商家是不是有等级卡功能,开通的人数是不是够用,开通是时间是不是到期--
    await getApp().request.promiseGet("/api/weshop/plus/plus/card/register",
        {data: {storeId: os.stoid}}).then(res => {
      if (res.data.code == 0) is_card = 1;
    })
    if (is_card == 0) {
		 getApp().my_warnning("请联系商家升级plus功能", 0, th);
		 return false;
	}
	 
    var item = th.data.is_plusCard[th.data.by_index];
    var req = {};
    req.cardId = item.CardId;
    req.rechargeType = 3;
    //如果不是你导购,也不是等级会员分享
    if (th.data.is_fengxiang == 1) {
       if (th.data.def_recommon) req.recommon = th.data.def_recommon
       if (th.data.def_serviceman) req.serviceman = th.data.def_serviceman
    } else {
      //---后台要求要输入邀请码---
      if (th.data.isyaoqingma) {
        if (!th.data.inp_recommon) {
          getApp().my_warnning("请输入邀请码", 0, th);
          return false;
        }
		
		//--邀请人不能是自己的手机号--
		if(th.data.inp_recommon==getApp().globalData.userInfo.mobile){
			getApp().my_warnning("邀请人不能填自已", 0, th);
			return false;
		}
		
        req.recommon = th.data.inp_recommon
      }
      //--后台要求要输入营业员--
      if (th.data.is_salesman) {
        if (!th.data.inp_serviceman) {
          getApp().my_warnning("请输入营业员", 0, th);
          return false;
        }
        req.serviceman = th.data.inp_serviceman
      }
    }
    req.storeId = os.stoid;
    req.userId = getApp().globalData.user_id;
    //--如果有邀请码,和营业员的情况下--
    if(req.recommon ||  req.serviceman ) {
      var req_data = {storeId: os.stoid, userId: req.userId};
	  if(req.recommon) req_data.inviteCode=req.recommon;
	  if(req.serviceman) req_data.staffNo=req.serviceman;
      getApp().request.get("/api/weshop/plus/vip/mem/referee/code/check", {
        data:req_data,
        success:function(res){
            if(res.data.code==0) {
              t_pay.pay(req, th.success, function () {
                getApp().my_warnning("支付失败", 0, th);
              });
            }else{
			  getApp().my_warnning(res.data.msg, 0, th);
			}
         }
      })
    }else{
      t_pay.pay(req, th.success, function () {
        getApp().my_warnning("支付失败", 0, th);
      });
    }
  },
  //返回会员页面
  OpeningClose: function() {
     this.setData({open_yq:0});
  },

  //返回会员中心
  back_user:function () {
    setTimeout(function () {
      wx.reLaunch({url:"/pages/user/index/index"});
    },1000)
  },
  
  //--支付成功,跳转到等级卡续费页面--
  success:function () {	
	//调一次就不再调用了  
	if(getApp().globalData.is_card_back) return false;	  
	if(getApp().globalData.plus_buy_back){
		//值在这里换
		getApp().globalData.is_card_back=1;		
		getApp().globalData.plus_buy_back=0; 
		//返回购物车购买
		wx.navigateBack({ delta: 1,});	
		return false;	
	}else{
		var u_url = "/pages/user/cardinfo/cardinfo";
		wx.reLaunch({url: u_url})
	}
	     
  },

  //--绑定邀请码的输入--
  b_input:function (e) {
      var value=e.detail.value;
      var name=e.currentTarget.dataset.name;
      var ob={};
      ob[name]=value
      this.setData(ob);
  },

  get_user_plus:async function(){
      var user_id=getApp().globalData.user_id;
      var fuser=null;
      var user_information =null;
      var store_id = os.stoid;
      var that=this;
      //--获取登记卡邀请信息--
      await getApp().request.promiseGet("/api/weshop/usersPlus/get/" + os.stoid + "/" + user_id,{1:1}).then(res=>{
        if (res.data.code==0){
          fuser=res.data.data;
        }
      })    
      if (!fuser) return false;
      var r_url = "/api/weshop/plus/vip/mem/list?storeId=" + store_id + "&userId=" + fuser.plus_user_id;	
      //--获取等级卡信息--
      await getApp().request.promiseGet(r_url,{1:1}).then(res=>{
         if (res.data.code == 0) {
           user_information = res.data.data[0];
         }
      })
      if (user_information){
        var now = ut.gettimestamp();
        var is_ok = 1;
        if (user_information['ExpiryDate']) {
          var str = user_information['ExpiryDate'].replace(/-/g, '/');
          var end = new Date(str);
          end = Date.parse(end) / 1000;
          if (end < now) { is_ok = 0;}
        } else {
          is_ok = 0;
        }

        if (is_ok == 1) {
           var fir_leader=fuser.plus_user_id
           that.setData({ def_recommon: user_information.MobileTel, def_serviceman: user_information.StaffNo, is_fengxiang: 1,fir_leader:fir_leader })
           //--先判断会员状态--
           var user_info = getApp().globalData.userInfo;
           if (user_info == null || user_info.mobile == undefined || user_info.mobile == "" || user_info.mobile == null) {
              wx.navigateTo({ url: '/pages/togoin/togoin?first_leader=' + fir_leader, })
              return false;
          }		   
      }
     }
  },
  
  save_th_plus_user:async function(){
    var f_user_id=this.data.fir_leader;
    if (f_user_id){
      var user_id = getApp().globalData.user_id;
      var user_plus = null;
      var now=ut.gettimestamp();
      //--获取登记卡邀请信息--
      await getApp().request.promiseGet("/api/weshop/usersPlus/get/" + os.stoid + "/" + user_id, { 1: 1 }).then(res => {
        if (res.data.code == 0) {
          user_plus = res.data.data;
        }
      }) 
      
      //--如果有值,要更新--
      if (!user_plus){
          getApp().request.post("/api/weshop/usersPlus/save", {
            data: { store_id: os.stoid, user_id: user_id, plus_user_id: f_user_id, addtime: now },
             success: function (res) { }
          })
      }else{
          getApp().request.put("/api/weshop/usersPlus/update",{
            data: { store_id: os.stoid, user_id: user_id, plus_user_id: f_user_id, edittime: now},
               success:function(res){ }
          })
      }
    }  
  }
})