useData.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. // 处理 区分处理管乐迷 管乐团的数据
  2. import {
  3. queryLessonCourseware_gym,
  4. getLessonCoursewareCourseList_gym,
  5. getMyCourseware_gyt,
  6. getMyCoursewareDetail_gyt,
  7. getuyAlbumInfo_klx,
  8. queryLessonCourseware_klx,
  9. getLessonCoursewareCourseList_klx
  10. } from "@/api/cloudTextbooks.api"
  11. import { httpAjaxErrMsg, httpAjax } from "@/plugin/httpAjax"
  12. import { CODE_ERR_CANCELED } from "@/libs/auth"
  13. import { ElMessage } from "element-plus"
  14. import userStore from "@/store/modules/user"
  15. import { ref, shallowRef, computed } from "vue"
  16. type listType = {
  17. type: string
  18. name: string
  19. img: string
  20. id: string
  21. courseNum: number // 课程数量
  22. }[][]
  23. /**
  24. * 列表数据
  25. */
  26. export const useDataList = () => {
  27. const userStoreHook = userStore()
  28. const listData = shallowRef<listType>([])
  29. let storeData: listType[number] = []
  30. // 专辑
  31. const albumId = ref("")
  32. const albumOpt = shallowRef<{ value: string; label: string }[]>([])
  33. const loading = ref(false)
  34. let coursewareController: AbortController
  35. function handleGetList() {
  36. // GYM,GYT,KLX 区分 获取列表数据
  37. if (userStoreHook.roles === "GYM") {
  38. handleGetList_gym("", "")
  39. } else if (userStoreHook.roles === "GYT") {
  40. handleGetList_gyt()
  41. } else if (userStoreHook.roles === "KLX") {
  42. loading.value = true
  43. httpAjax(getuyAlbumInfo_klx).then(res => {
  44. loading.value = false
  45. if (res.code === 200) {
  46. // 专辑赋值
  47. albumOpt.value = (res.data || []).reduce((arr: any[], item: any) => {
  48. if (item.coursewareCounts > 0) {
  49. arr.push({
  50. value: item.id,
  51. label: item.name
  52. })
  53. }
  54. return arr
  55. }, [])
  56. // 默认取第一个专辑
  57. albumOpt.value.length && (albumId.value = albumOpt.value[0].value)
  58. handleGetList_klx("", albumId.value, "")
  59. }
  60. })
  61. }
  62. }
  63. function handleListQuery(type: string, queryStr: string) {
  64. // GYM,GYT,KLX 区分 查询列表数据
  65. if (userStoreHook.roles === "GYM") {
  66. handleQueryGetList_gym(type, queryStr)
  67. } else if (userStoreHook.roles === "GYT") {
  68. handleQueryGetList_gyt(type, queryStr)
  69. } else if (userStoreHook.roles === "KLX") {
  70. handleQueryGetList_klx(type, albumId.value, queryStr)
  71. }
  72. }
  73. // 获取管乐团数据
  74. function handleGetList_gyt() {
  75. loading.value = true
  76. httpAjaxErrMsg(getMyCourseware_gyt).then(res => {
  77. loading.value = false
  78. if (res.code === 200) {
  79. storeData = (res.data || []).map((item: any) => {
  80. return {
  81. name: item.name,
  82. type: item.courseTypeCode,
  83. img: item.coverImg,
  84. id: item.id,
  85. courseNum: item.courseNum
  86. }
  87. })
  88. listData.value = chunkArray(storeData, 5)
  89. }
  90. })
  91. }
  92. // 管乐团数据查询
  93. function handleQueryGetList_gyt(type: string, queryStr: string) {
  94. const computeData = storeData.filter(item => {
  95. return (type ? item.type === type : true) && (queryStr ? item.name.includes(queryStr) : true)
  96. })
  97. listData.value = chunkArray(computeData, 5)
  98. }
  99. // 获取管乐迷数据
  100. function handleGetList_gym(type: string, queryStr: string) {
  101. if (coursewareController) {
  102. coursewareController.abort()
  103. }
  104. coursewareController = new AbortController()
  105. loading.value = true
  106. httpAjax(queryLessonCourseware_gym, type, coursewareController).then(res => {
  107. // 自己关闭的时候不取消加载
  108. if (res.code === CODE_ERR_CANCELED) {
  109. return
  110. }
  111. loading.value = false
  112. if (res.code === 200) {
  113. const data = (res.data?.rows || []).reduce((arr: any[], item: any) => {
  114. if ((type ? item.subjectId === type : true) && (queryStr ? item.name.includes(queryStr) : true)) {
  115. arr.push({
  116. name: item.name,
  117. type: item.subjectId,
  118. img: item.cover,
  119. id: item.lessonCoursewareId,
  120. courseNum: item.courseNum
  121. })
  122. }
  123. return arr
  124. }, [])
  125. listData.value = chunkArray(data, 5)
  126. } else {
  127. if (res.code !== 511) {
  128. ElMessage({
  129. showClose: true,
  130. message: res.message,
  131. type: "error"
  132. })
  133. }
  134. }
  135. })
  136. }
  137. // 管乐迷数据查询
  138. function handleQueryGetList_gym(type: string, queryStr: string) {
  139. handleGetList_gym(type, queryStr)
  140. }
  141. // 获取酷乐秀数据
  142. function handleGetList_klx(type: string, albumId: string, queryStr: string) {
  143. // 当没有专辑id时候 不查询
  144. if (!albumId) {
  145. return
  146. }
  147. if (coursewareController) {
  148. coursewareController.abort()
  149. }
  150. coursewareController = new AbortController()
  151. loading.value = true
  152. httpAjax(queryLessonCourseware_klx, type, albumId, coursewareController).then(res => {
  153. // 自己关闭的时候不取消加载
  154. if (res.code === CODE_ERR_CANCELED) {
  155. return
  156. }
  157. loading.value = false
  158. if (res.code === 200) {
  159. const data = (res.data?.rows || []).reduce((arr: any[], item: any) => {
  160. if (queryStr ? item.musicSheetName.includes(queryStr) : true) {
  161. arr.push({
  162. name: item.musicSheetName,
  163. type: item.subjectId,
  164. img: item.titleImg,
  165. id: item.id,
  166. courseNum: item.courseNum
  167. })
  168. }
  169. return arr
  170. }, [])
  171. listData.value = chunkArray(data, 5)
  172. } else {
  173. if (res.code !== 511) {
  174. ElMessage({
  175. showClose: true,
  176. message: res.message,
  177. type: "error"
  178. })
  179. }
  180. }
  181. })
  182. }
  183. // 管乐迷数据查询
  184. function handleQueryGetList_klx(type: string, albumId: string, queryStr: string) {
  185. handleGetList_klx(type, albumId, queryStr)
  186. }
  187. return { loading, listData, albumId, albumOpt, handleGetList, handleListQuery }
  188. }
  189. /**
  190. * 列表详情数据
  191. */
  192. type listDetail = {
  193. name: string
  194. id: string
  195. useNum?: number
  196. lockFlag?: boolean
  197. }[]
  198. type listDetailType = [listDetail, listDetail]
  199. export const useDataDetailList = () => {
  200. const userStoreHook = userStore()
  201. let coursewareDetailController: AbortController
  202. const listData = shallowRef<listDetail[][]>([])
  203. const listSearchData = shallowRef<any[]>([])
  204. const flattenCoursewareList = ref<any[]>([]) // 扁平化coursewareList
  205. const activeCollapse = ref<any>({
  206. parentData: {
  207. ids: []
  208. }
  209. })
  210. const pageNum = ref<number>(0)
  211. const loading = ref(false)
  212. const searchLoading = ref(false)
  213. const listDetailData = computed<listDetailType>(() => {
  214. const data = listData.value[pageNum.value] || []
  215. return [data[0] || [], data[1] || []]
  216. })
  217. function handleGetDetailList(id: string, isSearch = false, search?: string) {
  218. // GYM,GYT,KLX 区分 查询详情列表
  219. if (userStoreHook.roles === "GYM") {
  220. handleGetDetaList_gym(id, isSearch, search)
  221. } else if (userStoreHook.roles === "GYT") {
  222. handleGetDetailList_gyt(id)
  223. } else if (userStoreHook.roles === "KLX") {
  224. handleGetDetailList_klx(id)
  225. }
  226. }
  227. function handlePage(type: "next" | "prev") {
  228. type === "next" ? pageNum.value++ : pageNum.value--
  229. }
  230. let flattenCoursewareListData: any[] = []
  231. // 获取管乐迷
  232. function handleGetDetaList_gym(id: string, isSearch = false, search?: string) {
  233. if (coursewareDetailController) {
  234. coursewareDetailController.abort()
  235. }
  236. coursewareDetailController = new AbortController()
  237. if (!isSearch) loading.value = true
  238. searchLoading.value = true
  239. httpAjax(getLessonCoursewareCourseList_gym, { id, search, abortController: coursewareDetailController }).then(res => {
  240. if (!isSearch) loading.value = false
  241. searchLoading.value = false
  242. if (res.code === 200) {
  243. const data = (res.data || []).map((item: any) => {
  244. return {
  245. name: item.coursewareDetailName,
  246. id: item.coursewareDetailId
  247. }
  248. })
  249. if (!isSearch) {
  250. listData.value = chunkArray(chunkArray(data, 7), 2)
  251. }
  252. const resultList = res.data || []
  253. resultList.forEach((item: any) => {
  254. item.children = item.knowledgePointList || []
  255. item.id = item.coursewareDetailId
  256. item.name = item.coursewareDetailName
  257. })
  258. flattenCoursewareListData = []
  259. listSearchData.value = filterPointList(resultList)
  260. flattenCoursewareList.value = flattenCoursewareListData
  261. if (flattenCoursewareList.value[0]) {
  262. activeCollapse.value = flattenCoursewareList.value[0]
  263. }
  264. }
  265. })
  266. }
  267. // 获取管乐团
  268. function handleGetDetailList_gyt(id: string) {
  269. loading.value = true
  270. httpAjaxErrMsg(getMyCoursewareDetail_gyt, id).then(res => {
  271. loading.value = false
  272. if (res.code === 200) {
  273. const data = (res.data || []).map((item: any) => {
  274. return {
  275. name: item.coursewareDetailName,
  276. id: item.lessonCoursewareDetailId,
  277. useNum: item.useNum,
  278. lockFlag: false // 云课堂默认不锁
  279. }
  280. })
  281. listData.value = chunkArray(chunkArray(data, 7), 2)
  282. }
  283. })
  284. }
  285. // 获取酷乐秀
  286. function handleGetDetailList_klx(id: string) {
  287. loading.value = true
  288. httpAjaxErrMsg(getLessonCoursewareCourseList_klx, id).then(res => {
  289. loading.value = false
  290. if (res.code === 200) {
  291. const data = (res.data || []).map((item: any) => {
  292. return {
  293. name: item.coursewareDetailName,
  294. id: item.coursewareDetailId
  295. }
  296. })
  297. listData.value = chunkArray(chunkArray(data, 7), 2)
  298. }
  299. })
  300. }
  301. function filterPointList(pointList: any[], parentData?: { ids: string[]; name: string }): any[] {
  302. // 设置父级及以上id数组和父级name
  303. return pointList.map(point => {
  304. if (point.children) {
  305. return Object.assign(point, {
  306. children: filterPointList(point.children, { ids: [...(parentData?.ids || []), point.id], name: point.name })
  307. })
  308. } else {
  309. return Object.assign(point, {
  310. materialList: point.materialList.map((item: any) => {
  311. item.parentData = {
  312. ids: [...(parentData?.ids || []), point.id],
  313. name: point.name
  314. }
  315. flattenCoursewareListData.push(item)
  316. return item
  317. })
  318. })
  319. }
  320. })
  321. }
  322. return {
  323. handleGetDetailList,
  324. loading,
  325. listDetailData,
  326. searchLoading,
  327. listSearchData,
  328. activeCollapse,
  329. listData,
  330. pageNum,
  331. handlePage
  332. }
  333. }
  334. function chunkArray(array: any[], size: number) {
  335. const result = []
  336. for (let i = 0; i < array.length; i += size) {
  337. result.push(array.slice(i, i + size))
  338. }
  339. return result
  340. }