groupStore.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. let g = this.findGroup(group.id);
  32. Object.assign(g, group);
  33. },
  34. clear() {
  35. this.groups = [];
  36. this.activeGroup = -1;
  37. },
  38. loadGroup() {
  39. return new Promise((resolve, reject) => {
  40. http({
  41. url: '/group/list',
  42. method: 'GET'
  43. }).then((groups) => {
  44. this.setGroups(groups);
  45. resolve();
  46. }).catch((res) => {
  47. reject(res);
  48. })
  49. });
  50. }
  51. },
  52. getters: {
  53. findGroup: (state) => (id) => {
  54. return state.groups.find((g) => g.id == id);
  55. }
  56. }
  57. })