前端各页面对接数据
This commit is contained in:
@@ -772,64 +772,10 @@ const loadDefaultAddress = async () => {
|
||||
|
||||
// 获取当前用户ID
|
||||
const getCurrentUserId = (): string => {
|
||||
// 尝试从多个可能的键名获取用户ID
|
||||
const possibleKeys = ['user_id', 'userId', 'uid', 'user_uuid', 'userID', 'user.id']
|
||||
|
||||
for (const key of possibleKeys) {
|
||||
const value = uni.getStorageSync(key)
|
||||
console.log(`getCurrentUserId: 尝试键名 ${key}:`, value)
|
||||
if (value) {
|
||||
console.log(`getCurrentUserId: 从 ${key} 获取到用户ID:`, value)
|
||||
return value as string
|
||||
}
|
||||
}
|
||||
|
||||
// 尝试从userInfo对象获取
|
||||
const userInfo = uni.getStorageSync('userInfo')
|
||||
console.log('getCurrentUserId: 从userInfo获取:', userInfo)
|
||||
if (userInfo) {
|
||||
// userInfo可能是字符串(需要解析)或对象
|
||||
let userInfoObj: any = userInfo
|
||||
if (typeof userInfo === 'string') {
|
||||
try {
|
||||
userInfoObj = JSON.parse(userInfo)
|
||||
} catch (e) {
|
||||
console.error('解析userInfo失败:', e)
|
||||
}
|
||||
}
|
||||
|
||||
// 尝试多个可能的属性名
|
||||
const possibleProps = ['id', 'userId', 'uid', 'user_id', 'uuid', 'user_uuid']
|
||||
for (const prop of possibleProps) {
|
||||
if (userInfoObj && userInfoObj[prop]) {
|
||||
console.log(`getCurrentUserId: 从userInfo.${prop} 获取到用户ID:`, userInfoObj[prop])
|
||||
return userInfoObj[prop] as string
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 尝试从auth获取(如果使用Supabase Auth)
|
||||
const authData = uni.getStorageSync('supabase.auth.token')
|
||||
if (authData) {
|
||||
console.log('getCurrentUserId: 从supabase.auth.token获取:', authData)
|
||||
try {
|
||||
const authObj = typeof authData === 'string' ? JSON.parse(authData) : authData
|
||||
if (authObj.currentSession && authObj.currentSession.user && authObj.currentSession.user.id) {
|
||||
console.log('getCurrentUserId: 从auth session获取用户ID:', authObj.currentSession.user.id)
|
||||
return authObj.currentSession.user.id as string
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('解析auth数据失败:', e)
|
||||
}
|
||||
}
|
||||
|
||||
// 打印所有存储键,用于调试
|
||||
console.log('getCurrentUserId: 所有Storage键:')
|
||||
const allKeys = uni.getStorageInfoSync().keys
|
||||
console.log('Storage keys:', allKeys)
|
||||
|
||||
console.log('getCurrentUserId: 未找到用户ID')
|
||||
return ''
|
||||
// 使用 SupabaseService 获取当前用户ID
|
||||
const userId = supabaseService.getCurrentUserId()
|
||||
console.log('getCurrentUserId: 从SupabaseService获取到用户ID:', userId)
|
||||
return userId ?? ''
|
||||
}
|
||||
|
||||
// 用户登录状态
|
||||
@@ -1280,65 +1226,89 @@ const selectCoupon = () => {
|
||||
|
||||
// 提交订单
|
||||
const submitOrder = async () => {
|
||||
if (!selectedAddress.value) {
|
||||
uni.showToast({
|
||||
title: '请选择收货地址',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
// 校验地址
|
||||
if (!selectedAddress.value) {
|
||||
uni.showToast({
|
||||
title: '请选择收货地址',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 校验商品
|
||||
if (checkoutItems.value.length === 0) {
|
||||
uni.showToast({
|
||||
title: '订单中没有商品',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
uni.showLoading({ title: '提交中...' })
|
||||
|
||||
// MOCK ORDER SUBMISSION
|
||||
// 模拟创建成功
|
||||
try {
|
||||
const mockOrderId = `order_${Date.now()}`
|
||||
const userId = getCurrentUserId()
|
||||
// 确保使用当前登录用户ID (如果本地存储为空,可能需要处理)
|
||||
if (!userId) {
|
||||
uni.hideLoading()
|
||||
uni.showToast({
|
||||
title: '请先登录',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 创建订单对象
|
||||
const newOrder = {
|
||||
id: mockOrderId,
|
||||
order_no: generateOrderNo(),
|
||||
user_id: getCurrentUserId() || 'user_001',
|
||||
merchant_id: checkoutItems.value[0]?.product_id || 'merchant_001', // 简化处理,取第一个商品的merchant
|
||||
status: 1, // 待支付
|
||||
total_amount: totalAmount.value,
|
||||
discount_amount: discountAmount.value,
|
||||
delivery_fee: deliveryFee.value,
|
||||
actual_amount: actualAmount.value,
|
||||
payment_method: 0,
|
||||
payment_status: 0,
|
||||
delivery_address: selectedAddress.value,
|
||||
items: checkoutItems.value,
|
||||
created_at: new Date().toISOString()
|
||||
}
|
||||
// 准备订单项数据
|
||||
// 注意:需根据 checkoutItems 的实际结构转换为 createOrder 需要的 CartItem 结构
|
||||
// 假设 checkoutItems 已经包含了 product_id, quantity, price, name, image 等字段
|
||||
const orderItems = checkoutItems.value.map((item: any): any => ({
|
||||
id: item.id || '', // 这是一个临时ID或者购物车ID,createOrder 中会使用 product_id
|
||||
product_id: item.product_id || item.id, // 确保有 product_id
|
||||
quantity: item.quantity,
|
||||
price: item.price,
|
||||
product_name: item.name,
|
||||
product_image: item.image,
|
||||
spec: item.spec,
|
||||
checked: true
|
||||
}))
|
||||
|
||||
// 保存到本地存储
|
||||
const storedOrders = uni.getStorageSync('orders')
|
||||
let orders: any[] = []
|
||||
if (storedOrders) {
|
||||
try {
|
||||
orders = JSON.parse(storedOrders as string) as any[]
|
||||
} catch (e) {
|
||||
console.error('解析订单数据失败', e)
|
||||
}
|
||||
}
|
||||
orders.unshift(newOrder)
|
||||
uni.setStorageSync('orders', JSON.stringify(orders))
|
||||
|
||||
uni.showLoading({ title: '提交中...' })
|
||||
await new Promise(resolve => setTimeout(resolve, 500))
|
||||
// 调用 Supabase 服务创建订单
|
||||
const result = await supabaseService.createOrder(
|
||||
userId,
|
||||
selectedAddress.value!.id, // 地址ID
|
||||
actualAmount.value, // 实付金额
|
||||
orderItems
|
||||
)
|
||||
|
||||
uni.hideLoading()
|
||||
|
||||
// 携带价格详情跳转
|
||||
uni.navigateTo({
|
||||
url: `/pages/mall/consumer/payment?orderId=${mockOrderId}&amount=${actualAmount.value}&productAmount=${totalAmount.value}&deliveryFee=${deliveryFee.value}&discountAmount=${discountAmount.value}`
|
||||
})
|
||||
} catch (err) {
|
||||
console.error('创建订单失败:', err)
|
||||
uni.showToast({
|
||||
title: '订单创建失败',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
|
||||
if (result.success) {
|
||||
// 清除购买的商品 (如果来自购物车,应该在 createOrder 成功后清除,或者这里手动清除本地存储)
|
||||
// 这里我们假设购物车清理逻辑可能在 createOrder 后端处理,或者需要在这里清除本地
|
||||
try {
|
||||
uni.removeStorageSync('checkout_items')
|
||||
} catch(e) {
|
||||
console.error('清除结算商品失败', e)
|
||||
}
|
||||
|
||||
const activeOrderId = result.data as string
|
||||
|
||||
// 跳转支付页面
|
||||
uni.navigateTo({
|
||||
url: `/pages/mall/consumer/payment?orderId=${activeOrderId}&amount=${actualAmount.value}&productAmount=${totalAmount.value}&deliveryFee=${deliveryFee.value}&discountAmount=${discountAmount.value}`
|
||||
})
|
||||
} else {
|
||||
throw new Error(result.error)
|
||||
}
|
||||
|
||||
} catch (err: any) {
|
||||
uni.hideLoading()
|
||||
console.error('创建订单失败:', err)
|
||||
uni.showToast({
|
||||
title: err.message || '订单创建失败',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 生成订单号
|
||||
|
||||
Reference in New Issue
Block a user