index.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import { PropType, defineComponent, onMounted, reactive, watch } from 'vue';
  2. import ResourceSearchGroup from './resource-search-group';
  3. import { NModal, NScrollbar, NSpin } from 'naive-ui';
  4. import styles from './index.module.less';
  5. import CardType from '/src/components/card-type';
  6. import TheEmpty from '/src/components/TheEmpty';
  7. import { useThrottleFn } from '@vueuse/core';
  8. import { usePrepareStore } from '/src/store/modules/prepareLessons';
  9. import { musicSheetPage } from '/src/views/prepare-lessons/api';
  10. import TrainUpdate from '/src/views/attend-class/model/train-update';
  11. import requestOrigin from 'umi-request';
  12. import CardPreview from '/src/components/card-preview';
  13. import { evaluateDifficult } from '/src/utils/contants';
  14. import { eventGlobal } from '/src/utils';
  15. import { materialQueryPage } from '/src/views/natural-resources/api';
  16. const formatType = (type: string) => {
  17. if (type === 'sahreMusic') {
  18. return 2;
  19. } else if (type === 'myMusic') {
  20. return 3;
  21. } else if (type === 'collectMusic') {
  22. return 4;
  23. }
  24. };
  25. export const typeFormat = (trainingType: string, configJson: any) => {
  26. let tList: string[] = [];
  27. if (trainingType === 'EVALUATION') {
  28. tList = [
  29. `${evaluateDifficult[configJson.evaluateDifficult]}`,
  30. configJson.practiceChapterBegin || configJson.practiceChapterEnd
  31. ? `${configJson.practiceChapterBegin}-${configJson.practiceChapterEnd}小节`
  32. : '全部小节',
  33. // `速度${configJson.evaluateSpeed}`,
  34. `${configJson.trainingTimes}分合格`
  35. ];
  36. } else {
  37. tList = [
  38. `${configJson.practiceChapterBegin}-${configJson.practiceChapterEnd}小节`,
  39. `速度${configJson.practiceSpeed}`,
  40. `${configJson.trainingTimes}分钟`
  41. ];
  42. }
  43. return tList;
  44. };
  45. export default defineComponent({
  46. name: 'share-resources',
  47. props: {
  48. type: {
  49. type: String as PropType<'myMusic' | 'sahreMusic' | 'collectMusic'>,
  50. default: 'myMusic'
  51. },
  52. /** 类型 */
  53. cardType: {
  54. type: String as PropType<'' | 'homerowk-record'>,
  55. default: ''
  56. }
  57. },
  58. setup(props) {
  59. const prepareStore = usePrepareStore();
  60. const state = reactive({
  61. loading: false,
  62. finshed: false, // 是否加载完
  63. pagination: {
  64. page: 1,
  65. rows: 20
  66. },
  67. searchGroup: {
  68. name: '',
  69. type: 'MUSIC', //
  70. musicSheetCategoriesId: '',
  71. sourceType: formatType(props.type),
  72. status: 1,
  73. versionFlag: false,
  74. musicSubject: null
  75. },
  76. tableList: [] as any,
  77. editStatus: false,
  78. editItem: {} as any,
  79. show: false,
  80. item: {} as any
  81. });
  82. const getList = async () => {
  83. try {
  84. if (!prepareStore.getSubjectId && props.cardType !== 'homerowk-record')
  85. return;
  86. if (state.pagination.page === 1) {
  87. state.loading = true;
  88. }
  89. // const { data } = await musicSheetPage({
  90. // ...state.searchGroup,
  91. // ...state.pagination,
  92. // musicSubject: prepareStore.getSubjectId
  93. // });
  94. const { data } = await materialQueryPage({
  95. ...state.searchGroup,
  96. ...state.pagination,
  97. subjectId: prepareStore.getSubjectId
  98. });
  99. state.loading = false;
  100. const tempRows = data.rows || [];
  101. const temp: any = [];
  102. // tempRows.forEach((row: any) => {
  103. // const index = prepareStore.getTrainList.findIndex(
  104. // (course: any) => course.musicId === row.id
  105. // );
  106. // temp.push({
  107. // id: row.id,
  108. // coverImg: row.musicSvg,
  109. // type: 'MUSIC',
  110. // title: row.musicSheetName,
  111. // isCollect: false,
  112. // isSelected: true,
  113. // content: row.id,
  114. // xmlFileUrl: row.xmlFileUrl,
  115. // exist: index !== -1 ? true : false // 是否存在
  116. // });
  117. // });
  118. tempRows.forEach((row: any) => {
  119. const index = prepareStore.getCoursewareList.findIndex(
  120. (course: any) => course.materialId === row.id
  121. );
  122. temp.push({
  123. id: row.id,
  124. coverImg: row.coverImg,
  125. type: row.type,
  126. title: row.name,
  127. isCollect: !!row.favoriteFlag,
  128. isSelected: row.sourceFrom === 'PLATFORM' ? true : false,
  129. content: row.content,
  130. xmlFileUrl: row.xmlFileUrl,
  131. exist: index !== -1 ? true : false // 是否存在
  132. });
  133. });
  134. state.tableList.push(...temp);
  135. state.finshed = data.pages <= data.current ? true : false;
  136. } catch {
  137. state.loading = false;
  138. }
  139. };
  140. const onSearch = async (item: any) => {
  141. state.pagination.page = 1;
  142. state.tableList = [];
  143. state.searchGroup = Object.assign(state.searchGroup, item);
  144. getList();
  145. };
  146. // 声部变化时
  147. watch(
  148. () => prepareStore.getSubjectId,
  149. () => {
  150. onSearch(state.searchGroup);
  151. }
  152. );
  153. watch(
  154. () => prepareStore.trainList,
  155. () => {
  156. state.tableList.forEach((item: any) => {
  157. const index = prepareStore.getTrainList.findIndex(
  158. (course: any) => course.musicId === item.id
  159. );
  160. item.exist = index !== -1 ? true : false; // 是否存在
  161. });
  162. },
  163. {
  164. deep: true,
  165. immediate: true
  166. }
  167. );
  168. const throttledFn = useThrottleFn(() => {
  169. state.pagination.page = state.pagination.page + 1;
  170. getList();
  171. }, 500);
  172. // 添加资源
  173. const onAdd = async (item: any) => {
  174. let xmlStatus = 'init';
  175. // 第一个声部小节
  176. let firstMeasures: any = null;
  177. try {
  178. // 获取文件
  179. const res = await requestOrigin.get(item.xmlFileUrl, {
  180. mode: 'cors'
  181. });
  182. const xmlParse = new DOMParser().parseFromString(res, 'text/xml');
  183. const parts = xmlParse.getElementsByTagName('part');
  184. firstMeasures = parts[0]?.getElementsByTagName('measure');
  185. xmlStatus = 'success';
  186. } catch (error) {
  187. xmlStatus = 'error';
  188. }
  189. // 判断读取小节数
  190. if (xmlStatus == 'success') {
  191. item.practiceChapterMax = firstMeasures.length;
  192. } else {
  193. item.practiceChapterMax = 0;
  194. }
  195. item.coursewareKnowledgeDetailId = prepareStore.getSelectKey;
  196. item.subjectId = prepareStore.getSubjectId;
  197. state.editItem = item;
  198. state.editStatus = true;
  199. };
  200. onMounted(() => {
  201. getList();
  202. eventGlobal.on('onTrainDragItem', (item: any, point?: any) => {
  203. console.log('onTrainDragItem', Date.now());
  204. onAdd(item);
  205. });
  206. });
  207. return () => (
  208. <div>
  209. <ResourceSearchGroup onSearch={(item: any) => onSearch(item)} />
  210. <NScrollbar
  211. class={styles.listContainer}
  212. onScroll={(e: any) => {
  213. const clientHeight = e.target?.clientHeight;
  214. const scrollTop = e.target?.scrollTop;
  215. const scrollHeight = e.target?.scrollHeight;
  216. // 是否到底,是否加载完
  217. if (
  218. clientHeight + scrollTop + 20 >= scrollHeight &&
  219. !state.finshed &&
  220. !state.loading
  221. ) {
  222. throttledFn();
  223. }
  224. }}>
  225. <NSpin show={state.loading} size={'small'}>
  226. <div
  227. class={[
  228. styles.listSection,
  229. !state.loading && state.tableList.length <= 0
  230. ? styles.emptySection
  231. : ''
  232. ]}>
  233. {state.tableList.length > 0 && (
  234. <div class={styles.list}>
  235. {state.tableList.map((item: any) => (
  236. <CardType
  237. isShowAdd
  238. isShowCollect={false}
  239. item={item}
  240. draggable
  241. // isShowAddDisabled={!prepareStore.getIsEditTrain}
  242. disabledMouseHover={false}
  243. onClick={() => {
  244. if (item.type === 'IMG') return;
  245. state.show = true;
  246. state.item = item;
  247. }}
  248. onAdd={(child: any) => onAdd(child)}
  249. />
  250. ))}
  251. </div>
  252. )}
  253. {!state.loading && state.tableList.length <= 0 && <TheEmpty />}
  254. </div>
  255. </NSpin>
  256. </NScrollbar>
  257. {/* 弹窗查看 */}
  258. <CardPreview v-model:show={state.show} item={state.item} />
  259. <NModal
  260. v-model:show={state.editStatus}
  261. class={['modalTitle background', styles.trainEditModal]}
  262. preset="card"
  263. title="作业设置">
  264. <TrainUpdate
  265. item={state.editItem}
  266. type="homework"
  267. onClose={() => (state.editStatus = false)}
  268. onConfirm={(item: any) => {
  269. // state.editItem = {};
  270. // prepareStore.setIsAddTrain(true);
  271. const tList = typeFormat(
  272. item.trainingType,
  273. item.trainingConfigJson
  274. );
  275. //
  276. const train = {
  277. ...item,
  278. id: null,
  279. musicName: state.editItem.title,
  280. typeList: tList
  281. };
  282. eventGlobal.emit('onTrainAddItem', train);
  283. }}
  284. />
  285. </NModal>
  286. </div>
  287. );
  288. }
  289. });