397 行
10 KiB
Vue
397 行
10 KiB
Vue
<template>
|
||
<view class="list fc-page">
|
||
<!-- 规范化顶部导航 -->
|
||
<fc-navbar :title="pageTitle || '分类信息'" theme="gradient" :show-back="true" />
|
||
|
||
<!-- 分类横滑:仅在未指定具体分类的全量列表场景下显示;展示具体分类时隐藏,保持页面聚焦清爽 -->
|
||
<scroll-view v-if="!isSpecificCategory" class="list__cats" scroll-x :show-scrollbar="false">
|
||
<view class="list__cats-inner">
|
||
<view v-for="c in categories" :key="c.id" class="list__cat"
|
||
:class="{ 'list__cat--active': categoryId === c.id }" @click="changeCategory(c)">
|
||
<image class="list__cat-icon" :src="catIcon(c)" mode="aspectFit" />
|
||
<text>{{ c.name }}</text>
|
||
</view>
|
||
</view>
|
||
</scroll-view>
|
||
|
||
<!-- 多维筛选 -->
|
||
<view class="list__filters">
|
||
<view v-for="s in sorts" :key="s.value" class="list__filter"
|
||
:class="{ 'list__filter--active': sort === s.value }" @click="changeSort(s.value)">
|
||
<text>{{ s.label }}</text>
|
||
</view>
|
||
<fc-area-picker
|
||
class="list__filter-area"
|
||
:areas="areaRows"
|
||
:value="areaId"
|
||
allow-all
|
||
variant="chip"
|
||
placeholder="区域"
|
||
@change="onAreaChange"
|
||
/>
|
||
</view>
|
||
|
||
<!-- 骨架屏加载 -->
|
||
<view v-if="loading && !list.length" class="list__loading">
|
||
<view v-for="i in 3" :key="i" class="fc-skeleton list__skeleton"></view>
|
||
</view>
|
||
|
||
<fc-empty v-else-if="!list.length" text="该分类下暂无信息" btn-text="发布一条" @action="goCreate" />
|
||
|
||
<view v-else>
|
||
<fc-post-card v-for="p in list" :key="p.id" :post="p" @click="goDetail" />
|
||
<view class="list__more">
|
||
<text v-if="loading">加载中…</text>
|
||
<text v-else-if="!hasMore">— 没有更多了 —</text>
|
||
<text v-else @click="loadMore">加载更多</text>
|
||
</view>
|
||
</view>
|
||
|
||
<view class="list__fab" @click="goCreate">
|
||
<text>+ 发布</text>
|
||
</view>
|
||
</view>
|
||
</template>
|
||
|
||
<script>
|
||
import FcNavbar from '@/components/fc-navbar/fc-navbar.vue'
|
||
import FcPostCard from '@/components/fc-post-card/fc-post-card.vue'
|
||
import FcEmpty from '@/components/fc-empty/fc-empty.vue'
|
||
import FcAreaPicker from '@/components/fc-area-picker/fc-area-picker.vue'
|
||
import homeApi from '@/common/api/home.js'
|
||
import { iconUrl, categoryIconUrl, catIcon, loadUiConfig } from '@/common/store/ui.js'
|
||
|
||
export default {
|
||
components: { FcNavbar, FcPostCard, FcEmpty, FcAreaPicker },
|
||
data() {
|
||
return {
|
||
categories: [],
|
||
categoryId: 0,
|
||
// 固定具体分类ID(由路由传入,用于区分是否是进入具体分类列表)
|
||
fixedCategoryId: 0,
|
||
pageTitle: '',
|
||
list: [],
|
||
page: 1,
|
||
size: 10,
|
||
hasMore: true,
|
||
loading: false,
|
||
// 请求序号:快速切换分类/排序/区域时丢弃过期响应
|
||
reqSeq: 0,
|
||
sort: 'new',
|
||
// 区域筛选:业务只按服务区域(区县)筛选
|
||
areaId: 0,
|
||
areaText: '',
|
||
manualArea: false,
|
||
lat: null,
|
||
lng: null,
|
||
// 平铺服务区域(每条自带省/市/区县)
|
||
areaRows: [],
|
||
sorts: [
|
||
{ label: '最新', value: 'new' },
|
||
{ label: '最热', value: 'hot' },
|
||
{ label: '价格', value: 'price' },
|
||
],
|
||
}
|
||
},
|
||
computed: {
|
||
/**
|
||
* 判断是否为具体分类展示模式
|
||
* 当携带了具体 categoryId 进入时即为具体分类页,自动隐藏顶部横向切换栏
|
||
* @returns {boolean} 是否为具体分类页面
|
||
*/
|
||
isSpecificCategory() {
|
||
return !!this.fixedCategoryId
|
||
},
|
||
},
|
||
/**
|
||
* 页面加载:读取分类入参并彻底防止 URL 编码乱码
|
||
* @param {Object} query 路由参数
|
||
*/
|
||
onLoad(query) {
|
||
this.categoryId = Number(query.categoryId || 0)
|
||
this.fixedCategoryId = Number(query.categoryId || 0)
|
||
if (query.name) {
|
||
const decoded = this.safeDecode(query.name)
|
||
this.pageTitle = decoded
|
||
uni.setNavigationBarTitle({ title: decoded })
|
||
}
|
||
loadUiConfig()
|
||
this.init()
|
||
},
|
||
onReachBottom() {
|
||
this.loadMore()
|
||
},
|
||
methods: {
|
||
iconUrl,
|
||
categoryIconUrl,
|
||
catIcon,
|
||
/**
|
||
* 彻底安全解码 URL 参数(防一层、二层及多层 URL 编码导致的 %E6%95%... 乱码)
|
||
* @param {string} str 待解码的字符串
|
||
* @returns {string} 解码后的文本
|
||
*/
|
||
safeDecode(str) {
|
||
let res = String(str || '').trim()
|
||
try {
|
||
while (/%[0-9a-fA-F]{2}/.test(res)) {
|
||
const next = decodeURIComponent(res)
|
||
if (next === res) break
|
||
res = next
|
||
}
|
||
} catch (e) {}
|
||
return res
|
||
},
|
||
/**
|
||
* 页面初始化:拉取分类与区域筛选项,并执行首次信息流拉取
|
||
*/
|
||
async init() {
|
||
// 若是具体分类页面,无须加载全部分类横滑列表;非具体分类才需要拉取
|
||
if (!this.fixedCategoryId) {
|
||
try {
|
||
this.categories = await homeApi.categories()
|
||
} catch (e) {}
|
||
} else {
|
||
// 若未传分类名或标题仍残留编码字符,通过接口查到名称补齐/修正导航栏标题
|
||
try {
|
||
const cats = await homeApi.categories()
|
||
const current = (cats || []).find((c) => Number(c.id) === Number(this.categoryId))
|
||
if (current && (!this.pageTitle || /%[0-9a-fA-F]{2}/.test(this.pageTitle))) {
|
||
this.pageTitle = current.name
|
||
uni.setNavigationBarTitle({ title: current.name })
|
||
}
|
||
} catch (e) {}
|
||
}
|
||
// 服务区域:后台「区域管理」下发(平铺:每条自带省/市/区县),失败静默
|
||
try {
|
||
const rows = await homeApi.areas()
|
||
this.areaRows = Array.isArray(rows) ? rows : []
|
||
} catch (e) {}
|
||
this.fetch(true)
|
||
// LBS:按定位匹配默认区域(失败/无权限静默,不阻塞列表)
|
||
this.fetchLocation()
|
||
},
|
||
/**
|
||
* 取用户坐标并匹配区域(gcj02)
|
||
*/
|
||
fetchLocation() {
|
||
if (typeof uni === 'undefined' || !uni.getLocation) return
|
||
uni.getLocation({
|
||
type: 'gcj02',
|
||
success: (res) => {
|
||
this.lat = res.latitude
|
||
this.lng = res.longitude
|
||
this.applyLocationArea().finally(() => {
|
||
this.page = 1
|
||
this.fetch(true)
|
||
})
|
||
},
|
||
fail: () => {},
|
||
})
|
||
},
|
||
/**
|
||
* 把定位坐标转换成区域筛选默认值(用户手动切换过则不覆盖)
|
||
*/
|
||
async applyLocationArea() {
|
||
if (this.manualArea || this.lat == null || this.lng == null) return
|
||
try {
|
||
const found = await homeApi.locate({ lat: this.lat, lng: this.lng })
|
||
if (found && found.areaId) {
|
||
this.areaId = Number(found.areaId) || 0
|
||
this.areaText = found.district || ''
|
||
}
|
||
} catch (e) {}
|
||
},
|
||
/**
|
||
* 分页获取分类信息列表
|
||
* @param {boolean} reset 是否重置分页重新拉取第一页
|
||
*/
|
||
async fetch(reset = false) {
|
||
const seq = ++this.reqSeq
|
||
this.loading = true
|
||
try {
|
||
const res = await homeApi.feed({
|
||
page: this.page,
|
||
size: this.size,
|
||
categoryId: this.categoryId || undefined,
|
||
sort: this.sort,
|
||
areaId: this.areaId || undefined,
|
||
})
|
||
// 期间又切换了筛选条件:以最后一次请求为准
|
||
if (seq !== this.reqSeq) return
|
||
this.list = reset ? res.list : this.list.concat(res.list)
|
||
this.hasMore = res.hasMore
|
||
} catch (e) {
|
||
if (seq !== this.reqSeq) return
|
||
uni.showToast({ title: '加载失败', icon: 'none' })
|
||
} finally {
|
||
if (seq === this.reqSeq) this.loading = false
|
||
}
|
||
},
|
||
/**
|
||
* 滚动触底加载下一页数据
|
||
*/
|
||
loadMore() {
|
||
if (!this.hasMore || this.loading) return
|
||
this.page += 1
|
||
this.fetch()
|
||
},
|
||
/**
|
||
* 切换当前选中的分类(全量列表视角)
|
||
* @param {Object} c 选中的分类项
|
||
*/
|
||
changeCategory(c) {
|
||
this.categoryId = this.categoryId === c.id ? 0 : c.id
|
||
this.page = 1
|
||
this.fetch(true)
|
||
},
|
||
/**
|
||
* 切换排序方式
|
||
* @param {string} v 排序字段(new/hot/price)
|
||
*/
|
||
changeSort(v) {
|
||
if (this.sort === v) return
|
||
this.sort = v
|
||
this.page = 1
|
||
this.fetch(true)
|
||
},
|
||
/**
|
||
* 级联选择服务区域(null = 全部区域)
|
||
* @param {Object|null} area
|
||
*/
|
||
onAreaChange(area) {
|
||
this.areaId = area ? Number(area.areaId) || 0 : 0
|
||
this.areaText = area ? area.district || '' : ''
|
||
this.manualArea = true
|
||
this.page = 1
|
||
this.fetch(true)
|
||
},
|
||
/** 使用当前位置:清除手动选择,重新定位并匹配区域 */
|
||
useCurrentLocation() {
|
||
this.manualArea = false
|
||
this.fetchLocation()
|
||
uni.showToast({ title: '已按当前位置匹配', icon: 'none' })
|
||
},
|
||
/**
|
||
* 跳转到信息详情页
|
||
* @param {Object} p 信息项
|
||
*/
|
||
goDetail(p) {
|
||
uni.navigateTo({ url: `/pages/detail/detail?id=${p.id}` })
|
||
},
|
||
/**
|
||
* 跳转到信息发布页(自动携带当前选中的分类参数)
|
||
*/
|
||
goCreate() {
|
||
const targetCatId = Number(this.categoryId || this.fixedCategoryId || 0)
|
||
const url = targetCatId > 0 ? `/pages/post/create?categoryId=${targetCatId}` : '/pages/post/create'
|
||
uni.navigateTo({ url })
|
||
},
|
||
},
|
||
}
|
||
</script>
|
||
|
||
<style lang="scss" scoped>
|
||
.list__cats {
|
||
background: #ffffff;
|
||
white-space: nowrap;
|
||
padding: 16rpx 0;
|
||
border-bottom: 2rpx solid $fc-border;
|
||
}
|
||
.list__cats-inner {
|
||
display: inline-flex;
|
||
padding: 0 16rpx;
|
||
}
|
||
.list__cat {
|
||
margin: 0 10rpx;
|
||
padding: 10rpx 24rpx;
|
||
border-radius: $fc-radius-pill;
|
||
background: $fc-bg;
|
||
font-size: $fc-font-sm;
|
||
color: $fc-text-regular;
|
||
flex: 0 0 auto;
|
||
display: inline-flex;
|
||
align-items: center;
|
||
&--active {
|
||
background: $fc-gradient-primary;
|
||
color: $fc-primary-ink;
|
||
font-weight: 600;
|
||
box-shadow: 0 6rpx 16rpx rgba(4, 48, 30, 0.3);
|
||
}
|
||
}
|
||
.list__cat-icon {
|
||
width: 34rpx;
|
||
height: 34rpx;
|
||
margin-right: 8rpx;
|
||
}
|
||
.list {
|
||
min-height: 100vh;
|
||
padding-bottom: calc(120rpx + env(safe-area-inset-bottom));
|
||
background: #f8fafc;
|
||
}
|
||
.list__cats {
|
||
background: #ffffff;
|
||
white-space: nowrap;
|
||
padding: 16rpx 0;
|
||
border-bottom: 1rpx solid rgba(0, 0, 0, 0.05);
|
||
position: sticky;
|
||
top: 0;
|
||
z-index: 20;
|
||
}
|
||
.list__cat {
|
||
cursor: pointer;
|
||
transition: all 0.15s ease;
|
||
&:active {
|
||
transform: scale(0.95);
|
||
}
|
||
}
|
||
.list__filters {
|
||
display: flex;
|
||
align-items: center;
|
||
padding: 18rpx 28rpx;
|
||
background: #ffffff;
|
||
margin-bottom: 20rpx;
|
||
border-bottom: 1rpx solid rgba(0, 0, 0, 0.04);
|
||
}
|
||
.list__filter {
|
||
cursor: pointer;
|
||
transition: color 0.15s ease;
|
||
}
|
||
.list__filter {
|
||
margin-right: 28rpx;
|
||
font-size: $fc-font;
|
||
color: $fc-text-secondary;
|
||
&--active {
|
||
color: $fc-primary-text;
|
||
font-weight: 700;
|
||
}
|
||
}
|
||
.list__filter-area {
|
||
margin-left: auto;
|
||
}
|
||
.list__loading {
|
||
padding: 24rpx;
|
||
}
|
||
.list__skeleton {
|
||
height: 220rpx;
|
||
margin-bottom: 20rpx;
|
||
}
|
||
.list__more {
|
||
text-align: center;
|
||
padding: 24rpx 0 40rpx;
|
||
font-size: $fc-font-sm;
|
||
color: $fc-text-placeholder;
|
||
}
|
||
.list__fab {
|
||
position: fixed;
|
||
right: 32rpx;
|
||
bottom: 60rpx;
|
||
padding: 20rpx 36rpx;
|
||
border-radius: $fc-radius-pill;
|
||
background: $fc-gradient-primary;
|
||
color: $fc-primary-ink;
|
||
font-size: $fc-font;
|
||
font-weight: 700;
|
||
box-shadow: 0 10rpx 28rpx rgba(4, 48, 30, 0.4);
|
||
}
|
||
</style>
|