441 lines
12 KiB
JavaScript
441 lines
12 KiB
JavaScript
// pages/cartPay/cartPay.js
|
||
const app = getApp();
|
||
Page({
|
||
|
||
/**
|
||
* 页面的初始数据
|
||
*/
|
||
data: {
|
||
goods: [],
|
||
reduceType: false,//优惠方式
|
||
addressType: false,//收货方式
|
||
reduceTypeXz: 0,//0不选 1现金券 2折扣券
|
||
freight: 0,//运费
|
||
discount: 0,//优惠
|
||
payActual: 0,//实际支付
|
||
note: '',
|
||
addressId: '',//地址ID
|
||
address: '选择收货方式',//地址
|
||
province: '',
|
||
orderPick: 2,//1自提 2物流
|
||
isCar: 0,//购物车进入
|
||
|
||
date: '',
|
||
time: '',
|
||
startTime: '',
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面显示
|
||
*/
|
||
onShow: function () {
|
||
if (this.data.orderPick == 2) {
|
||
//收货地址
|
||
if (this.data.total && this.data.province != '') {
|
||
//订单个人信息获取
|
||
wx.showLoading({
|
||
title: '加载中',
|
||
})
|
||
wx.request({
|
||
url: app.globalData.ip + '/wisdommining/api/order/getUserInfo',
|
||
method: 'GET',
|
||
header: {
|
||
"content-type": "application/x-www-form-urlencoded",
|
||
},
|
||
data: {
|
||
userId: app.globalData.userId
|
||
},
|
||
success: res => {
|
||
wx.hideLoading();
|
||
console.log(res)
|
||
if (res.data.code == 1) {
|
||
let dataList = res.data.value;
|
||
this.setData({
|
||
dataList: dataList,
|
||
})
|
||
//运费获取
|
||
wx.request({
|
||
url: app.globalData.ip + '/wisdommining/api/order/getFreight',
|
||
method: 'GET',
|
||
header: {
|
||
"content-type": "application/x-www-form-urlencoded",
|
||
},
|
||
data: {
|
||
freightAddress: this.data.province,
|
||
price: this.data.total,
|
||
},
|
||
success: res => {
|
||
wx.hideLoading();
|
||
console.log(res)
|
||
if (res.data.code == 1) {
|
||
this.setData({
|
||
freight: res.data.value,
|
||
payActual: (parseFloat(this.data.total) + parseFloat(res.data.value)).toFixed(2),
|
||
})
|
||
} else {
|
||
wx.showModal({
|
||
title: '提示!',
|
||
content: res.data.message,
|
||
showCancel: false,
|
||
})
|
||
}
|
||
}
|
||
})
|
||
} else {
|
||
wx.showModal({
|
||
title: '提示!',
|
||
content: res.data.message,
|
||
showCancel: false,
|
||
})
|
||
}
|
||
}
|
||
})
|
||
}
|
||
} else {
|
||
// 自提点
|
||
this.setData({
|
||
freight: 0,
|
||
payActual:parseFloat(this.data.total).toFixed(2),
|
||
})
|
||
console.log(this.data.address)
|
||
}
|
||
},
|
||
|
||
/**
|
||
* 生命周期函数--监听页面加载
|
||
*/
|
||
onLoad: function (options) {
|
||
let myDate = new Date();
|
||
this.setData({
|
||
startTime: myDate.getFullYear() + '-' + (parseFloat(myDate.getMonth()) + 1) + '-' + (parseFloat(myDate.getDate()) + 3)
|
||
})
|
||
|
||
//购物车进入
|
||
if (options.isCar) {
|
||
console.log(options.isCar)
|
||
this.setData({
|
||
isCar: options.isCar
|
||
})
|
||
}
|
||
if (options.goods) {
|
||
let goods = JSON.parse(options.goods)
|
||
console.log(goods)
|
||
let total = 0;
|
||
let numAll = 0;
|
||
let specPriceArr = [];
|
||
for (let i = 0; i < goods.length; i++) {
|
||
total = total + parseFloat(goods[i].specs.specPromotionPrice) * parseFloat(goods[i].num)
|
||
numAll = numAll + parseFloat(goods[i].num);
|
||
specPriceArr.push(parseFloat(goods[i].specs.specPromotionPrice));
|
||
}
|
||
this.setData({
|
||
goods: goods,
|
||
total: total.toFixed(2),
|
||
total_int: parseInt(total),
|
||
specPriceArr: specPriceArr,
|
||
numAll: numAll,
|
||
payActual: (parseFloat(total) + parseFloat(this.data.freight)).toFixed(2),
|
||
})
|
||
//订单个人信息获取
|
||
wx.showLoading({
|
||
title: '加载中',
|
||
})
|
||
wx.request({
|
||
url: app.globalData.ip + '/wisdommining/api/order/getUserInfo',
|
||
method: 'GET',
|
||
header: {
|
||
"content-type": "application/x-www-form-urlencoded",
|
||
},
|
||
data: {
|
||
userId: app.globalData.userId
|
||
},
|
||
success: res => {
|
||
wx.hideLoading();
|
||
console.log(res)
|
||
if (res.data.code == 1) {
|
||
let dataList = res.data.value;
|
||
this.setData({
|
||
dataList: dataList,
|
||
})
|
||
if (dataList.wisdUserAddress) {
|
||
//默认地址ID
|
||
this.setData({
|
||
province: dataList.wisdUserAddress.addressProvince,
|
||
addressId: dataList.wisdUserAddress.id,
|
||
address: dataList.wisdUserAddress.addressProvince + dataList.wisdUserAddress.addressCity + dataList.wisdUserAddress.addressArea + dataList.wisdUserAddress.addressDetailed,
|
||
})
|
||
//运费获取
|
||
wx.request({
|
||
url: app.globalData.ip + '/wisdommining/api/order/getFreight',
|
||
method: 'GET',
|
||
header: {
|
||
"content-type": "application/x-www-form-urlencoded",
|
||
},
|
||
data: {
|
||
freightAddress: dataList.wisdUserAddress.addressProvince,
|
||
price: total,
|
||
},
|
||
success: res => {
|
||
wx.hideLoading();
|
||
console.log(res)
|
||
if (res.data.code == 1) {
|
||
this.setData({
|
||
freight: res.data.value,
|
||
payActual: (parseFloat(this.data.total) + parseFloat(res.data.value)).toFixed(2),
|
||
})
|
||
} else {
|
||
wx.showModal({
|
||
title: '提示!',
|
||
content: res.data.message,
|
||
showCancel: false,
|
||
})
|
||
}
|
||
}
|
||
})
|
||
}
|
||
} else {
|
||
wx.showModal({
|
||
title: '提示!',
|
||
content: res.data.message,
|
||
showCancel: false,
|
||
})
|
||
}
|
||
}
|
||
})
|
||
}
|
||
},
|
||
//备注
|
||
note(e) {
|
||
this.setData({
|
||
note: e.detail.value
|
||
})
|
||
},
|
||
//选择地址类型
|
||
addressType() {
|
||
this.setData({
|
||
addressType: true,//收货
|
||
})
|
||
},
|
||
|
||
//选择收货地址
|
||
address() {
|
||
wx.navigateTo({
|
||
url: '/pages/my/my_address/my_address?orderAddress=true&addressId=' + this.data.addressId,
|
||
})
|
||
this.setData({
|
||
addressType: false,//收货
|
||
})
|
||
},
|
||
//选择自提点
|
||
ztd() {
|
||
wx.navigateTo({
|
||
url: '/pages/index/index_address/index_address?pay=true',
|
||
})
|
||
this.setData({
|
||
addressType: false,//收货
|
||
})
|
||
},
|
||
//收获 遮罩层
|
||
addressZzc() {
|
||
this.setData({
|
||
addressType: false,//收货
|
||
})
|
||
},
|
||
|
||
//选择优惠方式
|
||
reduceType() {
|
||
this.setData({
|
||
reduceType: true,//优惠
|
||
})
|
||
},
|
||
|
||
//立即支付
|
||
pay(e) {
|
||
if (this.data.addressId == '') {
|
||
wx.showToast({
|
||
title: '请选择收货方式',
|
||
icon: 'none'
|
||
})
|
||
return
|
||
}
|
||
if (this.data.orderPick == 1) {
|
||
if (this.data.date == '') {
|
||
wx.showToast({
|
||
title: '请选择提货日期',
|
||
icon: 'none'
|
||
})
|
||
return
|
||
}
|
||
if (this.data.time == '') {
|
||
wx.showToast({
|
||
title: '请选择提货时间',
|
||
icon: 'none'
|
||
})
|
||
return
|
||
}
|
||
}
|
||
wx.showModal({
|
||
title: '提示!',
|
||
content: '确认要购买吗',
|
||
success: sss => {
|
||
if (sss.confirm) {
|
||
let orderActualPay = '';
|
||
let orderOtherPay = '';
|
||
let orderPayWay = '';
|
||
let orderPick = this.data.orderPick;
|
||
let wisdOrderDetailList = [];
|
||
let orderAllPay = +this.data.freight + (+this.data.total) //订单原价
|
||
let orderVolumePay = this.data.discount //优惠金额
|
||
console.log(this.data.goods)
|
||
//商品信息
|
||
for (let i = 0; i < this.data.goods.length; i++) {
|
||
wisdOrderDetailList.push({
|
||
goodsId: this.data.goods[i].goodsId,
|
||
goodsNum: this.data.goods[i].num,
|
||
specId: this.data.goods[i].specs.id,
|
||
specPrice: this.data.specPriceArr[i], //规格价格集合
|
||
})
|
||
}
|
||
console.log(wisdOrderDetailList)
|
||
//支付方式
|
||
orderActualPay = parseFloat(this.data.payActual)
|
||
if (this.data.reduceTypeXz == 0) {
|
||
//1纯微信支付
|
||
orderPayWay = 1;
|
||
orderOtherPay = 0;
|
||
}
|
||
wx.showLoading({
|
||
title: '加载中',
|
||
mask: true
|
||
})
|
||
wx.request({
|
||
url: app.globalData.ip + '/wisdommining/api/order/addOrder',
|
||
method: 'POST',
|
||
data: {
|
||
userId: app.globalData.userId,
|
||
addressId: this.data.addressId,
|
||
orderActualPay: orderActualPay, //微信支付的钱 其余传0
|
||
orderOtherPay: orderOtherPay, //积分 现金券 折扣券 套餐0
|
||
orderPayWay: orderPayWay, //1纯微信支付 2积分支付 3套餐券 4现金券(+微信支付) 5折扣券(+微信支付)
|
||
orderPick: orderPick, //1自提 2物流
|
||
orderPickTime:orderPick==1?this.data.date+' '+this.data.time:'',
|
||
orderRemark: this.data.note,
|
||
orderFreight: this.data.freight,
|
||
wisdOrderDetailList: wisdOrderDetailList,
|
||
isCar: parseInt(this.data.isCar), //购物车1 别的0
|
||
orderAllPay: orderAllPay,
|
||
orderVolumePay: orderVolumePay,
|
||
},
|
||
success: res => {
|
||
wx.hideLoading();
|
||
console.log(res)
|
||
if (res.data.code == 1) {
|
||
//支付
|
||
this.payM({
|
||
orderNumber: res.data.value
|
||
})
|
||
} else {
|
||
wx.showModal({
|
||
title: '提示!',
|
||
content: res.data.message,
|
||
showCancel: false,
|
||
})
|
||
}
|
||
}
|
||
})
|
||
}
|
||
}
|
||
})
|
||
},
|
||
|
||
//支付
|
||
payM(options) {
|
||
wx.showLoading({
|
||
title: '支付中',
|
||
mask: true
|
||
})
|
||
wx.request({
|
||
url: app.globalData.ip + '/wisdommining/api/pay/payOrder',
|
||
method: 'PUT',
|
||
header: {
|
||
"content-type": "application/x-www-form-urlencoded",
|
||
},
|
||
data: {
|
||
userId: app.globalData.userId,
|
||
orderNumber: options.orderNumber
|
||
},
|
||
success: res => {
|
||
wx.hideLoading();
|
||
console.log(res)
|
||
if (res.data.code == 1) {
|
||
//调用微信支付API
|
||
wx.requestPayment({
|
||
timeStamp: res.data.value.timeStamp,
|
||
nonceStr: res.data.value.nonceStr,
|
||
package: res.data.value.package,
|
||
signType: 'MD5',
|
||
paySign: res.data.value.paySign,
|
||
success(res) {
|
||
console.log(res)
|
||
//支付成功
|
||
wx.showToast({
|
||
title: '支付成功',
|
||
icon: 'success',
|
||
duration: 1000,
|
||
success() {
|
||
setTimeout(() => {
|
||
wx.redirectTo({
|
||
url: '/pages/order_details/order_details?orderNumber=' + options.orderNumber,
|
||
})
|
||
}, 1000);
|
||
}
|
||
})
|
||
},
|
||
fail(res) {
|
||
console.log(res)
|
||
wx.showToast({
|
||
title: '支付失败',
|
||
icon: 'none',
|
||
duration: 1000,
|
||
success() {
|
||
setTimeout(() => {
|
||
wx.redirectTo({
|
||
url: '/pages/order_details/order_details?orderNumber=' + options.orderNumber,
|
||
})
|
||
}, 1000);
|
||
}
|
||
})
|
||
}
|
||
})
|
||
}else {
|
||
wx.showModal({
|
||
title: '提示!',
|
||
content: res.data.message,
|
||
showCancel: false,
|
||
success: sss => {
|
||
if (sss.confirm) {
|
||
wx.navigateBack()
|
||
}
|
||
}
|
||
})
|
||
}
|
||
}
|
||
})
|
||
},
|
||
|
||
|
||
bindDateChange: function (e) {
|
||
console.log('picker发送选择改变,携带值为', e.detail.value)
|
||
this.setData({
|
||
date: e.detail.value
|
||
})
|
||
},
|
||
bindTimeChange: function (e) {
|
||
console.log('picker发送选择改变,携带值为', e.detail.value)
|
||
this.setData({
|
||
time: e.detail.value
|
||
})
|
||
},
|
||
|
||
|
||
}) |