index.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import {
  2. Transition,
  3. TransitionGroup,
  4. computed,
  5. defineComponent,
  6. reactive
  7. } from 'vue';
  8. import styles from './index.module.less';
  9. import icon_back from './images/icon_back.svg';
  10. import icon_separator from './images/icon_separator.svg';
  11. import {
  12. NBreadcrumb,
  13. NBreadcrumbItem,
  14. NButton,
  15. NImage,
  16. NSpace
  17. } from 'naive-ui';
  18. import TheSearch from '/src/components/TheSearch';
  19. import listData from './data.json';
  20. import { IMusicItem } from './type';
  21. import icon_arrow from './images/icon_arrow.svg';
  22. import icon_play from './images/icon_play.svg';
  23. import icon_pause from './images/icon_pause.svg';
  24. import icon_goXiaoku from './images/icon_goXiaoku.svg';
  25. import icon_favitor from '/src/common/images/icon-collect-default.png';
  26. import icon_favitorActive from '/src/common/images/icon-collect-active.png';
  27. import { useRouter } from 'vue-router';
  28. import PlayItem from './component/play-item';
  29. import PlayLoading from './component/play-loading';
  30. export default defineComponent({
  31. name: 'XiaokuMusic',
  32. setup() {
  33. const router = useRouter();
  34. const data = reactive({
  35. tags: [
  36. { name: '全部', id: 0 },
  37. { name: '竖笛', id: 1 },
  38. { name: '排箫', id: 2 },
  39. { name: '口风琴', id: 3 },
  40. { name: '陶笛', id: 4 },
  41. { name: '葫芦丝', id: 5 }
  42. ],
  43. tagIndex: 0,
  44. list: listData.rows as unknown as IMusicItem[],
  45. listActive: 0,
  46. playState: 'pause' as 'play' | 'pause',
  47. showPlayer: false
  48. });
  49. /** 改变模仿的曲谱 */
  50. const handleChange = (item: IMusicItem) => {
  51. const index = data.list.findIndex(_item => _item.id === item.id);
  52. if (index > -1) {
  53. data.listActive = index;
  54. }
  55. };
  56. /** 选中的item */
  57. const activeItem = computed(() => {
  58. return data.list[data.listActive] || {};
  59. });
  60. /** 收藏 */
  61. const handleFavitor = () => {
  62. data.list[data.listActive].delFlag = !data.list[data.listActive].delFlag;
  63. };
  64. /** 播放曲目 */
  65. const handlePlay = (item: IMusicItem) => {
  66. const index = data.list.findIndex(_item => _item.id === item.id);
  67. if (index > -1) {
  68. if (data.listActive === index) {
  69. data.playState = data.playState === 'play' ? 'pause' : 'play';
  70. } else {
  71. data.playState = 'play';
  72. }
  73. data.showPlayer = true;
  74. data.listActive = index;
  75. }
  76. };
  77. /** 音频控制 */
  78. const handleChangeAudio = (type: 'play' | 'pause' | 'pre' | 'next') => {
  79. console.log("🚀 ~ type:", type)
  80. if (type === 'play') {
  81. data.playState = 'play';
  82. } else if (type === 'pause') {
  83. data.playState = 'pause';
  84. } else if (type === 'pre') {
  85. if (data.list[data.listActive - 1]) {
  86. handlePlay(data.list[data.listActive - 1]);
  87. }
  88. } else if (type === 'next') {
  89. if (data.list[data.listActive + 1]) {
  90. handlePlay(data.list[data.listActive + 1]);
  91. }
  92. } else if (type === 'favitor') {
  93. handleFavitor()
  94. }
  95. };
  96. return () => (
  97. <div class={styles.container}>
  98. <NSpace align="center" wrapItem={false} size={16}>
  99. <img
  100. style={{ cursor: 'pointer' }}
  101. src={icon_back}
  102. onClick={() => router.push({ path: '/xiaoku-ai' })}
  103. />
  104. <NBreadcrumb separator="">
  105. <NBreadcrumbItem
  106. onClick={() => router.push({ path: '/xiaoku-ai' })}>
  107. 选择教材
  108. </NBreadcrumbItem>
  109. <img class={styles.separator} src={icon_separator} />
  110. <NBreadcrumbItem>一年级上册人教版(2013版)</NBreadcrumbItem>
  111. </NBreadcrumb>
  112. </NSpace>
  113. <div class={styles.wrap}>
  114. <div class={styles.content}>
  115. <div class={styles.tools}>
  116. <div class={styles.tags}>
  117. <NSpace size={[24, 12]} wrap={false}>
  118. {data.tags.map((item, index) => (
  119. <NButton
  120. round
  121. textColor={data.tagIndex === index ? '#fff' : '#000'}
  122. color={data.tagIndex === index ? '#198CFE' : '#fff'}
  123. onClick={() => (data.tagIndex = index)}>
  124. {item.name}
  125. </NButton>
  126. ))}
  127. </NSpace>
  128. </div>
  129. <TheSearch round />
  130. </div>
  131. <div
  132. class={styles.contentWrap}
  133. style={{ paddingBottom: data.showPlayer ? '90px' : '' }}>
  134. <div class={styles.musicList}>
  135. <div class={styles.wrapList}>
  136. {data.list.map((item: IMusicItem, index) => {
  137. return (
  138. <div
  139. class={[
  140. styles.item,
  141. data.listActive === index && styles.active
  142. ]}
  143. onClick={() => handleChange(item)}>
  144. <div class={styles.img}>
  145. <NImage
  146. lazy
  147. objectFit="cover"
  148. previewDisabled={true}
  149. src={item.titleImg}
  150. onLoad={e => {
  151. (e.target as any).dataset.loaded = 'true';
  152. }}
  153. />
  154. <PlayLoading
  155. class={[
  156. data.listActive === index &&
  157. data.playState === 'play'
  158. ? ''
  159. : styles.showPlayLoading
  160. ]}
  161. />
  162. </div>
  163. <div class={styles.title}>
  164. <div class={styles.titleName}>
  165. {item.musicSheetName}
  166. </div>
  167. <div class={styles.titleDes}>{item.composer}</div>
  168. </div>
  169. <NButton
  170. color="#259CFE"
  171. textColor="#fff"
  172. round
  173. class={styles.btn}
  174. type="primary"
  175. onClick={(e: Event) => {
  176. e.stopPropagation();
  177. handlePlay(item);
  178. }}>
  179. 试听
  180. <img
  181. src={
  182. data.listActive === index &&
  183. data.playState === 'play'
  184. ? icon_pause
  185. : icon_play
  186. }
  187. />
  188. </NButton>
  189. <img class={styles.arrow} src={icon_arrow} />
  190. </div>
  191. );
  192. })}
  193. </div>
  194. </div>
  195. <div class={styles.musicStaff}>
  196. <div class={styles.musicName}>
  197. {activeItem.value.musicSheetName}
  198. </div>
  199. <img class={styles.goBtn} src={icon_goXiaoku} onClick={() => {
  200. window.open('https://dev.kt.colexiu.com/instrument/');
  201. }} />
  202. <div class={styles.favitor} onClick={() => handleFavitor()}>
  203. <Transition name="favitor" mode="out-in">
  204. {activeItem.value.delFlag ? (
  205. <img src={icon_favitorActive} key="1" />
  206. ) : (
  207. <img src={icon_favitor} key="2" />
  208. )}
  209. </Transition>
  210. </div>
  211. <div class={styles.staffImgs}>
  212. <TransitionGroup name="van-fade">
  213. {activeItem.value?.firstTone
  214. ?.split(',')
  215. .map((item, index) => {
  216. return <img src={item} key={item} />;
  217. })}
  218. </TransitionGroup>
  219. </div>
  220. </div>
  221. </div>
  222. </div>
  223. </div>
  224. <PlayItem
  225. show={data.showPlayer}
  226. playState={data.playState}
  227. item={activeItem.value}
  228. onChange={value => handleChangeAudio(value)}
  229. />
  230. </div>
  231. );
  232. }
  233. });