teacher-list.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import OHeader from '@/components/o-header'
  2. import OPopup from '@/components/o-popup'
  3. import OSticky from '@/components/o-sticky'
  4. import { state } from '@/state'
  5. import { Button, Cell, CellGroup, Field, List, Image, Tag } from 'vant'
  6. import { defineComponent, onMounted, PropType, reactive, watch } from 'vue'
  7. import TeacherList from '../modal/teacher-list'
  8. import styles from './teacher-list.module.less'
  9. import iconTeacher from '@common/images/icon_teacher.png'
  10. import OEmpty from '@/components/o-empty'
  11. import request from '@/helpers/request'
  12. import OSearch from '@/components/o-search'
  13. export default defineComponent({
  14. name: 'teacher-list',
  15. props: {
  16. header: {
  17. // 是否显示头部
  18. type: Boolean,
  19. default: true
  20. },
  21. mode: {
  22. type: String as PropType<'fixed' | 'sticky'>,
  23. default: 'fixed'
  24. },
  25. courseType: {
  26. type: String,
  27. default: ''
  28. },
  29. subjectIdList: {
  30. type: Array,
  31. default: () => []
  32. }
  33. },
  34. emits: ['close', 'select'],
  35. setup(props, { slots, attrs, emit }) {
  36. const forms = reactive({
  37. teacherStatus: false,
  38. isLoad: false,
  39. list: [] as any,
  40. listState: {
  41. dataShow: true, // 判断是否有数据
  42. loading: false,
  43. finished: false
  44. },
  45. params: {
  46. keyword: null,
  47. courseType: props.courseType,
  48. subjectIdList: props.subjectIdList,
  49. page: 1,
  50. rows: 20
  51. }
  52. })
  53. const getList = async () => {
  54. try {
  55. if (forms.isLoad) return
  56. forms.isLoad = true
  57. const res = await request.post('/api-school/teacher/page', {
  58. data: {
  59. ...forms.params,
  60. schoolId: state.user.data.school.id,
  61. delFlag: false
  62. }
  63. })
  64. forms.listState.loading = false
  65. const result = res.data || {}
  66. // 处理重复请求数据
  67. if (forms.list.length > 0 && result.current === 1) {
  68. return
  69. }
  70. const rows = result.rows || []
  71. rows.forEach((item: any) => {
  72. item.subjectNames = item.subjectName ? item.subjectName.split(',') : []
  73. })
  74. forms.list = forms.list.concat(rows)
  75. forms.listState.finished = result.current >= result.pages
  76. forms.params.page = result.current + 1
  77. forms.listState.dataShow = forms.list.length > 0
  78. forms.isLoad = false
  79. } catch {
  80. forms.listState.dataShow = false
  81. forms.listState.finished = true
  82. forms.isLoad = false
  83. }
  84. }
  85. // 搜索
  86. const onSearch = () => {
  87. forms.params.page = 1
  88. forms.list = []
  89. forms.listState.dataShow = true // 判断是否有数据
  90. forms.listState.loading = false
  91. forms.listState.finished = false
  92. getList()
  93. }
  94. const onSelect = (item: any) => {
  95. emit('close')
  96. emit('select', item)
  97. }
  98. // 声部变化时重新请求接口
  99. watch(
  100. () => props.courseType,
  101. () => {
  102. console.log(props.courseType, 'course')
  103. forms.params.courseType = props.courseType
  104. onSearch()
  105. }
  106. )
  107. watch(
  108. () => props.subjectIdList,
  109. () => {
  110. forms.params.subjectIdList = props.subjectIdList
  111. onSearch()
  112. }
  113. )
  114. onMounted(() => {
  115. getList()
  116. })
  117. return () => (
  118. <div>
  119. <OSticky position="top" mode={props.mode}>
  120. {props.header && <OHeader title="选择老师" />}
  121. <OSearch
  122. inputBackground="white"
  123. background="#F8F8F8"
  124. placeholder="老师名称/手机号"
  125. onSearch={(val: any) => {
  126. forms.params.keyword = val
  127. onSearch()
  128. }}
  129. />
  130. </OSticky>
  131. {forms.listState.dataShow ? (
  132. <List
  133. v-model:loading={forms.listState.loading}
  134. finished={forms.listState.finished}
  135. finishedText=" "
  136. onLoad={getList}
  137. immediateCheck={false}
  138. >
  139. {forms.list.map((item: any) => (
  140. <Cell center class={styles.cellTeacher} onClick={() => onSelect(item)}>
  141. {{
  142. icon: () => <Image class={styles.img} src={item.avatar || iconTeacher} />,
  143. title: () => (
  144. <div class={styles.content}>
  145. <p class={styles.name}>{item.nickname}</p>
  146. <p class={styles.class}>
  147. {item.subjectNames &&
  148. item.subjectNames.map((subject: any) => (
  149. <Tag type="primary">{subject}</Tag>
  150. ))}
  151. </p>
  152. </div>
  153. )
  154. }}
  155. </Cell>
  156. ))}
  157. </List>
  158. ) : (
  159. <OEmpty btnStatus={false} classImgSize="SMALL" tips="暂无老师" />
  160. )}
  161. </div>
  162. )
  163. }
  164. })