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.

1701 lines
52 KiB
JavaScript

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

const myOrder = require('../../../utils/myOrder.js');
const card = require('../../../utils/card.js');
const md5 = require('../../../utils/md5.js');
const des = require('../../../utils/des.js');
const util = require('../../../utils/util.js');
const utils = require('../../../utils/utils.js');
const log = require('../../../utils/log.js');
const app = getApp();
Page({
/**
* 页面的初始数据
*/
data: {
hidden: true,
payFlag: true,
isPasswd: true,
passwd: "",
queryCount: 0,
busMode: 1,
boxFee: 0,
deliverType: 0,
deliverFee: 0,
comment: "",
payMode: "02",
outTime: "",
receiveAddress: "选择收货地址",
receiveName: "收货人",
receiveMobile: "联系方式",
address: null,
jsonString: null,
order: {},
coupon: null,
couponList: [],
formId: "",
is_member_pay: 0,
orgPrice: 0,
isSubmit: true,
isBoxFee: 0,
isShowPhone: true,
inter: "",
isEnablePromotion: 0,
promotionPrice: 0,
promotionReceivable: 0,
reduceCost: 0
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
console.log(app.orderTicket);
var store = wx.getStorageSync("store");
var deliverFee = 0.0;
app.orderTicket.isMember = 0;
app.orderTicket.ext2 = "";
app.orderTicket.coupon = this.data.coupon;
var nowDate = new Date();
nowDate.setMinutes(nowDate.getMinutes() + 2);
app.orderTicket.busMode = wx.getStorageSync('mode') || app.orderTicket.busMode;
log.info('app.orderTicket.busMode: ' + app.orderTicket.busMode);
if (app.orderTicket.busMode == undefined || app.orderTicket.busMode == '') {
app.orderTicket.busMode = 0;
}
app.orderTicket.reserveTime = utils.getFormatTime(nowDate, 0);
var sumMoney = app.orderTicket.receivable;
if (app.orderTicket.busMode == 3) {
this.data.deliverType = store.deliverType;
deliverFee = store.deliverFee;
sumMoney = parseFloat(app.orderTicket.receivable) + parseFloat(app.orderTicket.boxFee) + parseFloat(deliverFee);
this.cy_getProgramAddressList();
}
if (app.orderTicket.busMode == 0) {
if (store.busModes.indexOf(app.orderTicket.busMode) == -1) {
app.orderTicket.busMode = 1;
}
}
if (app.globalData.memberPayEnabled != 1) {
this.setData({
is_member_pay: 0
})
}
this.computePromotion();
this.setData({
busMode: app.orderTicket.busMode,
busModes: options.storeBusModes,
storeName: app.orderTicket.storeName,
storeAddress: app.orderTicket.storeAddress,
outTime: app.orderTicket.reserveTime,
startTime: app.orderTicket.reserveTime,
time: app.orderTicket.reserveTime,
tableName: app.orderTicket.tableName,
tableNo: app.orderTicket.tableNo,
phone: app.orderTicket.phone,
list: app.orderTicket.orderList,
boxFee: parseFloat(app.orderTicket.boxFee).toFixed(2),
deliverFee: deliverFee,
receivable: parseFloat(app.orderTicket.receivable).toFixed(2),
sumMoney: parseFloat(sumMoney).toFixed(2),
promotionReceivable: parseFloat(app.orderTicket.promotionReceivable).toFixed(2),
orgPrice: parseFloat(sumMoney).toFixed(2),
color: app.globalData.color,
takeOutBoxFeeFlag: app.globalData.takeOutBoxFeeFlag,
isBoxFee: app.globalData.userIsFee
});
// 查询优惠券
this.cy_queryCoupon();
//加载餐桌 去判断堂食是否必选桌号
this.cy_loadTableArea();
//清除兑换券的商品
// this.cy_clear_gift_product(app.orderTicket.orderList);
//console.error(" app.orderTicket ", app.orderTicket)
},
//计算订单促销价
computePromotion: function () {
var orderTicket = app.orderTicket;
var promotionPrice = 0;
for (var item of orderTicket.orderList) {
item.price = item.specList[0].price;
promotionPrice += myOrder.promotionComputePrice(item);
}
this.setData({
promotionPrice: promotionPrice
})
},
cy_click_promotion: function () {
if (this.data.isEnablePromotion == 0) {
this.setData({
isEnablePromotion: 1,
sumMoney: this.data.sumMoney - this.data.promotionPrice
});
} else {
this.setData({
isEnablePromotion: 0,
sumMoney: this.data.sumMoney + this.data.promotionPrice
});
}
},
/*
*查询默认收货地址
*/
cy_getProgramAddressList: function () {
var that = this;
var params = {
method: "program.useraddress.list",
programId: app.globalData.programId,
openId: app.openId,
pageNumber: 1,
pageSize: 100,
};
console.log(params);
var ignores = ["openId", "pageNumber", "pageSize"];
app.jsapi.memberApi(app.globalData.appKey, app.globalData.appSecret, app.globalData.serverUrl).ajax(params, ignores, function (json) {
console.log(json);
var data = json.data;
if (data.status == 1) {
var list = data.list;
if (list.length != 0) {
for (var item of list) {
if (item.defaultFlag == 1) {
var store = wx.getStorageSync("store");
var distance = app.utils.getDistance(store.latitude, store.longitude, item.latitude, item.longitude);
var deliverKilometre = 5;
if (store.deliverKilometre) {
deliverKilometre = parseFloat(store.deliverKilometre);
}
if (distance > deliverKilometre) {
wx.showToast({
title: '注意:默认地址超过' + deliverKilometre + "公里不支持配送, 请重新选择地址",
icon: "none",
duration: 3500
})
return;
}
that.cy_loadAddress(item);
return;
}
}
}
}
}, function (err) {
wx.showToast({
title: '网络异常,请检查',
icon: "none"
})
});
},
/**
* 餐桌区域
*/
cy_loadTableArea: function () {
var that = this;
var params = {
"method": "store.table.area",
"storeId": app.orderTicket.storeId,
}
var ignores = [];
app.jsapi.api(app.globalData.appKey, app.globalData.appSecret, app.globalData.serverUrl).ajax(params, ignores,
function (json) {
var data = json.data;
if (data.status == 1) {
var list = data.list;
if (list.length > 0) {
that.setData({
isTables: true
})
} else {
that.setData({
isTables: false
})
}
}
},
function (err) {
console.log("=-=-=", err);
});
},
/**
* 优惠券
*/
cy_queryCoupon: function () {
var store = wx.getStorageSync("store");
var that = this;
var status = 1;
var property = "memberId";
var keyword = app.globalData.memberId;
var params = {
"property": property,
"keyword": keyword,
"status": status,
"shopNo": store.storeNo,
"posNo": app.globalData.posNo,
"useFlag": 1,
"amount": parseInt(parseFloat(app.orderTicket.receivable) * 100),
"amountFilter": 0
}
console.log(params);
card.queryGroupCouponsList(params, function success(json) {
console.log(json)
var data = json.data;
if (data.status == 1) {
var list = [];
if (data.list.length > 0) {
var sumMoney = that.data.sumMoney;
var reduceCost = 0;
for (var item of data.list) {
if (item.useFlag == 0) {
continue;
}
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 = app.utils.getOne(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.type = 1;
item.color = app.globalData.color
} else if (item.status == 2) {
item.useStatus = "已使用";
item.type = 2;
item.color = "#999";
} else if (item.status == 3) {
item.useStatus = "已过期";
item.color = "#999";
item.type = 2;
}
item.condition = "满" + app.utils.getOne(parseFloat(item.leastCost) / 100) + "可用";
item.effective = item.beginTimestamp.split(" ")[0] + " 至 " + item.endTimestamp.split(" ")[0];
if (item.cardType == "CASH") { // 代金券
var reduceCost = parseFloat(app.utils.getZero(parseFloat(item.reduceCost) / 100));
var leastCost = parseFloat(app.utils.getOne(parseFloat(item.leastCost) / 100));
if (sumMoney >= leastCost) {
list.push(item);
}
} else if (item.cardType == "DISCOUNT") { // 折扣券
var leastCost = parseFloat(app.utils.getOne(parseFloat(item.leastCost) / 100)); // 启用金额
if (sumMoney >= leastCost) {
list.push(item);
}
} else if (item.cardType == "GIFT") {
if (item.giftType == "2") {
list.push(item);
}
}
}
that.setData({
couponList: list
});
}
}
}, function fail(err) {
})
},
/**
* 外卖模式选择收货地址
*/
cy_selectAddress: function () {
console.log("外卖选择收货地址");
wx.navigateTo({
url: "../../mine/managerAddress?busMode=3",
})
},
/**
* 加载收货地址
*/
cy_loadAddress: function (address) {
if (!address) {
return;
}
var receiveAddress = address.receiveAddress + address.receiveDoor;
this.setData({
receiveAddress: receiveAddress,
receiveName: address.receiveName,
receiveMobile: address.receiveMobile,
address: address
});
},
/**
* 就餐时间
*/
cy_selectTime: function (e) {
var nowDate = new Date();
nowDate.setMinutes(nowDate.getMinutes() + 2);
nowDate = utils.getFormatTime(nowDate, 0);
if (e.detail.value < nowDate) {
wx.showToast({
title: '时间选择错误',
icon: "none"
})
return;
}
app.orderTicket.reserveTime = e.detail.value;
this.setData({
outTime: app.orderTicket.reserveTime,
time: app.orderTicket.reserveTime,
});
},
/**
* 取消
*/
cy_cancelTime: function (e) {
var nowDate = new Date();
nowDate.setMinutes(nowDate.getMinutes() + 2);
this.setData({
startTime: utils.getFormatTime(nowDate, 0),
});
},
/**
* 就餐桌号
*/
cy_tapSelectTable: function () {
// var url = "../table/table?storeId=" + app.orderTicket.storeId;
// wx.navigateTo({
// url: url,
// })
},
cy_click_boxFee: function () {
if (this.data.isBoxFee == 0) {
var sumMoney = parseFloat(parseFloat(this.data.sumMoney) + parseFloat(app.orderTicket.boxFee)).toFixed(2);
var promotionReceivable = 0;
if (app.orderTicket.promotionFlag == 1) {
promotionReceivable = parseFloat(parseFloat(this.data.promotionReceivable) + parseFloat(app.orderTicket.boxFee)).toFixed(2);
}
app.globalData.userIsFee = 1;
this.setData({
isBoxFee: 1,
boxFee: app.orderTicket.boxFee,
sumMoney: sumMoney,
promotionReceivable: promotionReceivable
})
} else {
var sumMoney = parseFloat(parseFloat(this.data.sumMoney) - parseFloat(app.orderTicket.boxFee)).toFixed(2);
var promotionReceivable = 0;
if (app.orderTicket.promotionFlag == 1) {
promotionReceivable = parseFloat(parseFloat(this.data.promotionReceivable) - parseFloat(app.orderTicket.boxFee)).toFixed(2);
}
app.globalData.userIsFee = 0;
this.setData({
isBoxFee: 0,
boxFee: 0,
sumMoney: sumMoney,
promotionReceivable: promotionReceivable
})
}
},
/**
* 就餐方式
*/
cy_tapSelectEatType: function (e) {
var store = wx.getStorageSync("store");
var busMode = e.currentTarget.dataset.busmode;
if (store.busModes.indexOf(busMode) == -1) {
wx.showToast({
title: '当前门店不支持该模式',
icon: "none"
})
return;
}
app.orderTicket.busMode = busMode;
// var sumMoney = 0;
var sumMoney = this.data.sumMoney;
var promotionReceivable = this.data.promotionReceivable;
var boxFee = 0;
if (app.orderTicket.busMode == 0) { // 堂食
sumMoney = this.data.sumMoney;
promotionReceivable = this.data.promotionReceivable;
if (app.globalData.takeOutBoxFeeFlag == 1 || this.data.isBoxFee == 1) {
sumMoney = parseFloat(parseFloat(sumMoney) - parseFloat(app.orderTicket.boxFee));
promotionReceivable = parseFloat(parseFloat(promotionReceivable) - parseFloat(app.orderTicket.boxFee));
}
}
if (app.orderTicket.busMode == 1) { // 外带
if (app.globalData.takeOutBoxFeeFlag == 1 || this.data.isBoxFee == 1) {
sumMoney = parseFloat(parseFloat(sumMoney) + parseFloat(app.orderTicket.boxFee));
promotionReceivable = parseFloat(parseFloat(promotionReceivable) + parseFloat(app.orderTicket.boxFee));
boxFee = app.orderTicket.boxFee;
}
}
this.setData({
boxFee: parseFloat(boxFee).toFixed(2),
receivable: parseFloat(app.orderTicket.receivable).toFixed(2),
sumMoney: parseFloat(sumMoney).toFixed(2),
promotionReceivable: parseFloat(promotionReceivable).toFixed(2),
busMode: app.orderTicket.busMode
});
},
/**
* 手动输入手机号
*/
cy_bindblur: function (e) {
console.log(e);
app.orderTicket.phone = e.detail.value;
this.setData({
phone: app.orderTicket.phone,
});
},
/**
* 获取手机号
*/
cy_getPhoneNumber: function (e) {
var that = this;
if (e.detail.errMsg == 'getPhoneNumber:ok') {
var iv = e.detail.iv;
var encryptedData = e.detail.encryptedData;
card.wx_getPhoneNumber(iv, encryptedData, function (json) {
var result = json.data;
if (result.status == '1') {
var data = result.data.data;
wx.setStorageSync("phone", data.purePhoneNumber);
app.orderTicket.phone = data.purePhoneNumber;
app.globalData.phone = data.purePhoneNumber;
that.setData({
phone: data.purePhoneNumber,
isShowPhone: false
});
} else {
wx.showToast({
title: '手机号获取失败,请手动输入',
icon: "none"
})
}
}, function (err) {
wx.showToast({
title: '手机号获取失败,请手动输入',
icon: "none"
})
});
} else {
wx.showToast({
title: '手机号获取失败,请手动输入',
icon: "none"
})
}
},
/**
* 备注信息
*/
cy_tapInputCommont: function () {
wx.navigateTo({
url: 'userComment',
})
},
/**
* 支付
*/
cy_tapGoPay: function (e) {
console.error(" app.orderTicket.order ", app.orderTicket)
var that = this;
this.data.formId = e.detail.formId;
var tenantId = app.globalData.tenantId;
var params = {
method: "program.product",
programId: app.globalData.programId,
storeId: app.orderTicket.storeId
}
var ignores = ["programId", "storeId"]; // 不签名参数
app.jsapi.api(app.globalData.appKey, app.globalData.appSecret, app.globalData.serverUrl).ajax(params, ignores, function (json) {
var data = json.data;
if (data.status == 1) {
for(let i = 0; i < data.data.listProduct.length; i++) {
for(let j = 0; j < app.orderTicket.orderList; j++) {
if(data.data.listProduct[i].productId == app.orderTicket.orderList[j].productId && data.data.listProduct[i].stock < app.orderTicket.orderList[j].count) {
wx.showToast({
title: app.orderTicket.orderList[j].productName + '库存不足',
icon: "none"
})
return;
}
}
}
if ("0|1".indexOf(app.orderTicket.busMode) != -1) { // 堂食和外带
if (tenantId != "571022") {
if (that.data.isTables) {
if (app.orderTicket.busMode == 0 && app.orderTicket.tableName == "选择餐桌") {
wx.showToast({
title: '请选择餐桌',
icon: "none"
})
return;
}
}
}
/*if (app.orderTicket.busMode == 1 && app.orderTicket.phone.length != 11) {
var title = '手机号码格式错误';
if (app.orderTicket.phone.length == 0) {
title = '请填写联系方式';
}
wx.showToast({
title: title,
icon: "none"
})
return;
}*/
}
if (app.orderTicket.busMode == 3) { // 外卖
console.log("外卖");
if (!that.data.address) {
wx.showToast({
title: '请选择收货地址',
icon: "none"
})
return;
}
app.orderTicket.reserveTime = that.data.outTime;
var ticket_deliver = {};
ticket_deliver.id = "";
ticket_deliver.orderTel = that.data.receiveMobile;
ticket_deliver.name = that.data.receiveName;
ticket_deliver.address = that.data.receiveAddress;
ticket_deliver.latitude = that.data.address.latitude;
ticket_deliver.longitude = that.data.address.longitude;
ticket_deliver.deliverType = that.data.deliverType;
ticket_deliver.deliverFee = that.data.deliverFee;
app.orderTicket.ticket_deliver = ticket_deliver;
}
if (that.data.sumMoney == 0) {
if (app.globalData.deliverMoney > 0 && app.orderTicket.busMode == 3) {
wx.showToast({
title: '不满足外卖起送金额 ¥' + app.globalData.deliverMoney + ' 元 ',
icon: "none"
})
return;
}
that.cy_tapWxPay();
} else {
//计算会员卡支付优惠价格
var memberPayDiscountsAmount = 0;
var wechatPayDisCountsAmount = 0;
var discountsAmount = 0;
var jsonString = myOrder.getMemberCreateOrder();
console.error("jsonString ",jsonString)
if (that.data.memberPriceEnabled == 1) {
for (var map of jsonString.ticket_info) {
if (map.type == 3) {
discountsAmount = parseFloat(map.discountMoney);
}
}
if (app.globalData.deliverMoney > 0 && app.orderTicket.busMode == 3) {
if (app.globalData.deliverMoney > (that.data.orgPrice - that.data.deliverFee)) {
wx.showToast({
title: '不满足外卖起送金额 ¥' + app.globalData.deliverMoney + ' 元 ',
icon: "none"
})
return;
}
}
} else {
if (app.globalData.deliverMoney > 0 && app.orderTicket.busMode == 3) {
if (app.globalData.deliverMoney > (that.data.orgPrice - that.data.deliverFee)) {
wx.showToast({
title: '不满足外卖起送金额 ¥' + app.globalData.deliverMoney + ' 元 ',
icon: "none"
})
return;
}
}
}
var actualAmount = 0;
if (that.data.promotionReceivable > 0) {
//判断优惠为0元的情况
if (discountsAmount >= that.data.promotionReceivable) {
//判断是否是堂食打包
if (that.data.busMode == 1 && that.data.isBoxFee == 1) {
actualAmount = parseFloat(app.orderTicket.boxFee);
}
//判断是否是外卖
if (that.data.busMode == 3) {
actualAmount = (parseFloat(app.orderTicket.boxFee) + parseFloat(app.orderTicket.deliverFee)).toFixed(2);
}
} else {
actualAmount = (parseFloat(that.data.promotionReceivable) - parseFloat(discountsAmount)).toFixed(2)
}
} else {
actualAmount = parseFloat(that.data.sumMoney - discountsAmount).toFixed(2);
}
that.setData({
payFlag: false,
discountsAmount: parseFloat(discountsAmount).toFixed(2),
actualAmount: actualAmount,
});
}
if (that.data.is_member_pay == "1") {
app.orderTicket.isMember = 1;
}
app.globalData.isEnablePromotion = that.data.isEnablePromotion;
}
}, function (err) {
wx.showToast({
title: '网络连接失败,请检查',
icon: "none"
})
});
},
/**
* 零元支付上传
*/
cy_uploadOrder: function () {
console.log("支付上传");
var that = this;
var saleDate = app.utils.getFormatTime(new Date(), 1);
var jsonString = myOrder.getMemberOrder();
jsonString.paid = 0;
jsonString.money = 0;
jsonString.cardNo = "";
jsonString.payNo = "";
jsonString.payDate = saleDate;
jsonString.voucherNo = "";
jsonString.payTypeNo = "02"; // 付款方式编号
jsonString.payType = "会员支付"; //会员卡支付
jsonString.memo = "会员支付"; //
console.log(jsonString);
var params = {
method: "program.order.pay",
programId: app.globalData.programId, //
openId: app.openId, //
busNo: app.orderTicket.tradeNo, //
jsonString: JSON.stringify(jsonString)
};
var ignores = ["programId", "openId", "jsonString"];
app.jsapi.api(app.globalData.appKey, app.globalData.appSecret, app.globalData.serverUrl).ajax(params, ignores, function (json) {
console.log(json);
that.setData({
hidden: true
})
var data = json.data;
if (data.status == 1) {
var result = data.data;
console.log(result);
if (that.data.coupon) {
that.cy_elecCouponConsume(app.orderTicket.tradeNo);
}
that.cy_programuserweixinformid(app.orderTicket.tradeNo,1);
if (app.orderTicket.busMode == 0 || app.orderTicket.busMode == 1) {
wx.redirectTo({
url: "paySuccess?init=true&no=" + result.busNo,
})
}
if (app.orderTicket.busMode == 3) {
wx.redirectTo({
url: "payOrderSuccess?init=true&no=" + result.busNo,
})
}
} else {
that.setData({
hidden: true
})
wx.showToast({
title: data.message,
icon: "none"
})
}
},
function (err) {
wx.showToast({
title: "网络连接失败,订单上传异常",
icon: "none"
})
that.setData({
hidden: true
})
});
},
/**
* 关闭支付
*/
cy_closePay: function () {
this.setData({
payFlag: true,
is_member_pay: 0
});
},
cy_pay_select: function (e) {
if (e.currentTarget.dataset.memberpay == "1") {
if (app.globalData.memberPayEnabled != 1) {
wx.showToast({
title: "暂不支持会员卡支付",
icon: "none",
})
return;
}
app.orderTicket.isMember = 1;
} else {
app.orderTicket.isMember = 0;
}
var jsonString = myOrder.getOrder();
var discountsAmount = 0;
for (var map of jsonString.ticket_info) {
if (map.type == 3) {
discountsAmount = parseFloat(map.discountMoney);
}
}
this.setData({
is_member_pay: e.currentTarget.dataset.memberpay,
})
},
cy_goto_pay: function () {
var that = this;
if (that.data.isSubmit) {
that.data.isSubmit = false;
var int = setTimeout(function () {
that.data.isSubmit = true;
}, 2500)
if (that.data.is_member_pay == 1) {
var cardInfo = wx.getStorageSync('cardInfo');
if (cardInfo) {
if (cardInfo.status != 1) {
wx.showToast({
title: '会员卡状态不合法,暂无法支付',
icon: 'none'
})
return;
}
}
if (app.globalData.memberPayEnabled == 1) {
that.cy_tapMemberPay();
} else {
wx.showToast({
title: '暂不支持会员卡支付',
icon: "none"
})
return;
}
} else {
that.cy_tapWxPay();
}
that.cy_closePay();
}
},
/**
* 会员支付
*/
cy_tapMemberPay: function () {
var cardInfo = wx.getStorageSync("cardInfo");
if (!cardInfo) {
wx.showModal({
title: '提示',
content: '您还不是会员去注册',
success: function (res) {
if (res.confirm) {
wx.navigateTo({
url: '../../tabbar/mine',
})
}
}
})
return;
}
if (this.data.actualAmount > cardInfo.availableAmount) {
wx.showModal({
title: '提示',
content: '会员余额不足去充值',
success: function (res) {
if (res.confirm) {
wx.navigateTo({
url: '../../mine/memberBalance',
})
}
}
})
return;
}
var that = this;
that.setData({
msg: "处理中...",
hidden: false
})
app.orderTicket.isMember = 1;
that.cy_createProgramOrder();
},
/**
* 微信支付
*/
cy_tapWxPay: function () {
this.setData({
msg: "处理中...",
hidden: false
})
app.orderTicket.isMember = 0;
this.cy_createProgramOrder();
},
/**
* 创建订单
*/
cy_createProgramOrder: function () {
var jsonString = myOrder.getOrder();
this.data.jsonString = jsonString;
console.log("小程序订单创建", jsonString);
var payMoney = parseFloat(jsonString.receivable).toFixed(2);
var sumMoney = 0;
for (var product of jsonString.order_product) {
if (product.isSuit != 3) {
sumMoney = parseFloat(sumMoney) + parseFloat(product.receivableTotal);
}
}
sumMoney = parseFloat(sumMoney).toFixed(2);
console.log(sumMoney, payMoney);
if (payMoney != sumMoney) {
wx.showToast({
title: '订单金额异常,删除后请重新下单',
icon: "none"
})
return;
}
if (!jsonString) {
wx.showToast({
title: '订单信息异常,删除后请重新下单',
icon: "none"
})
return;
}
var that = this;
//校验商品规格库存
if (jsonString.order_product && jsonString.order_product.length > 0) {
var productList = jsonString.order_product;
var specIds = "";
var counts = "";
var storeId = jsonString.storeId;
for (var productItem of productList) {
specIds += "," + productItem.specId;
counts += "," + productItem.count;
}
var params = {
method: "program.spec.stock.check",
storeId: storeId,
specIds: specIds,
// counts:counts
};
app.jsapi.api(app.globalData.appKey, app.globalData.appSecret, app.globalData.serverUrl).ajax(params, [], function (json) {
console.log(json);
var data = json.data;
if (data.status != 1) {
wx.showToast({
title: "" + data.message,
icon: "none",
duration: 3000
})
that.setData({
hidden: true
})
return;
} else {
if (app.orderTicket.isMember == 0) { // 微信支付
if (app.globalData.deductionRate && app.globalData.deductionRate != undefined) {
jsonString.deductionRate = app.globalData.deductionRate;
jsonString.chargeBack = that.data.sumMoney * app.globalData.deductionRate
}
} else if (app.orderTicket.isMember == 1) { //会员卡支付
if (app.globalData.memberDeductionRate && app.globalData.memberDeductionRate != undefined) {
jsonString.deductionRate = app.globalData.memberDeductionRate;
jsonString.chargeBack = jsonString.receivable * app.globalData.memberDeductionRate
}
}
console.log("--------------", jsonString);
var params = {
method: "program.order.create",
programId: app.globalData.programId,
openId: app.openId,
jsonString: JSON.stringify(jsonString)
};
var ignores = ["programId", "openId", "jsonString"];
// app.globalData.serverUrl
app.jsapi.api(app.globalData.appKey, app.globalData.appSecret, app.globalData.serverUrl).ajax(params, ignores, function (json) {
console.log(json);
var data = json.data;
if (data.status == 1) {
app.orderTicket.tradeNo = data.data.busNo;
app.orderTicket.ticketId = data.data.ticketId;
if (that.data.sumMoney == 0) {
that.cy_uploadOrder();
} else {
if (app.orderTicket.isMember == 0) { // 微信支付
that.cy_programWeixinPay();
} else if (app.orderTicket.isMember == 1) { //会员卡支付
that.cy_memberCreateVoucherNoPlatform();
}
}
} else {
that.setData({
hidden: true
})
wx.showToast({
title: data.message,
icon: "none"
})
}
}, function (err) {
that.setData({
hidden: true
})
});
}
}, function (err) {
wx.showToast({
title: '网络异常',
})
return;
});
} else {
wx.showToast({
title: '订单信息异常,下单商品有问题请重新下单',
icon: "none"
})
return;
}
},
/**
* 统一订单创建
*/
cy_memberCreateVoucherNoPlatform: function () {
var that = this;
that.setData({
msg: "处理中...",
hidden: false
})
var saleDate = app.utils.getFormatTime(new Date(), 1);
var goodsDetails = [];
for (var product of this.data.jsonString.order_product) {
var detail = {};
detail.goodsId = product.productId;
detail.name = product.productName;
detail.spec = product.specName;
detail.quantity = product.count;
detail.discountMoney = product.discountTotal;
detail.money = product.receivable;
goodsDetails.push(detail);
}
var money = parseInt((this.data.jsonString.receivable * 100).toFixed(2));
if (app.orderTicket.busMode == 1) {
money = parseInt((this.data.jsonString.receivable * 100).toFixed(2)) + parseInt((this.data.jsonString.packageFee * 100).toFixed(0));
}
if (app.orderTicket.busMode == 3) {
money = parseInt((this.data.jsonString.receivable * 100).toFixed(0)) + parseInt((this.data.jsonString.deliverFee * 100).toFixed(0)) + parseInt((this.data.jsonString.packageFee * 100).toFixed(0));
}
var params = {
method: "jw.trade.create.voucherNo.platform",
tradeNo: app.orderTicket.tradeNo,
saleDate: saleDate,
subject: app.openId,
totalAmount: money,
amount: money,
shopId: app.orderTicket.storeId,
cardNo: app.globalData.cardNo,
body: "微信小程序点餐",
sourceSign: "wechat",
goodsDetails: JSON.stringify(goodsDetails)
}
console.log(params);
var ignores = ["body", "sourceSign", "goodsDetails"];
// app.globalData.memberUrl
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(params, ignores,
function (json) {
console.log(json);
var data = json.data;
if (data.status == 1) {
var result = data.data;
that.cy_memberJwtradepayplatform(result);
} else {
wx.showToast({
title: data.errMessage,
icon: "none"
})
that.setData({
hidden: true
})
}
},
function (err) {
wx.showToast({
title: '网络连接异常',
icon: "none"
})
that.setData({
hidden: true
})
});
},
/**
* 会员卡统一收单支付
*/
cy_memberJwtradepayplatform: function (tempData) {
var that = this;
var cardInfo = wx.getStorageSync("cardInfo");
// 卡支付明细
var cardPayInfo = [{
prepayment: tempData.prepayment,
cardNo: app.globalData.cardNo,
amount: tempData.totalAmount
}];
// 支付方式
var pays = [{
payTypeNo: "02",
payType: "会员卡",
money: tempData.totalAmount,
}];
// 优惠券明细
var coupon = that.data.coupon;
var coupons = [];
console.log(coupon)
if (coupon) {
var tempCoupon = {
couponId: coupon.couponId,
couponType: coupon.cardType,
title: coupon.title,
code: coupon.code,
cash: parseFloat(coupon.amount)
}
coupons.push(tempCoupon);
}
var params = {
method: "jw.trade.pay.platform",
tradeVoucherNo: tempData.tradeVoucherNo, // 交易参考号(预支付生成的订单号)
tradeNo: tempData.tradeNo, // 商户订单号
totalAmount: tempData.totalAmount, // 订单总金额(精确到分)
payCardAmount: tempData.totalAmount, // 刷卡总金额(精确到分)
pointAmount: tempData.totalAmount, // 参与积分运算总金额(精确到分)
shopId: app.orderTicket.storeId, //
memberId: cardInfo.memberId,
mobile: cardInfo.mobile, //
realAmount: tempData.totalAmount, // 实收总金额(精确到分)
programId:app.globalData.programId,
// couponTotalAmount: "", // 优惠券总金额(精确到分)
cardPayInfo: JSON.stringify(cardPayInfo), // 卡支付明细(参数值为JSON字符串,如果刷卡金额大于0此项必填)
pays: JSON.stringify(pays), // 支付方式明细(参数值为JSON字符串)
}
if (coupon) {
params.coupons = JSON.stringify(coupons);
params.couponTotalAmount = coupon.reduceCost;
}
console.log("----------------", params);
var ignores = ["memberId", "mobile", "realAmount", "cardPayInfo", "pays", "coupons", "couponTotalAmount"];
// app.globalData.memberUrl
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(params, ignores,
function (json) {
console.log("会员卡统一支付");
console.log("------------", json);
var data = json.data;
if (data.status == 1) {
var result = data.data;
if (that.data.coupon) {
that.cy_elecCouponConsume(app.orderTicket.tradeNo);
}
that.cy_programuserweixinformid(app.orderTicket.tradeNo,1);
var data = JSON.stringify(result);
if (app.orderTicket.busMode == 0 || app.orderTicket.busMode == 1) {
wx.redirectTo({
url: "paySuccess?init=true&isMemberPay=true&tempData=" + data,
})
}
if (app.orderTicket.busMode == 3) {
wx.redirectTo({
url: "payOrderSuccess?init=true&isMemberPay=true&tempData=" + data,
})
}
} else {
wx.showToast({
title: data.message,
icon: "none",
})
that.setData({
hidden: true
})
}
},
function (err) {
console.log(err);
that.setData({
hidden: true
})
});
},
/**
* 支付点上传
*/
cy_programorderpay: function (tempData) {
console.log("支付上传");
var that = this;
var saleDate = app.utils.getFormatTime(new Date(), 1);
var payCard = tempData.cardList[0];
var jsonString = myOrder.getMemberOrder();
jsonString.paid = app.utils.getTwo(payCard.amount / 100);
jsonString.money = app.utils.getTwo(payCard.amount / 100);
jsonString.cardNo = payCard.cardNo;
jsonString.payNo = payCard.tradeVoucherNo;
jsonString.payDate = saleDate;
jsonString.voucherNo = tempData.tradeVoucherNo;
console.log(jsonString);
var params = {
method: "program.order.pay",
programId: app.globalData.programId, //
openId: app.openId, //
busNo: tempData.tradeNo, //
jsonString: JSON.stringify(jsonString)
};
var ignores = ["programId", "openId", "jsonString"];
app.jsapi.api(app.globalData.appKey, app.globalData.appSecret, app.globalData.serverUrl).ajax(params, ignores, function (json) {
console.log(json);
that.setData({
hidden: true
})
var data = json.data;
if (data.status == 1) {
var result = data.data;
console.log(result);
if (app.orderTicket.busMode == 0 || app.orderTicket.busMode == 1) {
wx.redirectTo({
url: "paySuccess?init=true&no=" + result.busNo,
})
}
if (app.orderTicket.busMode == 3) {
wx.redirectTo({
url: "payOrderSuccess?init=true&no=" + result.busNo,
})
}
} else {
that.setData({
hidden: true
})
wx.showToast({
title: data.message,
icon: "none"
})
}
},
function (err) {
wx.showToast({
title: "网络连接失败,订单上传异常",
icon: "none"
})
that.setData({
hidden: true
})
});
},
/**
* 获取微信JSAPI支付参数
*/
cy_programWeixinPay: function () {
var that = this;
var amount = parseFloat(that.data.jsonString.receivable);
if (app.orderTicket.busMode == 1) { // 外带
amount = parseFloat(that.data.jsonString.receivable) + parseFloat(this.data.jsonString.packageFee);
}
if (app.orderTicket.busMode == 3) { // 外卖
amount = parseFloat(this.data.jsonString.receivable) + parseFloat(this.data.jsonString.deliverFee) + parseFloat(this.data.jsonString.packageFee);
}
console.log("amount", amount);
var params = {
method: "program.weixin.pay",
storeId: app.orderTicket.storeId,
programId: app.globalData.programId,
appid: app.globalData.appid,
openId: app.openId,
tradeNo: app.orderTicket.tradeNo,
amount: parseFloat(amount).toFixed(2),
}
console.log("获取微信JSAPI支付参数", params);
var ignores = ["appid", "openId", "tradeNo", "amount"];
app.jsapi.api(app.globalData.appKey, app.globalData.appSecret, app.globalData.serverUrl).ajax(params, ignores, function (json) {
console.log(json);
var data = json.data;
if (data.status == 1) {
var param = data.data;
that.setData({
hidden: true
})
that.cy_wxpay(param);
} else {
wx.showToast({
title: data.message,
icon: "none"
})
that.setData({
hidden: true
})
}
}, function (err) {
console.log(err);
that.setData({
hidden: true
})
});
},
/**
* 拉起微信支付
*/
cy_wxpay: function (param) {
console.log(param);
var that = this;
wx.requestPayment({
'timeStamp': param.timeStamp,
'nonceStr': param.nonceStr,
'package': param.package,
'signType': param.signType,
'paySign': param.paySign,
success: function (res) {
console.log("cy_wxpay-success", res);
var num = 0;
that.data.inter = setInterval(function () {
if (num < 25) {
that.cy_queryWXPayReult();
} else {
clearInterval(that.data.inter);
}
}, 300)
},
fail: function (res) {
app.msg.showMsg("提示", "微信支付失败");
},
complete: function (res) {
that.setData({
hidden: true,
});
console.log("cy_wxpay-complete", res);
}
})
},
/**
* 查询微信支付结果
*/
cy_queryWXPayReult: function () {
var that = this;
var params = {
method: "program.weixin.pay.query",
storeId: app.orderTicket.storeId,
programId: app.globalData.programId,
tradeNo: app.orderTicket.tradeNo
}
console.log(params);
var ignores = ["tradeNo"];
app.jsapi.api(app.globalData.appKey, app.globalData.appSecret, app.globalData.serverUrl).ajax(params, ignores, function (json) {
console.log(json);
var data = json.data;
if (data.status == 1) {
if (that.data.coupon) {
that.cy_elecCouponConsume(app.orderTicket.tradeNo);
}
that.cy_programuserweixinformid(app.orderTicket.tradeNo,2);
if (app.orderTicket.busMode == 0 || app.orderTicket.busMode == 1) {
clearInterval(that.data.inter);
wx.redirectTo({
url: 'paySuccess?init=true&no=' + app.orderTicket.tradeNo,
})
return;
}
if (app.orderTicket.busMode == 3) {
wx.redirectTo({
url: "payOrderSuccess?init=true&no=" + app.orderTicket.tradeNo,
})
}
} else {
console.error(json);
}
}, function (err) {
console.log(err);
});
},
/**
* 提交fromId
*/
cy_programuserweixinformid: function (busNo,consumeType) {
var that=this;
wx.requestSubscribeMessage({
tmplIds:app.globalData.diancanTemplateMsgList,
success: function (params) {
var amount = 0;
var type = "";
if (consumeType== 1) {
amount = that.data.actualAmount;
type = "会员卡支付"
} else {
amount = that.data.sumMoney;
type = "微信支付";
}
var data = {
'openId': wx.getStorageSync('openId'),
'formId': that.data.formId,
'wid': app.globalData.memberWid,
'sendType': "consume",
'method': 'get.smweixin.formId',
'amount': amount,
'busNo': busNo,
'message': that.data.storeName+"("+type+")",
'memberId': wx.getStorageSync('memberId')
}
var ignores = ["message"];
// app.globalData.memberUrl
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret,app.globalData.memberUrl ).ajax(data, ignores,
function (json) { },
function (error) { })
},
fail: function (params) {
console.error(" error ", params)
}
})
},
/****************************************************************************** */
cy_clear_gift_product: function (list) {
//先清除上次的赠送商品
if (list && list.length > 0) {
var tagList = [];
for (var index = 0; index < list.length; index++) {
if (!list[index].giftProduct || list[index].giftProduct != "1") {
tagList.push(list[index]);
}
}
this.setData({
list: tagList
});
}
//清除订单商品
app.orderTicket.orderList = tagList;
return tagList;
},
/**
* 使用优惠券
*/
cy_selectUseCoupon: function (coupon) {
console.error("券-----", coupon)
//先清除上次的赠送商品
var list = this.data.list;
list = this.cy_clear_gift_product(list);
console.log(app.orderTicket);
if (coupon.cardType == "CASH" && parseFloat(coupon.amount) > app.orderTicket.receivable) {
coupon.amount = app.orderTicket.amount;
}
if (coupon.cardType == "GIFT" && coupon.giftType == "2") {
var gift = JSON.parse(coupon.gift);
if (coupon.totalBoxPrice && coupon.totalBoxPrice > 0) {
app.orderTicket.boxFee = (parseFloat(app.orderTicket.boxFee) + parseFloat(coupon.totalBoxPrice)).toFixed(2);
this.setData({
boxFee: app.orderTicket.boxFee
})
}
list = myOrder.giftProductAddOrderProduct(gift, list, coupon);
this.setData({
list: list
});
coupon.amount = 0;
}
app.orderTicket.coupon = coupon;
this.setData({
coupon: coupon
});
},
//
onShow: function () {
//this.cy_selectCardInfo();
app.orderTicket.busMode = wx.getStorageSync('mode') || app.orderTicket.busMode;
log.info('app.orderTicket.busMode: ' + app.orderTicket.busMode);
this.setData({
busMode: app.orderTicket.busMode
});
var coupon = this.data.coupon;
var reduceCost = 0;
var sumMoney = parseFloat(parseFloat(app.orderTicket.receivable).toFixed(2));
var promotionReceivable = parseFloat(parseFloat(app.orderTicket.promotionReceivable).toFixed(2));
if (coupon) {
if (coupon.cardType == "CASH") {
reduceCost = parseFloat(coupon.amount);
} else if (coupon.cardType == "DISCOUNT") {
reduceCost = parseFloat(app.orderTicket.receivable) * (1 - parseFloat(coupon.amount) / 10);
}
//判断促销商品使用了兑换券 且兑换券的金额大于促销价直接价格为0 并且券的金额为促销价金额
if (app.orderTicket.promotionFlag == 1 && app.orderTicket.promotionReceivable > 0) {
if (reduceCost >= app.orderTicket.promotionReceivable) {
reduceCost = parseFloat(app.orderTicket.promotionReceivable);
sumMoney = 0;
coupon.amount = reduceCost;
}
}
}
if (this.data.busMode == 0) { // 堂食
if (app.orderTicket.promotionFlag == 1) {
promotionReceivable = parseFloat((promotionReceivable - reduceCost).toFixed(2));
} else sumMoney = parseFloat((sumMoney - reduceCost).toFixed(2));
} else if (this.data.busMode == 1) { // 打包
if (app.globalData.takeOutBoxFeeFlag == 1 || this.data.isBoxFee == 1) {
if (this.data.isBoxFee == 1) {
sumMoney = (sumMoney + parseFloat(app.orderTicket.boxFee)).toFixed(2);
if (app.orderTicket.promotionFlag == 1) {
promotionReceivable = (promotionReceivable + parseFloat(app.orderTicket.boxFee)).toFixed(2);
}
}
this.setData({
boxFee: parseFloat(app.orderTicket.boxFee)
});
} else {
if (this.data.isBoxFee != 1) {
this.setData({
boxFee: 0
});
}
}
if (app.orderTicket.promotionFlag == 1) {
promotionReceivable = parseFloat((promotionReceivable - reduceCost).toFixed(2));
} else sumMoney = parseFloat((sumMoney - reduceCost).toFixed(2));
} else if (this.data.busMode == 3) { // 外卖
if (app.orderTicket.promotionFlag == 1) {
var store = wx.getStorageSync("store");
var deliverFee = store.deliverFee;
if (!coupon) {
sumMoney = parseFloat(app.orderTicket.receivable) + parseFloat(app.orderTicket.boxFee) + parseFloat(deliverFee);
}
promotionReceivable = (promotionReceivable + parseFloat(app.orderTicket.boxFee) + parseFloat(app.orderTicket.deliverFee)).toFixed(2);
promotionReceivable = parseFloat((promotionReceivable - reduceCost).toFixed(2));
} else {
sumMoney = (sumMoney + parseFloat(app.orderTicket.boxFee) + parseFloat(app.orderTicket.deliverFee)).toFixed(2);
sumMoney = parseFloat((sumMoney - reduceCost).toFixed(2));
}
}
this.setData({
deliverFee: parseFloat(app.orderTicket.deliverFee),
reduceCost: reduceCost.toFixed(2),
sumMoney: sumMoney,
tableName: app.orderTicket.tableName,
tableNo: app.orderTicket.tableNo,
memberPriceEnabled: app.globalData.memberPriceEnabled,
promotionReceivable: promotionReceivable
});
},
/**
* 查询卡信息
*/
cy_selectCardInfo: function () {
var that = this;
card.wx_cardInfo(app.globalData.cardNo, function (json) {
console.log(json);
var result = json.data;
if (result.status == 1) {
var cardInfo = result.data;
wx.setStorageSync("cardInfo", cardInfo);
app.globalData.cardInfo = cardInfo;
that.setData({
availableAmount: cardInfo.availableAmount,
totalPoint: cardInfo.totalPoint,
level: cardInfo.level,
couponNum: cardInfo.couponNum
});
} else {
wx.showModal({
title: '提示',
content: '错误',
});
}
}, function (err) {
wx.showModal({
title: '提示',
content: '网络链接失败',
})
});
},
/**
* 使用代金券
*/
cy_useCoupon: function () {
if (this.data.couponList.length == 0) {
wx.showToast({
title: '暂无可用优惠券',
icon: "none"
})
return;
}
if (this.data.isEnablePromotion == 1) {
wx.showModal({
title: "促销价不能跟其他优惠叠加是否继续使用 ? ",
success(res) {
if (!res.confirm) {
return
} else {
this.setData({
isEnablePromotion: 0
});
this.cy_click_promotion();
}
}
})
}
var url = "useCoupon?money=" + this.data.sumMoney + "&list=" + JSON.stringify(this.data.couponList);
wx.navigateTo({
url: url,
})
},
/**
* 备注
*/
cy_inputComment: function () {
var url = "userComment?comment=" + this.data.comment + "&busMode=" + this.data.busMode;
wx.navigateTo({
url: url,
})
},
/**
* 优惠券核销
*/
cy_elecCouponConsume: function (tradeNo) {
var that = this;
var coupon = that.data.coupon;
var worth = coupon.reduceCost;
if (worth && worth > 0) {
worth = parseInt(coupon.reduceCost);
} else {
worth = 0;
}
var params = {
"method": "elec.coupon.consume.mobile",
"tradeNo": tradeNo,
"code": coupon.code,
"cardNo": app.globalData.cardNo,
"openid": app.openId,
"type": "zizhu",
"amount": parseInt(app.orderTicket.amount * 100), // 消费金额
"worth": worth, // 优惠券价值
"shopNo": app.orderTicket.storeNo, //
"posNo": app.globalData.posNo, //
"workerNo": app.globalData.workerNo, //
"sourceSign": "wechat", //
};
console.log(params);
var ignores = ["tradeNo"];
//
app.jsapi.memberApi(app.globalData.memberAppKey, app.globalData.memberAppSecret, app.globalData.memberUrl).ajax(params, ignores,
function (json) {
console.log("优惠券核销", json);
},
function (error) {
that.setData({
hidden: true,
});
}
);
},
onHide: function () { },
onUnload: function () {
var that = this;
clearInterval(that.data.inter);
app.orderTicket.busMode = that.data.busMode;
that.data.busMode = 0;
that.data.isBoxFee = 0;
app.globalData.userIsFee = 0;
that.cy_clear_gift_product(that.data.list);
}
})