1. // 默认
    2. public activate$ = new BehaviorSubject(false);
    3. // 登录成功
    4. this.activate$.next(true);
    5. // 处理登录前
    6. authService.activate$.pipe(filter(flag => !flag)).subscribe(flag => {
    7. this.orderList = [
    8. {
    9. id: 'test',
    10. image: '../../../static/logo.svg',
    11. name: '示例商品1',
    12. status: EOrderStatus.UNFULFILLED,
    13. currentPrice: `¥99999.99`,
    14. profitRate: '--',
    15. expectedProfit: `¥99999.99`,
    16. hesitatedTime: '4分钟52秒',
    17. payTime: format(new Date(), 'MM-dd HH:mm:ss'),
    18. accountName: '示例推广位',
    19. accountLogo: '../../../static/logo.svg',
    20. nickName: 'abc',
    21. },
    22. ];
    23. // 判断数据有没有完全加载
    24. this.orderAllLoadingFlag = false;
    25. this.orderNextFlag = false;
    26. this.cardList = this.getCardList(orderSumData);
    27. this.showLoading = false;
    28. });
    29. }, 1000);
    30. // 处理登录后
    31. authService.activate$
    32. .pipe(
    33. filter(flag => {
    34. this.isLogin = flag;
    35. return flag;
    36. }),
    37. switchMap(flag => {
    38. return this.searchStream$.pipe(
    39. switchMap(searchStream => {
    40. this.orderNextFlag = true;
    41. this.showLoading = true;
    42. if (searchStream.pi === 1) {
    43. this.orderList = [];
    44. // 跳回滚动顶部
    45. uni.pageScrollTo({
    46. scrollTop: 0,
    47. duration: 0,
    48. });
    49. }
    50. return zip(
    51. from(
    52. taobaoOrderService.getOrders({
    53. ...searchStream,
    54. }),
    55. ),
    56. from(
    57. taobaoOrderService.getOrdersTotal({
    58. ...searchStream,
    59. }),
    60. ),
    61. );
    62. }),
    63. );
    64. }),
    65. )
    66. .subscribe(result => {
    67. const orderData = result[0];
    68. const orderSumData = result[1];
    69. const newOrderList = this.renderOrderList(orderData.data);
    70. if (this.searchStream$.value.pi === 1) {
    71. this.orderList = newOrderList;
    72. uni.stopPullDownRefresh();
    73. } else {
    74. this.orderList = [...this.orderList, ...newOrderList];
    75. }
    76. // 判断数据有没有完全加载
    77. this.orderAllLoadingFlag = orderData.total === this.orderList.length;
    78. this.orderNextFlag = false;
    79. this.cardList = this.getCardList(orderSumData);
    80. this.showLoading = false;
    81. });

    有时,登录前后代码经常会使用一套逻辑来请求数据。登录前后要求展示的数据并不相同,更确切的说,登录前,我们希望直接给出假数据,登录后才会执行请求。
    代码中,我们可以根据登录前后发射的状态作为filter去进行数据的处理。