//index.js
//获取应用实例
const app = getApp()
Page({
data: {
// 统计商品数量和价格
orderCount: {
num: 0,
money: 0
},
bottomFlag: false,
// 提交的订单
orders: true,
items: []
},
// 点击对应菜单添加按钮
del: function (event) {
let that = this;
let id = event.target.dataset.id;
let index = event.target.dataset.index;
let param = this.data.items[index];
if(param.num > 0){
param.num--; // 每点一次减少1
} else {
param.num = 0; // 最低为0
}
// 改变添加按钮的状态
this.data.items.splice(index, 1, param);
that.setData({
items: this.data.items
});
let money = 0;
let num = 0;
// 将已经确定总价格和数量求和
this.data.items.forEach(item => {
money += item.price * item.num;
num += item.num;
});
let orderCount = {
num,
money
}
// 设置显示对应的总数和全部价钱
this.setData({
orderCount
});
},
// 点击对应菜单添加按钮
add: function(event) {
let that = this;
let id = event.target.dataset.id;
let index = event.target.dataset.index;
let param = this.data.items[index];
let subOrders = []; // 购物单列表存储数据
param.num++; // 每点一次增加1
// 改变添加按钮的状态
console.log(param);
this.data.items.splice(index, 1, param);
that.setData({
items: this.data.items
});
let money = 0;
let num = 0;
// 将已经确定总价格和数量求和
this.data.items.forEach(item => {
money += item.price*item.num;
num += item.num;
});
let orderCount = {
num,
money
}
// 设置显示对应的总数和全部价钱
this.setData({
orderCount
});
},
// 点击结账按钮
pay: function() {
let that = this;
let str = '选中' + that.data.orderCount.num + '件商品,共' + that.data.orderCount.money + '元,是否要支付?'
wx.showModal({
title: '提示',
content: str,
success: function (res) {
// 至少选中一个商品才能支付
if (that.data.orderCount.num !== 0){
if (res.confirm) {
console.log('确定支付');
return false;
// 打开扫码功能
// wx.scanCode({
// onlyFromCamera: true,
// success: (res) => {
// wx.redirectTo({
// url: '../pay/pay'
// });
// }
// });
wx.request({
url: '支付接口地址',
data: { 'open_id': wx.getStorageSync('open_id'), 'money': that.data.orderCount.money},
success: function (res){
if(res.code==200){
wx.requestPayment({
'timeStamp': res.data.timeStamp,
'nonceStr': res.data.nonceStr,
'package': res.data.package,
'signType': 'MD5',
'paySign': res.data.paySign,
success: function (res) {
wx.showToast({
title: '支付成功',
icon: 'success',
duration: 2000,
success: function () {
}
})
},
fail: function (res) {
},
complete: function (res) {
}
})
}
}
})
} else if (res.cancel) {
console.log('用户点击取消')
}
} else {
wx.showToast({
title: '您未选中任何商品',
icon: 'none',
duration: 2000
})
}
}
})
},
onLoad: function() {
let that = this;
// 取出订单传过来的数据
wx.getStorage({
key: 'orders',
success: function (res) {
that.setData({
items: res.data
});
// 价格统计汇总
let money = 0;
let num = res.data.length;
res.data.forEach(item => {
money += (item.price*item.num); // 总价格求和
});
let orderCount = {
num,
money
}
// 设置显示对应的总数和全部价钱
that.setData({
orderCount
});
}
})
}
})