base.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import {
  2. defineStore
  3. } from 'pinia';
  4. import {
  5. getDictionaryDataAll,
  6. getGroupSelector,
  7. getRoleSelector,
  8. } from '@/api/common'
  9. export const useBaseStore = defineStore({
  10. id: ' app-base',
  11. state: () => ({
  12. dictionaryList: [],
  13. groupList: [],
  14. roleList: [],
  15. relationData: {},
  16. }),
  17. getters: {
  18. getDictionaryList() {
  19. return this.dictionaryList
  20. },
  21. getRelationData() {
  22. return this.relationData
  23. },
  24. },
  25. actions: {
  26. setDictionaryList(dictionaryList) {
  27. this.dictionaryList = dictionaryList || []
  28. },
  29. setGroupList(groupList) {
  30. this.groupList = groupList
  31. },
  32. setRoleList(roleList) {
  33. this.roleList = roleList
  34. },
  35. updateRelationData(val) {
  36. this.relationData = val
  37. },
  38. getDictionaryDataAll() {
  39. return new Promise((resolve, reject) => {
  40. if (this.dictionaryList.length) {
  41. resolve(this.dictionaryList)
  42. } else {
  43. getDictionaryDataAll().then(res => {
  44. this.setDictionaryList(res.data.list)
  45. resolve(res.data.list)
  46. }).catch(error => {
  47. reject(error)
  48. })
  49. }
  50. })
  51. },
  52. getDictionaryData(info) {
  53. return new Promise(async resolve => {
  54. let list = [],
  55. data = [],
  56. json = []
  57. if (!this.dictionaryList.length) {
  58. list = await this.getDictionaryDataAll()
  59. } else {
  60. list = this.dictionaryList
  61. }
  62. if (info.sort) {
  63. data = list.filter(o => o.enCode === info.sort)[0]
  64. if (!info.id) {
  65. json = data?.dictionaryList || []
  66. } else {
  67. let rowData = [];
  68. if (!data.isTree) {
  69. rowData = data.dictionaryList.fliter(o => o.id == info.id)
  70. } else {
  71. const findData = list => {
  72. for (let i = 0; i < list.length; i++) {
  73. const e = list[i];
  74. if (e.id == info.id) {
  75. rowData[0] = e
  76. break
  77. }
  78. if (e.children && e.children.length) {
  79. findData(e.children)
  80. }
  81. }
  82. }
  83. findData(data.dictionaryList)
  84. }
  85. if (rowData.length) {
  86. json = rowData[0]
  87. } else {
  88. json = {
  89. id: "",
  90. fullName: ""
  91. };
  92. }
  93. }
  94. }
  95. resolve(json)
  96. })
  97. },
  98. getDicDataSelector(value, key = 'id') {
  99. return new Promise(async resolve => {
  100. let list = [],
  101. data = {},
  102. json = [];
  103. if (!this.dictionaryList.length) {
  104. list = await this.getDictionaryDataAll()
  105. } else {
  106. list = this.dictionaryList
  107. }
  108. if (!value) return resolve([])
  109. let arr = list.filter(o => o[key] === value);
  110. if (!arr.length) return resolve([])
  111. data = arr[0];
  112. json = data.dictionaryList;
  113. resolve(json)
  114. })
  115. },
  116. getGroupList() {
  117. return new Promise((resolve, reject) => {
  118. if (!this.groupList.length) {
  119. getGroupSelector().then(res => {
  120. this.setGroupList(res.data)
  121. resolve(res.data)
  122. }).catch(error => {
  123. reject(error)
  124. })
  125. } else {
  126. resolve(this.groupList)
  127. }
  128. })
  129. },
  130. getRoleList() {
  131. return new Promise((resolve, reject) => {
  132. if (!this.roleList.length) {
  133. getRoleSelector().then(res => {
  134. this.setRoleList(res.data.list)
  135. resolve(res.data.list)
  136. }).catch(error => {
  137. reject(error)
  138. })
  139. } else {
  140. resolve(this.roleList)
  141. }
  142. })
  143. },
  144. },
  145. });