连接数据库并修改页面

This commit is contained in:
not-like-juvenile
2026-02-02 18:20:22 +08:00
parent 5f856a96c9
commit 8efe6d5e89
22 changed files with 1630 additions and 992 deletions

View File

@@ -179,6 +179,9 @@
</template>
<script lang="uts">
import supa, { supaReady } from '@/components/supadb/aksupainstance.uts'
import { getCurrentUserId } from '@/utils/store.uts'
export default {
data() {
return {
@@ -243,76 +246,114 @@ export default {
uni.navigateBack()
},
loadOrderDetail(orderId: string) {
// ✅ 保留从 URL 获取的状态
async loadOrderDetail(orderId: string) {
const originalStatus = this.order.status
// 模拟加载订单详情数据
const mockOrder = {
id: orderId,
order_no: 'ORD202401150001',
user_id: 'user_001',
merchant_id: 'merchant_001',
// ✅ 使用传入的 status而不是硬编码
status: originalStatus,
total_amount: 299.98,
discount_amount: 30.00,
delivery_fee: 8.00,
actual_amount: 277.98,
payment_method: 1,
payment_status: 1,
delivery_address: {
name: '张三',
phone: '13800138000',
detail: '北京市朝阳区某某街道某某小区1号楼101室'
},
created_at: '2024-01-15 14:30:00'
}
// ✅ 合并数据,保留传入的 status
Object.assign(this.order, mockOrder)
this.orderItems = [
{
id: 'item_001',
order_id: orderId,
product_id: 'product_001',
sku_id: 'sku_001',
product_name: '精选好物商品',
sku_specifications: { color: '红色', size: 'M' },
price: 199.99,
quantity: 1,
total_amount: 199.99,
product_image: '/static/product1.jpg'
try {
await supaReady
console.log('loadOrderDetail called', { orderId })
// 获取订单主表:按 id(UUID) / cid(数字) / order_no 三种可能匹配
let orderRes: any = null
const isUuid = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/.test(orderId)
const isNumber = /^\d+$/.test(orderId)
console.log('loadOrderDetail: supa session=', supa.getSession && supa.getSession())
console.log('loadOrderDetail: detect id format', { isUuid, isNumber })
if (isUuid) {
console.log('loadOrderDetail: querying by id')
orderRes = await supa.from('ml_orders').select('*').eq('id', orderId).limit(1).execute()
} else if (isNumber) {
console.log('loadOrderDetail: querying by cid')
orderRes = await supa.from('ml_orders').select('*').eq('cid', Number(orderId)).limit(1).execute()
} else {
console.log('loadOrderDetail: querying by order_no')
orderRes = await supa.from('ml_orders').select('*').eq('order_no', orderId).limit(1).execute()
}
]
this.merchant = {
id: 'merchant_001',
user_id: 'user_001',
shop_name: '优质好店',
shop_logo: '/static/shop-logo.png',
shop_banner: '/static/shop-banner.png',
shop_description: '专注品质生活',
contact_name: '店主小王',
contact_phone: '13800138000',
shop_status: 1,
rating: 4.8,
total_sales: 15680,
created_at: '2023-06-01'
}
this.pickupAddress = '北京市朝阳区商家街道123号'
this.customerNote = '请送到门口,谢谢'
this.merchantNote = '商品易碎,小心搬运'
this.deliveryNote = '已按时送达,顾客签收'
this.deliveryInfo = {
distance: 3.2,
estimated_time: 25,
courier_id: 'courier_001',
pickup_time: '2024-01-15 15:00:00',
delivery_time: '2024-01-15 15:25:00'
console.log('loadOrderDetail: orderRes=', orderRes)
if (orderRes && Array.isArray(orderRes.data) && orderRes.data.length > 0) {
const row = orderRes.data[0]
const shipping = row.shipping_address || {}
this.order = Object.assign(this.order, {
id: row.id,
order_no: row.order_no || '',
user_id: row.user_id || '',
merchant_id: row.merchant_id || '',
status: Number(row.order_status) || originalStatus,
total_amount: Number(row.total_amount || row.product_amount || 0),
discount_amount: Number(row.discount_amount || 0),
delivery_fee: Number(row.shipping_fee || row.shipping_fee || 0),
actual_amount: Number(row.paid_amount || row.actual_amount || 0),
payment_method: row.payment_method || 0,
payment_status: row.payment_status || 0,
delivery_address: {
name: shipping.name || shipping.recipient || '',
phone: shipping.phone || shipping.mobile || '',
detail: shipping.detail || shipping.address || JSON.stringify(shipping)
},
created_at: row.created_at || ''
})
this.customerNote = row.remark || ''
this.merchantNote = row.merchant_memo || ''
// 读取订单商品 - 使用从主表获得的真实 order id
const realOrderId = row.id
const itemsRes: any = await supa.from('ml_order_items').select('*').eq('order_id', realOrderId).execute()
console.log('loadOrderDetail: itemsRes=', itemsRes)
if (itemsRes && Array.isArray(itemsRes.data)) {
this.orderItems = itemsRes.data.map((it:any) => ({
id: it.id,
order_id: it.order_id,
product_id: it.product_id,
sku_id: it.sku_id,
product_name: it.product_name,
sku_specifications: it.specifications || it.sku_name || null,
price: Number(it.price) || 0,
quantity: Number(it.quantity) || 0,
total_amount: Number(it.total_amount) || 0,
product_image: it.image_url || it.product_image || ''
}))
}
// 读取商家店铺信息ml_shops
if (row.merchant_id) {
const shopRes: any = await supa.from('ml_shops').select('*').eq('merchant_id', row.merchant_id).limit(1).execute()
console.log('loadOrderDetail: shopRes=', shopRes)
if (shopRes && Array.isArray(shopRes.data) && shopRes.data.length > 0) {
const s = shopRes.data[0]
this.merchant = Object.assign(this.merchant, {
id: s.id || '',
user_id: s.merchant_id || '',
shop_name: s.shop_name || s.name || '',
shop_logo: s.shop_logo || '',
shop_banner: s.shop_banner || '',
shop_description: s.description || s.shop_description || '',
contact_name: s.contact_name || '',
contact_phone: s.contact_phone || '',
shop_status: s.status || 0,
rating: s.rating || 0,
total_sales: s.total_sales || 0,
created_at: s.created_at || ''
})
this.pickupAddress = s.address || ''
}
}
// deliveryInfo 从 ml_delivery_tasks 中读取(如果存在)
const dtRes: any = await supa.from('ml_delivery_tasks').select('*').eq('order_id', realOrderId).limit(1).execute()
console.log('loadOrderDetail: dtRes=', dtRes)
if (dtRes && Array.isArray(dtRes.data) && dtRes.data.length > 0) {
const dt = dtRes.data[0]
this.deliveryInfo.distance = Number(dt.distance) || this.deliveryInfo.distance
this.deliveryInfo.estimated_time = Number(dt.estimated_time) || this.deliveryInfo.estimated_time
this.deliveryInfo.courier_id = dt.driver_id || ''
this.deliveryInfo.pickup_time = dt.pickup_time || ''
this.deliveryInfo.delivery_time = dt.delivered_time || ''
}
}
} catch (e) {
console.error('loadOrderDetail db error', e)
}
},
@@ -346,13 +387,20 @@ export default {
uni.showModal({
title: '确认取货',
content: '确认已从商家处取到商品?',
success: (res) => {
success: async (res) => {
if (res.confirm) {
this.order.status = 4 // 更新为已取货
uni.showToast({
title: '取货确认成功',
icon: 'success'
})
try {
await supaReady
// 更新 delivery task
const dtRes: any = await supa.from('ml_delivery_tasks').update({ status: 4, pickup_time: new Date().toISOString() }).eq('order_id', this.order.id).execute()
// 同步订单状态
await supa.from('ml_orders').update({ order_status: 4 }).eq('id', this.order.id).execute()
this.order.status = 4
uni.showToast({ title: '取货确认成功', icon: 'success' })
} catch (e) {
console.error('confirmPickup db error', e)
uni.showToast({ title: '取货确认失败', icon: 'none' })
}
}
}
})
@@ -364,13 +412,20 @@ export default {
uni.showModal({
title: '确认送达',
content: '确认商品已送达到顾客手中?',
success: (res) => {
success: async (res) => {
if (res.confirm) {
this.order.status = 5 // 更新为已完成
uni.showToast({
title: '送达确认成功',
icon: 'success'
})
try {
await supaReady
// 更新 delivery task
await supa.from('ml_delivery_tasks').update({ status: 6, delivered_time: new Date().toISOString() }).eq('order_id', this.order.id).execute()
// 更新订单状态为已送达
await supa.from('ml_orders').update({ order_status: 5 }).eq('id', this.order.id).execute()
this.order.status = 5
uni.showToast({ title: '送达确认成功', icon: 'success' })
} catch (e) {
console.error('confirmDelivery db error', e)
uni.showToast({ title: '送达确认失败', icon: 'none' })
}
}
}
})
@@ -382,13 +437,31 @@ export default {
uni.showModal({
title: '接受订单',
content: '确定接受这个配送订单吗?',
success: (res) => {
success: async (res) => {
if (res.confirm) {
this.order.status = 2 // 更新为已接单
uni.showToast({
title: '订单接受成功',
icon: 'success'
})
try {
await supaReady
const userId = getCurrentUserId()
let driverId = null
if (userId) {
const dRes: any = await supa.from('ml_delivery_drivers').select('id').eq('user_id', userId).limit(1).execute()
if (dRes && Array.isArray(dRes.data) && dRes.data.length > 0) driverId = dRes.data[0].id
}
// 尝试更新已有 delivery task
const updateRes: any = await supa.from('ml_delivery_tasks').update({ driver_id: driverId, status: 2 }).eq('order_id', this.order.id).execute()
if (!(updateRes && Array.isArray(updateRes.data) && updateRes.data.length > 0)) {
// 若不存在,则插入新的 task
await supa.from('ml_delivery_tasks').insert({ order_id: this.order.id, driver_id: driverId, status: 2, created_at: new Date().toISOString() }).execute()
}
// 更新订单状态
await supa.from('ml_orders').update({ order_status: 2 }).eq('id', this.order.id).execute()
this.order.status = 2
uni.showToast({ title: '订单接受成功', icon: 'success' })
} catch (e) {
console.error('acceptOrder db error', e)
uni.showToast({ title: '接受订单失败', icon: 'none' })
}
}
}
})
@@ -400,13 +473,18 @@ export default {
uni.showModal({
title: '拒绝订单',
content: '确定拒绝这个配送订单吗?',
success: (res) => {
success: async (res) => {
if (res.confirm) {
uni.showToast({
title: '订单已拒绝',
icon: 'success'
})
uni.navigateBack()
try {
await supaReady
// 标记订单为已拒绝order_status = 7
await supa.from('ml_orders').update({ order_status: 7 }).eq('id', this.order.id).execute()
uni.showToast({ title: '订单已拒绝', icon: 'success' })
uni.navigateBack()
} catch (e) {
console.error('rejectOrder db error', e)
uni.showToast({ title: '拒绝订单失败', icon: 'none' })
}
}
}
})