index.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import MHeader from '@/components/m-header';
  2. import MSticky from '@/components/m-sticky';
  3. import { defineComponent, onMounted, reactive, ref } from 'vue';
  4. import styles from './index.module.less';
  5. import { useRoute, useRouter } from 'vue-router';
  6. import {
  7. Button,
  8. Cell,
  9. CellGroup,
  10. Checkbox,
  11. CheckboxGroup,
  12. Image,
  13. List,
  14. showToast
  15. } from 'vant';
  16. import iconBook from './images/icon-book.png';
  17. import request from '@/helpers/request';
  18. import MEmpty from '@/components/m-empty';
  19. import { browser } from '@/helpers/utils';
  20. import { postMessage } from '@/helpers/native-message';
  21. export default defineComponent({
  22. name: 'wroing-book',
  23. setup() {
  24. const route = useRoute();
  25. const router = useRouter();
  26. const checkboxRefs = ref([] as any);
  27. const forms = reactive({
  28. type: route.query.type,
  29. list: [] as any,
  30. checked: [] as any,
  31. isClick: false,
  32. listState: {
  33. dataShow: true, // 判断是否有数据
  34. loading: false,
  35. finished: false
  36. },
  37. params: {
  38. page: 1,
  39. rows: 20
  40. }
  41. });
  42. const getList = async () => {
  43. try {
  44. if (forms.isClick) return;
  45. forms.isClick = true;
  46. const res = await request.post('/edu-app/knowledgePoint/list ', {
  47. data: {
  48. ...forms.params
  49. }
  50. });
  51. forms.listState.loading = false;
  52. const result = res.data || {};
  53. // 处理重复请求数据
  54. if (forms.list.length > 0 && result.current === 1) {
  55. return;
  56. }
  57. forms.list = forms.list.concat(result.rows || []);
  58. forms.listState.finished = result.current >= result.pages;
  59. forms.params.page = result.current + 1;
  60. forms.listState.dataShow = forms.list.length > 0;
  61. forms.isClick = false;
  62. } catch {
  63. forms.listState.dataShow = false;
  64. forms.listState.finished = true;
  65. forms.isClick = false;
  66. }
  67. };
  68. const checkboxToggle = (index: number) => {
  69. checkboxRefs.value[index].toggle();
  70. };
  71. const onSubmit = () => {
  72. try {
  73. if (forms.checked.length <= 0) {
  74. showToast('请选择知识点');
  75. return;
  76. }
  77. if (forms.type === 'TEST') {
  78. // 模拟测试
  79. router.push({
  80. path: '/examination-mode',
  81. query: { knowledgePointIds: forms.checked.join(',') }
  82. });
  83. } else {
  84. router.push({
  85. path: '/practice-mode',
  86. query: { knowledgePointIds: forms.checked.join(',') }
  87. });
  88. }
  89. } catch {
  90. //
  91. }
  92. };
  93. onMounted(() => {
  94. getList();
  95. });
  96. return () => (
  97. <div class={styles.woringBook}>
  98. <MSticky position="top">
  99. <MHeader border={false} background="transparent">
  100. {{
  101. content: () => (
  102. <div class={styles.woringHeader}>
  103. <i
  104. onClick={() => {
  105. if (browser().isApp) {
  106. postMessage({
  107. api: 'goBack'
  108. });
  109. } else {
  110. router.back();
  111. }
  112. }}
  113. class={[
  114. 'van-badge__wrapper van-icon van-icon-arrow-left van-nav-bar__arrow',
  115. styles.leftArrow
  116. ]}></i>
  117. <span class={styles.title}>
  118. <i></i>
  119. </span>
  120. </div>
  121. )
  122. }}
  123. </MHeader>
  124. </MSticky>
  125. <div class={styles.woringSecgtion}>
  126. <div class={styles.title}>请选择知识点</div>
  127. {forms.listState.dataShow ? (
  128. <List
  129. finished={forms.listState.finished}
  130. finishedText=" "
  131. class={[styles.container, styles.containerInformation]}
  132. onLoad={getList}
  133. immediateCheck={false}>
  134. <CellGroup border={false}>
  135. <CheckboxGroup v-model={forms.checked}>
  136. {forms.list.map((item: any, index: number) => (
  137. <Cell
  138. center
  139. onClick={() => checkboxToggle(index)}
  140. class={styles.cell}>
  141. {{
  142. icon: () => (
  143. <Image src={iconBook} class={styles.iconImg} />
  144. ),
  145. title: () => (
  146. <div class={styles.cellTitle}>{item.name}</div>
  147. ),
  148. label: () => (
  149. <p class={styles.cellContent}>
  150. 共<span>{item.questionNum}</span>道题
  151. </p>
  152. ),
  153. 'right-icon': () => (
  154. <Checkbox
  155. name={item.id}
  156. ref={el => (checkboxRefs.value[index] = el)}
  157. onClick={(e: MouseEvent) => e.stopPropagation()}
  158. />
  159. )
  160. }}
  161. </Cell>
  162. ))}
  163. </CheckboxGroup>
  164. </CellGroup>
  165. </List>
  166. ) : (
  167. <MEmpty description="暂无知识点" />
  168. )}
  169. </div>
  170. {/* {forms.list.length <= 0 && !forms.loading && (
  171. <div class={styles.emptyContainer}>
  172. <MEmpty description="暂无知识点" />
  173. </div>
  174. )} */}
  175. <MSticky position="bottom" varName="--bottom-height">
  176. <div class={'btnGroup'}>
  177. <Button round block type="primary" onClick={onSubmit}>
  178. 确认
  179. </Button>
  180. </div>
  181. </MSticky>
  182. </div>
  183. );
  184. }
  185. });