功能:新增微信小程序界面
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
const tagApi = require('../../api/tag')
|
||||
const productApi = require('../../api/product')
|
||||
const { buildProductCards } = require('../../utils/product')
|
||||
const navigation = require('../../utils/navigation')
|
||||
const { runWithLoading } = require('../../utils/loading')
|
||||
|
||||
Page({
|
||||
data: {
|
||||
initialized: false,
|
||||
loading: false,
|
||||
categories: ['全部'],
|
||||
selectedCategory: '全部',
|
||||
searchKeyword: '',
|
||||
products: [],
|
||||
activeBannerText: ''
|
||||
},
|
||||
|
||||
onLoad(options) {
|
||||
this.bootstrapPage(options || {})
|
||||
},
|
||||
|
||||
onShow() {
|
||||
const app = getApp()
|
||||
const pendingFilter = app && app.globalData ? app.globalData.pendingProductFilter : null
|
||||
if (this.data.initialized && pendingFilter && (pendingFilter.keyword || pendingFilter.tag)) {
|
||||
if (app && app.globalData) {
|
||||
app.globalData.pendingProductFilter = null
|
||||
}
|
||||
this.applyFilter(pendingFilter)
|
||||
}
|
||||
},
|
||||
|
||||
async onPullDownRefresh() {
|
||||
await this.loadProducts()
|
||||
wx.stopPullDownRefresh()
|
||||
},
|
||||
|
||||
async bootstrapPage(options) {
|
||||
this.setData({
|
||||
loading: true
|
||||
})
|
||||
|
||||
try {
|
||||
await this.loadTags()
|
||||
await this.applyInitialFilter(options)
|
||||
this.setData({
|
||||
initialized: true
|
||||
})
|
||||
} catch (error) {
|
||||
wx.showToast({
|
||||
title: error.message || '作品列表加载失败',
|
||||
icon: 'none'
|
||||
})
|
||||
} finally {
|
||||
this.setData({
|
||||
loading: false
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
async loadTags() {
|
||||
const tags = await tagApi.getAllTags()
|
||||
const names = Array.isArray(tags) ? tags.map(item => item.tagName).filter(Boolean) : []
|
||||
this.setData({
|
||||
categories: ['全部'].concat(names)
|
||||
})
|
||||
},
|
||||
|
||||
async applyInitialFilter(options) {
|
||||
const app = getApp()
|
||||
const pendingFilter = app && app.globalData ? app.globalData.pendingProductFilter : null
|
||||
if (pendingFilter && (pendingFilter.keyword || pendingFilter.tag)) {
|
||||
if (app && app.globalData) {
|
||||
app.globalData.pendingProductFilter = null
|
||||
}
|
||||
await this.applyFilter(pendingFilter)
|
||||
return
|
||||
}
|
||||
|
||||
await this.applyFilter(options || {})
|
||||
},
|
||||
|
||||
async applyFilter(filter) {
|
||||
const keyword = (filter.keyword || '').trim()
|
||||
const tag = (filter.tag || '').trim()
|
||||
this.setData({
|
||||
searchKeyword: keyword,
|
||||
selectedCategory: tag && this.data.categories.includes(tag) ? tag : '全部'
|
||||
})
|
||||
await this.loadProducts()
|
||||
},
|
||||
|
||||
async loadProducts() {
|
||||
this.setData({
|
||||
loading: true
|
||||
})
|
||||
|
||||
try {
|
||||
let products = []
|
||||
const keyword = this.data.searchKeyword.trim()
|
||||
const tag = this.data.selectedCategory
|
||||
|
||||
if (keyword) {
|
||||
products = await productApi.searchProducts(keyword)
|
||||
} else if (tag !== '全部') {
|
||||
products = await productApi.getProductsByTag(tag)
|
||||
} else {
|
||||
products = await productApi.getAllProducts()
|
||||
}
|
||||
|
||||
const cards = await buildProductCards(products || [])
|
||||
this.setData({
|
||||
products: cards,
|
||||
activeBannerText: keyword
|
||||
? `正在显示“${keyword}”的搜索结果`
|
||||
: (tag !== '全部' ? `当前按“${tag}”筛选` : '')
|
||||
})
|
||||
} catch (error) {
|
||||
this.setData({
|
||||
products: [],
|
||||
activeBannerText: ''
|
||||
})
|
||||
wx.showToast({
|
||||
title: error.message || '作品列表加载失败',
|
||||
icon: 'none'
|
||||
})
|
||||
} finally {
|
||||
this.setData({
|
||||
loading: false
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
handleSearchInput(event) {
|
||||
this.setData({
|
||||
searchKeyword: event.detail.value
|
||||
})
|
||||
},
|
||||
|
||||
async handleSearchConfirm() {
|
||||
this.setData({
|
||||
selectedCategory: '全部'
|
||||
})
|
||||
await runWithLoading(() => this.loadProducts())
|
||||
},
|
||||
|
||||
async handleSelectCategory(event) {
|
||||
const category = event.currentTarget.dataset.category
|
||||
this.setData({
|
||||
selectedCategory: category,
|
||||
searchKeyword: ''
|
||||
})
|
||||
await runWithLoading(() => this.loadProducts())
|
||||
},
|
||||
|
||||
async handleResetFilters() {
|
||||
this.setData({
|
||||
selectedCategory: '全部',
|
||||
searchKeyword: ''
|
||||
})
|
||||
await runWithLoading(() => this.loadProducts())
|
||||
},
|
||||
|
||||
handleProductSelect(event) {
|
||||
const product = event.detail || {}
|
||||
if (!product.id) {
|
||||
return
|
||||
}
|
||||
|
||||
navigation.navigate('/pages/product-detail/index', {
|
||||
id: product.id
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"usingComponents": {
|
||||
"navigation-bar": "/components/navigation-bar/navigation-bar",
|
||||
"product-card": "/components/product-card/product-card"
|
||||
},
|
||||
"enablePullDownRefresh": true
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<navigation-bar title="作品库" back="{{false}}" color="#2d2430" background="#fff7f8"></navigation-bar>
|
||||
|
||||
<view class="page-shell product-page">
|
||||
<view class="page-content">
|
||||
<view class="page-hero product-hero">
|
||||
<text class="hero-kicker">作品筛选</text>
|
||||
<text class="hero-title">{{selectedCategory !== '全部' ? selectedCategory : '找到适合你的美甲款式'}}</text>
|
||||
<text class="hero-desc">{{activeBannerText ? activeBannerText : '按标签筛选或输入关键词搜索,先缩小范围,再挑喜欢的作品。'}}</text>
|
||||
|
||||
<view class="search-card">
|
||||
<view class="search-row">
|
||||
<input
|
||||
class="search-input"
|
||||
placeholder="搜索作品名称或关键词"
|
||||
placeholder-class="field-placeholder"
|
||||
value="{{searchKeyword}}"
|
||||
bindinput="handleSearchInput"
|
||||
bindconfirm="handleSearchConfirm"
|
||||
confirm-type="search"
|
||||
/>
|
||||
<view class="search-btn" bindtap="handleSearchConfirm">搜索</view>
|
||||
</view>
|
||||
|
||||
<scroll-view class="category-scroll" scroll-x enable-flex>
|
||||
<view class="category-list">
|
||||
<view
|
||||
wx:for="{{categories}}"
|
||||
wx:key="*this"
|
||||
class="category-item {{selectedCategory === item ? 'active' : ''}}"
|
||||
data-category="{{item}}"
|
||||
bindtap="handleSelectCategory"
|
||||
>
|
||||
{{item}}
|
||||
</view>
|
||||
</view>
|
||||
</scroll-view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="results-head">
|
||||
<view class="results-count">{{products.length ? products.length : 0}} 个结果</view>
|
||||
<view wx:if="{{searchKeyword || selectedCategory !== '全部'}}" class="ghost-btn results-reset" bindtap="handleResetFilters">重置筛选</view>
|
||||
</view>
|
||||
|
||||
<view wx:if="{{activeBannerText}}" class="filter-banner">{{activeBannerText}}</view>
|
||||
|
||||
<view wx:if="{{products.length}}" class="product-grid">
|
||||
<view wx:for="{{products}}" wx:key="id" class="product-grid-item">
|
||||
<product-card item="{{item}}" show-stats="{{true}}" bindselect="handleProductSelect"></product-card>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view wx:elif="{{loading}}" class="page-card empty-card">
|
||||
<text class="empty-title">正在加载作品</text>
|
||||
</view>
|
||||
|
||||
<view wx:else class="page-card empty-card">
|
||||
<text class="empty-title">暂时没有匹配的作品</text>
|
||||
<text class="empty-desc">可以换个关键词试试,或者先浏览全部作品。</text>
|
||||
<view class="ghost-btn" bindtap="handleResetFilters">查看全部</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
@@ -0,0 +1,117 @@
|
||||
.product-page {
|
||||
background:
|
||||
radial-gradient(circle at top left, rgba(255, 207, 220, 0.35), transparent 34%),
|
||||
linear-gradient(180deg, #fff8fa 0%, #fffdfc 100%);
|
||||
}
|
||||
|
||||
.product-hero {
|
||||
background:
|
||||
radial-gradient(circle at top right, rgba(255, 194, 211, 0.42), transparent 34%),
|
||||
linear-gradient(145deg, #fffdfd 0%, #fff3f7 56%, #ffe7ef 100%);
|
||||
}
|
||||
|
||||
.search-card {
|
||||
margin-top: 28rpx;
|
||||
padding: 22rpx;
|
||||
border-radius: 28rpx;
|
||||
background: rgba(255, 255, 255, 0.78);
|
||||
}
|
||||
|
||||
.search-row {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 14rpx;
|
||||
}
|
||||
|
||||
.search-input {
|
||||
flex: 1;
|
||||
height: 76rpx;
|
||||
padding: 0 24rpx;
|
||||
border-radius: 999rpx;
|
||||
background: #fbf5f7;
|
||||
color: #2d2430;
|
||||
font-size: 28rpx;
|
||||
}
|
||||
|
||||
.search-btn {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
min-width: 132rpx;
|
||||
height: 76rpx;
|
||||
border-radius: 999rpx;
|
||||
background: linear-gradient(135deg, #ff8cb0 0%, #c6597d 100%);
|
||||
color: #ffffff;
|
||||
font-size: 26rpx;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.category-scroll {
|
||||
width: 100%;
|
||||
margin-top: 24rpx;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.category-list {
|
||||
display: inline-flex;
|
||||
gap: 16rpx;
|
||||
padding-bottom: 8rpx;
|
||||
}
|
||||
|
||||
.category-item {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
min-width: 120rpx;
|
||||
height: 60rpx;
|
||||
padding: 0 22rpx;
|
||||
border-radius: 999rpx;
|
||||
background: #f7edf1;
|
||||
color: #8d7a84;
|
||||
font-size: 24rpx;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.category-item.active {
|
||||
background: #fde8ef;
|
||||
color: #c6597d;
|
||||
}
|
||||
|
||||
.results-head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 16rpx;
|
||||
margin-top: 24rpx;
|
||||
}
|
||||
|
||||
.results-count {
|
||||
font-size: 26rpx;
|
||||
font-weight: 700;
|
||||
color: #5d4b55;
|
||||
}
|
||||
|
||||
.results-reset {
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.filter-banner {
|
||||
margin-top: 20rpx;
|
||||
padding: 20rpx 24rpx;
|
||||
border-radius: 22rpx;
|
||||
background: #fff1f5;
|
||||
color: #b45172;
|
||||
font-size: 24rpx;
|
||||
line-height: 1.7;
|
||||
}
|
||||
|
||||
.product-grid {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 18rpx;
|
||||
margin-top: 24rpx;
|
||||
}
|
||||
|
||||
.product-grid-item {
|
||||
width: calc(50% - 9rpx);
|
||||
}
|
||||
Reference in New Issue
Block a user