user.ts 922 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { UserAPI } from '@/api';
  2. import { ActionTree, Module, MutationTree } from 'vuex';
  3. import { RootState, UserState } from '../typings';
  4. const DEFAULT: UserState = {
  5. username: 'GUEST',
  6. email: '',
  7. id: -1,
  8. phone: '',
  9. role: '游客',
  10. withGitlab: false,
  11. };
  12. const actions: ActionTree<UserState, RootState> = {
  13. async updateUserInfo({ commit }) {
  14. const userinfo = await UserAPI.getUserInfo();
  15. if (!userinfo) {
  16. return;
  17. }
  18. commit('updateUserInfo', userinfo);
  19. },
  20. userLogOut({ commit }) {
  21. commit('updateUserInfo', DEFAULT);
  22. },
  23. };
  24. const mutations: MutationTree<UserState> = {
  25. updateUserInfo(state, {
  26. username, email, id, phone, role,
  27. }) {
  28. state.username = username;
  29. state.email = email;
  30. state.id = id;
  31. state.phone = phone;
  32. state.role = role;
  33. },
  34. };
  35. export const user: Module<UserState, RootState> = {
  36. state: () => DEFAULT,
  37. actions,
  38. mutations,
  39. };