// 默认public activate$ = new BehaviorSubject(false);// 登录成功this.activate$.next(true);// 处理登录前authService.activate$.pipe(filter(flag => !flag)).subscribe(flag => {this.orderList = [{id: 'test',image: '../../../static/logo.svg',name: '示例商品1',status: EOrderStatus.UNFULFILLED,currentPrice: `¥99999.99`,profitRate: '--',expectedProfit: `¥99999.99`,hesitatedTime: '4分钟52秒',payTime: format(new Date(), 'MM-dd HH:mm:ss'),accountName: '示例推广位',accountLogo: '../../../static/logo.svg',nickName: 'abc',},];// 判断数据有没有完全加载this.orderAllLoadingFlag = false;this.orderNextFlag = false;this.cardList = this.getCardList(orderSumData);this.showLoading = false;});}, 1000);// 处理登录后authService.activate$.pipe(filter(flag => {this.isLogin = flag;return flag;}),switchMap(flag => {return this.searchStream$.pipe(switchMap(searchStream => {this.orderNextFlag = true;this.showLoading = true;if (searchStream.pi === 1) {this.orderList = [];// 跳回滚动顶部uni.pageScrollTo({scrollTop: 0,duration: 0,});}return zip(from(taobaoOrderService.getOrders({...searchStream,}),),from(taobaoOrderService.getOrdersTotal({...searchStream,}),),);}),);}),).subscribe(result => {const orderData = result[0];const orderSumData = result[1];const newOrderList = this.renderOrderList(orderData.data);if (this.searchStream$.value.pi === 1) {this.orderList = newOrderList;uni.stopPullDownRefresh();} else {this.orderList = [...this.orderList, ...newOrderList];}// 判断数据有没有完全加载this.orderAllLoadingFlag = orderData.total === this.orderList.length;this.orderNextFlag = false;this.cardList = this.getCardList(orderSumData);this.showLoading = false;});
有时,登录前后代码经常会使用一套逻辑来请求数据。登录前后要求展示的数据并不相同,更确切的说,登录前,我们希望直接给出假数据,登录后才会执行请求。
代码中,我们可以根据登录前后发射的状态作为filter去进行数据的处理。
