Files
peiwan-uniapp/pages/tabbar/components/trend/layout.vue

180 lines
5.8 KiB
Vue
Raw Normal View History

2025-01-21 01:46:34 +08:00
<!-- 虚拟列表演示(不使用内置列表)(vue) -->
<!-- 写法较简单在页面中对当前需要渲染的虚拟列表数据进行for循环在vue3中兼容性良好 -->
<!-- 在各平台兼容性请查阅https://z-paging.zxlee.cn/module/virtual-list.html -->
<template>
<view class="content">
<!-- 如果页面中的cell高度是固定不变的则不需要设置cell-height-mode如果页面中高度是动态改变的则设置cell-height-mode="dynamic" -->
<!-- 原先的v-model修改为@virtualListChange="virtualListChange"并赋值处理后的虚拟列表 -->
<z-paging ref="paging" defaultPageSize="20" use-virtual-list :force-close-inner-list="true" auto-show-back-to-top :paging-style="{ paddingTop: paddingTop + 'px', paddingBottom: paddingBottom + 'rpx' }" cell-height-mode="dynamic" @virtualListChange="virtualListChange" @query="queryList">
<!-- 需要固定在顶部不滚动的view放在slot="top"的view中如果需要跟着滚动则不要设置slot="top" -->
<template #top>
<nav-bar :title="title" @changeClerk="changeClerk" @tabsChange="tabsChange" @initNav="initNav"></nav-bar>
</template>
<!-- #ifdef MP -->
<view v-if="showSubscribeBtn" class="subscribe-box">
<u-icon name="bell-fill" color="var(--ui-BG-Main)" size="44"></u-icon>
<view class="info">获取实时动态提醒</view>
<view class="sub-btn" @tap="subscribeMessage">立即订阅</view>
</view>
<!-- #endif -->
<user-card v-if="friendEnabled"></user-card>
<!-- :id="`zp-id-${item.zp_index}`":key="item.zp_index" 必须写必须写 -->
<!-- 这里for循环的index不是数组中真实的index了请使用item.zp_index获取真实的index -->
<view class="main-box">
<post-list :virtualList="virtualList"></post-list>
</view>
<!-- 自定义返回顶部view -->
<template #backToTop>
<custom-back-to-top></custom-back-to-top>
</template>
</z-paging>
<fb-popup></fb-popup>
<clerk-popup ref="clerk"></clerk-popup>
</view>
</template>
<script>
import FbPopup from "@/pages/tabbar/components/trend/fb-popup.vue";
import ClerkPopup from "@/pages/tabbar/components/trend/clerk-popup.vue";
import NavBar from '@/pages/tabbar/components/trend/navBar.vue';
import UserCard from '@/pages/tabbar/components/trend/userCard.vue';
import PostList from '@/pages/tabbar/components/trend/postList.vue';
import TrendApi from '@/sheep/api/worker/trend';
import { WxaSubscribeTemplate } from '@/sheep/util/const';
import sheep from '@/sheep';
export default {
components: {
NavBar,
FbPopup,
ClerkPopup,
UserCard,
PostList,
},
props: {
title: {
type: String,
default: '',
}
},
data() {
return {
// 虚拟列表数组,通过@virtualListChange监听获得最新数组
virtualList: [],
paddingTop: 0,
paddingBottom: 100,
height: 0,
queryParams: {
pageNo: 1,
pageSize: 10,
userId: null,
queryType: '2',
},
showSubscribeBtn: false,
}
},
computed: {
userInfo: {
get() {
return sheep.$store('user').userInfo;
},
},
friendEnabled() {
return sheep.$store('user').tradeConfig.friendEnabled
},
},
created() {
// #ifdef MP
// 订阅只能由用户主动触发,只能包一层 showModal 诱导用户点击
this.autoSubscribeMessage();
// #endif
},
methods: {
initNav(e) {
this.height = e.height;
this.paddingTop = this.height;
},
// 监听虚拟列表数组改变并赋值给virtualList进行重新渲染
virtualListChange(vList) {
this.virtualList = vList;
},
queryList(pageNo, pageSize) {
// 组件加载时会自动触发此方法,因此默认页面加载时会自动触发,无需手动调用
// 这里的pageNo和pageSize会自动计算好直接传给服务器即可
// 模拟请求服务器获取分页数据,请替换成自己的网络请求
this.queryParams.pageNo = pageNo;
this.queryParams.pageSize = pageSize;
this.queryParams.userId = this.userInfo.id;
TrendApi.getTrendPage(this.queryParams).then(res => {
// 将请求的结果数组传递给z-paging
this.$refs.paging.complete(res.data.list);
}).catch(res => {
// 如果请求失败写this.$refs.paging.complete(false);
// 注意每次都需要在catch中写这句话很麻烦z-paging提供了方案可以全局统一处理
// 在底层的网络请求抛出异常时写uni.$emit('z-paging-error-emit');即可
this.$refs.paging.complete(false);
})
},
tabsChange(e) {
this.queryParams.queryType = e;
this.$refs.paging.reload();
},
changeClerk() {
this.$refs.clerk.showPopup();
},
subscribeMessage() {
const event = [WxaSubscribeTemplate.TREND_APPLY_SUCCESS];
event.push(WxaSubscribeTemplate.CLERK_BLIND);
event.push(WxaSubscribeTemplate.CLERK_ORDER);
sheep.$platform.useProvider('wechat').subscribeMessage(event, () => {
// 订阅后记录一下订阅状态
uni.removeStorageSync(WxaSubscribeTemplate.TREND_APPLY_SUCCESS);
uni.setStorageSync(WxaSubscribeTemplate.TREND_APPLY_SUCCESS, '已订阅');
// 隐藏订阅按钮
this.showSubscribeBtn = false;
});
},
async autoSubscribeMessage() {
// 1. 校验是否手动订阅过
const subscribeBtnStatus = uni.getStorageSync(WxaSubscribeTemplate.TREND_APPLY_SUCCESS);
if (!subscribeBtnStatus) {
this.showSubscribeBtn = true;
return;
}
},
}
}
</script>
<style lang="scss" scoped>
.main-box {
}
.subscribe-box {
display: flex;
align-items: center;
padding: 10px;
padding-bottom: 0;
justify-content: center;
font-size: 28rpx;
.info {
margin: 0 10rpx;
}
.sub-btn {
color: var(--ui-BG-Main);
}
}
</style>