groupStore.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. clear(state){
  36. state.groups = [];
  37. state.activeGroup = -1;
  38. }
  39. },
  40. actions: {
  41. loadGroup(context) {
  42. return new Promise((resolve, reject) => {
  43. http({
  44. url: '/group/list',
  45. method: 'GET'
  46. }).then((groups) => {
  47. context.commit("setGroups", groups);
  48. resolve();
  49. }).catch((res) => {
  50. reject(res);
  51. })
  52. });
  53. }
  54. }
  55. }