OrderManagementViewModel.ets 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { YTAvoid } from "basic"
  2. import { OrderApi } from "../../apis/OrderApi"
  3. import { OrderDetailData } from "../../model/OrderModelIndex"
  4. import { userOrderQuery } from "../../model/Query"
  5. @ObservedV2
  6. export class OrderManagementViewModel{
  7. @Trace safeTop: number = AppStorage.get(YTAvoid.SAFE_TOP_KEY) as number
  8. @Trace safeBottom: number = AppStorage.get(YTAvoid.SAFE_BOTTOM_KEY) as number
  9. @Trace categoryIndex: number = -1
  10. @Trace isRefresh: boolean = false
  11. // 订单列表
  12. @Trace dataSource: OrderDetailData[] = []
  13. @Trace isFirst: boolean = false
  14. // 0 2 3 7
  15. categoryList: string[] = ['全部', '待支付', '待收书', '待还书', '已完成']
  16. tabControl: TabsController = new TabsController()
  17. query: userOrderQuery = new userOrderQuery()
  18. // 订单状态对照
  19. orderStatus: number[] = [0, 2, 3, 7]
  20. isLoading: boolean = false
  21. constructor() {}
  22. // 修改分类
  23. changeCategory(index: number) {
  24. if(this.categoryIndex == index) return
  25. this.categoryIndex = index
  26. this.isRefresh = true
  27. this.isFirst = true
  28. if(index == 0) {
  29. this.query.statusCode = undefined
  30. } else {
  31. this.query.statusCode = this.orderStatus[index-1]
  32. }
  33. this.onRefreshing(index)
  34. }
  35. onRefreshing(index: number){
  36. if(this.isLoading) return
  37. this.isLoading = true
  38. this.query.reload()
  39. this.getOrderList()
  40. .then((list: Array<OrderDetailData>) => {
  41. if (this.categoryIndex == index) {
  42. this.dataSource = list
  43. }
  44. })
  45. .catch(() => {
  46. })
  47. .finally(()=>{
  48. this.isRefresh = false
  49. this.isFirst =false
  50. this.isLoading = false
  51. })
  52. }
  53. onReachEnd(index: number){
  54. if(this.isLoading) return
  55. this.isLoading = true
  56. if(this.query.reachEnd(this.dataSource.length)) {
  57. this.getOrderList()
  58. .then((list: Array<OrderDetailData>)=>{
  59. if (this.categoryIndex == index) {
  60. this.dataSource.push(...list)
  61. }
  62. })
  63. .catch(()=>{
  64. // 回滚页数
  65. this.query.backPage()
  66. })
  67. .finally(()=>{
  68. this.isLoading = false
  69. })
  70. }
  71. }
  72. // 获取订单列表
  73. getOrderList(): Promise<OrderDetailData[]> {
  74. return new Promise((resolve, reject) => {
  75. OrderApi.getUserOrderList(this.query)
  76. .then(ans => {
  77. console.log(`返回的订单列表 = ${JSON.stringify(ans)}`);
  78. this.query.setTotal(ans.total)
  79. if (ans.records != undefined) {
  80. resolve(ans.records);
  81. } else {
  82. resolve([])
  83. }
  84. })
  85. .catch((error: Error) => {
  86. console.error('获取订单列表失败:', error);
  87. reject(error);
  88. });
  89. });
  90. }
  91. }