功能完成
This commit is contained in:
parent
7e90cc1757
commit
064be93e1b
2
app.js
2
app.js
|
@ -1,4 +1,4 @@
|
||||||
const baseUrl = "http://localhost:8082"
|
const baseUrl = "http://49.233.248.140:8082"
|
||||||
|
|
||||||
|
|
||||||
App({
|
App({
|
||||||
|
|
|
@ -73,111 +73,127 @@ Page({
|
||||||
toPay() {
|
toPay() {
|
||||||
const self = this;
|
const self = this;
|
||||||
const orderData = this.data.orders;
|
const orderData = this.data.orders;
|
||||||
|
|
||||||
// 假设用户ID暂时写死
|
|
||||||
orderData.openId = wx.getStorageSync('openid');
|
|
||||||
orderData.status = 1;
|
|
||||||
console.log("orderData", orderData)
|
|
||||||
// 生成订单并请求支付
|
|
||||||
wx.request({
|
wx.request({
|
||||||
url: baseUrl + "/order/addOrUpdate", // 替换为你的后端API地址
|
url: baseUrl + "/user/getById",
|
||||||
method: "POST",
|
method: "GET",
|
||||||
data: {
|
data: {
|
||||||
...orderData,
|
openId: wx.getStorageSync('openid'),
|
||||||
},
|
|
||||||
header: {
|
|
||||||
"Content-Type": "application/json", // 设置请求头
|
|
||||||
},
|
},
|
||||||
success(res) {
|
success(res) {
|
||||||
if (res.statusCode === 200) {
|
if (res.data.data.address != null && res.data.data.address != "" && res.data.data.phone != null && res.data.data.phone != "") {
|
||||||
console.log(res)
|
// 假设用户ID暂时写死
|
||||||
wx.cloud.init({
|
orderData.openId = wx.getStorageSync('openid');
|
||||||
env: 'zdt2024-9g7pr48u64f887f0', //填上你的云开发环境id
|
orderData.status = 1;
|
||||||
traceUser: true,
|
console.log("orderData", orderData)
|
||||||
});
|
// 生成订单并请求支付
|
||||||
const randomString = self.randomString();
|
wx.request({
|
||||||
const outTradeNo = res.data.data.orderCode
|
url: baseUrl + "/order/addOrUpdate", // 替换为你的后端API地址
|
||||||
const orderId = res.data.data.id
|
method: "POST",
|
||||||
wx.cloud.callFunction({
|
|
||||||
name: "pay",
|
|
||||||
data: {
|
data: {
|
||||||
nonceStr: randomString, //随机字符串,String(32)
|
...orderData,
|
||||||
outTradeNo: outTradeNo, //商户订单号,String(32)
|
|
||||||
totalFee: self.data.total, //Int
|
|
||||||
},
|
},
|
||||||
success: res => {
|
header: {
|
||||||
console.log('下单结果: ', res);
|
"Content-Type": "application/json", // 设置请求头
|
||||||
// 获取到预付单信息
|
|
||||||
const payment = res.result.payment
|
|
||||||
wx.hideLoading();
|
|
||||||
// 唤起微信支付组件,完成支付
|
|
||||||
wx.requestPayment({
|
|
||||||
...payment, //把payment展开
|
|
||||||
success(res) {
|
|
||||||
// 支付成功回调,实现自定义的业务逻辑
|
|
||||||
console.log('唤起支付组件成功:', res);
|
|
||||||
// 将订单的status改为已支付
|
|
||||||
orderData.status = 2;
|
|
||||||
orderData.id = orderId;
|
|
||||||
wx.request({
|
|
||||||
url: baseUrl + "/order/addOrUpdate", // 替换为你的后端API地址
|
|
||||||
method: "POST",
|
|
||||||
data: {
|
|
||||||
...orderData,
|
|
||||||
},
|
|
||||||
header: {
|
|
||||||
"Content-Type": "application/json", // 设置请求头
|
|
||||||
},
|
|
||||||
success(res) {
|
|
||||||
wx.showToast({
|
|
||||||
title: "订单支付成功",
|
|
||||||
icon: "success",
|
|
||||||
duration: 2000,
|
|
||||||
});
|
|
||||||
wx.switchTab({
|
|
||||||
url: '/page/component/index', // 替换为你的首页路径
|
|
||||||
});
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
fail(err) {
|
|
||||||
// 支付失败回调
|
|
||||||
console.error('唤起支付组件失败:', err);
|
|
||||||
// 将订单的status改为已关单
|
|
||||||
orderData.status = 0;
|
|
||||||
orderData.id = orderId;
|
|
||||||
wx.request({
|
|
||||||
url: baseUrl + "/order/addOrUpdate", // 替换为你的后端API地址
|
|
||||||
method: "POST",
|
|
||||||
data: {
|
|
||||||
...orderData,
|
|
||||||
},
|
|
||||||
header: {
|
|
||||||
"Content-Type": "application/json", // 设置请求头
|
|
||||||
},
|
|
||||||
success(res) {
|
|
||||||
wx.showToast({
|
|
||||||
title: "订单支付成功",
|
|
||||||
icon: "success",
|
|
||||||
duration: 2000,
|
|
||||||
});
|
|
||||||
wx.switchTab({
|
|
||||||
url: '/page/component/index', // 替换为你的首页路径
|
|
||||||
});
|
|
||||||
}
|
|
||||||
})
|
|
||||||
wx.showToast({
|
|
||||||
title: "支付失败",
|
|
||||||
icon: "error",
|
|
||||||
duration: 2000,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
fail: err => {
|
success(res) {
|
||||||
console.log(err)
|
if (res.statusCode === 200) {
|
||||||
|
console.log(res)
|
||||||
|
wx.cloud.init({
|
||||||
|
env: 'zdt2024-9g7pr48u64f887f0', //填上你的云开发环境id
|
||||||
|
traceUser: true,
|
||||||
|
});
|
||||||
|
const randomString = self.randomString();
|
||||||
|
const outTradeNo = res.data.data.orderCode
|
||||||
|
const orderId = res.data.data.id
|
||||||
|
wx.cloud.callFunction({
|
||||||
|
name: "pay",
|
||||||
|
data: {
|
||||||
|
nonceStr: randomString, //随机字符串,String(32)
|
||||||
|
outTradeNo: outTradeNo, //商户订单号,String(32)
|
||||||
|
totalFee: self.data.total * 100, //Int
|
||||||
|
},
|
||||||
|
success: res => {
|
||||||
|
console.log('下单结果: ', res);
|
||||||
|
// 获取到预付单信息
|
||||||
|
const payment = res.result.payment
|
||||||
|
wx.hideLoading();
|
||||||
|
// 唤起微信支付组件,完成支付
|
||||||
|
wx.requestPayment({
|
||||||
|
...payment, //把payment展开
|
||||||
|
success(res) {
|
||||||
|
// 支付成功回调,实现自定义的业务逻辑
|
||||||
|
console.log('唤起支付组件成功:', res);
|
||||||
|
// 将订单的status改为已支付
|
||||||
|
orderData.status = 2;
|
||||||
|
orderData.id = orderId;
|
||||||
|
wx.request({
|
||||||
|
url: baseUrl + "/order/addOrUpdate", // 替换为你的后端API地址
|
||||||
|
method: "POST",
|
||||||
|
data: {
|
||||||
|
...orderData,
|
||||||
|
},
|
||||||
|
header: {
|
||||||
|
"Content-Type": "application/json", // 设置请求头
|
||||||
|
},
|
||||||
|
success(res) {
|
||||||
|
wx.showToast({
|
||||||
|
title: "订单支付成功",
|
||||||
|
icon: "success",
|
||||||
|
duration: 2000,
|
||||||
|
});
|
||||||
|
wx.switchTab({
|
||||||
|
url: '/page/component/index', // 替换为你的首页路径
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
fail(err) {
|
||||||
|
// 支付失败回调
|
||||||
|
console.error('唤起支付组件失败:', err);
|
||||||
|
// 将订单的status改为已关单
|
||||||
|
orderData.status = 0;
|
||||||
|
orderData.id = orderId;
|
||||||
|
wx.request({
|
||||||
|
url: baseUrl + "/order/addOrUpdate", // 替换为你的后端API地址
|
||||||
|
method: "POST",
|
||||||
|
data: {
|
||||||
|
...orderData,
|
||||||
|
},
|
||||||
|
header: {
|
||||||
|
"Content-Type": "application/json", // 设置请求头
|
||||||
|
},
|
||||||
|
success(res) {
|
||||||
|
wx.showToast({
|
||||||
|
title: "订单支付成功",
|
||||||
|
icon: "success",
|
||||||
|
duration: 2000,
|
||||||
|
});
|
||||||
|
wx.switchTab({
|
||||||
|
url: '/page/component/index', // 替换为你的首页路径
|
||||||
|
});
|
||||||
|
}
|
||||||
|
})
|
||||||
|
wx.showToast({
|
||||||
|
title: "支付失败",
|
||||||
|
icon: "error",
|
||||||
|
duration: 2000,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
fail: err => {
|
||||||
|
console.log(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
wx.showToast({
|
||||||
|
title: "请填写收货信息",
|
||||||
|
icon: "error",
|
||||||
|
duration: 2000,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue