|
|
|
var app = getApp();
|
|
|
|
var util = require('../../utils/util.js')
|
|
|
|
var WXAPI = require('../../utils/server.js');
|
|
|
|
Page({
|
|
|
|
data: {
|
|
|
|
ispayOrder: false,
|
|
|
|
pays: [],
|
|
|
|
itemData: {},
|
|
|
|
userId: 0,
|
|
|
|
paytype: 'wxPay', //支付方式
|
|
|
|
cartId: 0,
|
|
|
|
addrId: 0, //收货地址//测试--
|
|
|
|
btnDisabled: false,
|
|
|
|
productData: [],
|
|
|
|
address: {},
|
|
|
|
total: 0,
|
|
|
|
yuan: 0, //临时储存先前的总价格
|
|
|
|
d_yuan: 0, //抵扣余额显示
|
|
|
|
vprice: 0,
|
|
|
|
vid: 0,
|
|
|
|
paswnum: 0,
|
|
|
|
reduce_money: 0, //优惠金额
|
|
|
|
addemt: 1, //加入次数
|
|
|
|
vou: [],
|
|
|
|
checked: false,
|
|
|
|
score: 0, //用户积分
|
|
|
|
allowscore: 0, //当前订单可用积分
|
|
|
|
checked01: false,
|
|
|
|
allow: 0,
|
|
|
|
pass_num: 4,
|
|
|
|
remind: true,
|
|
|
|
title: '请输入支付密码',
|
|
|
|
xsmoney: true, //控制显示金额
|
|
|
|
money: false,
|
|
|
|
pay_xs: true,
|
|
|
|
pages_sx: true,
|
|
|
|
dz_stu: false,
|
|
|
|
productAry: '',
|
|
|
|
},
|
|
|
|
go: function (e) {
|
|
|
|
console.log(e);
|
|
|
|
this.setData({
|
|
|
|
dz_stu: true,
|
|
|
|
});
|
|
|
|
var url = e.currentTarget.dataset.url
|
|
|
|
wx.navigateTo({
|
|
|
|
url: url
|
|
|
|
})
|
|
|
|
},
|
|
|
|
//下拉刷新
|
|
|
|
onPullDownRefresh: function () {
|
|
|
|
wx.showNavigationBarLoading() //在标题栏中显示加载
|
|
|
|
this.Settlement();
|
|
|
|
wx.hideNavigationBarLoading() //完成停止加载
|
|
|
|
wx.stopPullDownRefresh() //停止下拉刷新
|
|
|
|
},
|
|
|
|
//调取分享
|
|
|
|
onShareAppMessage: function (res) {
|
|
|
|
var that = this;
|
|
|
|
var id = that.data.productId;
|
|
|
|
var title = that.data.title;
|
|
|
|
|
|
|
|
return {
|
|
|
|
title: title,
|
|
|
|
path: '/product/detail?productId=' + id,
|
|
|
|
success: function (res) {
|
|
|
|
console.log('转发成功')
|
|
|
|
},
|
|
|
|
fail: function (res) {
|
|
|
|
console.log('转发失败')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onLoad: function (options) {
|
|
|
|
console.log(app.globalData)
|
|
|
|
console.log(options)
|
|
|
|
var that = this;
|
|
|
|
that.get_plug();
|
|
|
|
|
|
|
|
var uid = app.globalData.userInfo.openid; // 微信id
|
|
|
|
var plug_ins = app.globalData.userInfo.plug_ins; // 插件
|
|
|
|
var coupon = app.globalData.userInfo.coupon; // 优惠券状态
|
|
|
|
var wallet = app.globalData.userInfo.wallet; // 钱包状态
|
|
|
|
this.setData({
|
|
|
|
cartId: options.cartId, // 购物车id
|
|
|
|
num1: options.num,
|
|
|
|
type: options.type ? options.type : 0, //(1.直接结算 0购物车结算)
|
|
|
|
bgcolor: '#FF6347', // 背景颜色
|
|
|
|
userId: uid, // 微信id
|
|
|
|
plug_ins: plug_ins, // 插件
|
|
|
|
coupon: coupon, // 优惠券状态
|
|
|
|
wallet: wallet, // 钱包状态
|
|
|
|
});
|
|
|
|
this.Settlement(options);
|
|
|
|
},
|
|
|
|
//页面加载完成函数
|
|
|
|
onReady: function () {
|
|
|
|
|
|
|
|
},
|
|
|
|
onUnload() { //onUnload监听页面卸载
|
|
|
|
var that = this;
|
|
|
|
// util.getUesrBgplus(that, app, true) //刷新显示购物车数量
|
|
|
|
// util.getUesrBgplus(that, app, false) //刷新显示购物车数量
|
|
|
|
var cartid = that.data.cartId
|
|
|
|
var type = that.data.type
|
|
|
|
if (type == 1) { //直接结算离开页面清除购物车数量
|
|
|
|
wx.request({
|
|
|
|
url: app.d.ceshiUrl + '&action=product&m=delcart',
|
|
|
|
method: 'post',
|
|
|
|
data: {
|
|
|
|
carts: that.data.cartId, // 购物车id
|
|
|
|
},
|
|
|
|
header: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
|
},
|
|
|
|
success: function (res) {
|
|
|
|
var status = res.data.status;
|
|
|
|
if (status == 1) {
|
|
|
|
that.setData({
|
|
|
|
remind: false
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
that.setData({
|
|
|
|
remind: true
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function (e) {
|
|
|
|
wx.showToast({
|
|
|
|
title: '网络异常!',
|
|
|
|
duration: 2000
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onShow: function () {
|
|
|
|
var that = this;
|
|
|
|
// console.log(that.data.dz_stu)
|
|
|
|
// if (that.data.dz_stu) {
|
|
|
|
// that.setData({
|
|
|
|
// dz_stu: false,
|
|
|
|
// });
|
|
|
|
// // util.getUesrBgplus(that, app, false)
|
|
|
|
// wx.navigateTo({
|
|
|
|
// url: '../order/pay?cartId=' + that.data.cartId,
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if (that.data.pages_sx) {
|
|
|
|
// setTimeout(function () {
|
|
|
|
// that.Settlement();
|
|
|
|
// }, 500);
|
|
|
|
// }
|
|
|
|
|
|
|
|
},
|
|
|
|
// 进入结算页面
|
|
|
|
Settlement: function (options) {
|
|
|
|
var that = this;
|
|
|
|
WXAPI.settlement({
|
|
|
|
accessId: app.globalData.accessId,
|
|
|
|
storeId: 239,
|
|
|
|
cart_id: '',
|
|
|
|
coupon_id: 0,
|
|
|
|
vipSource: 0,
|
|
|
|
canshu: true,
|
|
|
|
product: options.product,
|
|
|
|
}).then(res => {
|
|
|
|
if (res.code == 200) {
|
|
|
|
//设置购物车刷新
|
|
|
|
app.d.purchase = 1;
|
|
|
|
var data = res.data;
|
|
|
|
if (that.data.allow) {
|
|
|
|
data.coupon_money = Number(data.coupon_money) - Number(that.data.allow);
|
|
|
|
}
|
|
|
|
if (that.data.d_yuan) {
|
|
|
|
data.coupon_money = Number(data.coupon_money) - Number(that.data.user_money);
|
|
|
|
}
|
|
|
|
var pays = []
|
|
|
|
if (data.payment.bank_pay == 1) {
|
|
|
|
pays.push({'name':'公对公支付','icon':'/images/公对公.png'})
|
|
|
|
}
|
|
|
|
// if (data.payment.wallet_pay == 1) {
|
|
|
|
// pays.push({'name':'余额支付','icon':'/images/wx.png'})
|
|
|
|
// }
|
|
|
|
that.setData({
|
|
|
|
productAry: options.product,
|
|
|
|
addemt: data.addemt, // 是否有收货地址
|
|
|
|
address: data.address, // 收货地址
|
|
|
|
addrId: data.address.id ? data.address.id : '', // 收货地址id
|
|
|
|
productData: data.products, // 商品信息
|
|
|
|
total: data.total, // 总价
|
|
|
|
money: data.reduce_money, // 优惠券金额
|
|
|
|
coupon_money: Number(parseFloat(data.coupon_money).toFixed(3).slice(0, -1)), // 优惠后金额
|
|
|
|
user_money: data.user_money, // 用户余额
|
|
|
|
coupon_id: data.coupon_id, // 优惠券id
|
|
|
|
discount: data.discount, //控制优惠方式
|
|
|
|
scorebuy: data.grade_rate_amount, //积分消费规则
|
|
|
|
zhekou: data.preferential_amount ? data.preferential_amount : '', //会员折扣
|
|
|
|
freight: data.freight ? data.freight : 0, //运费
|
|
|
|
remind: false,
|
|
|
|
pays: pays,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
that.setData({
|
|
|
|
remind: true
|
|
|
|
});
|
|
|
|
wx.showToast({
|
|
|
|
title: res.message,
|
|
|
|
icon: 'none',
|
|
|
|
duration: 2000,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
// 点击优惠券
|
|
|
|
tapMainMenu: function (e) { // 获取当前显示的一级菜单标识
|
|
|
|
var coupon_id = e.target.id;
|
|
|
|
var taht = this;
|
|
|
|
var showModalStatus = taht.data.showModalStatus;
|
|
|
|
|
|
|
|
taht.getvou(coupon_id);
|
|
|
|
taht.setData({
|
|
|
|
showModalStatus: false
|
|
|
|
});
|
|
|
|
},
|
|
|
|
// 我的优惠券(可以使用的优惠券)
|
|
|
|
my_coupon: function () {
|
|
|
|
var that = this;
|
|
|
|
wx.request({
|
|
|
|
url: app.d.ceshiUrl + '&action=Coupon&m=my_coupon',
|
|
|
|
method: 'post',
|
|
|
|
data: {
|
|
|
|
openid: app.globalData.userInfo.openid,
|
|
|
|
cart_id: that.data.cartId, // 购物车id
|
|
|
|
},
|
|
|
|
header: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
|
},
|
|
|
|
success: function (res) {
|
|
|
|
var list = res.data.list;
|
|
|
|
that.setData({
|
|
|
|
list: list,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
error: function (e) {
|
|
|
|
wx.showToast({
|
|
|
|
title: '网络异常!',
|
|
|
|
duration: 2000
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
// 得到优惠券
|
|
|
|
getvou: function (id) {
|
|
|
|
var that = this;
|
|
|
|
wx.request({
|
|
|
|
url: app.d.ceshiUrl + '&action=Coupon&m=getvou',
|
|
|
|
method: 'post',
|
|
|
|
data: {
|
|
|
|
cart_id: that.data.cartId, // 购物车id
|
|
|
|
coupon_money: that.data.coupon_money, // 付款金额
|
|
|
|
openid: app.globalData.userInfo.openid, // 微信id
|
|
|
|
coupon_id: id, // 优惠券id
|
|
|
|
},
|
|
|
|
header: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded'
|
|
|
|
},
|
|
|
|
success: function (res) {
|
|
|
|
|
|
|
|
if (res.data.status == 0) {
|
|
|
|
wx.showToast({
|
|
|
|
title: res.data.err,
|
|
|
|
icon: 'none',
|
|
|
|
duration: 2000
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
that.setData({
|
|
|
|
coupon_id: res.data.id, // 优惠券id
|
|
|
|
money: res.data.money, // 优惠券金额
|
|
|
|
total: res.data.zong, // 总金额
|
|
|
|
coupon_money: res.data.coupon_money, // 优惠券后金额
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function (e) {
|
|
|
|
wx.showToast({
|
|
|
|
title: '网络异常!',
|
|
|
|
duration: 2000
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 选择支付方式
|
|
|
|
switchChange: function (e) {
|
|
|
|
console.log(e)
|
|
|
|
var that = this;
|
|
|
|
var check = e.currentTarget.dataset.check;
|
|
|
|
var index = e.currentTarget.dataset.index;
|
|
|
|
var value = e.detail.value;
|
|
|
|
var pays = that.data.pays[index];
|
|
|
|
var coupon_money = that.data.coupon_money;
|
|
|
|
var user_money = that.data.user_money;
|
|
|
|
if (value) {
|
|
|
|
pays.checked = true;
|
|
|
|
} else {
|
|
|
|
pays.checked = false;
|
|
|
|
}
|
|
|
|
var pay_type = that.data.pays;
|
|
|
|
var i = 0;
|
|
|
|
var one_pay = '';
|
|
|
|
console.log(pay_type)
|
|
|
|
for (var j = 0; j < pay_type.length; j++) {
|
|
|
|
if (pay_type[j].checked) {
|
|
|
|
i += 1;
|
|
|
|
one_pay = pay_type[j].value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == 0) {
|
|
|
|
that.setData({
|
|
|
|
paytype: false,
|
|
|
|
});
|
|
|
|
} else if (i == 1) {
|
|
|
|
//单选支付方式时 设置支付方式
|
|
|
|
//如果是之前点击了钱包支付 需要还原金额
|
|
|
|
var yuan = that.data.yuan;
|
|
|
|
if (yuan) {
|
|
|
|
that.setData({
|
|
|
|
paytype: one_pay,
|
|
|
|
coupon_money: yuan,
|
|
|
|
d_yuan: false,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
that.setData({
|
|
|
|
paytype: one_pay,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (one_pay == 'wallet_Pay') {
|
|
|
|
if (user_money < coupon_money) {
|
|
|
|
wx.showToast({
|
|
|
|
title: '余额不足,请更换支付方式或选择组合支付!',
|
|
|
|
icon: 'none',
|
|
|
|
duration: 2000,
|
|
|
|
});
|
|
|
|
|
|
|
|
for (var j = 0; j < pay_type.length; j++) {
|
|
|
|
if (pay_type[j].value == 'wxPay') {
|
|
|
|
pay_type[j].checked = true;
|
|
|
|
} else {
|
|
|
|
pay_type[j].checked = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
that.setData({
|
|
|
|
paytype: false,
|
|
|
|
pays: pay_type
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (i == 2) {
|
|
|
|
console.log('两种支付方式')
|
|
|
|
//两种支付方式时当支付金额大于用户余额 则抵扣全部余额 并验证 //否则 直接选择默认钱包支付
|
|
|
|
if (Number(user_money) >= Number(coupon_money)) {
|
|
|
|
for (var j = 0; j < pay_type.length; j++) {
|
|
|
|
if (pay_type[j].value == check) {
|
|
|
|
pay_type[j].checked = true;
|
|
|
|
} else {
|
|
|
|
pay_type[j].checked = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//设置支付方式和点击默认
|
|
|
|
that.setData({
|
|
|
|
paytype: check,
|
|
|
|
pays: pay_type
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// 组合支付
|
|
|
|
wx.showModal({
|
|
|
|
title: '组合支付',
|
|
|
|
content: '是否使用余额抵用?',
|
|
|
|
success: function (res) {
|
|
|
|
if (res.confirm) {
|
|
|
|
//组合支付 替换数据
|
|
|
|
var price = Number(coupon_money) - Number(user_money); //防止出现小数点后多余2位以上 .toFixed(2)
|
|
|
|
that.setData({
|
|
|
|
paytype: 'wxPay',
|
|
|
|
yuan: coupon_money,
|
|
|
|
coupon_money: price.toFixed(2),
|
|
|
|
d_yuan: user_money,
|
|
|
|
});
|
|
|
|
} else if (res.cancel) {
|
|
|
|
//设置支付方式和点击默认
|
|
|
|
for (var j = 0; j < pay_type.length; j++) {
|
|
|
|
if (pay_type[j].value == 'wxPay') {
|
|
|
|
pay_type[j].checked = true;
|
|
|
|
} else {
|
|
|
|
pay_type[j].checked = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
that.setData({
|
|
|
|
paytype: false,
|
|
|
|
pays: pay_type
|
|
|
|
});
|
|
|
|
//安全提示
|
|
|
|
wx.showToast({
|
|
|
|
title: '已取消',
|
|
|
|
icon: 'none',
|
|
|
|
duration: 2000,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// 提交订单支付
|
|
|
|
createProductOrderByWX: function (e) {
|
|
|
|
var that = this;
|
|
|
|
// if (that.data.ispayOrder) {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
// that.setData({
|
|
|
|
// ispayOrder: true
|
|
|
|
// })
|
|
|
|
|
|
|
|
// var paytype = that.data.paytype;
|
|
|
|
// if (paytype) {
|
|
|
|
// that.setData({
|
|
|
|
// paytype: paytype,
|
|
|
|
// });
|
|
|
|
// } else {
|
|
|
|
// wx.showToast({
|
|
|
|
// title: '已为您选择默认支付方式',
|
|
|
|
// icon: 'none',
|
|
|
|
// duration: 2000,
|
|
|
|
// });
|
|
|
|
// //当都没有选中时 循环找到默认的支付方式 在设置支付方式数据
|
|
|
|
// var pays = that.data.pays,
|
|
|
|
// j = 0;
|
|
|
|
// for (j = 0; j < pays.length; j++) {
|
|
|
|
// if (pays[j].value == 'wxPay') {
|
|
|
|
// pays[j].checked = true;
|
|
|
|
// } else {
|
|
|
|
// pays[j].checked = false;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
// that.setData({
|
|
|
|
// pays: pays,
|
|
|
|
// paytype: 'wxPay',
|
|
|
|
// });
|
|
|
|
|
|
|
|
// paytype = 'wxPay';
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// that.setData({
|
|
|
|
// form_id: e.detail.formId,
|
|
|
|
// });
|
|
|
|
var address = that.data.address;
|
|
|
|
if (address) {
|
|
|
|
// // 收货地址存在
|
|
|
|
// if (paytype == 'wallet_Pay') {
|
|
|
|
// if (that.data.pay_xs) {
|
|
|
|
// wx.showModal({
|
|
|
|
// title: '余额支付',
|
|
|
|
// content: '是否使用余额支付?',
|
|
|
|
// success: function (res) {
|
|
|
|
// if (res.confirm) {
|
|
|
|
// //组合支付 替换数据
|
|
|
|
// that.createProductOrder();
|
|
|
|
// console.log('用户点击确定');
|
|
|
|
// } else if (res.cancel) {
|
|
|
|
// that.setData({
|
|
|
|
// ispayOrder: false
|
|
|
|
// })
|
|
|
|
// wx.hideLoading()
|
|
|
|
// console.log('用户点击取消')
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
// } else {
|
|
|
|
// wx.showModal({
|
|
|
|
// title: '订单提交',
|
|
|
|
// content: '是否使用消费金支付?',
|
|
|
|
// success: function (res) {
|
|
|
|
// if (res.confirm) {
|
|
|
|
// //组合支付 替换数据
|
|
|
|
// that.createProductOrder();
|
|
|
|
// console.log('用户点击确定');
|
|
|
|
// } else if (res.cancel) {
|
|
|
|
// this.setData({
|
|
|
|
// ispayOrder: false
|
|
|
|
// })
|
|
|
|
// wx.hideLoading()
|
|
|
|
// console.log('用户点击取消')
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
// }
|
|
|
|
// } else {
|
|
|
|
// that.createProductOrder();
|
|
|
|
// }
|
|
|
|
that.createProductOrder();
|
|
|
|
} else {
|
|
|
|
// 没有收货地址
|
|
|
|
wx.showToast({
|
|
|
|
title: "请添加收货地址!",
|
|
|
|
icon: 'none',
|
|
|
|
duration: 3000
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// 确认订单
|
|
|
|
createProductOrder: function () {
|
|
|
|
var that = this;
|
|
|
|
WXAPI.orderPayment({
|
|
|
|
accessId: app.globalData.accessId,
|
|
|
|
storeId: 239,
|
|
|
|
storeType: 1,
|
|
|
|
cart_id: '',
|
|
|
|
address_id: that.data.addrId,
|
|
|
|
coupon_id: 0,
|
|
|
|
remarks: '',
|
|
|
|
vipSource: 0,
|
|
|
|
product: that.data.productAry,
|
|
|
|
pay_type: 'bank_pay',
|
|
|
|
}).then(res => {
|
|
|
|
if (res.code == 200) {
|
|
|
|
var data = res.data
|
|
|
|
var jsonstr = JSON.stringify(data)
|
|
|
|
var result = {
|
|
|
|
orderStr: jsonstr,
|
|
|
|
price: data.total
|
|
|
|
}
|
|
|