2024-08-30 15:23:27 +08:00
|
|
|
const app = getApp()
|
|
|
|
const baseUrl = app.globalData.baseUrl
|
2024-08-29 14:36:16 +08:00
|
|
|
// page/component/orders/orders.js
|
|
|
|
Page({
|
|
|
|
onLoad: function (options) {
|
|
|
|
const orders = {
|
|
|
|
id: options.id,
|
|
|
|
title: options.title,
|
|
|
|
price: options.price,
|
|
|
|
num: options.num,
|
|
|
|
image: options.image,
|
2024-08-30 15:23:27 +08:00
|
|
|
}
|
|
|
|
this.setData({ orders })
|
2024-08-29 14:36:16 +08:00
|
|
|
},
|
|
|
|
data: {
|
|
|
|
address: {},
|
|
|
|
hasAddress: false,
|
|
|
|
total: 0,
|
2024-08-30 15:23:27 +08:00
|
|
|
orders: {},
|
|
|
|
// {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-29 14:36:16 +08:00
|
|
|
for (let i = 0; i < orders.length; i++) {
|
2024-08-30 15:23:27 +08:00
|
|
|
total += orders[i].num * orders[i].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
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
toPay() {
|
2024-08-30 15:23:27 +08:00
|
|
|
const self = this
|
2024-08-29 14:36:16 +08:00
|
|
|
|
|
|
|
// 假设订单信息在 this.data.orders 中
|
2024-08-30 15:23:27 +08:00
|
|
|
const orderData = this.data.orders
|
2024-08-29 14:36:16 +08:00
|
|
|
|
|
|
|
wx.showModal({
|
2024-08-30 15:23:27 +08:00
|
|
|
title: "提示",
|
|
|
|
content: "本系统只做演示,支付系统已屏蔽",
|
|
|
|
text: "center",
|
2024-08-29 14:36:16 +08:00
|
|
|
complete() {
|
|
|
|
// 发送订单数据到后端
|
|
|
|
wx.request({
|
2024-08-30 15:23:27 +08:00
|
|
|
// TODO 未测试
|
|
|
|
url: baseUrl + "/order/addOrUpdate", // 替换为你的后端API地址
|
|
|
|
method: "POST",
|
2024-08-29 14:36:16 +08:00
|
|
|
data: {
|
2024-08-30 15:23:27 +08:00
|
|
|
...orderData,
|
2024-08-29 14:36:16 +08:00
|
|
|
},
|
|
|
|
header: {
|
2024-08-30 15:23:27 +08:00
|
|
|
"Content-Type": "application/json", // 设置请求头
|
2024-08-29 14:36:16 +08:00
|
|
|
},
|
|
|
|
success(res) {
|
|
|
|
if (res.statusCode === 200) {
|
|
|
|
wx.showToast({
|
2024-08-30 15:23:27 +08:00
|
|
|
title: "订单已提交",
|
|
|
|
icon: "success",
|
|
|
|
duration: 2000,
|
|
|
|
})
|
2024-08-29 14:36:16 +08:00
|
|
|
// 跳转到用户页面
|
|
|
|
wx.switchTab({
|
2024-08-30 15:23:27 +08:00
|
|
|
url: "/page/component/user/user",
|
|
|
|
})
|
2024-08-29 14:36:16 +08:00
|
|
|
} else {
|
|
|
|
wx.showToast({
|
2024-08-30 15:23:27 +08:00
|
|
|
title: "提交失败",
|
|
|
|
icon: "none",
|
|
|
|
duration: 2000,
|
|
|
|
})
|
2024-08-29 14:36:16 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
fail() {
|
|
|
|
wx.showToast({
|
2024-08-30 15:23:27 +08:00
|
|
|
title: "请求失败",
|
|
|
|
icon: "none",
|
|
|
|
duration: 2000,
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|