baseIndex.tsx 8.6 KB

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