teacher-list.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. }
  62. })
  63. forms.listState.loading = false
  64. const result = res.data || {}
  65. // 处理重复请求数据
  66. if (forms.list.length > 0 && result.current === 1) {
  67. return
  68. }
  69. const rows = result.rows || []
  70. rows.forEach((item: any) => {
  71. item.subjectNames = item.subjectName ? item.subjectName.split(',') : []
  72. })
  73. forms.list = forms.list.concat(rows)
  74. forms.listState.finished = result.current >= result.pages
  75. forms.params.page = result.current + 1
  76. forms.listState.dataShow = forms.list.length > 0
  77. forms.isLoad = false
  78. } catch {
  79. forms.listState.dataShow = false
  80. forms.listState.finished = true
  81. forms.isLoad = false
  82. }
  83. }
  84. // 搜索
  85. const onSearch = () => {
  86. forms.params.page = 1
  87. forms.list = []
  88. forms.listState.dataShow = true // 判断是否有数据
  89. forms.listState.loading = false
  90. forms.listState.finished = false
  91. getList()
  92. }
  93. const onSelect = (item: any) => {
  94. emit('close')
  95. emit('select', item)
  96. }
  97. // 声部变化时重新请求接口
  98. watch(
  99. () => props.courseType,
  100. () => {
  101. forms.params.courseType = props.courseType
  102. onSearch()
  103. }
  104. )
  105. watch(
  106. () => props.subjectIdList,
  107. () => {
  108. forms.params.subjectIdList = props.subjectIdList
  109. onSearch()
  110. }
  111. )
  112. onMounted(() => {
  113. getList()
  114. })
  115. return () => (
  116. <div>
  117. <OSticky position="top" mode={props.mode}>
  118. {props.header && <OHeader title="选择老师" />}
  119. <OSearch
  120. inputBackground="white"
  121. background="#F8F8F8"
  122. placeholder="老师名称/手机号"
  123. onSearch={(val: any) => {
  124. forms.params.keyword = val
  125. onSearch()
  126. }}
  127. />
  128. </OSticky>
  129. {forms.listState.dataShow ? (
  130. <List
  131. v-model:loading={forms.listState.loading}
  132. finished={forms.listState.finished}
  133. finishedText=" "
  134. onLoad={getList}
  135. immediateCheck={false}
  136. >
  137. {forms.list.map((item: any) => (
  138. <Cell center class={styles.cellTeacher} onClick={() => onSelect(item)}>
  139. {{
  140. icon: () => <Image class={styles.img} src={item.avatar || iconTeacher} />,
  141. title: () => (
  142. <div class={styles.content}>
  143. <p class={styles.name}>{item.nickname}</p>
  144. <p class={styles.class}>
  145. {item.subjectNames &&
  146. item.subjectNames.map((subject: any) => (
  147. <Tag type="primary">{subject}</Tag>
  148. ))}
  149. </p>
  150. </div>
  151. )
  152. }}
  153. </Cell>
  154. ))}
  155. </List>
  156. ) : (
  157. <OEmpty btnStatus={false} classImgSize="SMALL" tips="暂无老师" />
  158. )}
  159. </div>
  160. )
  161. }
  162. })