group_list.js 6.24 KB
var e = getApp(), i = e.request, a = e.globalData.setting,os=a,ut = require("../../../utils/util.js");
var regeneratorRuntime = require('../../../utils/runtime.js');
const app = getApp();
let self = null;
		
Page({

  /**
   * 页面的初始数据
   */
  data: {
		url: a.imghost,
		type:1,
		// goodlist: null,
		
		isLogin: false,
		list: null,
		isLoading: false, // 检测是否已经发送请求,防止重复发送请求
		noMore: false, // 检测是否有更多数据,true为没有更多数据,false为还有数据
		pageNum: 1, // 当前页数
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
		self = this;
		
		app.isLogin().then(function(data) {//进入页面前已经授权登录成功
			self.setData({
				userInfo: data,
			});
		});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
        getApp().check_can_share();
		if(app.globalData.userInfo) {
			if(!this.data.isLogin) {
				this.setData({
					userInfo: app.globalData.userInfo,
					imghost: app.globalData.setting.imghost,
					isLogin: true,
				});
		
				let typeVal = this.data.type;
				let url = '/api/weshop/goods/groupBuy/page';
				let data = {
					store_id: app.globalData.setting.stoid,
					is_show: 1,
					is_end: 0,
					timetype: typeVal
				};
				
				this.setData({
					list: null,
					pageNum: 1,
					noMore: false,
				});
		
				this.getData(true, url, data).then(function() {
					self.setData({
						currentData: data,
					});
				});
				
				this.data.is_timer = 1;

			};
			
			//设置全局定时器
			clearInterval(this.data.timer);
			this.data.timer = setInterval(function () {
				self.countDown();
			}, 1000);

		};

  },

	// 底部按钮切换
	tip: function(e) {
		// 1进行中 0预热
		let typeValue = e.currentTarget.dataset.ty;
		this.data.type = typeValue;
		let url = '/api/weshop/goods/groupBuy/page';
		let data = {
			store_id: app.globalData.setting.stoid,
			is_show: 1,
			is_end: 0,
			timetype: typeValue
		};
		
		this.getData(true, url, data).then(function() {
			self.setData({
				currentData: data,
			});

		});


		this.setData({
			list: null,
			type: typeValue,
		});

	},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
		clearInterval(this.data.timer);
  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
		let data = this.data.currentData;
		this.scrollToLower('/api/weshop/goods/groupBuy/page', data);
  },

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

  },
	
	
	/**
	 * 请求数据
	 */
	getData: function(isInit, url, data) {
		let p = app.request.promiseGet(url, {
			data: data,
			isShowLoading: true,
		})
		.then(function(res) {
			if(res.data.code == 0) {
				
				self.setData({
					isLoading: false
				});
				
				if(isInit) {// 第一次加载
					self.setData({
						list: res.data.data
					});
				} else {
					self.setData({
						'list.pageData': self.data.list.pageData.concat(res.data.data.pageData)
					});
				};
				
				if((res.data.data.pageData.length == 0) || (res.data.data.pageSize * res.data.data.page >= res.data.data.total)) {
					self.setData({
						noMore: true
					});
				};
				
			} else {
				self.setData({
					'list.pageData': []
				});
			};
			
		});
	
		return p;
	},
	
	
	/**
	 * 上拉加载
	 */
	scrollToLower(url, requestData) {
		// 数据总量
		let total = this.data.list.total;
		// 单页最大数据量
		let pageSize = this.data.list.pageSize;
		// 如果数据总量不为0且小于或等于单页最大数据量,说明数据已全部加载,显示‘没有更多了’
		if((total != 0)&&(total <= pageSize)) {
			this.setData({
				noMore: true
			});
		};
		
		if(!this.data.isLoading && !this.data.noMore) {
			this.setData({
				isLoading: true,
				pageNum: this.data.pageNum + 1
			});
			requestData.page = this.data.pageNum;
			this.getData(false, url, requestData);
		};
	},
	
	
	//---小于10的格式化函数----
	timeFormat(param) {
	   return param < 10 ? '0' + param : param;
	},
	
	
	//----倒计时函数-----
	 countDown() {
	  if(!this.data.is_timer) return false;
	  var th=this;
	  // 获取当前时间,同时得到活动结束时间数组
	  let newTime = ut.gettimestamp();
	  var endTimeList = this.data.list.pageData;
	  if(endTimeList == null) return null;
	  // 对结束时间进行处理渲染到页面
	  for(var i = 0; i < endTimeList.length; i++) {
	       var o = endTimeList[i];
	       var endTime = this.data.type == 1?o.end_time:o.start_time;
	       // if(th.data.type==0) endTime = o.start_time;
	       let obj = null;
	       // 如果活动未结束,对时间进行处理
	       if (endTime - newTime > 0) {
	            let time = (endTime - newTime);
	            // 获取天、时、分、秒
	            let day = parseInt(time / (60 * 60 * 24));
	            let hou = parseInt(time % (60 * 60 * 24) / 3600);
	            let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
	            let 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), hide: 1
	            }
	        } else {
	           //活动已结束,全部设置为'00'
	         obj = { day: '00', hou: '00', min: '00', sec: '00' }
	        }
	        var txt ="goodlist["+i+"].djs";
	        th.setData({ [txt]: obj});
	      }
	 },
	 
	 
	 
	 //图片失败,默认图片
	 bind_bnerr2: function (e) {
	    var _errImg = e.target.dataset.errorimg;
	    var val = e.target.dataset.val;
	    if(val!=undefined && val!=null && val!='null' ){
	       var _errObj = {};
	       _errObj[_errImg] = "/public/images/default_goods_image_240.gif";
	       this.setData(_errObj) //注意这里的赋值方式,只是将数据列表中的此项图片路径值替换掉 ;
	    }
	 },
	 
	 goto(e) {
		 let url = e.currentTarget.dataset.url;
		 app.goto(url);
	 },
	
	
})