feat: 整体页面优化,删除无用svg
This commit is contained in:
+54
-149
@@ -29,7 +29,9 @@ Page({
|
||||
myDrafts: [],
|
||||
|
||||
// App version
|
||||
appVersion: '1.0.0'
|
||||
// App version
|
||||
appVersion: '1.0.0',
|
||||
scrollTop: 0
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
@@ -44,6 +46,13 @@ Page({
|
||||
this.loadUserInfo();
|
||||
},
|
||||
|
||||
onTabItemTap() {
|
||||
this.setData({
|
||||
view: 'profile',
|
||||
scrollTop: Math.random() * 0.01
|
||||
});
|
||||
},
|
||||
|
||||
// ======== User Info ========
|
||||
loadUserInfo() {
|
||||
request.get('/profile/detail').then(res => {
|
||||
@@ -57,6 +66,7 @@ Page({
|
||||
this.setData({
|
||||
userName: res.nickname || '植物爱好者',
|
||||
userAvatar: avatarUrl,
|
||||
currentAvatarId: res.avatarId || (res.avatar ? res.avatar.id : ''),
|
||||
|
||||
// Stats
|
||||
plantCount: res.plantCount || 0,
|
||||
@@ -88,127 +98,16 @@ Page({
|
||||
|
||||
|
||||
// ======== Navigation ========
|
||||
setView(e) {
|
||||
const view = e.currentTarget.dataset.view;
|
||||
this.setData({ view });
|
||||
|
||||
if (view === 'favorites') {
|
||||
this.loadFavorites();
|
||||
} else if (view === 'posts') {
|
||||
this.loadMyPosts();
|
||||
this.loadDrafts();
|
||||
}
|
||||
},
|
||||
|
||||
goBack() {
|
||||
this.setData({ view: 'profile' });
|
||||
},
|
||||
|
||||
// ======== Favorites ========
|
||||
loadFavorites() {
|
||||
// TODO: Call favorites API when available
|
||||
// request.get('/user/favorites').then(...)
|
||||
this.filterFavorites();
|
||||
goToFavorites() {
|
||||
wx.navigateTo({ url: '/pages/profile/favorites/index' });
|
||||
},
|
||||
|
||||
onFavTabChange(e) {
|
||||
this.setData({ favTab: e.detail.value }, () => {
|
||||
this.filterFavorites();
|
||||
});
|
||||
},
|
||||
|
||||
filterFavorites() {
|
||||
const { favorites, favTab } = this.data;
|
||||
const filtered = favorites.filter(item => {
|
||||
if (favTab === 'all') return true;
|
||||
return item.type === favTab;
|
||||
});
|
||||
this.setData({ filteredFavorites: filtered });
|
||||
},
|
||||
|
||||
// ======== Posts ========
|
||||
loadMyPosts() {
|
||||
request.post('/post/page', { current: 1, pageSize: 50, onlyMine: true }).then(res => {
|
||||
const records = res.records || res.list || [];
|
||||
const posts = records.map(item => {
|
||||
const publisher = item.publisher || {};
|
||||
const imgList = item.imgList || [];
|
||||
return {
|
||||
id: item.id,
|
||||
content: item.content || '',
|
||||
time: this._formatTime(item.createdAt || item.createTime),
|
||||
images: imgList.map(img => img.url),
|
||||
likes: item.likeList || [],
|
||||
comments: item.commentList || []
|
||||
};
|
||||
});
|
||||
this.setData({ myPublishedPosts: posts });
|
||||
}).catch(() => {
|
||||
this.setData({ myPublishedPosts: [] });
|
||||
});
|
||||
},
|
||||
|
||||
loadDrafts() {
|
||||
try {
|
||||
const draft = wx.getStorageSync('post_draft');
|
||||
if (draft && (draft.content || (draft.images && draft.images.length > 0))) {
|
||||
this.setData({
|
||||
myDrafts: [{
|
||||
id: 'draft_1',
|
||||
content: draft.content || '',
|
||||
images: draft.images || [],
|
||||
selectedTopics: draft.selectedTopics || []
|
||||
}]
|
||||
});
|
||||
} else {
|
||||
this.setData({ myDrafts: [] });
|
||||
}
|
||||
} catch (e) {
|
||||
this.setData({ myDrafts: [] });
|
||||
}
|
||||
},
|
||||
|
||||
onPostsTabChange(e) {
|
||||
this.setData({ postsTab: e.detail.value });
|
||||
},
|
||||
|
||||
deletePost(e) {
|
||||
const postId = e.currentTarget.dataset.id;
|
||||
wx.showModal({
|
||||
title: '删除动态',
|
||||
content: '确定要删除这条动态吗?',
|
||||
confirmColor: '#EF5350',
|
||||
success: (res) => {
|
||||
if (!res.confirm) return;
|
||||
wx.showLoading({ title: '删除中...' });
|
||||
request.get('/post/delete', { id: postId }).then(() => {
|
||||
wx.hideLoading();
|
||||
this.loadMyPosts();
|
||||
wx.showToast({ title: '已删除', icon: 'success' });
|
||||
}).catch(() => {
|
||||
wx.hideLoading();
|
||||
wx.showToast({ title: '删除失败', icon: 'none' });
|
||||
});
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
editDraft() {
|
||||
wx.navigateTo({ url: '/pages/community/create/index' });
|
||||
},
|
||||
|
||||
deleteDraft() {
|
||||
wx.showModal({
|
||||
title: '删除草稿',
|
||||
content: '确定要删除这份草稿吗?',
|
||||
confirmColor: '#EF5350',
|
||||
success: (res) => {
|
||||
if (!res.confirm) return;
|
||||
try { wx.removeStorageSync('post_draft'); } catch (e) { }
|
||||
this.setData({ myDrafts: [] });
|
||||
wx.showToast({ title: '已删除', icon: 'success' });
|
||||
}
|
||||
});
|
||||
goToPosts() {
|
||||
wx.navigateTo({ url: '/pages/profile/posts/index' });
|
||||
},
|
||||
|
||||
// ======== Menu Actions ========
|
||||
@@ -284,10 +183,12 @@ Page({
|
||||
},
|
||||
|
||||
async saveProfile() {
|
||||
const { tempAvatar, tempNickname, userName, userAvatar } = this.data;
|
||||
const { tempAvatar, tempNickname, userName, userAvatar, currentAvatarId } = this.data;
|
||||
|
||||
// Check if anything changed
|
||||
const isNameChanged = tempNickname && tempNickname !== userName;
|
||||
// Determine if there are changes
|
||||
// tempNickname might be undefined if user didn't edit nickname input
|
||||
const finalNickname = tempNickname !== undefined ? tempNickname : userName;
|
||||
const isNameChanged = finalNickname !== userName;
|
||||
const isAvatarChanged = tempAvatar && tempAvatar !== userAvatar;
|
||||
|
||||
if (!isNameChanged && !isAvatarChanged) {
|
||||
@@ -295,61 +196,65 @@ Page({
|
||||
return;
|
||||
}
|
||||
|
||||
if (!finalNickname.trim()) {
|
||||
wx.showToast({ title: '昵称不能为空', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
|
||||
wx.showLoading({ title: '保存中...', mask: true });
|
||||
|
||||
try {
|
||||
const updatePayload = {};
|
||||
let finalAvatarId = currentAvatarId;
|
||||
|
||||
// 1. Upload avatar if changed
|
||||
// Upload new avatar if changed
|
||||
if (isAvatarChanged) {
|
||||
const uploadRes = await request.upload(tempAvatar);
|
||||
// Correctly extract ID from file object based on known API response
|
||||
let fileId = '';
|
||||
if (uploadRes && uploadRes.file && uploadRes.file.id) {
|
||||
fileId = uploadRes.file.id;
|
||||
finalAvatarId = uploadRes.file.id;
|
||||
} else if (uploadRes && uploadRes.id) {
|
||||
fileId = uploadRes.id;
|
||||
}
|
||||
|
||||
if (fileId) {
|
||||
updatePayload.avatarId = fileId;
|
||||
finalAvatarId = uploadRes.id;
|
||||
} else {
|
||||
throw new Error('Avatar upload failed, no ID returned');
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Set nickname if changed
|
||||
if (isNameChanged) {
|
||||
updatePayload.nickname = tempNickname;
|
||||
}
|
||||
// Construct Full Payload
|
||||
const updatePayload = {
|
||||
nickname: finalNickname,
|
||||
avatarId: finalAvatarId
|
||||
};
|
||||
|
||||
// 3. Call update API
|
||||
if (Object.keys(updatePayload).length > 0) {
|
||||
await request.post('/profile/update', updatePayload);
|
||||
}
|
||||
// Call API
|
||||
await request.post('/profile/update', updatePayload);
|
||||
|
||||
wx.hideLoading();
|
||||
|
||||
// 4. Update local state
|
||||
// Update UI State
|
||||
this.setData({
|
||||
userName: tempNickname || userName,
|
||||
userAvatar: tempAvatar || userAvatar, // Use tempAvatar for immediate display
|
||||
userName: finalNickname,
|
||||
userAvatar: tempAvatar || userAvatar,
|
||||
currentAvatarId: finalAvatarId,
|
||||
showProfileEditor: false
|
||||
});
|
||||
|
||||
wx.showToast({ title: '资料已更新', icon: 'success' });
|
||||
|
||||
// 5. Update globalData
|
||||
// Update Global Data
|
||||
const userInfo = app.globalData.userInfo || {};
|
||||
if (updatePayload.nickname) userInfo.name = updatePayload.nickname;
|
||||
if (updatePayload.avatarId) userInfo.avatarId = updatePayload.avatarId;
|
||||
// Also update URL if we have it locally?
|
||||
// Better to re-fetch profile to get canonical URL, but optimistic update is fine.
|
||||
userInfo.avatar = tempAvatar;
|
||||
userInfo.nickname = finalNickname;
|
||||
userInfo.name = finalNickname;
|
||||
// Update avatar structure in global store too
|
||||
if (isAvatarChanged) {
|
||||
userInfo.avatar = { ...(userInfo.avatar || {}), url: tempAvatar, id: finalAvatarId };
|
||||
}
|
||||
userInfo.avatarId = finalAvatarId;
|
||||
|
||||
app.globalData.userInfo = userInfo;
|
||||
wx.setStorageSync('userInfo', userInfo);
|
||||
|
||||
} catch (err) {
|
||||
wx.hideLoading();
|
||||
console.error('Save profile failed', err);
|
||||
wx.showToast({ title: '保存失败', icon: 'none' });
|
||||
} finally {
|
||||
wx.hideLoading();
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
Reference in New Issue
Block a user