hongBao.js 7.68 KB
// packageA//pages/hongBao/hongBao.js
const app = getApp();
let self = null;

let ut = require("../../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
		url: app.globalData.setting.imghost,
		qiangHongBao: false,
		qianging: false,
		time: null,
		
		// hongBaoData: null, //保存抢红包活动的相关时间等信息
		isStart: 0, //记录抢红包活动是否开始
		result: null, //保存抢红包的结果
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		console.log('onload');
		self = this; 
		
		this.isLogin()
		.then(function(data) {
		
			self.requestData('/api/weshop/redmoney/redConfig/get/'+app.globalData.setting.stoid, {})
			.then(function(data) {
				self.data.hongBaoData = data.data.data;
				self.countDown();
				setTimeout(function (){
					if(app.globalData.config &&  app.globalData.config.store_name){
						self.setData({
							storeName: app.globalData.config.store_name
						});
					}

				},1000)

			});
			
		});
  },
	
	// 判断是否授权登录
	isLogin() {
		return new Promise((resolve, reject) => {
			let user_info = app.globalData.userInfo;
			if(user_info == null || user_info.mobile == undefined || user_info.mobile == "" || user_info.mobile == null) {
			  wx.navigateTo({
			    url: '/pages/togoin/togoin',
			  })
			} else {
				resolve();
			};
		});
	},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
		console.log('onready');
		
		
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
		console.log('onshow');
		
		
		
		
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
		console.log('onhide');
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

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

  },

  /**
   * 用户点击右上角分享
   */
   onShareAppMessage: function () {   	  getApp().globalData.no_clear=1

  },
	
	// 请求数据
	requestData(url, data) {
		return new Promise((resolve, reject) => {
			app.request.promiseGet(url, {data: data})
			.then(res => {
			  if(res.data.code == 0) {
					console.log('resolve', res);
					resolve(res);
			  } else {
					reject(res);
				};
			})
		});
	},
	
	//秒杀倒计时
	//---小于10的格式化函数----
	timeFormat(param) {
	  return param < 10 ? '0' + param : param;
	},
	
	handleTime(time) {
		var day = parseInt(time / (60 * 60 * 24));
		var hou = parseInt(time % (60 * 60 * 24) / 3600);
		var min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
		var sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
		return {
		  day: this.timeFormat(day),
		  hou: this.timeFormat(hou),
		  min: this.timeFormat(min),
		  sec: this.timeFormat(sec),
		};
	},
	
	//----秒杀倒计时函数-----
	countDown() {
	  // if (!this.data.is_timer) return false;
		var time;
	  // 获取当前时间,同时得到活动结束时间数组
	  var newTime = ut.gettimestamp();
	  // var List = this.data.saleGoods;
	  // for (var j = 0; j < List.length; j++) {
	    // var endTimeList = List[j];
	    // 对结束时间进行处理渲染到页面
	    // for (var i = 0; i < endTimeList.length; i++) {
	      // var o = endTimeList[i];
				var startTime = this.data.hongBaoData.start_time;
				// var startTime = 1600;
	      var endTime = this.data.hongBaoData.end_time;
	      // if (o.status == 0) endTime = o.start_time
	      var obj = null;
				
				if(startTime - newTime > 0) { //活动还没开始
					this.setData({
						isStart: 0,
					});
					time = startTime - newTime;
					// var day = parseInt(time / (60 * 60 * 24));
					// var hou = parseInt(time % (60 * 60 * 24) / 3600);
					// var min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
					// var sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
					// obj = {
					//   day: this.timeFormat(day),
					//   hou: this.timeFormat(hou),
					//   min: this.timeFormat(min),
					//   sec: this.timeFormat(sec),
					// };
					
					obj = this.handleTime(time);
				}
	      // 如果活动未结束,对时间进行处理
	      if (endTime - newTime > 0) { //活动还没结束
					this.setData({
						isStart: 1,
					});
	        time = endTime - newTime;
	        // 获取天、时、分、秒
	        // var day = parseInt(time / (60 * 60 * 24));
	        // var hou = parseInt(time % (60 * 60 * 24) / 3600);
	        // var min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
	        // var sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
	        // obj = {
	        //   day: this.timeFormat(day),
	        //   hou: this.timeFormat(hou),
	        //   min: this.timeFormat(min),
	        //   sec: this.timeFormat(sec),
	        // };
					
					obj = this.handleTime(time);
					
	      } else { //活动已经结束
	        //活动已结束,全部设置为'00'
					this.setData({
						isStart: 2,
					});
	        obj = {
	          day: '00',
	          hou: '00',
	          min: '00',
	          sec: '00'
	        }
	      }
	      // var txt = "saleGoods[" + j + "][" + i + "].djs";
	      self.setData({
	        time: obj,
	      });
	    // }
	
	  // }
	  setTimeout(self.countDown, 1000);
	},
	
	qiangHongBao() {
		if(!this.data.qianging) {
			this.data.qianging = true;
			app.request.promisePost('/api/weshop/redmoney/redRecord/saveRecord', {
				data: {
					store_id: app.globalData.setting.stoid,
					user_id: app.globalData.user_id,
					redid: this.data.hongBaoData.id,
				}
			})
			.then(res => {
			 //  if(res.data.code == 0){
				// 	// wx.hideLoading();
				// 	// wx.showToast({
				// 	//   title: '领取成功',
				// 	//   icon: 'success',
				// 	//   duration: 2000
				// 	// });
				// 	console.log('请求提交成功',res.data);
					
				//  //  if(callback) {
				//  //  	callback(res.data.data);
				//  //  }
			 //  } else if(res.data.code === -1) {
				// 	// wx.hideLoading();
				// 	// wx.showToast({
				// 	//   title: '领取失败:' + res.data.msg,
				// 	//   icon: 'none',
				// 	//   duration: 2000
				// 	// });
				// 	// self.setData({
				// 	// 	result: res.data
				// 	// });
					
				// };
				self.setData({
					result: res.data,
					qiangHongBao: true,
				});
			})
		}
		
		
	},
	
	receive() {
		// app.postData('/api/wemd/stafftask/saveGuideTaskListGive', {
		// 	store_id: app.globalData.userInfo.store_id,
		// 	StaffId: _this.data.StaffId,
		// 	Id: _this.data.details.Id,
		// 	TaskType: _this.data.details.TaskType,
		// });
		
		// postData(url, data, callback) {
			wx.showLoading({
				title: '奖励领取中'
			});
			app.request.promisePost('/api/wemd/stafftask/saveGuideTaskListGive', {
				data: {
					store_id: app.globalData.userInfo.store_id,
					StaffId: _this.data.StaffId,
					Id: _this.data.details.Id,
					TaskType: _this.data.details.TaskType,
				}
			})
			.then(res => {
			  if(res.data.code == 0){
					wx.hideLoading();
					wx.showToast({
					  title: '领取成功',
					  icon: 'success',
					  duration: 2000
					});
					// console.log('请求提交成功',res.data.data);
				 //  if(callback) {
				 //  	callback(res.data.data);
				 //  }
			  } else if(res.data.code === -1) {
					wx.hideLoading();
					wx.showToast({
					  title: '领取失败:' + res.data.msg,
					  icon: 'none',
					  duration: 2000
					});
				};
			})
		// },
	},
	
	
	closePopup() {
		this.setData({
			qiangHongBao: false,
			qianging: false,
		});
	},
	
	
	gotoDetails() {
		// this.data.qiangHongBao = false;
		this.setData({
			qiangHongBao: false
		});
		app.goto('/packageA/pages/hongBaoDetails/hongBaoDetails?redid=' + this.data.hongBaoData.id);
	},
 	
	
})