baseIndex.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import {
  2. computed,
  3. defineComponent,
  4. onMounted,
  5. reactive,
  6. ref,
  7. watch
  8. } from 'vue';
  9. import styles from './baseIndex.module.less';
  10. import icon_back from './image/icon_back.svg';
  11. import { Button, Space, Tab, Tabs, showConfirmDialog, Image } from 'vant';
  12. import {
  13. api_lessonCoursewareFavoriteRemove,
  14. api_lessonCoursewareFavoriteSave
  15. } from '../courseware-list/api';
  16. import { NImage } from 'naive-ui';
  17. import { state } from '@/state';
  18. import TheFavorite from '@/components/the-favorite';
  19. import { useRouter } from 'vue-router';
  20. import { postMessage } from '@/helpers/native-message';
  21. import MEmpty from '@/components/m-empty';
  22. import MSearch from '@/components/m-search';
  23. import { api_musicSheetCategoriesPage, api_musicTagTree } from './api';
  24. import queryString from 'query-string';
  25. import { useRect } from '@vant/use';
  26. import { nextTick } from 'process';
  27. import iconDefaultCo from './image/icon-default-co.png';
  28. export default defineComponent({
  29. name: 'baseIndex-list',
  30. setup() {
  31. const router = useRouter();
  32. const popoverShow = ref(false);
  33. const bookVersionList = ref([] as any);
  34. // 返回
  35. const goback = () => {
  36. postMessage({ api: 'goBack' });
  37. };
  38. const forms = reactive({
  39. // currentGradeNum: null,
  40. musicTagId: null,
  41. keyword: null,
  42. page: 1,
  43. rows: 999
  44. // type: 'COURSEWARE'
  45. });
  46. const isShowGuide = ref(false);
  47. const data = reactive({
  48. list: [] as any[],
  49. loading: false,
  50. favoriteList: [] as any[],
  51. details: [] as any[],
  52. bookData: {} as any,
  53. showBook: false,
  54. book: {} as DOMRect
  55. });
  56. const wrapRef = ref();
  57. const wrapItemRef = ref([] as any);
  58. const getTanentList = async () => {
  59. try {
  60. const { data } = await api_musicTagTree();
  61. bookVersionList.value = data || [];
  62. if (bookVersionList.value.length > 0) {
  63. forms.musicTagId = bookVersionList.value[0].id;
  64. }
  65. } catch {
  66. //
  67. }
  68. };
  69. const getList = async () => {
  70. data.loading = true;
  71. const { musicTagId, ...music } = forms;
  72. const res = await api_musicSheetCategoriesPage({
  73. musicTagIds: [musicTagId],
  74. subjectId: state.user.data?.subjectId || '',
  75. ...music
  76. // currentGradeNum: forms.currentGradeNum ? forms.currentGradeNum : ''
  77. });
  78. if (res?.code === 200 && Array.isArray(res?.data?.rows)) {
  79. data.list = res.data.rows.map((item: any) => {
  80. item.load = false;
  81. item.key = Date.now() + item.id;
  82. return item;
  83. });
  84. }
  85. data.loading = false;
  86. isShowGuide.value = true;
  87. // nextTick(() => {
  88. // // 设置样式
  89. // const wrap = useRect(wrapRef);
  90. // const wrapItem = useRect(wrapItemRef.value[0]);
  91. // if (wrapItem.width > 0) {
  92. // wrapRef.value.style.width =
  93. // Math.floor(wrap.width / wrapItem.width) * wrapItem.width + 'px';
  94. // wrapRef.value.style.margin = '0 auto';
  95. // }
  96. // });
  97. };
  98. onMounted(async () => {
  99. // 安卓的状态栏
  100. postMessage({
  101. api: 'setStatusBarVisibility',
  102. content: {
  103. isVisibility: 0
  104. }
  105. });
  106. data.loading = true;
  107. await getTanentList();
  108. await getList();
  109. data.loading = false;
  110. // if (wrapItemRef.value && wrapItemRef.length > 0) {
  111. // console.log(wrapItemRef[0], '121');
  112. // }
  113. });
  114. const handleFavorite = async (item: any) => {
  115. if (item.favoriteFlag) {
  116. await api_lessonCoursewareFavoriteSave({
  117. lessonCoursewareId: item.id
  118. });
  119. } else {
  120. await api_lessonCoursewareFavoriteRemove({
  121. lessonCoursewareId: item.id
  122. });
  123. getList();
  124. }
  125. };
  126. let timer: any = null;
  127. const dubounce = (fn: any, delay: number = 300) => {
  128. if (timer) {
  129. clearTimeout(timer);
  130. }
  131. timer = setTimeout(fn, delay);
  132. };
  133. const onDetail = (item: any) => {
  134. // router.push({
  135. // path: '/co-ai-detail',
  136. // query: {
  137. // musicTagId: forms.musicTagId,
  138. // id: item.id,
  139. // name: item.name
  140. // }
  141. // });
  142. const query = queryString.stringify({
  143. musicTagId: forms.musicTagId,
  144. id: item.id,
  145. name: item.name
  146. });
  147. const url =
  148. location.origin + location.pathname + '#/co-ai-detail?' + query;
  149. console.log('🚀 ~ url:', url);
  150. postMessage({
  151. api: 'openWebView',
  152. content: {
  153. url,
  154. orientation: 0,
  155. isHideTitle: false,
  156. c_orientation: 0 // 0 横屏 1 竖屏
  157. }
  158. });
  159. };
  160. return () => (
  161. <div class={styles.container}>
  162. {/* <div class={styles.head} style={{ opacity: data.showBook ? 0 : '' }}>
  163. <div class={styles.back} onClick={goback}>
  164. <img src={icon_back} />
  165. </div>
  166. </div> */}
  167. <div class={styles.back} onClick={goback}>
  168. <img src={icon_back} />
  169. </div>
  170. <div class={styles.centerSearch}>
  171. <div id="coai-0">
  172. <MSearch
  173. class={[
  174. 'searchNotice'
  175. // data.searchNoticeShow ? styles.searchNoticeShow : ''
  176. ]}
  177. shape="round"
  178. background="transparent"
  179. clearable={false}
  180. placeholder="请输入关键字"
  181. // modelValue={forms.keyword}
  182. // onFocus={() => (data.searchNoticeShow = false)}
  183. // onBlur={val => {
  184. // musicForms.keyword = val?.trim() || '';
  185. // requestAnimationFrame(() => {
  186. // requestAnimationFrame(() => {
  187. // if (musicForms.keyword) {
  188. // data.searchNoticeShow = true;
  189. // }
  190. // });
  191. // });
  192. // }}
  193. onSearch={val => {
  194. forms.keyword = val;
  195. getList();
  196. }}
  197. />
  198. </div>
  199. </div>
  200. <div class={styles.section}>
  201. <div class={styles.directorySection}>
  202. <div class={styles.directionc}>
  203. {bookVersionList.value.map((item: any) => (
  204. <p
  205. class={[
  206. styles.dirItem,
  207. forms.musicTagId == item.id ? styles.active : ''
  208. ]}
  209. onClick={() => {
  210. forms.musicTagId = item.id;
  211. getList();
  212. }}>
  213. {item.name}
  214. </p>
  215. ))}
  216. </div>
  217. </div>
  218. <div class={styles.content}>
  219. <div
  220. class={[
  221. styles.wrap,
  222. data.list.length <= 0 && !data.loading ? styles.emtpyWrap : ''
  223. ]}
  224. ref={wrapRef}>
  225. {data.list.map((item, index) => {
  226. return (
  227. <div class={styles.wrapfirstI}>
  228. <div
  229. ref={(el: any) => (wrapItemRef.value[index] = el)}
  230. class={[
  231. styles.wrapItem,
  232. data.bookData.id === item.id && data.showBook
  233. ? styles.wrapItemHide
  234. : ''
  235. ]}
  236. key={item.key}
  237. onClick={() => onDetail(item)}>
  238. <div class={styles.item}>
  239. <NImage
  240. data-id={item.id}
  241. {...{ id: index == 0 ? 'courseware-0' : '' }}
  242. class={[styles.cover, item.load ? styles.loaded : '']}
  243. objectFit="cover"
  244. src={item.coverImg}
  245. fallbackSrc={iconDefaultCo}
  246. onLoad={() => {
  247. item.load = true;
  248. }}
  249. onError={() => {
  250. item.load = true;
  251. item.error = true;
  252. }}
  253. />
  254. </div>
  255. <div class={styles.name}>{item.name}</div>
  256. {/* <div
  257. id={index === 0 ? 'courseware-1' : ''}
  258. class={styles.favoriteBtn}
  259. onClick={(e: Event) => {
  260. e.stopPropagation();
  261. item.favoriteFlag = !item.favoriteFlag;
  262. dubounce(() => handleFavorite(item));
  263. }}>
  264. <TheFavorite isFavorite={item.favoriteFlag} />
  265. </div> */}
  266. </div>
  267. </div>
  268. );
  269. })}
  270. {data.list.length <= 0 && !data.loading && (
  271. <MEmpty image="list" description="暂无数据" />
  272. )}
  273. </div>
  274. </div>
  275. </div>
  276. </div>
  277. );
  278. }
  279. });