You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

547 lines
18 KiB
JavaScript

const app = getApp();
const card = require('../../utils/card.js');
const util = require('../../utils/util.js');
const wxbarcode = require('../../utils/wxbarcode.js');
const mqttMessage = require('../../utils/mqttMessage.js');
var intercode;
Page({
/**
* 页面的初始数据
*/
data: {
couponStatus: [{
status: 1,
title: "代金券",
select: true,
value: "CASH"
}, {
status: 2,
title: "兑换券",
select: false,
value: "GIFT"
}, {
status: 3,
title: "折扣券",
select: false,
value: "DISCOUNT"
}],
title: [],
list: []
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.cy_queryCoupon();
this.setData({
color: app.globalData.color
})
},
/**
* 优惠券
*/
cy_queryCoupon: function () {
var that = this;
var property = "memberId";
var keyword = app.globalData.memberId;
var params = {
"property": property,
"keyword": keyword,
"status": 1,
"shopNo": app.globalData.shopNo,
"posNo": app.globalData.posNo,
"amount": 0,
"amountFilter": 0
}
card.queryGroupCouponsList(params, function success(json) {
console.log(json)
var data = json.data;
if (data.status == 1) {
var list = [];
if (data.list.length > 0) {
for (var item of data.list) {
if (item.cardType == "DISCOUNT") {
console.log(item);
item.unitName = "折";
item.amount = item.discount;
item.size = "60rpx";
item.height = "90rpx;";
} else if (item.cardType == "CASH") {
item.unitName = "¥";
item.size = "60rpx";
item.height = "90rpx;";
item.amount = app.utils.getZero(parseFloat(item.reduceCost) / 100);
} else if (item.cardType == "GIFT") {
item.unitName = "兑换";
item.amount = "券";
item.size = "45rpx";
item.height = "80rpx;";
} else if (item.cardType = "PRODUCT") {
item.unitName = "商品";
item.amount = "券";
item.size = "45rpx";
item.height = "80rpx;";
}
item.bgImage = "http://pos.juweiyun.cn/373001/file/wechatAppnewCoupon.png";
if (item.status == 1) {
item.useStatus = "立即使用";
item.color = app.globalData.color;
} else if (item.status == 2) {
// item.useStatus = "已使用";
// item.color = "#999";
continue;
} else if (item.status == 3) {
// item.useStatus = "已过期";
// item.color = "#999";
continue;
}
item.type = 0;
item.condition = "满" + app.utils.getOne(parseFloat(item.leastCost) / 100) + "可用";
item.effective = item.beginTimestamp.split(" ")[0] + " 至 " + item.endTimestamp.split(" ")[0];
item.useFalg = 1
item.expireDay="剩余 "+item.expireDay+" 天过期"
list.push(item);
}
that.setData({
list: list
})
}
}
that.cy_GiftcouponGrounp();
}, function fail(err) {
console.log(err);
wx.showToast({
title: "网络连接不稳定, 请重试",
icon: "none"
})
})
},
cy_GiftcouponGrounp: function () {
var that = this;
card.couponCenter("GIFT", 2, function (json) {
console.log(json)
var data = json.data;
if (data.status == 1) {
var list = that.data.list;
if (data.list.length > 0) {
var memberReceiveCoupons = data.memberReceiveCoupons;
var memberDayReceiveCoupons = data.memberDayReceiveCoupons;
for (var item of data.list) {
if (memberReceiveCoupons[item.id]) {
var num = memberReceiveCoupons[item.id] ? memberReceiveCoupons[item.id] : 0;
if (item.personLimitNum != 0) {
if (parseFloat(num) >= parseFloat(item.personLimitNum)) {
continue;
}
}
}
var dayPersonLimitNum = item.dayPersonLimitNum;
if (memberDayReceiveCoupons[item.id]) {
var num = memberDayReceiveCoupons[item.id] ? memberDayReceiveCoupons[item.id] : 0;
if (parseFloat(num) >= parseFloat(dayPersonLimitNum)) {
continue;
} else {
if (item.personLimitNum != 0) {
item.syNum = item.personLimitNum - num
}
}
} else {
if (item.personLimitNum != 0) {
item.syNum = item.personLimitNum - num
}
}
if (item.cardType == "DISCOUNT") {
item.unitName = "折";
item.amount = item.discount;
item.size = "60rpx";
item.height = "90rpx;";
} else if (item.cardType == "CASH") {
item.unitName = "¥";
item.size = "60rpx";
item.height = "90rpx;";
item.amount = item.reduceCost;
// item.amount = app.utils.getZero(parseFloat(item.reduceCost) / 100);
} else if (item.cardType == "GIFT") {
item.unitName = "兑";
item.amount = "换券";
item.size = "34rpx";
item.height = "60rpx;";
}
//判断是否兑换券显示图片
if (that.data.couponStatus[2].select) {
item.showImage = true;
} else {
item.showImage = false;
}
item.color = app.globalData.color
// item.condition = "满" + app.utils.getOne(parseFloat(item.leastCost) / 100) + "可用";
item.condition = "满" + item.leastCost + "可用";
item.effective = item.startDate + " 至 " + item.endDate;
item.bgImage = "http://pos.juweiyun.cn/373001/file/wechatAppnewCoupon.png";
item.expireDay="剩余 "+item.expireDay+" 天过期"
if (item.status == 0) {
// item.bgImage = "../../images/icon-coupon-unimage.png";
item.type = 2;
item.useStatus = "条件不足";
item.expireDay=item.effective
} else if (item.status == 1) {
item.useStatus = "立即领取";
item.expireDay=item.effective
item.type = 1;
}
item.useFalg = 0
list.push(item);
}
that.setData({
list: list
})
}
}
that.cy_CASHcouponGrounp();
}, function fail(err) {
console.log(err);
wx.showToast({
title: "网络连接不稳定, 请重试",
icon: "none"
})
})
},
cy_CASHcouponGrounp: function () {
var that = this;
card.couponCenter("CASH", 2, function (json) {
console.log(json)
var data = json.data;
if (data.status == 1) {
var list = that.data.list;
if (data.list.length > 0) {
var memberReceiveCoupons = data.memberReceiveCoupons;
var memberDayReceiveCoupons = data.memberDayReceiveCoupons;
for (var item of data.list) {
if (memberReceiveCoupons[item.id]) {
var num = memberReceiveCoupons[item.id] ? memberReceiveCoupons[item.id] : 0;
if (item.personLimitNum != 0) {
if (parseFloat(num) >= parseFloat(item.personLimitNum)) {
continue;
}
}
}
var dayPersonLimitNum = item.dayPersonLimitNum;
if (memberDayReceiveCoupons[item.id]) {
var num = memberDayReceiveCoupons[item.id] ? memberDayReceiveCoupons[item.id] : 0;
if (parseFloat(num) >= parseFloat(dayPersonLimitNum)) {
continue;
} else {
if (item.personLimitNum != 0) {
item.syNum = item.personLimitNum - num
}
}
} else {
if (item.personLimitNum != 0) {
item.syNum = item.personLimitNum - num
}
}
if (item.cardType == "DISCOUNT") {
item.unitName = "折";
item.amount = item.discount;
item.size = "60rpx";
item.height = "90rpx;";
} else if (item.cardType == "CASH") {
item.unitName = "¥";
item.size = "60rpx";
item.height = "90rpx;";
item.amount = item.reduceCost;
// item.amount = app.utils.getZero(parseFloat(item.reduceCost) / 100);
} else if (item.cardType == "GIFT") {
item.unitName = "兑";
item.amount = "换券";
item.size = "34rpx";
item.height = "60rpx;";
}
//判断是否兑换券显示图片
if (that.data.couponStatus[2].select) {
item.showImage = true;
} else {
item.showImage = false;
}
item.color = app.globalData.color
// item.condition = "满" + app.utils.getOne(parseFloat(item.leastCost) / 100) + "可用";
item.condition = "满" + item.leastCost + "可用";
item.effective = item.startDate + " 至 " + item.endDate;
item.bgImage = "http://pos.juweiyun.cn/373001/file/wechatAppnewCoupon.png";
item.expireDay="剩余 "+item.expireDay+" 天过期"
if (item.status == 0) {
item.useStatus = "条件不足";
item.type = 2;
item.expireDay=item.effective
} else if (item.status == 1) {
item.useStatus = "立即领取";
item.expireDay=item.effective
item.type = 1;
}
item.useFalg = 0
list.push(item);
}
that.setData({
list: list
})
}
}
that.cy_DISCOUNTcouponGrounp();
}, function fail(err) {
console.log(err);
wx.showToast({
title: "网络连接不稳定, 请重试",
icon: "none"
})
})
},
cy_DISCOUNTcouponGrounp: function () {
var that = this;
card.couponCenter("DISCOUNT", 2, function (json) {
console.log(json)
var data = json.data;
if (data.status == 1) {
var list = that.data.list;
if (data.list.length > 0) {
var memberReceiveCoupons = data.memberReceiveCoupons;
var memberDayReceiveCoupons = data.memberDayReceiveCoupons;
for (var item of data.list) {
if (memberReceiveCoupons[item.id]) {
var num = memberReceiveCoupons[item.id] ? memberReceiveCoupons[item.id] : 0;
if (item.personLimitNum != 0) {
if (parseFloat(num) >= parseFloat(item.personLimitNum)) {
continue;
}
}
}
var dayPersonLimitNum = item.dayPersonLimitNum;
if (memberDayReceiveCoupons[item.id]) {
var num = memberDayReceiveCoupons[item.id] ? memberDayReceiveCoupons[item.id] : 0;
if (parseFloat(num) >= parseFloat(dayPersonLimitNum)) {
continue;
} else {
if (item.personLimitNum != 0) {
item.syNum = item.personLimitNum - num
}
}
} else {
if (item.personLimitNum != 0) {
item.syNum = item.personLimitNum - num
}
}
if (item.cardType == "DISCOUNT") {
item.unitName = "折";
item.amount = item.discount;
item.size = "60rpx";
item.height = "90rpx;";
} else if (item.cardType == "CASH") {
item.unitName = "¥";
item.size = "60rpx";
item.height = "90rpx;";
item.amount = item.reduceCost;
// item.amount = app.utils.getZero(parseFloat(item.reduceCost) / 100);
} else if (item.cardType == "GIFT") {
item.unitName = "兑";
item.amount = "换券";
item.size = "34rpx";
item.height = "60rpx;";
}
//判断是否兑换券显示图片
if (that.data.couponStatus[2].select) {
item.showImage = true;
} else {
item.showImage = false;
}
item.color = app.globalData.color
// item.condition = "满" + app.utils.getOne(parseFloat(item.leastCost) / 100) + "可用";
item.condition = "满" + item.leastCost + "可用";
item.effective = item.startDate + " 至 " + item.endDate;
item.bgImage = "http://pos.juweiyun.cn/373001/file/wechatAppnewCoupon.png";
item.expireDay="剩余 "+item.expireDay+" 天过期"
if (item.status == 0) {
item.useStatus = "条件不足";
item.type = 2;
item.expireDay=item.effective
} else if (item.status == 1) {
item.useStatus = "立即领取";
item.type = 1;
item.expireDay=item.effective
}
item.useFalg = 0;
list.push(item);
}
that.setData({
list: list,
// background:list.length>0?"#f3f3f3":"white"
})
}
}
}, function fail(err) {
console.log(err);
wx.showToast({
title: "网络连接不稳定, 请重试",
icon: "none"
})
})
},
queryCouponDetail: function (e) {
var index = e.currentTarget.dataset.index;
var coupon = this.data.list[index];
if (coupon) {
console.error(coupon, ".......---");
wx.navigateTo({
url: 'couponDetailNew?couponDetail=' + JSON.stringify(coupon),
})
}
},
useCode: function (e) {
console.error(e, ".......");
var couponId = e.currentTarget.dataset.couponid;
var formId = e.detail.formId;
var code = e.currentTarget.dataset.code;
this.setData({
code: code
})
app.globalData.oldCode = code;
wxbarcode.barcode('barcode', code, 680, 200);
wxbarcode.qrcode('qrcode', code, 450, 450);
var that = this;
that.setData({
hiden: true
})
var i = 0;
if (intercode)
clearInterval(intercode);
intercode = setInterval(function () {
i = i + 1;
wxbarcode.barcode('barcode', code, 680, 200);
wxbarcode.qrcode('qrcode', code, 450, 450);
if (i == 10) {
clearInterval(intercode);
}
}, 1000)
that.subscribeCouponCode();
that.senUseCodedMessage(formId, couponId);
},
receive: function (e) {
var status = e.currentTarget.dataset.status;
var index = e.currentTarget.dataset.index;
var coupon = this.data.list[index];
if (coupon) {
if (coupon.useFalg == 1) {
this.useCode(e);
return;
}
} else {
wx.showToast({
title: '小程序获取卡券失败',
})
return;
}
console.error(e, "...")
if (status == 0) {
util.showWaring("条件不足!")
return;
}
var schemeId = e.currentTarget.dataset.id;
var formId = e.detail.formId;
wx.showLoading({
title: '兑换中',
})
var that = this;
card.exchangeCouponScheme(schemeId, function (success) {
var result = success.data;
if (result.status == 1) {
wx.showToast({
title: "兑换成功",
icon: "none",
duration: 2000
})
that.cy_queryCoupon();
var codes = result.data.codes;
wx.requestSubscribeMessage({
tmplIds:app.globalData.templateMsgList,
success:function (params) {
console.error(" success ",params)
card.sendTemPlate("receiveCoupon", formId, codes, function (success) {},function (error) {});
},
fail:function (params) {
console.error(" error ",params)
}
})
} else {
wx.showToast({
title: result.errMessage,
icon: "none",
duration: 3000
})
}
wx.hideLoading();
}, function fail(err) {
console.log(err);
wx.showToast({
title: "网络连接不稳定, 请重试",
icon: "none"
})
})
},
cy_goto_myCoupon: function () {
wx.redirectTo({
url: '/pages/mine/myCoupon',
})
},
/**
* 订阅优惠券核销主题
*/
subscribeCouponCode: function () {
mqttMessage.mqttSubscribe(4);
mqttMessage.mqttOn(this);
},
senUseCodedMessage: function (formId, couponId) {
console.error(couponId + ".......")
card.sendTemPlate("couponCode", formId, couponId, function (success) {}, function fail(err) {
console.log(err);
wx.showToast({
title: "网络连接不稳定, 请重试",
icon: "none"
})
})
},
close: function () {
this.setData({
hiden: false
})
clearInterval(intercode);
mqttMessage.mqttunSubscribe(app.getCouponConsumeCode(this.data.code));
},
onMessage: function (top, msg) {
app.globalFunction.consumeCouponCallBack(JSON.stringify(msg));
},
onHide: function () {
if (this.data.code) {
mqttMessage.mqttunSubscribe(app.getCouponConsumeCode(this.data.code));
}
clearInterval(intercode);
},
onUnload: function () {
if (this.data.code) {
mqttMessage.mqttunSubscribe(app.getCouponConsumeCode(this.data.code));
}
clearInterval(intercode);
},
})