mutations.js 1008 B

123456789101112131415161718192021222324252627282930313233
  1. export const setUser = (state, payload) => {
  2. state.user = payload;
  3. // let res = true;
  4. // //登陆时
  5. // if (payload) {
  6. // state.token = payload;
  7. // } else {
  8. // //已经登陆,切换路由时取出用户信息放在总store中
  9. // let token = localStorage.getItem('token');
  10. // if (token && token !== 'guest') {
  11. // state.user = jwt.decode(token);
  12. // } else if (token && token == 'guest') {
  13. // let user = localStorage.getItem('user');
  14. // state.user = JSON.parse(user);
  15. // } else {
  16. // let timestamp = new Date().getTime();
  17. // let user = {
  18. // // id: `guest${timestamp}`,
  19. // name: `游客${timestamp}`,
  20. // };
  21. // state.user = user;
  22. // localStorage.setItem('token', 'guest');
  23. // localStorage.setItem('user', JSON.stringify(user));
  24. // console.warn('游客身份');
  25. // }
  26. // }
  27. // return res;
  28. };
  29. export const deleteUser = (state, payload) => {
  30. state.user = {};
  31. localStorage.removeItem('token');
  32. };