index.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { state } from '@/state'
  2. import {
  3. Button,
  4. Cell,
  5. CellGroup,
  6. Field,
  7. List,
  8. Image,
  9. Tag,
  10. Sticky,
  11. Checkbox,
  12. CheckboxGroup
  13. } from 'vant'
  14. import { defineComponent, onMounted, PropType, reactive, watch } from 'vue'
  15. import styles from './index.module.less'
  16. import iconTeacher from '@common/images/icon_teacher.png'
  17. import OEmpty from '@/components/o-empty'
  18. import request from '@/helpers/request'
  19. import OSearch from '@/components/o-search'
  20. export default defineComponent({
  21. name: 'teacher-list',
  22. props: {
  23. height: {
  24. type: [String, Number],
  25. default: 'auto'
  26. },
  27. bottomHeight: {
  28. type: [String, Number],
  29. default: 0
  30. },
  31. selectItem: {
  32. type: Array,
  33. default: () => []
  34. }
  35. },
  36. emits: ['close', 'select', 'update:selectItem'],
  37. setup(props, { emit }) {
  38. const forms = reactive({
  39. teacherStatus: false,
  40. isLoad: false,
  41. list: [] as any,
  42. listState: {
  43. dataShow: true, // 判断是否有数据
  44. loading: false,
  45. finished: false
  46. },
  47. params: {
  48. keyword: null,
  49. page: 1,
  50. rows: 20
  51. },
  52. check: (props.selectItem || []) as any,
  53. checkboxRefs: [] as any
  54. })
  55. const getList = async () => {
  56. try {
  57. if (forms.isLoad) return
  58. forms.isLoad = true
  59. const res = await request.post('/api-school/student/page', {
  60. data: {
  61. ...forms.params,
  62. schoolId: state.user.data.school.id
  63. }
  64. })
  65. forms.listState.loading = false
  66. const result = res.data || {}
  67. // 处理重复请求数据
  68. if (forms.list.length > 0 && result.current === 1) {
  69. return
  70. }
  71. const rows = result.rows || []
  72. rows.forEach((item: any) => {
  73. item.subjectNames = item.subjectNames ? item.subjectNames.split(',') : []
  74. })
  75. forms.list = forms.list.concat(rows)
  76. forms.listState.finished = result.current >= result.pages
  77. forms.params.page = result.current + 1
  78. forms.listState.dataShow = forms.list.length > 0
  79. forms.isLoad = false
  80. } catch {
  81. forms.listState.dataShow = false
  82. forms.listState.finished = true
  83. forms.isLoad = false
  84. }
  85. }
  86. // 搜索
  87. const onSearch = () => {
  88. forms.params.page = 1
  89. forms.list = []
  90. forms.listState.dataShow = true // 判断是否有数据
  91. forms.listState.loading = false
  92. forms.listState.finished = false
  93. getList()
  94. }
  95. const onSelect = (type: string) => {
  96. forms.checkboxRefs[type].toggle()
  97. const list: any = []
  98. forms.list.forEach((item: any) => {
  99. if (forms.check.includes(item.id)) {
  100. list.push({
  101. id: item.id,
  102. value: item.nickname,
  103. avatar: item.avatar
  104. })
  105. }
  106. })
  107. emit('update:selectItem', list)
  108. }
  109. watch(
  110. () => props.selectItem,
  111. () => {
  112. initSelectItem()
  113. },
  114. { deep: true }
  115. )
  116. const initSelectItem = () => {
  117. const selectItem = props.selectItem || []
  118. const temp: any = []
  119. selectItem.forEach((item: any) => {
  120. temp.push(item.id)
  121. })
  122. forms.check = temp
  123. }
  124. onMounted(() => {
  125. getList()
  126. initSelectItem()
  127. })
  128. return () => (
  129. <div
  130. class={[!forms.listState.dataShow && 'emptyRootContainer']}
  131. style={{ 'min-height': `calc(100vh - ${props.height}px - ${props.bottomHeight}px)` }}
  132. >
  133. <Sticky position="top" offsetTop={props.height} style={{ width: '100%' }}>
  134. <OSearch
  135. // inputBackground="white"
  136. // background="#F8F8F8"
  137. placeholder="学员名称/手机号"
  138. onSearch={(val: any) => {
  139. forms.params.keyword = val
  140. onSearch()
  141. }}
  142. />
  143. </Sticky>
  144. {forms.listState.dataShow ? (
  145. <List
  146. // v-model:loading={forms.listState.loading}
  147. finished={forms.listState.finished}
  148. finishedText=" "
  149. onLoad={getList}
  150. style={{
  151. paddingTop: '12px'
  152. }}
  153. immediateCheck={false}
  154. >
  155. <CheckboxGroup class={[styles.gridContainer, styles.gridClass]} v-model={forms.check}>
  156. {forms.list.map((item: any) => (
  157. <CellGroup inset style={{ marginBottom: '12px' }} onClick={() => onSelect(item.id)}>
  158. <Cell center class={styles.cellTeacher} valueClass={styles.checkboxValue}>
  159. {{
  160. icon: () => (
  161. <Image class={styles.img} src={item.avatar || iconTeacher} fit="cover" />
  162. ),
  163. title: () => (
  164. <div class={styles.content}>
  165. <p class={[styles.name, 'van-ellipsis']}>{item.nickname}</p>
  166. <p class={styles.class}>
  167. {item.subjectNames &&
  168. item.subjectNames.map((subject: any) => (
  169. <Tag
  170. type="primary"
  171. class={styles.tagSubject}
  172. color="#FFE7DA"
  173. textColor="#F67146"
  174. >
  175. {subject}
  176. </Tag>
  177. ))}
  178. </p>
  179. </div>
  180. ),
  181. value: () => (
  182. <Checkbox
  183. name={item.id}
  184. ref={(el: any) => (forms.checkboxRefs[item.id] = el)}
  185. onClick={(e: any) => {
  186. e.preventDefault()
  187. e.stopPropagation()
  188. onSelect(item.id)
  189. }}
  190. ></Checkbox>
  191. )
  192. }}
  193. </Cell>
  194. </CellGroup>
  195. ))}
  196. </CheckboxGroup>
  197. </List>
  198. ) : (
  199. <OEmpty btnStatus={false} tips="暂无学生" />
  200. )}
  201. </div>
  202. )
  203. }
  204. })