Staff.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. Page({
  2. data: {
  3. isLogin: false, // 登录状态
  4. userInfo: {}, // 存储用户信息(头像和昵称)
  5. },
  6. // 页面加载时获取缓存的昵称和头像
  7. onLoad() {
  8. const storedUserInfo = wx.getStorageSync('userinfo'); // 获取缓存中的用户信息
  9. if (storedUserInfo) {
  10. // 如果缓存中有用户信息,更新页面上的数据
  11. this.setData({
  12. userInfo: storedUserInfo, // 更新用户信息
  13. isLogin: true, // 设置登录状态为 true
  14. });
  15. }
  16. },
  17. // 获取用户头像
  18. getAvatar(e) {
  19. const avatarUrl = e.detail.avatarUrl;
  20. this.setData({
  21. 'userInfo.avatarUrl': avatarUrl
  22. });
  23. // 保存头像到缓存
  24. let userInfo = wx.getStorageSync('userinfo') || {};
  25. userInfo.avatarUrl = avatarUrl;
  26. wx.setStorageSync('userinfo', userInfo);
  27. },
  28. // 获取用户昵称
  29. getName(e) {
  30. const nickName = e.detail.value;
  31. this.setData({
  32. 'userInfo.nickName': nickName
  33. });
  34. // 保存昵称到缓存
  35. let userInfo = wx.getStorageSync('userinfo') || {};
  36. userInfo.nickName = nickName;
  37. wx.setStorageSync('userinfo', userInfo);
  38. },
  39. onShow: function() {
  40. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  41. this.getTabBar().setData({
  42. selected: 3 //这个数字是当前页面在tabBar中list数组的索引
  43. })
  44. }
  45. },
  46. // 显示登录弹窗
  47. gologin() {
  48. this.setData({
  49. isHidden: false
  50. });
  51. },
  52. // 取消登录弹窗
  53. potNo() {
  54. this.setData({
  55. isHidden: true
  56. });
  57. },
  58. // 确认登录弹窗
  59. popYes() {
  60. const { avatarUrl, nickName } = this.data.userInfo;
  61. if (!avatarUrl || !nickName) {
  62. wx.showToast({
  63. icon: 'error',
  64. title: '请获取头像和昵称',
  65. });
  66. return;
  67. }
  68. // 保存头像和昵称到缓存
  69. wx.setStorageSync('userinfo', this.data.userInfo);
  70. this.setData({
  71. isLogin: true, // 设置登录状态为 true
  72. isHidden: true, // 隐藏弹窗
  73. });
  74. },
  75. goToThreshold() {
  76. wx.navigateTo({
  77. url: '/pages/threshold/threshold'
  78. });
  79. },
  80. /// 点击退出登录
  81. tuichu() {
  82. // 清除缓存
  83. wx.clearStorageSync();
  84. // 重置登录状态
  85. this.setData({
  86. isLogin: false,
  87. userInfo: {}
  88. });
  89. // 跳转到登录页面
  90. wx.redirectTo({
  91. url: '/pages/RoleSelectionPage/RoleSelectionPage' // 登录页面的路径
  92. });
  93. }
  94. });