feat: 后端版本迁移修改
This commit is contained in:
@@ -42,9 +42,9 @@ Page({
|
||||
|
||||
if (eventChannel) {
|
||||
eventChannel.on('acceptDataFromOpenerPage', (data) => {
|
||||
if (data && data.plant) {
|
||||
if (data) {
|
||||
hasReceivedData = true;
|
||||
this.renderPlantUI(data.plant);
|
||||
this.renderPlantUI(data);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -63,20 +63,24 @@ Page({
|
||||
});
|
||||
},
|
||||
|
||||
renderPlantUI(plant) {
|
||||
renderPlantUI(data) {
|
||||
if (!data) return;
|
||||
|
||||
// Detect if data is the raw API response or a flat eventChannel payload
|
||||
const isRawResponse = data.hasOwnProperty('plant') && typeof data.plant === 'object';
|
||||
const plant = isRawResponse ? data.plant : (data.plant || data);
|
||||
const imgList = data.imgList || [];
|
||||
|
||||
const defaultIcon = CARE_TASK_ICONS.find(i => i.id === 'water') || CARE_TASK_ICONS[0];
|
||||
|
||||
let tasks = [];
|
||||
if (plant.careSchedule) {
|
||||
tasks = plant.careSchedule.map(cp => ({
|
||||
id: cp.id, name: cp.name, period: cp.period,
|
||||
taskIcon: cp.taskIcon, isNew: false,
|
||||
_original: { name: cp.name, period: cp.period, icon: JSON.stringify(cp.taskIcon || {}) }
|
||||
}));
|
||||
} else if (plant.carePlans) {
|
||||
tasks = plant.carePlans.map(cp => {
|
||||
const carePlansSrc = isRawResponse ? (data.carePlans || []) : (data.carePlans || plant.careSchedule || []);
|
||||
if (carePlansSrc && carePlansSrc.length > 0) {
|
||||
tasks = carePlansSrc.map(cp => {
|
||||
let iconObj = defaultIcon;
|
||||
if (typeof cp.icon === 'string' && cp.icon.startsWith('{')) {
|
||||
if (cp.taskIcon) {
|
||||
iconObj = cp.taskIcon;
|
||||
} else if (typeof cp.icon === 'string' && cp.icon.startsWith('{')) {
|
||||
try { iconObj = JSON.parse(cp.icon); } catch (e) { }
|
||||
}
|
||||
const iconStr = JSON.stringify(iconObj);
|
||||
@@ -89,12 +93,11 @@ Page({
|
||||
}
|
||||
|
||||
let imageUrl = '', imageId = '';
|
||||
if (plant.imgList && plant.imgList.length > 0) {
|
||||
imageUrl = plant.imgList[0].url || '';
|
||||
imageId = plant.imgList[0].id || '';
|
||||
if (imgList && imgList.length > 0) {
|
||||
imageUrl = imgList[0].url || '';
|
||||
imageId = imgList[0].id || '';
|
||||
}
|
||||
|
||||
|
||||
let adoptionDate = plant.plantTime || '';
|
||||
if (adoptionDate.includes('T')) adoptionDate = adoptionDate.split('T')[0];
|
||||
|
||||
@@ -142,9 +145,10 @@ Page({
|
||||
wx.showLoading({ title: '上传中...' });
|
||||
request.upload(tempFilePath).then(data => {
|
||||
wx.hideLoading();
|
||||
const fileData = data?.file || {};
|
||||
if (fileData.id) {
|
||||
this.setData({ uploadedImageId: fileData.id, newPlantImage: fileData.url });
|
||||
const imageUrl = data?.url || '';
|
||||
const imageId = data?.id || '';
|
||||
if (imageId) {
|
||||
this.setData({ uploadedImageId: imageId, newPlantImage: imageUrl });
|
||||
}
|
||||
}).catch(() => {
|
||||
wx.hideLoading();
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
>
|
||||
<!-- Upload Area -->
|
||||
<view class="upload-section">
|
||||
<view class="image-upload-area {{newPlantImage ? 'has-image' : ''}}" bindtap="showActionSheet">
|
||||
<view class="image-upload-area {{newPlantImage ? 'has-image' : ''}}">
|
||||
<t-image
|
||||
wx:if="{{newPlantImage}}"
|
||||
src="{{newPlantImage}}"
|
||||
@@ -23,10 +23,6 @@
|
||||
<t-icon name="upload" size="64rpx" color="#999" />
|
||||
<text>点击设置封面图</text>
|
||||
</view>
|
||||
<view wx:if="{{newPlantImage}}" class="edit-overlay">
|
||||
<t-icon name="camera" size="32rpx" color="#FFF" />
|
||||
<text>更换照片</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
|
||||
@@ -61,10 +61,7 @@ Page({
|
||||
if (this.data.image) {
|
||||
const uploadRes = await request.upload(this.data.image);
|
||||
// Correctly extract ID from nested 'file' object based on API response
|
||||
if (uploadRes && uploadRes.file && uploadRes.file.id) {
|
||||
ossIds.push(uploadRes.file.id);
|
||||
} else if (uploadRes && uploadRes.id) {
|
||||
// Fallback just in case
|
||||
if (uploadRes && uploadRes.id) {
|
||||
ossIds.push(uploadRes.id);
|
||||
} else {
|
||||
console.warn('Upload response structure mismatch:', uploadRes);
|
||||
|
||||
+20
-12
@@ -36,11 +36,17 @@ Page({
|
||||
|
||||
initData(id) {
|
||||
request.get('/plant/detail', { id }).then(plant => {
|
||||
const swiperImages = plant.imgList.map(img => {
|
||||
return img.url;
|
||||
});
|
||||
// Legacy handler returns {plant: PlantInfo, carePlans: [...], growthRecords: [...], imgList: [...], careRecords: [...]}
|
||||
const plantData = plant.plant || {};
|
||||
const carePlansList = plant.carePlans || [];
|
||||
const careRecordsList = plant.careRecords || [];
|
||||
const growthRecordsList = plant.growthRecords || [];
|
||||
const imgList = plant.imgList || [];
|
||||
|
||||
const swiperImages = imgList.map(img => img.url);
|
||||
|
||||
// Parse carePlans icon if it's a string
|
||||
const carePlans = (plant.carePlans || []).map(cp => {
|
||||
const carePlans = carePlansList.map(cp => {
|
||||
let iconObj = {};
|
||||
if (typeof cp.icon === 'string' && cp.icon.startsWith('{')) {
|
||||
try {
|
||||
@@ -54,16 +60,16 @@ Page({
|
||||
|
||||
// Calculate days planted and format date
|
||||
let adoptionDate = '未知';
|
||||
const daysPlanted = calculateDaysSince(plant.plantTime);
|
||||
const daysPlanted = calculateDaysSince(plantData.plantTime);
|
||||
const ageBadge = getPlantAgeBadge(daysPlanted);
|
||||
if (plant.plantTime) {
|
||||
adoptionDate = plant.plantTime.split('T')[0];
|
||||
if (plantData.plantTime) {
|
||||
adoptionDate = plantData.plantTime.split('T')[0];
|
||||
}
|
||||
|
||||
this.setData({
|
||||
currentPlant: {
|
||||
...plant,
|
||||
location: plant.placement || '',
|
||||
...plantData,
|
||||
location: plantData.placement || '',
|
||||
adoptionDate: adoptionDate,
|
||||
daysPlanted: daysPlanted,
|
||||
ageBadge: ageBadge,
|
||||
@@ -71,8 +77,8 @@ Page({
|
||||
},
|
||||
swiperImages: swiperImages,
|
||||
// Map logs and records directly from plant detail response
|
||||
careLogs: this.processLogs(plant.careRecords || []),
|
||||
records: (plant.growthRecords || plant.recordList || []).map(item => {
|
||||
careLogs: this.processLogs(careRecordsList),
|
||||
records: growthRecordsList.map(item => {
|
||||
// Extract image URL safely
|
||||
let imageUrl = '';
|
||||
if (item.imgList && item.imgList.length > 0) {
|
||||
@@ -235,7 +241,9 @@ Page({
|
||||
success: (res) => {
|
||||
// Send current data to the opened page
|
||||
res.eventChannel.emit('acceptDataFromOpenerPage', {
|
||||
plant: this.data.currentPlant
|
||||
plant: this.data.currentPlant,
|
||||
imgList: this.data.swiperImages.map((url, i) => ({ id: i === 0 ? this.data.currentPlant.imageId || 'primary' : '', url })),
|
||||
carePlans: this.data.currentPlant.careSchedule
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user