student-list.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. import OHeader from '@/components/o-header'
  2. import OSearch from '@/components/o-search'
  3. import {
  4. Cell,
  5. Icon,
  6. Popover,
  7. Image,
  8. Checkbox,
  9. CheckboxGroup,
  10. Button,
  11. Popup,
  12. Picker,
  13. List
  14. } from 'vant'
  15. import { defineComponent, onMounted, reactive, watch } from 'vue'
  16. import styles from './student-list.module.less'
  17. import checkboxCheck from '@/common/images/icon-checkbox-check.png'
  18. import checkboxDefault from '@/common/images/icon-checkbox-default.png'
  19. import iconStudent from '@/common/images/icon_student.png'
  20. import OSticky from '@/components/o-sticky'
  21. import { state as baseState } from '@/state'
  22. import request from '@/helpers/request'
  23. import OEmpty from '@/components/o-empty'
  24. export const classStr = {
  25. 1: '一年级',
  26. 2: '二年级',
  27. 3: '三年级',
  28. 4: '四年级',
  29. 5: '五年级',
  30. 6: '六年级'
  31. }
  32. export default defineComponent({
  33. name: 'student-list',
  34. props: {
  35. orchestraList: {
  36. // 乐团列表
  37. type: Array,
  38. default: () => []
  39. },
  40. subjectId: {
  41. // 声部编号
  42. type: [String, Number],
  43. default: ''
  44. },
  45. selectStudentIds: {
  46. // 选中的学生列表
  47. type: Array,
  48. default: () => []
  49. }
  50. },
  51. emits: ['close', 'select'],
  52. setup(props, { slots, attrs, emit }) {
  53. const state = reactive({
  54. showPopover: false,
  55. oPopover: false,
  56. isLoad: false,
  57. classList: [
  58. { text: '一年级', value: 1 },
  59. { text: '二年级', value: 2 },
  60. { text: '三年级', value: 3 },
  61. { text: '四年级', value: 4 },
  62. { text: '五年级', value: 5 }
  63. ] as any, // 年级列表
  64. check: [] as any,
  65. checkboxRefs: [] as any,
  66. orchestra: {
  67. id: null,
  68. name: '全部乐团'
  69. } as any,
  70. class: {
  71. id: null,
  72. name: '年级'
  73. } as any,
  74. list: [] as any,
  75. listState: {
  76. dataShow: true, // 判断是否有数据
  77. loading: false,
  78. finished: false
  79. },
  80. params: {
  81. keyword: null,
  82. page: 1,
  83. rows: 20
  84. }
  85. })
  86. const onSelect = (type: string) => {
  87. // console.log(state.checkboxRefs[type])
  88. state.checkboxRefs[type].toggle()
  89. }
  90. const getList = async () => {
  91. try {
  92. if (state.isLoad) return
  93. state.isLoad = true
  94. const res = await request.post('/api-school/student/page', {
  95. data: {
  96. ...state.params,
  97. subjectId: props.subjectId,
  98. orchestraId: state.orchestra.id,
  99. classId: state.class.id
  100. }
  101. })
  102. state.listState.loading = false
  103. const result = res.data || {}
  104. // 处理重复请求数据
  105. if (state.list.length > 0 && result.current === 1) {
  106. return
  107. }
  108. state.list = state.list.concat(result.rows || [])
  109. state.listState.finished = result.current >= result.pages
  110. state.params.page = result.current + 1
  111. state.listState.dataShow = state.list.length > 0
  112. state.isLoad = false
  113. } catch {
  114. state.listState.dataShow = false
  115. state.listState.finished = true
  116. state.isLoad = false
  117. }
  118. }
  119. // 搜索
  120. const onSearch = () => {
  121. state.params.page = 1
  122. state.list = []
  123. state.listState.dataShow = true // 判断是否有数据
  124. state.listState.loading = false
  125. state.listState.finished = false
  126. getList()
  127. }
  128. const onSubmit = () => {
  129. emit('close')
  130. emit('select', state.check)
  131. setTimeout(() => {
  132. state.check = []
  133. }, 100)
  134. }
  135. // 监听声部是否变化
  136. watch(
  137. () => props.subjectId,
  138. () => {
  139. onSearch()
  140. }
  141. )
  142. // 监听选择学生变化
  143. watch(
  144. () => props.selectStudentIds,
  145. () => {
  146. state.check = [...props.selectStudentIds]
  147. }
  148. )
  149. onMounted(() => {
  150. // 判断年级
  151. if (baseState.user.data.school.schoolSystem === 'sixYearSystem') {
  152. state.classList.push({ text: '六年级', value: 6 })
  153. }
  154. if (props.orchestraList.length > 0) {
  155. const o: any = props.orchestraList[0]
  156. state.orchestra.id = o.value
  157. state.orchestra.name = o.text
  158. }
  159. // 获取学生列表
  160. getList()
  161. state.check = [...props.selectStudentIds]
  162. })
  163. return () => (
  164. <div class={styles.studentList}>
  165. <OSticky position="top">
  166. <OHeader title="选择学生" />
  167. <OSearch
  168. inputBackground="white"
  169. background="#F8F8F8"
  170. placeholder="学生名称/手机号"
  171. onSearch={(val: any) => {
  172. state.params.keyword = val
  173. onSearch()
  174. }}
  175. />
  176. <div style={{ padding: '8px 13px 16px', background: '#F8F8F8' }}>
  177. <div class={styles.searchBand} onClick={() => (state.showPopover = true)}>
  178. {state.class.name} <Icon name={state.showPopover ? 'arrow-up' : 'arrow-down'} />
  179. </div>
  180. <div
  181. class={styles.searchBand}
  182. style="margin-left: 16px"
  183. onClick={() => (state.oPopover = true)}
  184. >
  185. {state.orchestra.name} <Icon name={state.oPopover ? 'arrow-up' : 'arrow-down'} />
  186. </div>
  187. </div>
  188. </OSticky>
  189. {state.listState.dataShow ? (
  190. <List
  191. v-model:loading={state.listState.loading}
  192. finished={state.listState.finished}
  193. finishedText=" "
  194. class={[styles.liveList]}
  195. onLoad={getList}
  196. immediateCheck={false}
  197. >
  198. <CheckboxGroup v-model={state.check}>
  199. {state.list.map((item: any) => (
  200. <Cell v-model={state.check} center onClick={() => onSelect(item.id)}>
  201. {{
  202. icon: () => <Image class={styles.img} src={item.avatar || iconStudent} />,
  203. title: () => (
  204. <div class={styles.content}>
  205. <p class={styles.name}>{item.nickname}</p>
  206. <p class={styles.class}>
  207. {item.currentGradeNum > 0 ? classStr[item.currentGradeNum] : ''}
  208. </p>
  209. </div>
  210. ),
  211. 'right-icon': () => (
  212. <Checkbox
  213. name={item.id}
  214. ref={(el: any) => (state.checkboxRefs[item.id] = el)}
  215. onClick={(e: any) => {
  216. e.stopPropagation()
  217. e.preventDefault()
  218. }}
  219. v-slots={{
  220. icon: (props: any) => (
  221. <Icon
  222. class={styles.iconChecked}
  223. name={props.checked ? checkboxCheck : checkboxDefault}
  224. />
  225. )
  226. }}
  227. />
  228. )
  229. }}
  230. </Cell>
  231. ))}
  232. </CheckboxGroup>
  233. </List>
  234. ) : (
  235. <OEmpty btnStatus={false} classImgSize="SMALL" tips="暂无学生" />
  236. )}
  237. <OSticky position="bottom">
  238. <div class={['btnGroup', styles.btnMore]}>
  239. <Button
  240. type="primary"
  241. plain
  242. round
  243. onClick={() => {
  244. state.list.forEach((item: any) => {
  245. if (!state.check.includes(item.id)) {
  246. state.check.push(item.id)
  247. }
  248. })
  249. state.check
  250. }}
  251. >
  252. 全选
  253. </Button>
  254. <Button type="primary" round block onClick={onSubmit}>
  255. 确认
  256. </Button>
  257. </div>
  258. </OSticky>
  259. {/* 乐团 */}
  260. <Popup v-model:show={state.oPopover} position="bottom" round>
  261. <Picker
  262. columns={props.orchestraList as any}
  263. onCancel={() => (state.oPopover = false)}
  264. onConfirm={(item: any) => {
  265. const selectedOptions = item.selectedOptions[0]
  266. state.orchestra.id = selectedOptions.value
  267. state.orchestra.name = selectedOptions.text
  268. state.oPopover = false
  269. onSearch()
  270. }}
  271. />
  272. </Popup>
  273. {/* 年级 */}
  274. <Popup v-model:show={state.showPopover} position="bottom" round>
  275. <Picker
  276. columns={state.classList as any}
  277. onCancel={() => (state.showPopover = false)}
  278. onConfirm={(item: any) => {
  279. const selectedOptions = item.selectedOptions[0]
  280. state.class.id = selectedOptions.value
  281. state.class.name = selectedOptions.text
  282. state.showPopover = false
  283. onSearch()
  284. }}
  285. />
  286. </Popup>
  287. </div>
  288. )
  289. }
  290. })