settings.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. const app = getApp();
  2. Page({
  3. data: {
  4. userInfo: null,
  5. isLoggedIn: false,
  6. hasUserInfo: false,
  7. canIUse: wx.canIUse('button.open-type.getUserInfo'),
  8. theme: {
  9. bgColor: '#F8F8F8',
  10. textColor: '#333',
  11. primaryColor: '#E7624F'
  12. },
  13. containerStyle: ''
  14. },
  15. onLoad: function () {
  16. this.loadThemeSettings();
  17. this.checkLoginStatus();
  18. if (app.globalData.userInfo) {
  19. this.setData({
  20. userInfo: app.globalData.userInfo,
  21. hasUserInfo: true
  22. });
  23. } else if (this.data.canIUse) {
  24. app.userInfoReadyCallback = res => {
  25. this.setData({
  26. userInfo: res.userInfo,
  27. hasUserInfo: true
  28. });
  29. };
  30. } else {
  31. wx.getUserInfo({
  32. success: res => {
  33. app.globalData.userInfo = res.userInfo;
  34. this.setData({
  35. userInfo: res.userInfo,
  36. hasUserInfo: true
  37. });
  38. }
  39. });
  40. }
  41. if (app.eventBus) {
  42. app.eventBus.on('themeChange', this.handleThemeChange);
  43. }
  44. },
  45. onShow: function() {
  46. // 页面显示时更新导航栏颜色
  47. this.updateNavigationBarColor();
  48. },
  49. onUnload: function() {
  50. if (app.eventBus) {
  51. app.eventBus.off('themeChange', this.handleThemeChange);
  52. }
  53. },
  54. loadThemeSettings: function() {
  55. const savedTheme = wx.getStorageSync('theme') || app.globalData.theme || {
  56. bgColor: '#F8F8F8',
  57. textColor: '#333',
  58. primaryColor: '#E7624F'
  59. };
  60. this.setData({
  61. theme: savedTheme,
  62. containerStyle: `background-color: ${savedTheme.bgColor}; color: ${savedTheme.textColor};`
  63. });
  64. this.updateNavigationBarColor();
  65. },
  66. handleThemeChange: function(theme) {
  67. this.setData({
  68. theme: theme,
  69. containerStyle: `background-color: ${theme.bgColor}; color: ${theme.textColor};`
  70. });
  71. this.updateNavigationBarColor();
  72. },
  73. checkLoginStatus: function() {
  74. const userInfo = app.globalData.userInfo;
  75. const isLoggedIn = !!userInfo;
  76. this.setData({
  77. userInfo: userInfo,
  78. isLoggedIn: isLoggedIn,
  79. hasUserInfo: isLoggedIn
  80. });
  81. },
  82. handleUserInfo: function(e) {
  83. if (e.detail.userInfo) {
  84. const userInfo = e.detail.userInfo;
  85. app.globalData.userInfo = userInfo;
  86. this.setData({
  87. userInfo: userInfo,
  88. isLoggedIn: true,
  89. hasUserInfo: true
  90. });
  91. wx.showToast({
  92. title: '登录成功',
  93. icon: 'success'
  94. });
  95. } else {
  96. wx.showToast({
  97. title: '您拒绝了授权',
  98. icon: 'none'
  99. });
  100. }
  101. },
  102. handleLogout: function() {
  103. wx.showModal({
  104. title: '提示',
  105. content: '确定要退出登录吗?',
  106. success: (res) => {
  107. if (res.confirm) {
  108. app.globalData.userInfo = null;
  109. this.setData({
  110. userInfo: null,
  111. isLoggedIn: false,
  112. hasUserInfo: false
  113. });
  114. wx.showToast({
  115. title: '已退出登录',
  116. icon: 'success'
  117. });
  118. }
  119. }
  120. });
  121. },
  122. goToThemeSettings: function() {
  123. wx.navigateTo({
  124. url: '../theme/theme'
  125. });
  126. },
  127. goToNotificationSettings: function() {
  128. wx.showToast({
  129. title: '通知设置功能开发中',
  130. icon: 'none'
  131. });
  132. },
  133. goToAbout: function() {
  134. wx.showToast({
  135. title: '关于功能开发中',
  136. icon: 'none'
  137. });
  138. },
  139. // 更新导航栏颜色的方法
  140. updateNavigationBarColor: function() {
  141. const { theme } = this.data;
  142. if (theme && theme.primaryColor && theme.textColor) {
  143. wx.setNavigationBarColor({
  144. frontColor: theme.textColor === '#333' ? '#000000' : '#ffffff',
  145. backgroundColor: theme.primaryColor,
  146. animation: {
  147. duration: 300,
  148. timingFunc: 'easeIn'
  149. }
  150. });
  151. }
  152. }
  153. });