同步修改页面逻辑
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import supa from '@/components/supadb/aksupainstance.uts'
|
||||
import { getCurrentUserId } from '@/utils/store.uts'
|
||||
|
||||
/**
|
||||
* 配送模块数据服务 (Express Service)
|
||||
@@ -9,6 +10,8 @@ import supa from '@/components/supadb/aksupainstance.uts'
|
||||
export interface MockOrder {
|
||||
id?: string
|
||||
order_no: string
|
||||
user_id?: string
|
||||
merchant_id?: string
|
||||
status: string
|
||||
created_at: string
|
||||
receiver_name: string
|
||||
@@ -104,19 +107,81 @@ class MockService {
|
||||
constructor() {}
|
||||
|
||||
async getMockOrders(): Promise<MockOrder[]> {
|
||||
const { data, error } = await supa.from('platform_express_waybills')
|
||||
.select('*,ml_orders(*)')
|
||||
const userId = getCurrentUserId()
|
||||
console.log('DEBUG: currentUserId =', userId)
|
||||
|
||||
// 方案 1:直接通过运单表关联订单表的 user_id 过滤
|
||||
// 注意:确保 ml_orders!inner(*) 强制关联,否则 eq 会失效
|
||||
const query = supa.from('platform_express_waybills')
|
||||
.select('*, ml_orders!inner(*)')
|
||||
.order('created_at', { ascending: false })
|
||||
.execute()
|
||||
|
||||
if (error != null) {
|
||||
console.error('Fetch orders error:', error)
|
||||
return []
|
||||
if (userId != '' && userId != 'admin') {
|
||||
console.log('DEBUG: filtering by ml_orders.user_id =', userId)
|
||||
query.eq('ml_orders.user_id', userId)
|
||||
}
|
||||
|
||||
const list = data as Array<UTSJSONObject>
|
||||
return list.map((item: UTSJSONObject): MockOrder => {
|
||||
// 兼容 Supabase 关联查询返回对象或数组的情况
|
||||
const { data: d1, error: e1 } = await query.execute()
|
||||
|
||||
// 如果请求本身报错,先记录并尝试降级策略
|
||||
if (e1 != null) {
|
||||
console.error('DEBUG: primary query error =', e1)
|
||||
}
|
||||
|
||||
// 规范化返回的数据为数组(Supabase 客户端在某些情况下可能返回包装对象)
|
||||
let resultArray: Array<UTSJSONObject> | null = null
|
||||
if (Array.isArray(d1)) {
|
||||
resultArray = d1 as Array<UTSJSONObject>
|
||||
} else if (d1 != null && (d1 as any).data && Array.isArray((d1 as any).data)) {
|
||||
resultArray = (d1 as any).data as Array<UTSJSONObject>
|
||||
}
|
||||
|
||||
// 方案 2(保底):如果方案 1 没查到数据,可能是因为某些运单没有关联订单,或者 !inner 过滤太严格
|
||||
if (resultArray == null || resultArray.length === 0) {
|
||||
console.log('DEBUG: first query empty or error, trying fallback...')
|
||||
const query2 = supa.from('platform_express_waybills')
|
||||
.select('*, ml_orders(*)')
|
||||
.order('created_at', { ascending: false })
|
||||
|
||||
const { data: d2, error: e2 } = await query2.execute()
|
||||
if (e2 != null) {
|
||||
console.error('DEBUG: fallback query error =', e2)
|
||||
}
|
||||
|
||||
const allData = Array.isArray(d2) ? (d2 as Array<UTSJSONObject>) : ((d2 && (d2 as any).data && Array.isArray((d2 as any).data)) ? (d2 as any).data : null)
|
||||
|
||||
if (allData != null) {
|
||||
// 在内存中过滤归属于当前用户的订单(用于数据还没同步完全的情况)
|
||||
const filtered = allData.filter((item: UTSJSONObject): boolean => {
|
||||
if (userId == '' || userId == 'admin') return true
|
||||
|
||||
let ml: UTSJSONObject | null = null
|
||||
const mlData = item['ml_orders']
|
||||
if (mlData instanceof Array && (mlData as Array<any>).length > 0) {
|
||||
ml = (mlData as Array<UTSJSONObject>)[0]
|
||||
} else if (!(mlData instanceof Array)) {
|
||||
ml = mlData as UTSJSONObject
|
||||
}
|
||||
|
||||
try {
|
||||
return ml != null && (ml as any).user_id == userId
|
||||
} catch (err) {
|
||||
return false
|
||||
}
|
||||
})
|
||||
console.log('DEBUG: filtered in memory length =', filtered.length)
|
||||
return this.mapDataToOrders(filtered)
|
||||
}
|
||||
}
|
||||
|
||||
console.log('DEBUG: query result data length =', resultArray ? resultArray.length : 0)
|
||||
return this.mapDataToOrders(resultArray)
|
||||
}
|
||||
|
||||
// 抽离数据映射逻辑
|
||||
private mapDataToOrders(data: Array<UTSJSONObject> | null): MockOrder[] {
|
||||
if (data == null) return []
|
||||
return data.map((item: UTSJSONObject): MockOrder => {
|
||||
let ml: UTSJSONObject | null = null
|
||||
const mlData = item['ml_orders']
|
||||
if (mlData != null) {
|
||||
@@ -138,6 +203,7 @@ class MockService {
|
||||
amount: (ml != null ? ml['amount'] as string : '0.00'),
|
||||
carrier: item['carrier'] as string,
|
||||
tracking_no: item['tracking_no'] as string,
|
||||
merchant_id: (ml != null ? ml['merchant_id'] as string : ''),
|
||||
last_synced_at: this.formatDBTime(item['last_synced_at'] as string),
|
||||
current_status_text: item['current_status_text'] as string
|
||||
} as MockOrder
|
||||
@@ -210,6 +276,24 @@ class MockService {
|
||||
return []
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理/调试用:获取所有运单(不做用户过滤)
|
||||
*/
|
||||
async getAllOrders(): Promise<MockOrder[]> {
|
||||
const { data, error } = await supa.from('platform_express_waybills')
|
||||
.select('*, ml_orders(*)')
|
||||
.order('created_at', { ascending: false })
|
||||
.execute()
|
||||
|
||||
if (error != null) {
|
||||
console.error('Fetch all orders error:', error)
|
||||
return []
|
||||
}
|
||||
|
||||
const list = data as Array<UTSJSONObject>
|
||||
return this.mapDataToOrders(list)
|
||||
}
|
||||
|
||||
/**
|
||||
* 模拟各种物流场景 (生成生产测试数据)
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user