groupStore.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import request 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. request({
  40. url: '/group/list',
  41. method: 'GET'
  42. }).then((groups) => {
  43. context.commit("setGroups", groups);
  44. resolve();
  45. }).catch(() => {
  46. reject();
  47. })
  48. });
  49. }
  50. }
  51. }