feat: login rest
This commit is contained in:
+73
-56
@@ -40,58 +40,52 @@ Page({
|
||||
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
|
||||
this.getTabBar().setData({ selected: 4 });
|
||||
}
|
||||
// Always fetch fresh profile data
|
||||
this.loadUserInfo();
|
||||
},
|
||||
|
||||
// ======== User Info ========
|
||||
loadUserInfo() {
|
||||
// Try to get from globalData or storage
|
||||
const userInfo = app.globalData.userInfo || wx.getStorageSync('userInfo');
|
||||
if (userInfo && userInfo.name) {
|
||||
this.setData({
|
||||
userName: userInfo.name || '植物爱好者',
|
||||
userAvatar: userInfo.avatarUrl || userInfo.avatar || ''
|
||||
});
|
||||
return; // Use cached data, no API call
|
||||
}
|
||||
request.get('/profile/detail').then(res => {
|
||||
if (!res) return;
|
||||
|
||||
// Map stats and level info
|
||||
const avatarUrl = res.avatar && res.avatar.url ? res.avatar.url : '';
|
||||
const levelInfo = res.level || {};
|
||||
const levelTag = levelInfo.level ? `Lv.${levelInfo.level} ${levelInfo.title || ''}` : '';
|
||||
|
||||
// Only fetch from backend if no cached info
|
||||
request.get('/user/info').then(user => {
|
||||
if (!user) return;
|
||||
const avatarUrl = user.avatar ? user.avatar.url : '';
|
||||
this.setData({
|
||||
userName: user.name || '植物爱好者',
|
||||
userAvatar: avatarUrl
|
||||
userName: res.nickname || '植物爱好者',
|
||||
userAvatar: avatarUrl,
|
||||
|
||||
// Stats
|
||||
plantCount: res.plantCount || 0,
|
||||
taskDoneCount: res.careCount || 0,
|
||||
postCount: res.postCount || 0,
|
||||
|
||||
// Level (if available)
|
||||
userLevel: levelInfo.level || 0,
|
||||
userLevelTag: levelTag,
|
||||
|
||||
// EXP / Sunlight
|
||||
userExp: res.currentSunlight || 0
|
||||
});
|
||||
|
||||
// Update global cache
|
||||
const info = {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
avatarUrl: avatarUrl,
|
||||
account: user.account,
|
||||
phone: user.phone,
|
||||
avatarId: user.avatarId
|
||||
...res,
|
||||
avatarId: res.avatarId || (res.avatar ? res.avatar.id : '')
|
||||
};
|
||||
app.globalData.userInfo = info;
|
||||
wx.setStorageSync('userInfo', info);
|
||||
}).catch(() => { });
|
||||
|
||||
}).catch(err => {
|
||||
console.error('Load profile failed', err);
|
||||
});
|
||||
},
|
||||
|
||||
// ======== Stats ========
|
||||
loadStats() {
|
||||
// Fetch plant count
|
||||
request.post('/plant/page', { current: 1, pageSize: 1 }).then(res => {
|
||||
this.setData({ plantCount: res.total || 0 });
|
||||
}).catch(() => { });
|
||||
|
||||
// Fetch post count - user's own posts
|
||||
request.post('/post/page', { current: 1, pageSize: 1, onlyMine: true }).then(res => {
|
||||
this.setData({ postCount: res.total || 0 });
|
||||
}).catch(() => { });
|
||||
|
||||
// Fetch completed tasks count
|
||||
request.get('/plant/taskCount').then(res => {
|
||||
this.setData({ taskDoneCount: res || 0 });
|
||||
}).catch(() => { });
|
||||
},
|
||||
|
||||
// ======== Navigation ========
|
||||
setView(e) {
|
||||
@@ -222,6 +216,10 @@ Page({
|
||||
wx.navigateTo({ url: '/pages/profile/identify-history/index' });
|
||||
},
|
||||
|
||||
goToBadges() {
|
||||
wx.navigateTo({ url: '/pages/profile/badges/index' });
|
||||
},
|
||||
|
||||
goToNotificationSettings() {
|
||||
// Open WeChat notification settings
|
||||
wx.openSetting({
|
||||
@@ -284,10 +282,14 @@ Page({
|
||||
},
|
||||
|
||||
async saveProfile() {
|
||||
const { tempAvatar, tempNickname } = this.data;
|
||||
const { tempAvatar, tempNickname, userName, userAvatar } = this.data;
|
||||
|
||||
if (!tempAvatar && !tempNickname) {
|
||||
wx.showToast({ title: '请选择头像或输入昵称', icon: 'none' });
|
||||
// Check if anything changed
|
||||
const isNameChanged = tempNickname && tempNickname !== userName;
|
||||
const isAvatarChanged = tempAvatar && tempAvatar !== userAvatar;
|
||||
|
||||
if (!isNameChanged && !isAvatarChanged) {
|
||||
this.setData({ showProfileEditor: false });
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -297,36 +299,51 @@ Page({
|
||||
const updatePayload = {};
|
||||
|
||||
// 1. Upload avatar if changed
|
||||
if (tempAvatar) {
|
||||
const data = await request.upload(tempAvatar);
|
||||
const fileData = data?.file || {};
|
||||
if (fileData.id) {
|
||||
updatePayload.avatar_id = fileData.id;
|
||||
// Update local display
|
||||
this.setData({ userAvatar: fileData.url || tempAvatar });
|
||||
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;
|
||||
} else if (uploadRes && uploadRes.id) {
|
||||
fileId = uploadRes.id;
|
||||
}
|
||||
|
||||
if (fileId) {
|
||||
updatePayload.avatarId = fileId;
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Set name if provided
|
||||
if (tempNickname) {
|
||||
updatePayload.name = tempNickname;
|
||||
this.setData({ userName: tempNickname });
|
||||
// 2. Set nickname if changed
|
||||
if (isNameChanged) {
|
||||
updatePayload.nickname = tempNickname;
|
||||
}
|
||||
|
||||
// 3. Call update API
|
||||
if (Object.keys(updatePayload).length > 0) {
|
||||
await request.post('/user/update', updatePayload);
|
||||
await request.post('/profile/update', updatePayload);
|
||||
}
|
||||
|
||||
wx.hideLoading();
|
||||
this.setData({ showProfileEditor: false });
|
||||
|
||||
// 4. Update local state
|
||||
this.setData({
|
||||
userName: tempNickname || userName,
|
||||
userAvatar: tempAvatar || userAvatar, // Use tempAvatar for immediate display
|
||||
showProfileEditor: false
|
||||
});
|
||||
|
||||
wx.showToast({ title: '资料已更新', icon: 'success' });
|
||||
|
||||
// Update globalData
|
||||
// 5. Update globalData
|
||||
const userInfo = app.globalData.userInfo || {};
|
||||
if (updatePayload.name) userInfo.name = updatePayload.name;
|
||||
if (updatePayload.avatar_id) userInfo.avatarId = updatePayload.avatar_id;
|
||||
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;
|
||||
app.globalData.userInfo = userInfo;
|
||||
|
||||
} catch (err) {
|
||||
wx.hideLoading();
|
||||
console.error('Save profile failed', err);
|
||||
|
||||
Reference in New Issue
Block a user