Page({
  data: {
    username: '请登录',   // 用户名
    avatarUrl: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.jHUH4s7TQ48X_B-1iozuJgHaHa?w=207&h=207&c=7&r=0&o=5&dpr=1.5&pid=1.7'   // 用户头像
  },

  onShow() {
    my.getStorage({
      key: 'userInfo',
      success: (res) => {
        // 成功获取到用户信息
        const userInfo = res.data;
        console.log('用户信息:', userInfo);
        if(userInfo){
          this.setData({
            username: userInfo.username,
            avatarUrl: userInfo.avatarUrl,
            messagelogin:userInfo,
          });
        }else{
          this.setData({
            username: '请登录',
            avatarUrl: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.jHUH4s7TQ48X_B-1iozuJgHaHa?w=207&h=207&c=7&r=0&o=5&dpr=1.5&pid=1.7',
          });
        }
      },
      fail: (err) => {
        console.log('获取用户信息失败:', err);
      }
    });
  },

  // 以下是一些页面跳转的示例函数
  dingdan() {
    my.navigateTo({
      url: '/pages/wodedingdan/wodedingdan'
    });
  },
  pingjia() {
    my.navigateTo({
      url: '/pages/wodepingjia/wodepingjia'
    });
  },
  xinxi() {
    my.navigateTo({
      url: '/pages/gerenxinxi/gerenxinxi'
    });
  },
  shoucang() {
    my.navigateTo({
      url: '/pages/wodeshoucang/wodeshoucang'
    });
  },
  ruzhu() {
    my.navigateTo({
      url: '/pages/shangjiaruzhu/shangjiaruzhu'
    });
  },
  message() {
    my.navigateTo({
      url: '/pages/message/message'
    });
  },
  login() {
    console.log(this.data.messagelogin);
    if (!this.data.messagelogin) {
      my.navigateTo({
        url: '/pages/denglu/denglu'
      });
    } else {
      my.showToast({
        content: '您已登录，无需重复登录',
        duration: 2000 // 提示持续时间
      });
    }
  },
  
  meijishi() {
    my.navigateTo({
      url: '/pages/meijiashirenzheng/meijiashirenzheng'
    });
  },
  out(){
    my.removeStorage({
      key: 'userInfo',
      success: () => {
        console.log('用户已退出登录，缓存已清除');

        // 清除缓存后更新页面为默认信息
        this.setData({
          username: '请登录',
          avatarUrl: 'https://tse2-mm.cn.bing.net/th/id/OIP-C.jHUH4s7TQ48X_B-1iozuJgHaHa?w=207&h=207&c=7&r=0&o=5&dpr=1.5&pid=1.7',
          messagelogin:""
        });
       my.alert({
        content:'您已退出登录'
       })
      },
      fail: (err) => {
        console.error('退出登录失败:', err);
      }
    });
  },
});
