myGift.js 6.59 KB
// packageA//pages/myGift/myGift.js
const app = getApp();
let self = null;
var os = app.globalData.setting;

Page({

  /**
   * 页面的初始数据
   */
  data: {
		tabArr: ['礼包列表', '我的礼包'],
		currentIndex: 0,
		
		list: null,
		isLoading: false, // 检测是否已经发送请求,防止重复发送请求
		noMore: false, // 检测是否有更多数据,true为没有更多数据,false为还有数据
		pageNum: 1, // 当前页数
  },
	
	clickTab(e) {
		let index = e.target.dataset.index;
		let url = '';
		let data = {
			store_id: app.globalData.setting.stoid,
			user_id: app.globalData.user_id,
		};
		
		this.setData({
			list: null,
			pageNum: 1,
			noMore: false,
			currentIndex: index,
		});
		
		if(index == 0) {
			url = '/api/weshop/libao/libaoForm/page';
		} else if(index == 1) {
			url = '/api/weshop/libao/libaoFormvip/page';
		};
		
		this.getData(true, url, data);		
	},

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

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

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
		if(app.globalData.userInfo) {
			if(!this.data.isLogin) {
				this.setData({
					userInfo: app.globalData.userInfo,
					imghost: app.globalData.setting.imghost,
					isLogin: true,
				});

				var index=this.data.currentIndex;
				let url = '';
				let data = {
					store_id: app.globalData.setting.stoid,
					user_id: app.globalData.user_id,
				};
				this.setData({
					list: null,
					pageNum: 1,
					noMore: false,
				});

				if(index == 0) {
					url = '/api/weshop/libao/libaoForm/page';
				} else if(index == 1) {
					url = '/api/weshop/libao/libaoFormvip/page';
				};

				this.getData(true, url, data);
			};
		};
  },

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

  },

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

  },

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

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
		this.scrollToLower('/api/weshop/libao/libaoFormvip/page', {
			store_id: app.globalData.setting.stoid,
			user_id: app.globalData.user_id,
		});
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
	
	/**
	 * 请求数据
	 */
	getData: function(isInit, url, data) {
		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': []
				});
			};
			
		})
		.catch(function(err) {
			console.log('出错拉!!!!',err);
			self.setData({
				'list.pageData': []
			});
		});
	},
	
	
	/**
	 * 上拉加载
	 */
	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);
		};
	},
	
	/**
	 * 点击跳转
	 */
	goto(e) {
		let url = '';
		if(e.target.dataset.url) {
			url = e.target.dataset.url;
		} else {
			url = e.currentTarget.dataset.url;
		}
		app.goto(url);
	},


	//-- 礼包购买 --
	GetBuyPrice: function(e) {
		var th = this,that=th;
		var index=e.currentTarget.dataset.index;
		var item=that.data.list.pageData[index];

		var id = item.id; //活动id
		var my_confirm = th.selectComponent("#my_confirm"); //组件的id
		var user=getApp().globalData.userInfo;

		my_confirm.open(
			"是否确定购买该礼包",
			"取消",
			"确定",
			function() {
				my_confirm.open_cancel(0);
			},
			function() {
				my_confirm.open_cancel(0);
				var json = {
					"lbid": id, //活动Id
					"paytype": 2, //1=积分兑换 2=购买
					"store_id": os.stoid, //商家Id
					"user_id": user.user_id, //用户ID
				};
				var url = "/api/weshop/libao/libaoFormvip/libaoOrder";
				getApp().request.post(url,
					{
						data:json,
						success:function(res) {
							if (res.data.code == 0 && res.data.data) {
								res = res.data.data;
								wx.requestPayment({
									timeStamp: String(res.timeStamp),
									nonceStr: res.nonceStr,
									package: res.packageValue,
									signType: res.signType,
									paySign: res.paySign,
									success: function(res) {
										getApp().my_warnning("支付成功", 1, th);
									},
									fail: function(res) {
										getApp().my_warnning("取消支付", 0, th);
									}
								});
							} else {
								getApp().my_warnning(res.data.msg, 0, th);
							}
						}
					})
			})
	},

	//-- 积分兑换 --
	GetBuyIntegral: function(e) {
		var th = this,that=th;
		var index=e.currentTarget.dataset.index;
		var item=that.data.list.pageData[index];

		var id = item.id; //活动id
		var my_confirm = th.selectComponent("#my_confirm"); //组件的id
		var user=getApp().globalData.userInfo;
		my_confirm.open(
			"是否确定兑换该礼包",
			"取消",
			"确定",
			function() {
				my_confirm.open_cancel(0);
			},
			function() {
				my_confirm.open_cancel(0);
				var json = {
					"lbid": id, //活动Id
					"paytype": 1, //1=积分兑换 2=购买
					"store_id": os.stoid, //商家Id
					"user_id": user.user_id, //用户ID
				};
				var url = "/api/weshop/libao/libaoFormvip/libaoOrder";
				getApp().request.post(url,{
					data:json,
					success:function(res) {
						if (res.data.code == 0) {
							getApp().my_warnning("兑换成功!", 1, th);
						} else {
							getApp().my_warnning(res.data.msg, 0, th);
						}
					},
				})

			})
	},

})