const app = getApp(); Page({ data: { userInfo: null, isLoggedIn: false, hasUserInfo: false, canIUse: wx.canIUse('button.open-type.getUserInfo'), theme: { bgColor: '#F8F8F8', textColor: '#333', primaryColor: '#E7624F' }, containerStyle: '' }, onLoad: function () { this.loadThemeSettings(); this.checkLoginStatus(); if (app.globalData.userInfo) { this.setData({ userInfo: app.globalData.userInfo, hasUserInfo: true }); } else if (this.data.canIUse) { app.userInfoReadyCallback = res => { this.setData({ userInfo: res.userInfo, hasUserInfo: true }); }; } else { wx.getUserInfo({ success: res => { app.globalData.userInfo = res.userInfo; this.setData({ userInfo: res.userInfo, hasUserInfo: true }); } }); } if (app.eventBus) { app.eventBus.on('themeChange', this.handleThemeChange); } }, onShow: function() { // 页面显示时更新导航栏颜色 this.updateNavigationBarColor(); }, onUnload: function() { if (app.eventBus) { app.eventBus.off('themeChange', this.handleThemeChange); } }, loadThemeSettings: function() { const savedTheme = wx.getStorageSync('theme') || app.globalData.theme || { bgColor: '#F8F8F8', textColor: '#333', primaryColor: '#E7624F' }; this.setData({ theme: savedTheme, containerStyle: `background-color: ${savedTheme.bgColor}; color: ${savedTheme.textColor};` }); this.updateNavigationBarColor(); }, handleThemeChange: function(theme) { this.setData({ theme: theme, containerStyle: `background-color: ${theme.bgColor}; color: ${theme.textColor};` }); this.updateNavigationBarColor(); }, checkLoginStatus: function() { const userInfo = app.globalData.userInfo; const isLoggedIn = !!userInfo; this.setData({ userInfo: userInfo, isLoggedIn: isLoggedIn, hasUserInfo: isLoggedIn }); }, handleUserInfo: function(e) { if (e.detail.userInfo) { const userInfo = e.detail.userInfo; app.globalData.userInfo = userInfo; this.setData({ userInfo: userInfo, isLoggedIn: true, hasUserInfo: true }); wx.showToast({ title: '登录成功', icon: 'success' }); } else { wx.showToast({ title: '您拒绝了授权', icon: 'none' }); } }, handleLogout: function() { wx.showModal({ title: '提示', content: '确定要退出登录吗?', success: (res) => { if (res.confirm) { app.globalData.userInfo = null; this.setData({ userInfo: null, isLoggedIn: false, hasUserInfo: false }); wx.showToast({ title: '已退出登录', icon: 'success' }); } } }); }, goToThemeSettings: function() { wx.navigateTo({ url: '../theme/theme' }); }, goToNotificationSettings: function() { wx.showToast({ title: '通知设置功能开发中', icon: 'none' }); }, goToAbout: function() { wx.showToast({ title: '关于功能开发中', icon: 'none' }); }, // 更新导航栏颜色的方法 updateNavigationBarColor: function() { const { theme } = this.data; if (theme && theme.primaryColor && theme.textColor) { wx.setNavigationBarColor({ frontColor: theme.textColor === '#333' ? '#000000' : '#ffffff', backgroundColor: theme.primaryColor, animation: { duration: 300, timingFunc: 'easeIn' } }); } } });