const jwt = require('jsonwebtoken');
export const setUser = (state, payload) => {
  let res = true;
  //登陆时
  if (payload) state.user = payload;
  else {
    //已经登陆,切换路由时取出用户信息放在总store中
    let token = sessionStorage.getItem('token');
    let user;
    if (token) {
      user = jwt.decode(token);
    }
    if (user) state.user = user;
    else {
      res = false;
      state.user = undefined;
      console.warn('用户未登录');
    }
  }
  return res;
};

export const deleteUser = (state, payload) => {
  state.user = undefined;
  sessionStorage.removeItem('user');
};
