import { YTAvoid } from "basic" import { OrderApi } from "../../apis/OrderApi" import { OrderDetailData } from "../../model/OrderModelIndex" import { userOrderQuery } from "../../model/Query" @ObservedV2 export class OrderManagementViewModel{ @Trace safeTop: number = AppStorage.get(YTAvoid.SAFE_TOP_KEY) as number @Trace safeBottom: number = AppStorage.get(YTAvoid.SAFE_BOTTOM_KEY) as number @Trace categoryIndex: number = -1 @Trace isRefresh: boolean = false // 订单列表 @Trace dataSource: OrderDetailData[] = [] @Trace isFirst: boolean = false // 0 2 3 7 categoryList: string[] = ['全部', '待支付', '待收书', '待还书', '已完成'] tabControl: TabsController = new TabsController() query: userOrderQuery = new userOrderQuery() // 订单状态对照 orderStatus: number[] = [0, 2, 3, 7] isLoading: boolean = false constructor() {} // 修改分类 changeCategory(index: number) { if(this.categoryIndex == index) return this.categoryIndex = index this.isRefresh = true this.isFirst = true if(index == 0) { this.query.statusCode = undefined } else { this.query.statusCode = this.orderStatus[index-1] } this.onRefreshing(index) } onRefreshing(index: number){ if(this.isLoading) return this.isLoading = true this.query.reload() this.getOrderList() .then((list: Array) => { if (this.categoryIndex == index) { this.dataSource = list } }) .catch(() => { }) .finally(()=>{ this.isRefresh = false this.isFirst =false this.isLoading = false }) } onReachEnd(index: number){ if(this.isLoading) return this.isLoading = true if(this.query.reachEnd(this.dataSource.length)) { this.getOrderList() .then((list: Array)=>{ if (this.categoryIndex == index) { this.dataSource.push(...list) } }) .catch(()=>{ // 回滚页数 this.query.backPage() }) .finally(()=>{ this.isLoading = false }) } } // 获取订单列表 getOrderList(): Promise { return new Promise((resolve, reject) => { OrderApi.getUserOrderList(this.query) .then(ans => { console.log(`返回的订单列表 = ${JSON.stringify(ans)}`); this.query.setTotal(ans.total) if (ans.records != undefined) { resolve(ans.records); } else { resolve([]) } }) .catch((error: Error) => { console.error('获取订单列表失败:', error); reject(error); }); }); } }