index.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. import {
  2. TransitionGroup,
  3. defineComponent,
  4. nextTick,
  5. onMounted,
  6. reactive,
  7. ref,
  8. watch
  9. } from 'vue';
  10. import styles from './index.module.less';
  11. import MSearch from '@/components/m-search';
  12. import icon_play from '@/common/images/icon_play.svg';
  13. import {
  14. Empty,
  15. List,
  16. Loading,
  17. NoticeBar,
  18. Popup,
  19. showLoadingToast,
  20. showToast
  21. } from 'vant';
  22. import icon_back from './image/icon_back.svg';
  23. import icon_down from '@/common/images/icon_down.svg';
  24. import icon_jianpu from '@/common/images/icon_jianpu.svg';
  25. import icon_jianpuActive from '@/common/images/icon_jianpuActive.svg';
  26. import icons from '@/common/images/index.json';
  27. import {
  28. listenerMessage,
  29. postMessage,
  30. promisefiyPostMessage
  31. } from '@/helpers/native-message';
  32. import { rows } from './data.json';
  33. import html2canvas from 'html2canvas';
  34. import { api_musicSheetCategoriesPage, api_musicSheetPage } from './api';
  35. import { state } from '@/state';
  36. import MEmpty from '@/components/m-empty';
  37. import Coaiguide from '@/custom-plugins/guide-page/coai-guide';
  38. import { usePageVisibility } from '@vant/use';
  39. import TheVip from '@/components/the-vip';
  40. import request from '@/helpers/request';
  41. export default defineComponent({
  42. name: 'co-ai',
  43. setup() {
  44. const categorForms = reactive({
  45. page: 1,
  46. rows: 999,
  47. subjectId: state.user.data?.subjectId || ''
  48. });
  49. const musicForms = reactive({
  50. page: 1,
  51. rows: 20,
  52. status: 1,
  53. keyword: '', // 关键词
  54. musicSheetCategoriesId: ''
  55. });
  56. const titles = rows;
  57. const data = reactive({
  58. /** 教材Index */
  59. typeIndex: 0,
  60. /** 音乐Index */
  61. musicIndex: 0,
  62. /** 显示简谱 */
  63. isShowJianpu: true,
  64. /** 教材列表 */
  65. types: [] as any[],
  66. /** 音乐列表 */
  67. musics: [] as any[],
  68. loading: true,
  69. finshed: false,
  70. searchNoticeShow: false,
  71. searchNotice: {
  72. left: '',
  73. top: '',
  74. width: '',
  75. height: ''
  76. },
  77. showVip: false,
  78. vipMember: state.user.data?.vipMember
  79. });
  80. const downRef = ref();
  81. const showGuide = ref(false);
  82. // 返回
  83. const goback = () => {
  84. postMessage({ api: 'goBack' });
  85. };
  86. /** 去云教练 */
  87. const handleGoto = () => {
  88. if (!data.vipMember) {
  89. data.showVip = true;
  90. return;
  91. }
  92. let src = `${location.origin}/instrument?id=${
  93. data.musics[data.musicIndex]?.id
  94. }&showGuide=true`;
  95. console.log(src);
  96. postMessage({
  97. api: 'openAccompanyWebView',
  98. content: {
  99. url: src,
  100. orientation: 0,
  101. isHideTitle: true,
  102. statusBarTextColor: false,
  103. isOpenLight: true,
  104. c_orientation: 0 // 0 横屏 1 竖屏
  105. }
  106. });
  107. };
  108. /** 保存图片 */
  109. const handleSave = async () => {
  110. showLoadingToast({ message: '正在保存', duration: 0 });
  111. try {
  112. html2canvas(downRef.value, {
  113. backgroundColor: '#fff',
  114. allowTaint: true,
  115. useCORS: true
  116. })
  117. .then(async canvas => {
  118. var dataURL = canvas.toDataURL('image/png', 1); //可选取多种模式
  119. // console.log('🚀 ~ dataURL:', dataURL);
  120. setTimeout(() => {
  121. showToast('已保存到相册');
  122. }, 500);
  123. const res = await promisefiyPostMessage({
  124. api: 'savePicture',
  125. content: {
  126. base64: dataURL
  127. }
  128. });
  129. })
  130. .catch(() => {
  131. setTimeout(() => {
  132. showToast('保存失败');
  133. }, 500);
  134. });
  135. } catch (error) {
  136. setTimeout(() => {
  137. showToast('保存失败');
  138. }, 500);
  139. }
  140. };
  141. /** 获取音乐教材列表 */
  142. const getMusicSheetCategories = async () => {
  143. try {
  144. const res = await api_musicSheetCategoriesPage({
  145. ...categorForms
  146. });
  147. if (res.code === 200 && Array.isArray(res?.data?.rows)) {
  148. data.types = res.data.rows;
  149. if (!musicForms.musicSheetCategoriesId && data.types.length > 0) {
  150. musicForms.musicSheetCategoriesId = data.types[0].id;
  151. }
  152. }
  153. } catch (error) {
  154. console.log('🚀 ~ error:', error);
  155. }
  156. };
  157. /** 获取曲谱列表 */
  158. const getMusicList = async () => {
  159. data.loading = true;
  160. try {
  161. const res = await api_musicSheetPage({
  162. ...musicForms
  163. });
  164. if (res.code === 200 && Array.isArray(res?.data?.rows)) {
  165. data.musics = [...data.musics, ...res.data.rows];
  166. data.finshed = !res.data.next;
  167. }
  168. showGuide.value = true;
  169. } catch (error) {
  170. console.log('🚀 ~ error:', error);
  171. }
  172. data.loading = false;
  173. };
  174. const handleReset = () => {
  175. musicForms.page = 1;
  176. data.musics = [];
  177. getMusicList();
  178. };
  179. const spinRef = ref();
  180. const handleResh = () => {
  181. if (data.loading || data.finshed) return;
  182. musicForms.page = musicForms.page + 1;
  183. getMusicList();
  184. };
  185. const setSearchBox = () => {
  186. const el = document.querySelector('.searchNotice .van-field__control');
  187. if (el) {
  188. const rect = el.getBoundingClientRect();
  189. data.searchNotice.left = rect.x + 'px';
  190. data.searchNotice.top = rect.y + 'px';
  191. data.searchNotice.width = rect.width + 'px';
  192. data.searchNotice.height = rect.height + 'px';
  193. }
  194. };
  195. onMounted(async () => {
  196. await getMusicSheetCategories();
  197. getMusicList();
  198. const obv = new IntersectionObserver(entries => {
  199. if (entries[0].intersectionRatio > 0) {
  200. handleResh();
  201. }
  202. });
  203. nextTick(() => {
  204. obv.observe(spinRef.value);
  205. });
  206. const getUserInfo = async () => {
  207. const res = await request.get('/edu-app/user/getUserInfo', {
  208. initRequest: true, // 初始化接口
  209. requestType: 'form',
  210. hideLoading: true
  211. });
  212. if (res?.code === 200) {
  213. data.vipMember = res.data.vipMember;
  214. }
  215. };
  216. listenerMessage('webViewOnResume', () => {
  217. console.log('页面显示');
  218. getUserInfo();
  219. data.typeIndex = 0;
  220. data.musicIndex = 0;
  221. handleReset();
  222. });
  223. setSearchBox();
  224. });
  225. return () => (
  226. <div class={styles.container}>
  227. <div class={styles.back} onClick={goback}>
  228. <img src={icon_back} />
  229. </div>
  230. <div class={styles.content}>
  231. <div class={[styles.leftContent]}>
  232. <div class={styles.leftBg2}></div>
  233. <div class={styles.leftBg}></div>
  234. <div class={styles.types}>
  235. {data.types.map((item, index) => {
  236. return (
  237. <div
  238. class={[
  239. styles.type,
  240. musicForms.musicSheetCategoriesId === item.id &&
  241. styles.typeActive
  242. ]}
  243. onClick={() => {
  244. musicForms.musicSheetCategoriesId = item.id;
  245. handleReset();
  246. }}>
  247. <div class={styles.typeImg}>
  248. <img
  249. class={styles.typeIcon}
  250. src={item.coverImg}
  251. onLoad={(e: Event) => {
  252. const el = e.target as HTMLImageElement;
  253. el.setAttribute('loaded', 'true');
  254. }}
  255. />
  256. </div>
  257. </div>
  258. );
  259. })}
  260. </div>
  261. <div class={styles.center}>
  262. <div class={styles.centerSearch}>
  263. <div id="coai-0">
  264. <MSearch
  265. class={[
  266. 'searchNotice',
  267. data.searchNoticeShow ? styles.searchNoticeShow : ''
  268. ]}
  269. shape="round"
  270. background="transparent"
  271. clearable={false}
  272. placeholder="请输入关键字"
  273. modelValue={musicForms.keyword}
  274. onFocus={() => (data.searchNoticeShow = false)}
  275. onBlur={val => {
  276. musicForms.keyword = val?.trim() || '';
  277. requestAnimationFrame(() => {
  278. requestAnimationFrame(() => {
  279. if (musicForms.keyword){
  280. data.searchNoticeShow = true;
  281. }
  282. });
  283. });
  284. }}
  285. onSearch={val => {
  286. musicForms.keyword = val;
  287. handleReset();
  288. }}
  289. />
  290. </div>
  291. </div>
  292. <div class={styles.musicContent}>
  293. {data.musics.map((item: any, index: number) => {
  294. return (
  295. <div
  296. class={[
  297. styles.musicItem,
  298. data.musicIndex === index
  299. ? styles.musicActive
  300. : styles.disableNotic
  301. ]}
  302. onClick={() => (data.musicIndex = index)}>
  303. <img
  304. class={styles.musicAvtor}
  305. src={item.titleImg}
  306. onLoad={(e: Event) => {
  307. const el = e.target as HTMLImageElement;
  308. el.setAttribute('loaded', 'true');
  309. }}
  310. />
  311. <div class={styles.musicInfo}>
  312. <div class={styles.musicName}>
  313. <NoticeBar
  314. text={item.musicSheetName}
  315. class={styles.noticeBar}
  316. background="none"
  317. />
  318. </div>
  319. <div class={styles.musicDes}>
  320. <div class={styles.musicFavitor}>{item.usedNum}</div>
  321. <div class={[styles.musicAuthor, 'van-ellipsis']}>
  322. {item.composer || '佚名'}
  323. </div>
  324. </div>
  325. </div>
  326. {/* <img class={[styles.musicIcon]} src={icon_play} /> */}
  327. </div>
  328. );
  329. })}
  330. {!data.finshed && (
  331. <div ref={spinRef} class={styles.loadingWrap}>
  332. <Loading color="#259CFE" />
  333. </div>
  334. )}
  335. {!data.loading && data.musics.length === 0 && (
  336. <div class={styles.empty}>
  337. <MEmpty description="暂无曲谱" />
  338. </div>
  339. )}
  340. </div>
  341. </div>
  342. </div>
  343. <div class={[styles.opacityBg, styles.right]}>
  344. <div class={styles.rightBox}>
  345. <div ref={downRef}>
  346. <div class={styles['right-musicName']}>
  347. {data.musics[data.musicIndex]?.musicSheetName}
  348. </div>
  349. {data.isShowJianpu ? (
  350. <>
  351. {data.musics[data.musicIndex]?.musicSvg
  352. ?.split(',')
  353. .map((item: any, index: number) => {
  354. return (
  355. <img
  356. class={styles.staff}
  357. src={item + '?v=' + Date.now()}
  358. key={item}
  359. crossorigin="anonymous"
  360. />
  361. );
  362. })}
  363. </>
  364. ) : (
  365. <>
  366. {data.musics[data.musicIndex]?.musicImg
  367. ?.split(',')
  368. .map((item: any, index: number) => {
  369. return (
  370. <img
  371. class={styles.staff}
  372. src={item + '?v=' + Date.now()}
  373. key={item}
  374. crossorigin="anonymous"
  375. />
  376. );
  377. })}
  378. </>
  379. )}
  380. </div>
  381. </div>
  382. <div class={styles.rightBtns}>
  383. <img
  384. id="coai-1"
  385. src={data.isShowJianpu ? icon_jianpuActive : icon_jianpu}
  386. onClick={() => (data.isShowJianpu = !data.isShowJianpu)}
  387. />
  388. <img id="coai-2" src={icon_down} onClick={handleSave} />
  389. <div class={styles.rightBtnsRight} id="coai-3">
  390. <img src={icons.icon_start} onClick={() => handleGoto()} />
  391. </div>
  392. </div>
  393. </div>
  394. </div>
  395. {data.searchNotice.width && data.searchNoticeShow && (
  396. <div class={styles.searchNotice} style={{ ...data.searchNotice }}>
  397. <NoticeBar
  398. text={musicForms.keyword}
  399. color="#333"
  400. background="none"
  401. />
  402. </div>
  403. )}
  404. {showGuide.value && <Coaiguide></Coaiguide>}
  405. <Popup
  406. class="popup-custom van-scale"
  407. transition="van-scale"
  408. closeOnClickOverlay={false}
  409. v-model:show={data.showVip}>
  410. <TheVip
  411. onClose={val => {
  412. if (val) {
  413. postMessage({
  414. api: 'openWebView',
  415. content: {
  416. url: `${location.origin}${location.pathname}#/member-center`,
  417. orientation: 1
  418. }
  419. });
  420. }
  421. data.showVip = false;
  422. }}
  423. />
  424. </Popup>
  425. </div>
  426. );
  427. }
  428. });