dictionary.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import { defineStore } from 'pinia'
  2. import { tagsTenant } from '@/api/modules/knowledge/tags'
  3. import { ElMessage } from 'element-plus'
  4. import { datasetGroupsGetAllByTenantId } from '@/api/modules/knowledge/group'
  5. import { pluginConfigs, pluginGetModelTypeList } from '@/api/modules/model'
  6. import { dictionary, dictionaryDefine } from './dictionary-define'
  7. import { sysDictFindByDictType } from '@/api/modules/global/dictionary'
  8. // @ts-ignore
  9. import FileExcel from '@/assets/images/file-excel.png'
  10. // @ts-ignore
  11. import FileTxt from '@/assets/images/file-txt.png'
  12. // @ts-ignore
  13. import FileUnknown from '@/assets/images/file-unknown.png'
  14. // @ts-ignore
  15. import FileWord from '@/assets/images/file-word.png'
  16. const listToMap = ({
  17. list,
  18. labelKey = 'label',
  19. valueKey = 'value',
  20. isObj = false,
  21. }) => {
  22. const map = new Map()
  23. list.forEach((v) => {
  24. map.set(v[valueKey], isObj ? v : v[labelKey])
  25. })
  26. return map
  27. }
  28. export const useDictionaryStore = defineStore('dictionary', {
  29. state: () => ({
  30. ...dictionary,
  31. knowledgeTags: {
  32. waiting: false,
  33. list: [],
  34. map: new Map(),
  35. objMap: new Map(),
  36. },
  37. knowledgeGroups: {
  38. waiting: false,
  39. list: [],
  40. map: new Map(),
  41. objMap: new Map(),
  42. },
  43. modelProvides: {
  44. waiting: false,
  45. list: [],
  46. map: new Map(),
  47. objMap: new Map(),
  48. },
  49. modelTypes: {
  50. waiting: false,
  51. list: [],
  52. map: new Map(),
  53. objMap: new Map(),
  54. },
  55. shareConditions: [
  56. { label: '不开放', value: 'NO_SHARE' },
  57. { label: '全部开放', value: 'OPEN_SHARE' },
  58. { label: '申请开放', value: 'SHARE_ON_CONDITION' },
  59. ],
  60. modelStatus: [
  61. { label: '停用', value: 0 },
  62. { label: '启用', value: 1 },
  63. ],
  64. documentStatus: [
  65. { label: '启用', value: 0 },
  66. { label: '停用', value: 1 },
  67. ],
  68. appTypes: [
  69. { label: '简洁', value: 0 },
  70. { label: '高级', value: 1 },
  71. ],
  72. appStatus: [
  73. { label: '草稿', value: 0 },
  74. { label: '发布', value: 1 },
  75. ],
  76. tenantStatus: [
  77. { label: '启用', value: true },
  78. { label: '停用', value: false },
  79. ],
  80. }),
  81. getters: {
  82. // shareConditionsMap() {
  83. // return listToMap((this as any).shareConditions)
  84. // },
  85. },
  86. actions: {
  87. getStaticDict(dict, value) {
  88. const map = listToMap((this as any)[dict])
  89. return map.get(value) || value
  90. },
  91. initDict(type) {
  92. return new Promise((resolve, reject) => {
  93. if (this[dictionaryDefine[type][0]].length === 0) {
  94. sysDictFindByDictType(type)
  95. .then(({ data }: any) => {
  96. const map = new Map()
  97. const objMap = new Map()
  98. if (data.length > 0) {
  99. data.forEach((v: any) => {
  100. v.label = v.dictName
  101. v.value = v.dictValue
  102. map.set(v.value, v.label)
  103. objMap.set(v.value, v)
  104. })
  105. }
  106. this[dictionaryDefine[type][0]] = data
  107. this[dictionaryDefine[type][1]] = map
  108. this[dictionaryDefine[type]?.[2]] = objMap
  109. resolve(data)
  110. })
  111. .catch(() => {})
  112. } else {
  113. resolve(this[dictionaryDefine[type][0]])
  114. }
  115. })
  116. },
  117. initKnowledgeTags(tenantId) {
  118. if (!this.knowledgeTags.waiting) {
  119. this.knowledgeTags.waiting = true
  120. tagsTenant(tenantId)
  121. .then(({ data }: any) => {
  122. const arr: any = data.map((v) => {
  123. v.label = v.name
  124. v.value = v.id
  125. v.total = v.targetSize
  126. return v
  127. })
  128. this.knowledgeTags.list = arr
  129. this.knowledgeTags.map = listToMap({ list: arr })
  130. this.knowledgeTags.objMap = listToMap({ list: arr, isObj: true })
  131. })
  132. .catch(() => {})
  133. .finally(() => {
  134. this.knowledgeTags.waiting = false
  135. })
  136. }
  137. },
  138. initKnowledgeGroups(tenantId) {
  139. if (!this.knowledgeGroups.waiting) {
  140. this.knowledgeGroups.waiting = true
  141. datasetGroupsGetAllByTenantId(tenantId)
  142. .then(({ data }: any) => {
  143. const arr: any = data.map((v) => {
  144. v.label = v.name
  145. v.value = v.id
  146. return v
  147. })
  148. this.knowledgeGroups.list = arr
  149. this.knowledgeGroups.map = listToMap({ list: arr })
  150. this.knowledgeGroups.objMap = listToMap({ list: arr, isObj: true })
  151. })
  152. .catch(() => {})
  153. .finally(() => {
  154. this.knowledgeGroups.waiting = false
  155. })
  156. }
  157. },
  158. initModelTypes() {
  159. if (!this.modelTypes.waiting) {
  160. this.modelTypes.waiting = true
  161. pluginGetModelTypeList()
  162. .then(({ data }: any) => {
  163. const arr: any = data.map((v) => {
  164. v.label = v.label
  165. v.value = v.code
  166. return v
  167. })
  168. this.modelTypes.list = arr
  169. this.modelTypes.map = listToMap({ list: arr })
  170. this.modelTypes.objMap = listToMap({ list: arr, isObj: true })
  171. })
  172. .catch(() => {})
  173. .finally(() => {
  174. this.modelTypes.waiting = false
  175. })
  176. }
  177. },
  178. initModelProvides() {
  179. if (!this.modelProvides.waiting) {
  180. this.modelProvides.waiting = true
  181. pluginConfigs()
  182. .then(({ data }: any) => {
  183. const arr: any = []
  184. Object.values(data).forEach((v: any) => {
  185. arr.push(
  186. ...v.map((v) => {
  187. v.label = v.providerName
  188. v.value = v.id
  189. return v
  190. }),
  191. )
  192. })
  193. this.modelProvides.list = arr
  194. this.modelProvides.map = listToMap({ list: arr })
  195. this.modelProvides.objMap = listToMap({ list: arr, isObj: true })
  196. })
  197. .catch(() => {})
  198. .finally(() => {
  199. this.modelProvides.waiting = false
  200. })
  201. }
  202. },
  203. getFileIcon(name) {
  204. if (name.toLowerCase().includes('.txt')) {
  205. return FileTxt
  206. } else if (
  207. name.toLowerCase().includes('.xls') ||
  208. name.toLowerCase().includes('.xlsx')
  209. ) {
  210. return FileExcel
  211. } else if (
  212. name.toLowerCase().includes('.doc') ||
  213. name.toLowerCase().includes('.docx')
  214. ) {
  215. return FileWord
  216. }
  217. return FileUnknown
  218. },
  219. },
  220. })