app.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import { defineStore } from 'pinia'
  2. import { systemStudent, systemUserGetInfo } from '@/api/modules/global'
  3. import { systemSubjectList } from '@/api/modules/study'
  4. import router from '@/router'
  5. export const useAppStore = defineStore('app', {
  6. state: () => ({
  7. userInfo: null,
  8. studentInfo: null,
  9. categoryMenus: [],
  10. subjectMap: new Map(),
  11. }),
  12. getters: {},
  13. actions: {
  14. initUserInfo() {
  15. return new Promise((resolve, reject) => {
  16. systemUserGetInfo()
  17. .then(({ data: userData }: any) => {
  18. this.userInfo = userData.user
  19. systemStudent(this.userInfo?.userId).then(
  20. ({ data: studentData }: any) => {
  21. this.studentInfo = studentData
  22. systemSubjectList({
  23. pageNum: 1,
  24. pageSize: 10,
  25. category: this.studentInfo?.grade,
  26. }).then(({ rows }: any) => {
  27. const categoryMenus: any = []
  28. const map = new Map()
  29. rows.forEach((su) => {
  30. map.set(su.subjectId, su.subject)
  31. const menu1 = {
  32. path: '/' + su.subjectId,
  33. name: su.subjectId,
  34. component: () =>
  35. import('@/views/study/subject/index.vue'),
  36. meta: {
  37. subjectId: su.subjectId,
  38. isMenu: true,
  39. fa: 'fa-book',
  40. res: su,
  41. title: su.subject,
  42. },
  43. }
  44. const menu2 = {
  45. path: menu1.path + '/question',
  46. name: menu1.name + 'question',
  47. component: () =>
  48. import('@/views/study/subject/question/index.vue'),
  49. meta: {
  50. subjectId: su.subjectId,
  51. res: su,
  52. title: su.subject,
  53. root: menu1.name,
  54. },
  55. }
  56. const menu3 = {
  57. path: menu1.path + '/plan',
  58. name: menu1.name + 'plan',
  59. component: () =>
  60. import('@/views/study/subject/question/plan.vue'),
  61. meta: {
  62. subjectId: su.subjectId,
  63. res: su,
  64. title: su.subject,
  65. root: menu1.name,
  66. },
  67. }
  68. categoryMenus.push(menu1)
  69. categoryMenus.push(menu2)
  70. categoryMenus.push(menu3)
  71. })
  72. this.subjectMap = map
  73. categoryMenus.forEach((v) => {
  74. router.addRoute(v)
  75. })
  76. this.categoryMenus = categoryMenus
  77. resolve(this.userInfo)
  78. })
  79. },
  80. )
  81. })
  82. .catch((e) => {
  83. reject(e)
  84. })
  85. })
  86. },
  87. loadingStart() {
  88. const l = document.getElementById('loader')
  89. if (l) {
  90. l.style.display = 'flex'
  91. }
  92. },
  93. loadingEnd() {
  94. const l = document.getElementById('loader')
  95. if (l) {
  96. l.style.display = 'none'
  97. }
  98. },
  99. },
  100. })