Files
sundynix-plant-mp/pages/wiki/detail/index.js
T
2026-02-10 09:20:24 +08:00

133 lines
4.2 KiB
JavaScript

// pages/wiki/detail/index.js
import request from '../../../utils/request';
Page({
data: {
plant: null,
activeImageIndex: 0,
swiperList: []
},
onLoad(options) {
if (options.id) {
this.loadPlantDetail(options.id);
}
},
loadPlantDetail(id) {
// 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: item.name });
// Prepare swiper list from imgList
const swiperList = (item.imgList || []).map(img => img.url);
// 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: plant,
swiperList: swiperList
});
}).catch(err => {
console.error('Load plant detail failed', err);
wx.showToast({ title: '加载失败', icon: 'none' });
});
},
onSwiperChange(e) {
this.setData({
activeImageIndex: e.detail.current
});
},
onShareAppMessage() {
if (!this.data.plant) return {};
return {
title: `植物百科 - ${this.data.plant.name}`,
path: `/pages/wiki/detail/index?id=${this.data.plant.id}`
};
}
});