useMaterial.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { MaterialAPI } from '@/config/API.ts'
  2. import type { ResponseInfo } from '@/types/axios.ts'
  3. import axiosInstance from '@/utils/axios/axiosInstance.ts'
  4. import type {
  5. GameData,
  6. GameSelectItem,
  7. TagItem,
  8. TagsRes,
  9. } from '@/views/Material/types/uploadFileType.ts'
  10. import type { Reactive } from 'vue'
  11. export function useMaterial() {
  12. const getGameInfo = async () => {
  13. const res = (await axiosInstance.post(
  14. MaterialAPI.getAllGamesInfo,
  15. {},
  16. )) as ResponseInfo
  17. if (res.code !== 0) {
  18. console.error('获取游戏信息失败')
  19. return null
  20. }
  21. return res.data as GameData
  22. }
  23. const generateGameSelect = (
  24. gameInfoList: GameData,
  25. gameSelect: Reactive<GameSelectItem[]>,
  26. ) => {
  27. gameSelect.splice(0, gameSelect.length)
  28. for (let [k, v] of Object.entries(gameInfoList)) {
  29. const children = v.map(item => {
  30. return {
  31. value: item.gid,
  32. label: item.gameName,
  33. }
  34. })
  35. gameSelect.push({
  36. value: k,
  37. label: k === '' ? '默认' : k,
  38. children,
  39. })
  40. }
  41. }
  42. const getAllTags = async () => {
  43. const res = (await axiosInstance.post(
  44. MaterialAPI.getAllTags,
  45. {},
  46. )) as TagsRes
  47. if (res.code !== 0) {
  48. ElMessage.error('获取标签失败')
  49. return []
  50. }
  51. if (!res.data) return []
  52. return res.data
  53. }
  54. /**
  55. * 构建过滤函数
  56. * @param queryString 查询值
  57. */
  58. const createFilter = (queryString: string) => {
  59. return (restaurant: TagItem) => {
  60. return (
  61. restaurant.name.toLowerCase().indexOf(queryString.toLowerCase()) === 0
  62. )
  63. }
  64. }
  65. return {
  66. getGameInfo,
  67. generateGameSelect,
  68. getAllTags,
  69. createFilter,
  70. }
  71. }