groupStore.js 1.1 KB

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