feat: 百科页面
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
// pages/garden/add/index.js
|
||||
import { MOCK_PLANTS, CARE_TASK_ICONS } from '../../../utils/mockData';
|
||||
import request from '../../../utils/request';
|
||||
import { requestSubscription, checkSubscriptionSettings } from '../../../utils/subscribe';
|
||||
|
||||
Page({
|
||||
data: {
|
||||
@@ -236,7 +237,6 @@ Page({
|
||||
placement: newPlantLocation || '',
|
||||
ossIds: [uploadedImageId],
|
||||
carePlans: carePlans,
|
||||
// Default fields as not in UI yet
|
||||
potMaterial: '',
|
||||
potSize: '',
|
||||
sunlight: '',
|
||||
@@ -245,18 +245,40 @@ Page({
|
||||
|
||||
// Submit
|
||||
wx.showLoading({ title: 'Creating...' });
|
||||
request.post('/plant/add', payload).then(res => {
|
||||
request.post('/plant/add', payload).then(async () => {
|
||||
wx.hideLoading();
|
||||
wx.showToast({ title: '添加成功', icon: 'success' });
|
||||
|
||||
// Refresh previous page (e.g. garden list) if needed
|
||||
// const pages = getCurrentPages();
|
||||
// const prevPage = pages[pages.length - 2];
|
||||
// if (prevPage && prevPage.onRefresh) prevPage.onRefresh();
|
||||
// Smart Subscription Check
|
||||
const subStatus = await checkSubscriptionSettings();
|
||||
|
||||
if (subStatus === 'accept') {
|
||||
// Already authorized 'Always', just call API to consume quota
|
||||
requestSubscription().finally(() => {
|
||||
wx.navigateBack();
|
||||
});
|
||||
} else if (subStatus === 'reject' || subStatus === 'ban') {
|
||||
// User rejected 'Always', do not disturb
|
||||
setTimeout(() => wx.navigateBack(), 500);
|
||||
} else {
|
||||
// Not set, ask user
|
||||
wx.showModal({
|
||||
title: '添加成功',
|
||||
content: '是否订阅植物养护提醒?',
|
||||
confirmText: '订阅',
|
||||
cancelText: '暂不',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
requestSubscription().finally(() => {
|
||||
wx.navigateBack();
|
||||
});
|
||||
} else {
|
||||
wx.navigateBack();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
setTimeout(() => {
|
||||
wx.navigateBack();
|
||||
}, 1000);
|
||||
}).catch(err => {
|
||||
wx.hideLoading();
|
||||
console.error('Add plant failed', err);
|
||||
|
||||
@@ -203,6 +203,7 @@ Page({
|
||||
|
||||
// Sync with backend
|
||||
this.fetchTodayTasks();
|
||||
|
||||
}).catch(err => {
|
||||
wx.hideLoading();
|
||||
console.error('Complete task failed', err);
|
||||
|
||||
+92
-25
@@ -1,5 +1,5 @@
|
||||
// pages/wiki/detail/index.js
|
||||
import { MOCK_WIKI } from '../../../utils/mockData';
|
||||
import request from '../../../utils/request';
|
||||
|
||||
Page({
|
||||
data: {
|
||||
@@ -15,38 +15,105 @@ Page({
|
||||
},
|
||||
|
||||
loadPlantDetail(id) {
|
||||
// Find plant in MOCK_WIKI
|
||||
const plant = MOCK_WIKI.find(p => p.id === id);
|
||||
if (plant) {
|
||||
// Fetch detail via wiki/page with specific ID
|
||||
// Since there's no /wiki/detail endpoint, we use /wiki/page to get it
|
||||
request.post('/wiki/page', {
|
||||
current: 1,
|
||||
pageSize: 1,
|
||||
id: id
|
||||
}).then(res => {
|
||||
const data = res || {};
|
||||
const list = data.list || [];
|
||||
const item = list.length > 0 ? list[0] : null;
|
||||
|
||||
if (!item) {
|
||||
wx.showToast({ title: '未找到该植物', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
|
||||
// Set Page Title
|
||||
wx.setNavigationBarTitle({
|
||||
title: plant.name
|
||||
});
|
||||
wx.setNavigationBarTitle({ title: item.name });
|
||||
|
||||
// Prepare swiper list
|
||||
const swiperList = (plant.images || []).map(img => {
|
||||
return (img.indexOf('http') === 0 || img.indexOf('wxfile') === 0) ? img : `/assets/${img}`;
|
||||
});
|
||||
// Prepare swiper list from imgList
|
||||
const swiperList = (item.imgList || []).map(img => img.url);
|
||||
|
||||
// Ensure some default values if missing
|
||||
const enrichedPlant = {
|
||||
...plant,
|
||||
light: plant.light || { level: '中等', description: '适合明亮的散射光' },
|
||||
water: plant.water || { frequency: '每周1-2次', description: '保持土壤微润' },
|
||||
temperature: plant.temperature || '15-28℃',
|
||||
humidity: plant.humidity || '50%-70%',
|
||||
soil: plant.soil || '疏松透气的营养土',
|
||||
fertilizer: plant.fertilizer || '生长季每月施一次薄肥',
|
||||
toxicity: plant.toxicity || '无毒',
|
||||
commonPests: plant.commonPests || ['红蜘蛛'],
|
||||
careTips: plant.careTips || ['注意通风', '定期清洁叶面']
|
||||
// Parse pest/disease list
|
||||
const commonPests = item.pestsDiseases
|
||||
? item.pestsDiseases.split(',').map(s => s.trim()).filter(Boolean)
|
||||
: [];
|
||||
|
||||
// Parse aliases
|
||||
const aliasesList = item.aliases
|
||||
? item.aliases.split(/[,,、]/).map(s => s.trim()).filter(Boolean)
|
||||
: [];
|
||||
|
||||
// Parse reproduction methods
|
||||
const reproductionList = item.reproductionMethod
|
||||
? item.reproductionMethod.split(/[,,、]/).map(s => s.trim()).filter(Boolean)
|
||||
: [];
|
||||
|
||||
// Difficulty label
|
||||
const diffLabels = { 1: '简单', 2: '中等', 3: '较难', 4: '困难', 5: '专家' };
|
||||
|
||||
// Map API data to display model
|
||||
const plant = {
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
latinName: item.latinName || '',
|
||||
aliases: item.aliases || '',
|
||||
aliasesList: aliasesList,
|
||||
genus: item.genus || '',
|
||||
distributionArea: item.distributionArea || '',
|
||||
difficulty: item.difficulty || 0,
|
||||
difficultyLabel: diffLabels[item.difficulty] || '未知',
|
||||
isHot: item.isHot === 1,
|
||||
lifeCycle: item.lifeCycle || '',
|
||||
growthHabit: item.growthHabit || '',
|
||||
reproductionMethod: item.reproductionMethod || '',
|
||||
reproductionList: reproductionList,
|
||||
|
||||
// Light
|
||||
lightIntensity: item.lightIntensity || '',
|
||||
lightType: item.lightType || '',
|
||||
|
||||
// Temperature
|
||||
optimalTempPeriod: item.optimalTempPeriod || '',
|
||||
|
||||
// Morphology
|
||||
stem: item.stem || '',
|
||||
foliageType: item.foliageType || '',
|
||||
foliageColor: item.foliageColor || '',
|
||||
foliageShape: item.foliageShape || '',
|
||||
height: item.height || 0,
|
||||
|
||||
// Flowering
|
||||
floweringPeriod: item.floweringPeriod || '',
|
||||
floweringColor: item.floweringColor || '',
|
||||
floweringShape: item.floweringShape || '',
|
||||
flowerDiameter: item.flowerDiameter || 0,
|
||||
|
||||
// Fruit
|
||||
fruit: item.fruit || '',
|
||||
|
||||
// Pests
|
||||
pestsDiseases: item.pestsDiseases || '',
|
||||
commonPests: commonPests,
|
||||
|
||||
// Classes
|
||||
classes: (item.classes || []).map(c => c.name),
|
||||
|
||||
// Images
|
||||
imgList: item.imgList || []
|
||||
};
|
||||
|
||||
this.setData({
|
||||
plant: enrichedPlant,
|
||||
plant: plant,
|
||||
swiperList: swiperList
|
||||
});
|
||||
}
|
||||
}).catch(err => {
|
||||
console.error('Load plant detail failed', err);
|
||||
wx.showToast({ title: '加载失败', icon: 'none' });
|
||||
});
|
||||
},
|
||||
|
||||
onSwiperChange(e) {
|
||||
|
||||
+112
-57
@@ -1,5 +1,5 @@
|
||||
<!--pages/wiki/detail/index.wxml-->
|
||||
<view class="wiki-detail">
|
||||
<view class="wiki-detail" wx:if="{{plant}}">
|
||||
<!-- Header Area -->
|
||||
<view class="wd-header">
|
||||
<view class="wd-gallery-container">
|
||||
@@ -11,7 +11,6 @@
|
||||
list="{{swiperList}}"
|
||||
navigation="{{ { type: '' } }}"
|
||||
/>
|
||||
<!-- Gradient applied externally to match plant-detail if needed or just rely on image -->
|
||||
<view class="wd-gradient-overlay"></view>
|
||||
</view>
|
||||
|
||||
@@ -28,12 +27,12 @@
|
||||
<view class="wd-overlay">
|
||||
<view class="wd-title">
|
||||
<text class="wd-name">{{plant.name}}</text>
|
||||
<text class="wd-scientific">{{plant.scientificName}}</text>
|
||||
<text class="wd-scientific">{{plant.latinName}}</text>
|
||||
</view>
|
||||
<view class="wd-badges">
|
||||
<text class="wd-badge">{{plant.family}}</text>
|
||||
<text class="wd-badge">{{plant.category}}</text>
|
||||
<text class="wd-badge">难度: {{plant.difficulty || 'Easy'}}</text>
|
||||
<text class="wd-badge" wx:if="{{plant.genus}}">{{plant.genus}}</text>
|
||||
<text class="wd-badge" wx:for="{{plant.classes}}" wx:key="*this">{{item}}</text>
|
||||
<text class="wd-badge">难度: {{plant.difficultyLabel}}</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
@@ -41,10 +40,11 @@
|
||||
<!-- Content Area -->
|
||||
<view class="wd-content-wrapper">
|
||||
<scroll-view class="wd-content" scroll-y enhanced show-scrollbar="{{false}}">
|
||||
<!-- Description Section -->
|
||||
<section class="wd-section">
|
||||
|
||||
<!-- Growth Habit Section -->
|
||||
<section class="wd-section" wx:if="{{plant.growthHabit}}">
|
||||
<view class="wd-card">
|
||||
<text class="wd-text">{{plant.description}}</text>
|
||||
<text class="wd-text">{{plant.growthHabit}}</text>
|
||||
</view>
|
||||
</section>
|
||||
|
||||
@@ -56,13 +56,21 @@
|
||||
</view>
|
||||
<view class="wd-card">
|
||||
<view class="wd-grid">
|
||||
<view class="wd-stat-item">
|
||||
<text class="wd-label">原产地</text>
|
||||
<text class="wd-value">{{plant.origin}}</text>
|
||||
<view class="wd-stat-item" wx:if="{{plant.distributionArea}}">
|
||||
<text class="wd-label">分布区域</text>
|
||||
<text class="wd-value">{{plant.distributionArea}}</text>
|
||||
</view>
|
||||
<view class="wd-stat-item">
|
||||
<text class="wd-label">毒性</text>
|
||||
<text class="wd-value">{{plant.toxicity}}</text>
|
||||
<view class="wd-stat-item" wx:if="{{plant.aliases}}">
|
||||
<text class="wd-label">别名</text>
|
||||
<text class="wd-value">{{plant.aliases}}</text>
|
||||
</view>
|
||||
<view class="wd-stat-item" wx:if="{{plant.lifeCycle}}">
|
||||
<text class="wd-label">生命周期</text>
|
||||
<text class="wd-value">{{plant.lifeCycle === 'perennial' ? '多年生' : (plant.lifeCycle === 'annual' ? '一年生' : plant.lifeCycle)}}</text>
|
||||
</view>
|
||||
<view class="wd-stat-item" wx:if="{{plant.height}}">
|
||||
<text class="wd-label">株高</text>
|
||||
<text class="wd-value">约 {{plant.height}} cm</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
@@ -71,79 +79,121 @@
|
||||
<!-- Care Guide Section -->
|
||||
<section class="wd-section">
|
||||
<view class="section-title">
|
||||
<t-icon name="sprout" size="40rpx" color="#558B2F" />
|
||||
<t-icon name="sunny" size="40rpx" color="#558B2F" />
|
||||
<text>养护指南</text>
|
||||
</view>
|
||||
<view class="wd-card">
|
||||
<!-- Light -->
|
||||
<view class="requirement-item">
|
||||
<view class="requirement-item" wx:if="{{plant.lightIntensity}}">
|
||||
<view class="req-icon">
|
||||
<t-icon name="sunny" size="40rpx" color="#558B2F" />
|
||||
</view>
|
||||
<view class="req-content">
|
||||
<text class="req-title">光照 ({{plant.light.level}})</text>
|
||||
<text class="req-desc">{{plant.light.description}}</text>
|
||||
<text class="req-title">光照</text>
|
||||
<text class="req-desc">{{plant.lightIntensity}}</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- Water -->
|
||||
<view class="requirement-item">
|
||||
<!-- Temperature -->
|
||||
<view class="requirement-item" wx:if="{{plant.optimalTempPeriod}}">
|
||||
<view class="req-icon">
|
||||
<t-icon name="water" size="40rpx" color="#558B2F" />
|
||||
<t-icon name="pin" size="40rpx" color="#558B2F" />
|
||||
</view>
|
||||
<view class="req-content">
|
||||
<text class="req-title">水分 ({{plant.water.frequency}})</text>
|
||||
<text class="req-desc">{{plant.water.description}}</text>
|
||||
<text class="req-title">适宜温度</text>
|
||||
<text class="req-desc">{{plant.optimalTempPeriod}}</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- Environment -->
|
||||
<view class="requirement-item">
|
||||
<!-- Reproduction -->
|
||||
<view class="requirement-item" wx:if="{{plant.reproductionMethod}}">
|
||||
<view class="req-icon">
|
||||
<t-icon name="temperate" size="40rpx" color="#558B2F" />
|
||||
<t-icon name="fork-node" size="40rpx" color="#558B2F" />
|
||||
</view>
|
||||
<view class="req-content">
|
||||
<text class="req-title">环境</text>
|
||||
<text class="req-desc">温度: {{plant.temperature}}</text>
|
||||
<text class="req-desc" style="display: block; margin-top: 4rpx;">湿度: {{plant.humidity}}</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- Soil & Fertilizer -->
|
||||
<view class="requirement-item">
|
||||
<view class="req-icon">
|
||||
<t-icon name="layers" size="40rpx" color="#558B2F" />
|
||||
</view>
|
||||
<view class="req-content">
|
||||
<text class="req-title">土壤与肥料</text>
|
||||
<text class="req-desc">土: {{plant.soil}}</text>
|
||||
<text class="req-desc" style="display: block; margin-top: 4rpx;">肥: {{plant.fertilizer}}</text>
|
||||
<text class="req-title">繁殖方式</text>
|
||||
<text class="req-desc">{{plant.reproductionMethod}}</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</section>
|
||||
|
||||
<!-- FAQ Section -->
|
||||
<section class="wd-section">
|
||||
<!-- Morphology Section -->
|
||||
<section class="wd-section" wx:if="{{plant.stem || plant.foliageShape || plant.foliageColor}}">
|
||||
<view class="section-title">
|
||||
<t-icon name="error-circle" size="40rpx" color="#558B2F" />
|
||||
<text>常见问题</text>
|
||||
<t-icon name="tree-round-dot" size="40rpx" color="#558B2F" />
|
||||
<text>形态特征</text>
|
||||
</view>
|
||||
<view class="wd-card">
|
||||
<view class="pest-section">
|
||||
<text class="wd-label" style="display: block; margin-bottom: 16rpx;">易发病虫害</text>
|
||||
<view class="pest-tags">
|
||||
<text wx:for="{{plant.commonPests}}" wx:key="*this" class="pest-tag">{{item}}</text>
|
||||
<view class="wd-grid">
|
||||
<view class="wd-stat-item" wx:if="{{plant.stem}}">
|
||||
<text class="wd-label">茎</text>
|
||||
<text class="wd-value">{{plant.stem}}</text>
|
||||
</view>
|
||||
<view class="wd-stat-item" wx:if="{{plant.foliageShape}}">
|
||||
<text class="wd-label">叶形</text>
|
||||
<text class="wd-value">{{plant.foliageShape}}</text>
|
||||
</view>
|
||||
<view class="wd-stat-item" wx:if="{{plant.foliageColor}}">
|
||||
<text class="wd-label">叶色</text>
|
||||
<text class="wd-value">{{plant.foliageColor}}</text>
|
||||
</view>
|
||||
<view class="wd-stat-item" wx:if="{{plant.foliageType}}">
|
||||
<text class="wd-label">叶质</text>
|
||||
<text class="wd-value">{{plant.foliageType}}</text>
|
||||
</view>
|
||||
</view>
|
||||
<view class="tips-section">
|
||||
<text class="wd-label" style="display: block; margin-bottom: 16rpx; margin-top: 32rpx;">专家提示</text>
|
||||
<view class="care-tips-list">
|
||||
<view wx:for="{{plant.careTips}}" wx:key="*this" class="tip-item">
|
||||
<view class="tip-dot"></view>
|
||||
<text class="tip-text">{{item}}</text>
|
||||
</view>
|
||||
</view>
|
||||
</section>
|
||||
|
||||
<!-- Flowering Section -->
|
||||
<section class="wd-section" wx:if="{{plant.floweringPeriod || plant.floweringColor}}">
|
||||
<view class="section-title">
|
||||
<t-icon name="flower" size="40rpx" color="#558B2F" />
|
||||
<text>开花信息</text>
|
||||
</view>
|
||||
<view class="wd-card">
|
||||
<view class="wd-grid">
|
||||
<view class="wd-stat-item" wx:if="{{plant.floweringPeriod}}">
|
||||
<text class="wd-label">花期</text>
|
||||
<text class="wd-value">{{plant.floweringPeriod}}</text>
|
||||
</view>
|
||||
<view class="wd-stat-item" wx:if="{{plant.floweringColor}}">
|
||||
<text class="wd-label">花色</text>
|
||||
<text class="wd-value">{{plant.floweringColor}}</text>
|
||||
</view>
|
||||
<view class="wd-stat-item" wx:if="{{plant.floweringShape}}">
|
||||
<text class="wd-label">花型</text>
|
||||
<text class="wd-value">{{plant.floweringShape}}</text>
|
||||
</view>
|
||||
<view class="wd-stat-item" wx:if="{{plant.flowerDiameter}}">
|
||||
<text class="wd-label">花径</text>
|
||||
<text class="wd-value">约 {{plant.flowerDiameter}} mm</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</section>
|
||||
|
||||
<!-- Fruit Section -->
|
||||
<section class="wd-section" wx:if="{{plant.fruit}}">
|
||||
<view class="section-title">
|
||||
<t-icon name="apple" size="40rpx" color="#558B2F" />
|
||||
<text>果实</text>
|
||||
</view>
|
||||
<view class="wd-card">
|
||||
<text class="wd-text">{{plant.fruit}}</text>
|
||||
</view>
|
||||
</section>
|
||||
|
||||
<!-- Pests & Diseases Section -->
|
||||
<section class="wd-section" wx:if="{{plant.commonPests.length > 0}}">
|
||||
<view class="section-title">
|
||||
<t-icon name="error-circle" size="40rpx" color="#558B2F" />
|
||||
<text>常见病虫害</text>
|
||||
</view>
|
||||
<view class="wd-card">
|
||||
<view class="pest-tags">
|
||||
<text wx:for="{{plant.commonPests}}" wx:key="*this" class="pest-tag">{{item}}</text>
|
||||
</view>
|
||||
</view>
|
||||
</section>
|
||||
@@ -153,3 +203,8 @@
|
||||
</scroll-view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- Loading State -->
|
||||
<view wx:if="{{!plant}}" class="wiki-detail-loading">
|
||||
<t-loading theme="circular" size="64rpx" text="加载中..." />
|
||||
</view>
|
||||
|
||||
@@ -313,3 +313,11 @@ t-swiper {
|
||||
color: #4B5563;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.wiki-detail-loading {
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background: #F9FAFB;
|
||||
}
|
||||
|
||||
+115
-91
@@ -1,21 +1,21 @@
|
||||
// pages/wiki/index.js
|
||||
import { MOCK_WIKI } from '../../utils/mockData';
|
||||
import request from '../../utils/request';
|
||||
|
||||
Page({
|
||||
data: {
|
||||
// Data Source (effectively the backend result)
|
||||
filteredSourceList: [],
|
||||
// Categories
|
||||
categories: [],
|
||||
activeCategory: 'all', // 'all' or category id
|
||||
|
||||
// Display Data (rendered list)
|
||||
// Display Data
|
||||
displayedList: [],
|
||||
|
||||
// Filter State
|
||||
searchQuery: '',
|
||||
activeCategory: '全部',
|
||||
|
||||
// Pagination State
|
||||
page: 1,
|
||||
pageSize: 5,
|
||||
current: 1,
|
||||
pageSize: 10,
|
||||
isLoading: false,
|
||||
hasMore: true,
|
||||
|
||||
@@ -24,8 +24,8 @@ Page({
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
// Initial Load
|
||||
this.filterList();
|
||||
this.fetchCategories();
|
||||
this.fetchWikiList(true);
|
||||
},
|
||||
|
||||
onShow() {
|
||||
@@ -35,98 +35,116 @@ Page({
|
||||
}
|
||||
},
|
||||
|
||||
// Search Input Handler
|
||||
onSearchInput(e) {
|
||||
this.setData({ searchQuery: e.detail.value }, () => {
|
||||
this.filterList();
|
||||
// Fetch categories from API
|
||||
fetchCategories() {
|
||||
request.get('/wiki-class/list').then(res => {
|
||||
const list = (res && res.list) || (Array.isArray(res) ? res : []);
|
||||
this.setData({ categories: list });
|
||||
}).catch(err => {
|
||||
console.error('Fetch categories failed', err);
|
||||
});
|
||||
},
|
||||
|
||||
// Fetch wiki list from API
|
||||
fetchWikiList(reset = false) {
|
||||
if (this.data.isLoading) return;
|
||||
if (!reset && !this.data.hasMore) return;
|
||||
|
||||
const current = reset ? 1 : this.data.current;
|
||||
|
||||
this.setData({ isLoading: true });
|
||||
|
||||
// Build params
|
||||
const params = {
|
||||
current: current,
|
||||
pageSize: this.data.pageSize
|
||||
};
|
||||
|
||||
// Search query
|
||||
if (this.data.searchQuery) {
|
||||
params.name = this.data.searchQuery;
|
||||
}
|
||||
|
||||
// Category filter
|
||||
if (this.data.activeCategory !== 'all') {
|
||||
params.classId = [this.data.activeCategory];
|
||||
}
|
||||
|
||||
request.post('/wiki/page', params).then(res => {
|
||||
const data = res || {};
|
||||
const list = data.list || [];
|
||||
const total = data.total || 0;
|
||||
|
||||
// Map API data to display model
|
||||
const mappedList = list.map(item => ({
|
||||
id: item.id,
|
||||
name: item.name,
|
||||
latinName: item.latinName || '',
|
||||
aliases: item.aliases || '',
|
||||
genus: item.genus || '',
|
||||
difficulty: item.difficulty || 0,
|
||||
isHot: item.isHot === 1,
|
||||
image: (item.imgList && item.imgList.length > 0) ? item.imgList[0].url : '',
|
||||
classes: (item.classes || []).map(c => c.name),
|
||||
// Pass the full item for detail navigation
|
||||
raw: item
|
||||
}));
|
||||
|
||||
if (reset) {
|
||||
this.setData({
|
||||
displayedList: mappedList,
|
||||
current: 2,
|
||||
hasMore: mappedList.length < total,
|
||||
isLoading: false
|
||||
});
|
||||
} else {
|
||||
// Append using data path for performance
|
||||
const updateData = {};
|
||||
const currentLen = this.data.displayedList.length;
|
||||
mappedList.forEach((item, index) => {
|
||||
updateData[`displayedList[${currentLen + index}]`] = item;
|
||||
});
|
||||
updateData['current'] = current + 1;
|
||||
updateData['hasMore'] = (currentLen + mappedList.length) < total;
|
||||
updateData['isLoading'] = false;
|
||||
this.setData(updateData);
|
||||
}
|
||||
}).catch(err => {
|
||||
console.error('Fetch wiki list failed', err);
|
||||
this.setData({ isLoading: false });
|
||||
});
|
||||
},
|
||||
|
||||
// Search Input Handler (debounced)
|
||||
onSearchInput(e) {
|
||||
const value = e.detail.value;
|
||||
this.setData({ searchQuery: value });
|
||||
|
||||
// Debounce search
|
||||
if (this._searchTimer) clearTimeout(this._searchTimer);
|
||||
this._searchTimer = setTimeout(() => {
|
||||
this.fetchWikiList(true);
|
||||
}, 500);
|
||||
},
|
||||
|
||||
// Category Filter Handler
|
||||
setCategory(e) {
|
||||
this.setData({ activeCategory: e.currentTarget.dataset.cat }, () => {
|
||||
this.filterList();
|
||||
const catId = e.currentTarget.dataset.cat;
|
||||
this.setData({ activeCategory: catId }, () => {
|
||||
this.fetchWikiList(true);
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Simulates "Backend" Search & Filtering
|
||||
* Resets pagination and prepares the filtered data source.
|
||||
*/
|
||||
filterList() {
|
||||
const { searchQuery, activeCategory } = this.data;
|
||||
let result = MOCK_WIKI;
|
||||
|
||||
// Filter by Search Query
|
||||
if (searchQuery) {
|
||||
const q = searchQuery.toLowerCase();
|
||||
result = result.filter(item =>
|
||||
item.name.toLowerCase().includes(q) ||
|
||||
item.scientificName.toLowerCase().includes(q)
|
||||
);
|
||||
}
|
||||
|
||||
// Filter by Category
|
||||
if (activeCategory !== '全部') {
|
||||
result = result.filter(item => item.category.includes(activeCategory));
|
||||
}
|
||||
|
||||
this.setData({
|
||||
filteredSourceList: result,
|
||||
displayedList: [],
|
||||
page: 1,
|
||||
hasMore: true
|
||||
}, () => {
|
||||
this.loadMoreData();
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* Simulates "Backend" Pagination
|
||||
* Appends the next page of data from filteredSourceList to displayedList.
|
||||
* key-value data path update is used for performance optimization.
|
||||
*/
|
||||
loadMoreData() {
|
||||
const { isLoading, hasMore, page, pageSize, filteredSourceList, displayedList } = this.data;
|
||||
|
||||
if (isLoading || !hasMore) return;
|
||||
|
||||
this.setData({ isLoading: true });
|
||||
|
||||
// Simulate Network Delay
|
||||
setTimeout(() => {
|
||||
const startIndex = (page - 1) * pageSize;
|
||||
const endIndex = startIndex + pageSize;
|
||||
const newItems = filteredSourceList.slice(startIndex, endIndex);
|
||||
|
||||
const isLastPage = endIndex >= filteredSourceList.length;
|
||||
|
||||
if (newItems.length > 0) {
|
||||
// Performance Optimization: Use data path to append items
|
||||
// Instead of setData({ displayedList: [...old, ...new] })
|
||||
const updateData = {};
|
||||
const currentLen = displayedList.length;
|
||||
newItems.forEach((item, index) => {
|
||||
updateData[`displayedList[${currentLen + index}]`] = item;
|
||||
});
|
||||
|
||||
updateData['page'] = page + 1;
|
||||
updateData['hasMore'] = !isLastPage;
|
||||
updateData['isLoading'] = false;
|
||||
|
||||
this.setData(updateData);
|
||||
} else {
|
||||
this.setData({
|
||||
hasMore: false,
|
||||
isLoading: false
|
||||
});
|
||||
}
|
||||
}, 500);
|
||||
},
|
||||
|
||||
// Infinite Scroll Handler
|
||||
onReachBottom() {
|
||||
this.loadMoreData();
|
||||
this.fetchWikiList(false);
|
||||
},
|
||||
|
||||
// Pull down refresh
|
||||
onPullDownRefresh() {
|
||||
this.fetchCategories();
|
||||
this.fetchWikiList(true);
|
||||
wx.stopPullDownRefresh();
|
||||
},
|
||||
|
||||
goToDetail(e) {
|
||||
@@ -136,6 +154,12 @@ Page({
|
||||
});
|
||||
},
|
||||
|
||||
// Difficulty label helper
|
||||
getDifficultyLabel(level) {
|
||||
const labels = { 1: '简单', 2: '中等', 3: '较难', 4: '困难', 5: '专家' };
|
||||
return labels[level] || '未知';
|
||||
},
|
||||
|
||||
openIdentifyModal() { this.setData({ showIdentifyModal: true }); },
|
||||
|
||||
onPopupVisibleChange(e) {
|
||||
|
||||
+53
-11
@@ -1,4 +1,13 @@
|
||||
<!--pages/wiki/index.wxml-->
|
||||
<wxs module="tools">
|
||||
var diffLabels = { '1': '简单', '2': '中等', '3': '较难', '4': '困难', '5': '专家' };
|
||||
module.exports = {
|
||||
getDifficulty: function(level) {
|
||||
return diffLabels['' + level] || '未知';
|
||||
}
|
||||
};
|
||||
</wxs>
|
||||
|
||||
<view class="wiki-page">
|
||||
|
||||
<scroll-view
|
||||
@@ -12,45 +21,78 @@
|
||||
<t-search placeholder="搜索植物名称,如:龟背竹" value="{{searchQuery}}" bind:change="onSearchInput" shape="round" />
|
||||
</view>
|
||||
|
||||
<!-- Dynamic Categories from API -->
|
||||
<view class="category-scroll">
|
||||
<t-tag
|
||||
wx:for="{{['全部', '观叶', '观花', '多肉']}}"
|
||||
wx:key="*this"
|
||||
variant="{{activeCategory === item ? 'dark' : 'outline'}}"
|
||||
theme="{{activeCategory === item ? 'primary' : 'default'}}"
|
||||
variant="{{activeCategory === 'all' ? 'dark' : 'outline'}}"
|
||||
theme="{{activeCategory === 'all' ? 'primary' : 'default'}}"
|
||||
shape="mark"
|
||||
size="medium"
|
||||
style="margin-right: 16rpx;"
|
||||
bind:tap="setCategory"
|
||||
data-cat="{{item}}"
|
||||
data-cat="all"
|
||||
>
|
||||
{{item}}
|
||||
全部
|
||||
</t-tag>
|
||||
<t-tag
|
||||
wx:for="{{categories}}"
|
||||
wx:key="id"
|
||||
variant="{{activeCategory === item.id ? 'dark' : 'outline'}}"
|
||||
theme="{{activeCategory === item.id ? 'primary' : 'default'}}"
|
||||
shape="mark"
|
||||
size="medium"
|
||||
style="margin-right: 16rpx;"
|
||||
bind:tap="setCategory"
|
||||
data-cat="{{item.id}}"
|
||||
>
|
||||
{{item.name}}
|
||||
</t-tag>
|
||||
</view>
|
||||
|
||||
<view class="wiki-list">
|
||||
<view wx:for="{{displayedList}}" wx:key="id" class="wiki-card" bindtap="goToDetail" data-item="{{item}}">
|
||||
<view class="wiki-image">
|
||||
<t-image src="/assets/{{item.images[0]}}" mode="aspectFill" width="100%" height="100%" lazy />
|
||||
<image wx:if="{{item.image}}" src="{{item.image}}" mode="aspectFill" style="width: 100%; height: 100%; display: block;" lazy-load />
|
||||
<view wx:else class="wiki-image-placeholder">
|
||||
<t-icon name="image" size="48rpx" color="#ccc" />
|
||||
</view>
|
||||
</view>
|
||||
<view class="wiki-info">
|
||||
<view class="wiki-top">
|
||||
<text class="wiki-name">{{item.name}}</text>
|
||||
<text class="scientific-name">{{item.scientificName}}</text>
|
||||
<text class="scientific-name">{{item.latinName}}</text>
|
||||
</view>
|
||||
|
||||
<!-- Tags Row -->
|
||||
<view class="tags-row">
|
||||
<t-tag
|
||||
wx:for="{{item.tags}}"
|
||||
wx:for="{{item.classes}}"
|
||||
wx:key="*this"
|
||||
wx:for-item="tag"
|
||||
wx:for-item="cls"
|
||||
size="small"
|
||||
variant="light"
|
||||
theme="primary"
|
||||
style="margin-right: 8rpx; margin-bottom: 8rpx;"
|
||||
>
|
||||
{{tag}}
|
||||
{{cls}}
|
||||
</t-tag>
|
||||
<t-tag
|
||||
wx:if="{{item.difficulty}}"
|
||||
size="small"
|
||||
variant="light"
|
||||
theme="{{item.difficulty <= 2 ? 'success' : (item.difficulty <= 3 ? 'warning' : 'danger')}}"
|
||||
style="margin-right: 8rpx; margin-bottom: 8rpx;"
|
||||
>
|
||||
{{tools.getDifficulty(item.difficulty)}}
|
||||
</t-tag>
|
||||
<t-tag
|
||||
wx:if="{{item.isHot}}"
|
||||
size="small"
|
||||
variant="light"
|
||||
theme="danger"
|
||||
style="margin-right: 8rpx; margin-bottom: 8rpx;"
|
||||
>
|
||||
热门
|
||||
</t-tag>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
@@ -60,7 +60,14 @@
|
||||
flex-shrink: 0;
|
||||
background: #f0f0f0;
|
||||
}
|
||||
.wiki-image t-image { width: 100%; height: 100%; display: block; }
|
||||
.wiki-image image { width: 100%; height: 100%; display: block; }
|
||||
.wiki-image-placeholder {
|
||||
width: 100%; height: 100%;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background: #f5f5f5;
|
||||
}
|
||||
|
||||
.wiki-info { flex: 1; min-width: 0; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user