const app = getApp();
Page({
  data: {
    navbarData:{title:'我的',showCapsule:true,share:false},
    roleMap:{admin:"管理员",personnel:"操作员",doctor:"医生",register:"自由注册者"},
  },
  onShow:function(){
    const that = this;
    //移除之前缓存的用户信息，确保拿到用户最新数据
    wx.removeStorageSync('userInfo');
    //通过token来判断是否是登录状态
    var token = wx.getStorageSync('token');
    that.setData({
      islogin: !!token
    })
    //如果有token,则去拉取用户数据，相反则只显示界面
    if(token)app.requestP({
      url: "/user/getUserInfo",
    }).then(function(res){
      wx.setStorageSync('userInfo',res.data.data)
      that.setData({
        user : res.data.data
      })
    }).catch(err=>{
      app.showToptip(that,'error',err.data.msg)
    })
  },
  exitLogin:function(){
    var that = this;
    app.requestP({
      url:'/logout',
      method:'get',
    }).then(res=>{
      app.showToptip(that,'success',res.data.msg);
      wx.removeStorageSync("token");
      wx.removeStorageSync("userInfo");
      wx.redirectTo({
        url: '/pages/login/login',
      })
    }).catch(err=>{
      app.showToptip(that,'error',err.data.msg);
    })
  },
  goSetting:function(){
    if(this.data.islogin){
      wx.navigateTo({
        url: '/pages/setting/setting',
      })
    }else{
      wx.redirectTo({
        url: '/pages/login/login',
      })
    }
  },
  goMessage:function(){
    if(this.data.islogin){
      wx.navigateTo({
        url: '/pages/news/newList',
      })
    }else{
      wx.redirectTo({
        url: '/pages/login/login',
      })
    }
  },
  goChangeRole:function(){
    if(this.data.islogin){
      wx.navigateTo({
        url: '/pages/userAuth/chooseRole',
      })
    }else{
      wx.redirectTo({
        url: '/pages/login/login',
      })
    }
  },
  goSetMobileEmail(){
    wx.navigateTo({
      url: '/pages/userinfo/index?userInfo='+JSON.stringify(this.data.user),
    })
  },
})