groupStore.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import http from '@/common/request';
  2. export default {
  3. state: {
  4. groups: [],
  5. activeIndex: -1,
  6. },
  7. mutations: {
  8. setGroups(state, groups) {
  9. state.groups = groups;
  10. },
  11. activeGroup(state, index) {
  12. state.activeIndex = index;
  13. },
  14. addGroup(state, group) {
  15. state.groups.unshift(group);
  16. },
  17. removeGroup(state, groupId) {
  18. state.groups.forEach((g, index) => {
  19. if (g.id == groupId) {
  20. state.groups.splice(index, 1);
  21. if (state.activeIndex >= state.groups.length) {
  22. state.activeIndex = state.groups.length - 1;
  23. }
  24. }
  25. })
  26. },
  27. updateGroup(state, group) {
  28. state.groups.forEach((g, idx) => {
  29. if (g.id == group.id) {
  30. // 拷贝属性
  31. Object.assign(state.groups[idx], group);
  32. }
  33. })
  34. }
  35. },
  36. actions: {
  37. loadGroup(context) {
  38. return new Promise((resolve, reject) => {
  39. http({
  40. url: '/group/list',
  41. method: 'GET'
  42. }).then((groups) => {
  43. context.commit("setGroups", groups);
  44. resolve();
  45. }).catch((res) => {
  46. reject(res);
  47. })
  48. });
  49. }
  50. }
  51. }