index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // index.js
  2. const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
  3. const app = getApp();
  4. Page({
  5. data: {
  6. motto: 'Hello World',
  7. userInfo: {
  8. avatarUrl: defaultAvatarUrl,
  9. nickName: '',
  10. },
  11. hasUserInfo: false,
  12. canIUseGetUserProfile: wx.canIUse('getUserProfile'),
  13. canIUseNicknameComp: wx.canIUse('input.type.nickname'),
  14. isLoggedIn: false,
  15. },
  16. onLoad() {
  17. if(wx.getStorageSync('userInfo')){
  18. this.setData({
  19. userInfo: wx.getStorageSync('userInfo'),
  20. hasUserInfo: true
  21. })
  22. }
  23. this.checkLoginStatus();
  24. },
  25. onShow() {
  26. // 同步底部导航高亮
  27. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  28. this.getTabBar().setData({
  29. selected: 4,
  30. isShowTabar: true
  31. })
  32. }
  33. // 每次显示页面时检查登录状态
  34. this.checkLoginStatus();
  35. },
  36. bindViewTap() {
  37. wx.navigateTo({
  38. url: '../logs/logs'
  39. })
  40. },
  41. onChooseAvatar(e) {
  42. const { avatarUrl } = e.detail
  43. const { nickName } = this.data.userInfo
  44. // 更新页面数据
  45. this.setData({
  46. "userInfo.avatarUrl": avatarUrl,
  47. hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
  48. hasUserInfo: true
  49. })
  50. // 更新本地存储和全局数据
  51. const updatedUserInfo = {
  52. ...this.data.userInfo,
  53. avatarUrl: avatarUrl,
  54. realUserInfo: true
  55. }
  56. wx.setStorageSync('userInfo', updatedUserInfo)
  57. if (app && app.globalData) {
  58. app.globalData.userInfo = updatedUserInfo
  59. }
  60. console.log('头像已更新:', avatarUrl)
  61. },
  62. onInputChange(e) {
  63. const nickName = e.detail.value
  64. const { avatarUrl } = this.data.userInfo
  65. // 更新页面数据
  66. this.setData({
  67. "userInfo.nickName": nickName,
  68. hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
  69. hasUserInfo: true
  70. })
  71. // 更新本地存储和全局数据
  72. const updatedUserInfo = {
  73. ...this.data.userInfo,
  74. nickName: nickName,
  75. realUserInfo: true
  76. }
  77. wx.setStorageSync('userInfo', updatedUserInfo)
  78. if (app && app.globalData) {
  79. app.globalData.userInfo = updatedUserInfo
  80. }
  81. console.log('昵称已更新:', nickName)
  82. },
  83. getUserProfile(e) {
  84. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  85. wx.getUserProfile({
  86. desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  87. success: (res) => {
  88. console.log(res)
  89. this.setData({
  90. userInfo: res.userInfo,
  91. hasUserInfo: true
  92. })
  93. }
  94. })
  95. },
  96. // 跳转到我的参观人页面
  97. goToUserList() {
  98. if (!this.data.isLoggedIn) {
  99. this.startLogin();
  100. return;
  101. }
  102. wx.navigateTo({
  103. url: '/pages/user/userList/index'
  104. })
  105. },
  106. // 跳转到我的预约
  107. startPreview() {
  108. if (!this.data.isLoggedIn) {
  109. this.startLogin();
  110. return;
  111. }
  112. console.log(11111)
  113. wx.navigateTo({
  114. url: '/pages/user/my-preview/index'
  115. })
  116. },
  117. // 意见反馈
  118. goToFeedback() {
  119. if (!this.data.isLoggedIn) {
  120. this.startLogin();
  121. return;
  122. }
  123. wx.navigateTo({
  124. url: '/pages/user/feedback/index'
  125. });
  126. },
  127. // 退出登录
  128. logout() {
  129. const token = wx.getStorageSync('token');
  130. if (!token) {
  131. wx.showToast({
  132. title: '您还未登录',
  133. icon: 'none'
  134. });
  135. return;
  136. }
  137. wx.showModal({
  138. title: '提示',
  139. content: '确定要退出登录吗?',
  140. success: (res) => {
  141. if (res.confirm) {
  142. // 清除本地存储的token和用户信息
  143. wx.removeStorageSync('token');
  144. wx.removeStorageSync('userInfo');
  145. if (app && app.globalData) {
  146. app.globalData.token = null;
  147. app.globalData.userInfo = null;
  148. }
  149. // 调用app的logout方法统一处理退出登录
  150. if (app && app.logout) {
  151. app.logout();
  152. }
  153. // 清除页面用户信息,设置为未登录状态
  154. this.setData({
  155. userInfo: {
  156. avatarUrl: defaultAvatarUrl,
  157. nickName: '',
  158. },
  159. hasUserInfo: false,
  160. isLoggedIn: false,
  161. });
  162. wx.showToast({
  163. title: '已退出登录',
  164. icon: 'success',
  165. duration: 1500
  166. });
  167. // // 延迟跳转到首页
  168. // setTimeout(() => {
  169. // wx.switchTab({
  170. // url: '/pages/index/index'
  171. // });
  172. // }, 1500);
  173. }
  174. }
  175. });
  176. },
  177. startLogin() {
  178. // 触发自动登录
  179. wx.showModal({
  180. title: '登录授权',
  181. content: '为了给您提供更好的服务,需要获取您的微信登录信息,是否同意?',
  182. confirmText: '同意',
  183. cancelText: '暂不',
  184. success: (res) => {
  185. if (res.confirm) {
  186. if (app && app.wxLogin) {
  187. wx.showToast({
  188. title: '正在登录',
  189. icon: 'none',
  190. duration: 1000
  191. });
  192. app.wxLogin();
  193. this.setData({
  194. userInfo: {
  195. avatarUrl: defaultAvatarUrl,
  196. nickName: '微信用户',
  197. },
  198. hasUserInfo: true,
  199. });
  200. }
  201. } else {
  202. // 用户拒绝,保持未登录状态
  203. console.log('用户拒绝登录授权,保持未登录状态')
  204. this.globalData.isGuest = true
  205. }
  206. }
  207. })
  208. },
  209. /**
  210. * 检查登录状态
  211. */
  212. checkLoginStatus() {
  213. const app = getApp();
  214. const token = wx.getStorageSync('token');
  215. const isLoggedIn = !!(token && !app.globalData.isGuest);
  216. this.setData({
  217. isLoggedIn: isLoggedIn
  218. });
  219. }
  220. })