index.js 5.8 KB

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