index.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. onFocus={() => (data.searchNoticeShow = false)}
  274. onBlur={val => {
  275. musicForms.keyword = val;
  276. requestAnimationFrame(() => {
  277. requestAnimationFrame(() => {
  278. data.searchNoticeShow = true;
  279. });
  280. });
  281. }}
  282. onSearch={val => {
  283. musicForms.keyword = val;
  284. handleReset();
  285. }}
  286. />
  287. </div>
  288. </div>
  289. <div class={styles.musicContent}>
  290. {data.musics.map((item: any, index: number) => {
  291. return (
  292. <div
  293. class={[
  294. styles.musicItem,
  295. data.musicIndex === index
  296. ? styles.musicActive
  297. : styles.disableNotic
  298. ]}
  299. onClick={() => (data.musicIndex = index)}>
  300. <img
  301. class={styles.musicAvtor}
  302. src={item.titleImg}
  303. onLoad={(e: Event) => {
  304. const el = e.target as HTMLImageElement;
  305. el.setAttribute('loaded', 'true');
  306. }}
  307. />
  308. <div class={styles.musicInfo}>
  309. <div class={styles.musicName}>
  310. <NoticeBar
  311. text={item.musicSheetName}
  312. class={styles.noticeBar}
  313. background="none"
  314. />
  315. </div>
  316. <div class={styles.musicDes}>
  317. <div class={styles.musicFavitor}>{item.usedNum}</div>
  318. <div class={[styles.musicAuthor, 'van-ellipsis']}>
  319. {item.composer || '佚名'}
  320. </div>
  321. </div>
  322. </div>
  323. {/* <img class={[styles.musicIcon]} src={icon_play} /> */}
  324. </div>
  325. );
  326. })}
  327. {!data.finshed && (
  328. <div ref={spinRef} class={styles.loadingWrap}>
  329. <Loading color="#259CFE" />
  330. </div>
  331. )}
  332. {!data.loading && data.musics.length === 0 && (
  333. <div class={styles.empty}>
  334. <MEmpty description="暂无曲谱" />
  335. </div>
  336. )}
  337. </div>
  338. </div>
  339. </div>
  340. <div class={[styles.opacityBg, styles.right]}>
  341. <div class={styles.rightBox}>
  342. <div ref={downRef}>
  343. <div class={styles['right-musicName']}>
  344. {data.musics[data.musicIndex]?.musicSheetName}
  345. </div>
  346. {data.isShowJianpu ? (
  347. <>
  348. {data.musics[data.musicIndex]?.musicSvg
  349. ?.split(',')
  350. .map((item: any, index: number) => {
  351. return (
  352. <img
  353. class={styles.staff}
  354. src={item + '?v=' + Date.now()}
  355. key={item}
  356. crossorigin="anonymous"
  357. />
  358. );
  359. })}
  360. </>
  361. ) : (
  362. <>
  363. {data.musics[data.musicIndex]?.musicImg
  364. ?.split(',')
  365. .map((item: any, index: number) => {
  366. return (
  367. <img
  368. class={styles.staff}
  369. src={item + '?v=' + Date.now()}
  370. key={item}
  371. crossorigin="anonymous"
  372. />
  373. );
  374. })}
  375. </>
  376. )}
  377. </div>
  378. </div>
  379. <div class={styles.rightBtns}>
  380. <img
  381. id="coai-1"
  382. src={data.isShowJianpu ? icon_jianpuActive : icon_jianpu}
  383. onClick={() => (data.isShowJianpu = !data.isShowJianpu)}
  384. />
  385. <img id="coai-2" src={icon_down} onClick={handleSave} />
  386. <div class={styles.rightBtnsRight} id="coai-3">
  387. <img src={icons.icon_start} onClick={() => handleGoto()} />
  388. </div>
  389. </div>
  390. </div>
  391. </div>
  392. {data.searchNotice.width && data.searchNoticeShow && (
  393. <div class={styles.searchNotice} style={{ ...data.searchNotice }}>
  394. <NoticeBar
  395. text={musicForms.keyword}
  396. color="#333"
  397. background="none"
  398. />
  399. </div>
  400. )}
  401. {showGuide.value && <Coaiguide></Coaiguide>}
  402. <Popup
  403. class="popup-custom van-scale"
  404. transition="van-scale"
  405. closeOnClickOverlay={false}
  406. v-model:show={data.showVip}>
  407. <TheVip
  408. onClose={val => {
  409. if (val) {
  410. postMessage({
  411. api: 'openWebView',
  412. content: {
  413. url: `${location.origin}${location.pathname}#/member-center`,
  414. orientation: 1
  415. }
  416. });
  417. }
  418. data.showVip = false;
  419. }}
  420. />
  421. </Popup>
  422. </div>
  423. );
  424. }
  425. });