文件
fenleixinxi/uniapp/pages/favorite/follows.vue

358 行
7.8 KiB
Vue

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

<template>
<view class="follows-page fc-page">
<!-- 统一顶部深绿沉浸式导航栏 -->
<fc-navbar title="我的关注" theme="gradient" :show-back="true" />
<!-- 骨架屏加载中 -->
<view v-if="loading && !list.length" class="follows-page__loading">
<view v-for="i in 4" :key="i" class="fc-card follows-page__skeleton-card">
<view class="fc-skeleton follows-page__sk-avatar"></view>
<view class="follows-page__sk-meta">
<view class="fc-skeleton follows-page__sk-line" style="width: 200rpx; height: 32rpx;"></view>
<view class="fc-skeleton follows-page__sk-line" style="width: 140rpx; height: 24rpx; margin-top: 14rpx;"></view>
</view>
<view class="fc-skeleton follows-page__sk-btn"></view>
</view>
</view>
<!-- 空数据提示 -->
<fc-empty
v-else-if="!list.length"
icon="bar-fav"
text="暂无关注的同城用户或商家"
btn-text="去同城圈逛逛"
@action="goHome"
/>
<!-- 关注列表主体 -->
<view v-else class="follows-page__list">
<view
v-for="item in list"
:key="item.id || item.userId"
class="fc-card follows-page__card"
@click="goTarget(item)"
>
<!-- 头像与标识 -->
<view class="follows-page__avatar-wrap">
<image
v-if="item.avatar"
class="follows-page__avatar follows-page__avatar--img"
:src="assetUrl(item.avatar)"
mode="aspectFill"
/>
<view v-else class="follows-page__avatar follows-page__avatar--char">
{{ (item.nickname || '同').slice(0, 1) }}
</view>
<view v-if="item.isMerchant" class="follows-page__badge-icon">
<image class="fc-icon fc-icon--xs" :src="iconUrl('shield') || iconUrl('check')" mode="aspectFit" />
</view>
</view>
<!-- 信息主体 -->
<view class="follows-page__meta">
<view class="follows-page__name-row">
<text class="follows-page__name fc-ellipsis">{{ item.nickname || '同城用户' }}</text>
<text v-if="item.isMerchant" class="follows-page__tag follows-page__tag--merchant">
{{ item.category || '认证商家' }}
</text>
<text v-else class="follows-page__tag follows-page__tag--person">个人用户</text>
</view>
<text class="follows-page__sub">
累计发布 {{ item.postCount || 0 }} 条同城信息
</text>
</view>
<!-- 关注/取消关注按钮 -->
<view
class="follows-page__btn"
:class="{ 'follows-page__btn--followed': item.followed }"
@click.stop="toggleFollow(item)"
>
<text>{{ item.followed ? '已关注' : '+ 关注' }}</text>
</view>
</view>
<!-- 触底分页状态 -->
<view class="follows-page__more">
<text v-if="loading">加载中…</text>
<text v-else-if="!hasMore">— 没有更多了 —</text>
<text v-else @click="loadMore">加载更多</text>
</view>
</view>
</view>
</template>
<script>
/**
* 我的关注页面
* 展示当前用户关注的商家与个人列表,支持跳转对应的主页与一键切换关注状态
*/
import FcNavbar from '@/components/fc-navbar/fc-navbar.vue'
import FcEmpty from '@/components/fc-empty/fc-empty.vue'
import favoriteApi from '@/common/api/favorite.js'
import { iconUrl } from '@/common/store/ui.js'
import { assetUrl } from '@/common/utils/format.js'
export default {
components: { FcNavbar, FcEmpty },
data() {
return {
list: [],
page: 1,
size: 10,
hasMore: true,
loading: false,
}
},
onLoad() {
this.fetch(true)
},
onShow() {
// 返回时静默拉取第一页最新状态
this.fetch(true)
},
onPullDownRefresh() {
this.fetch(true).finally(() => {
uni.stopPullDownRefresh()
})
},
onReachBottom() {
this.loadMore()
},
methods: {
iconUrl,
assetUrl,
/**
* 拉取关注列表数据
* @param {boolean} reset 是否重置分页刷新
*/
async fetch(reset = false) {
if (reset) {
this.page = 1
this.hasMore = true
}
this.loading = true
try {
const res = await favoriteApi.followList({ page: this.page, size: this.size })
const arr = Array.isArray(res && res.list) ? res.list : []
this.list = reset ? arr : this.list.concat(arr)
this.hasMore = !!(res && res.hasMore)
} catch (e) {
uni.showToast({ title: e.message || '关注列表加载失败', icon: 'none' })
} finally {
this.loading = false
}
},
/**
* 触底加载下一页
*/
loadMore() {
if (this.loading || !this.hasMore) return
this.page += 1
this.fetch(false)
},
/**
* 切换关注/取消关注
* @param {Object} item 关注项
*/
async toggleFollow(item) {
const prev = item.followed
item.followed = !prev
try {
const res = await favoriteApi.follow({ userId: item.userId })
item.followed = !!(res && res.followed)
uni.showToast({ title: item.followed ? '已关注' : '已取消关注', icon: 'none' })
} catch (e) {
item.followed = prev
uni.showToast({ title: e.message || '操作失败', icon: 'none' })
}
},
/**
* 点击跳转主页(商家跳转商家门店主页,个人跳转个人主页)
* @param {Object} item 用户项
*/
goTarget(item) {
if (!item || !item.userId) return
if (item.isMerchant && item.merchantId) {
uni.navigateTo({
url: `/pages/merchant/detail?id=${item.merchantId}`,
})
} else {
uni.navigateTo({
url: `/pages/user/detail?id=${item.userId}`,
})
}
},
/**
* 返回小程序首页
*/
goHome() {
uni.reLaunch({
url: '/pages/index/index',
})
},
},
}
</script>
<style lang="scss" scoped>
.follows-page {
min-height: 100vh;
background: $fc-bg;
padding-bottom: 40rpx;
}
.follows-page__list {
padding: 10rpx 20rpx 40rpx;
}
.follows-page__card {
display: flex;
align-items: center;
padding: 24rpx;
margin: 16rpx 4rpx;
background: #ffffff;
border-radius: $fc-radius-lg;
box-shadow: 0 4rpx 16rpx rgba(4, 48, 30, 0.04);
transition: all 0.2s ease;
&:active {
background: #fbfdfc;
}
}
.follows-page__avatar-wrap {
position: relative;
width: 96rpx;
height: 96rpx;
flex-shrink: 0;
}
.follows-page__avatar {
width: 96rpx;
height: 96rpx;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
&--img {
background: #f0f0f0;
}
&--char {
background: linear-gradient(135deg, #00e070 0%, #00b85c 100%);
color: #04301e;
font-size: 38rpx;
font-weight: 700;
}
}
.follows-page__badge-icon {
position: absolute;
right: -4rpx;
bottom: -4rpx;
width: 32rpx;
height: 32rpx;
border-radius: 50%;
background: #ffffff;
display: flex;
align-items: center;
justify-content: center;
box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
}
.follows-page__meta {
flex: 1;
min-width: 0;
margin-left: 20rpx;
}
.follows-page__name-row {
display: flex;
align-items: center;
gap: 12rpx;
}
.follows-page__name {
font-size: 30rpx;
font-weight: 700;
color: #04291c;
max-width: 280rpx;
}
.follows-page__tag {
font-size: 20rpx;
padding: 2rpx 12rpx;
border-radius: 6rpx;
font-weight: 600;
&--merchant {
background: #e6f9ef;
color: #04874a;
}
&--person {
background: #f1f5f3;
color: #5c786a;
}
}
.follows-page__sub {
display: block;
font-size: 24rpx;
color: #64748b;
margin-top: 10rpx;
}
.follows-page__btn {
padding: 10rpx 28rpx;
border-radius: 30rpx;
font-size: 24rpx;
font-weight: 600;
background: #edfbf3;
color: #04874a;
border: 1rpx solid #b7edd4;
flex-shrink: 0;
cursor: pointer;
&--followed {
background: #f8faf8;
color: #94a3b8;
border-color: #e2e8f0;
}
}
.follows-page__more {
text-align: center;
font-size: 24rpx;
color: #94a3b8;
padding: 24rpx 0;
}
/* 骨架屏 */
.follows-page__skeleton-card {
display: flex;
align-items: center;
padding: 24rpx;
margin: 16rpx 4rpx;
}
.follows-page__sk-avatar {
width: 96rpx;
height: 96rpx;
border-radius: 50%;
}
.follows-page__sk-meta {
flex: 1;
margin-left: 20rpx;
}
.follows-page__sk-line {
border-radius: 6rpx;
}
.follows-page__sk-btn {
width: 120rpx;
height: 52rpx;
border-radius: 26rpx;
}
</style>