WxApp/page/component/orders/orders.js

202 lines
6.6 KiB
JavaScript
Raw Normal View History

2024-08-30 15:23:27 +08:00
const app = getApp()
const baseUrl = app.globalData.baseUrl
2024-09-03 23:14:06 +08:00
const md5 = require('js-md5');
2024-08-29 14:36:16 +08:00
// page/component/orders/orders.js
Page({
onLoad: function (options) {
const orders = {
2024-09-04 23:56:21 +08:00
goodsId: options.goodsId,
2024-08-29 14:36:16 +08:00
title: options.title,
price: options.price,
num: options.num,
image: options.image,
2024-08-31 22:40:03 +08:00
specs: options.specs
2024-08-30 15:23:27 +08:00
}
this.setData({ orders })
2024-08-31 22:40:03 +08:00
console.log(this.data.orders)
2024-08-29 14:36:16 +08:00
},
data: {
address: {},
hasAddress: false,
total: 0,
2024-08-30 15:23:27 +08:00
orders: {},
2024-09-03 23:14:06 +08:00
config: {
appid: "wx865aefa5a7115ae0",
key: "d5a58d44588b42cbbe01daa5cfa4e792"
}
2024-08-30 15:23:27 +08:00
// {id:1,title:'新鲜芹菜 半斤',image:'/image/s5.png',num:4,price:0.01},
// {id:2,title:'素米 500g',image:'/image/s6.png',num:1,price:0.03}
2024-08-29 14:36:16 +08:00
},
onReady() {
2024-08-30 15:23:27 +08:00
this.getTotalPrice()
2024-08-29 14:36:16 +08:00
},
onShow: function () {
2024-08-30 15:23:27 +08:00
const self = this
2024-08-29 14:36:16 +08:00
wx.getStorage({
2024-08-30 15:23:27 +08:00
key: "address",
2024-08-29 14:36:16 +08:00
success(res) {
self.setData({
address: res.data,
2024-08-30 15:23:27 +08:00
hasAddress: true,
2024-08-29 14:36:16 +08:00
})
2024-08-30 15:23:27 +08:00
},
2024-08-29 14:36:16 +08:00
})
},
/**
* 计算总价
*/
getTotalPrice() {
2024-08-30 15:23:27 +08:00
let orders = this.data.orders
let total = 0
2024-08-31 22:40:03 +08:00
total += orders.num * orders.price
2024-08-29 14:36:16 +08:00
this.setData({
2024-08-30 15:23:27 +08:00
total: total,
2024-08-29 14:36:16 +08:00
})
},
2024-09-03 23:14:06 +08:00
timeStamp: function () {
return parseInt(new Date().getTime() / 1000) + ''
},
/* 随机数 */
randomString: function () {
var chars = 'A2345678';
var maxPos = chars.length;
var pwd = '';
for (var i = 0; i < 32; i++) {
pwd += chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
},
2024-08-29 14:36:16 +08:00
toPay() {
2024-09-03 23:14:06 +08:00
const self = this;
const orderData = this.data.orders;
2024-08-31 22:40:03 +08:00
wx.request({
2024-09-05 00:22:07 +08:00
url: baseUrl + "/user/getById",
method: "GET",
2024-08-31 22:40:03 +08:00
data: {
2024-09-05 00:22:07 +08:00
openId: wx.getStorageSync('openid'),
2024-08-31 22:40:03 +08:00
},
success(res) {
2024-09-05 00:22:07 +08:00
if (res.data.data.address != null && res.data.data.address != "" && res.data.data.phone != null && res.data.data.phone != "") {
// 假设用户ID暂时写死
orderData.openId = wx.getStorageSync('openid');
orderData.status = 1;
console.log("orderData", orderData)
// 生成订单并请求支付
wx.request({
url: baseUrl + "/order/addOrUpdate", // 替换为你的后端API地址
method: "POST",
2024-09-04 23:56:21 +08:00
data: {
2024-09-05 00:22:07 +08:00
...orderData,
2024-09-03 23:14:06 +08:00
},
2024-09-05 00:22:07 +08:00
header: {
"Content-Type": "application/json", // 设置请求头
2024-09-03 23:14:06 +08:00
},
2024-09-05 00:22:07 +08:00
success(res) {
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)
}
})
}
2024-09-03 23:14:06 +08:00
}
2024-09-04 23:56:21 +08:00
})
2024-09-05 00:22:07 +08:00
} else {
wx.showToast({
title: "请填写收货信息",
icon: "error",
duration: 2000,
});
2024-08-31 22:40:03 +08:00
}
2024-09-04 23:56:21 +08:00
}
})
}
2024-08-30 15:23:27 +08:00
})