user.ts 903 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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: 'GUEST',
  10. };
  11. const actions: ActionTree<UserState, RootState> = {
  12. async updateUserInfo({ commit }) {
  13. const res = (await UserAPI.getUserInfo()) as unknown as {
  14. user_info: UserState;
  15. };
  16. commit('updateUserInfo', res.user_info);
  17. },
  18. userLogOut({ commit }) {
  19. commit('updateUserInfo', DEFAULT);
  20. },
  21. };
  22. const mutations: MutationTree<UserState> = {
  23. updateUserInfo(state, {
  24. name, email, id, phone, role,
  25. }) {
  26. state.username = name;
  27. state.email = email;
  28. state.id = id;
  29. state.phone = phone;
  30. state.role = role;
  31. },
  32. };
  33. export const user: Module<UserState, RootState> = {
  34. state: () => DEFAULT,
  35. actions,
  36. mutations,
  37. };