123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- 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'
- }
- });
- }
- }
- });
|