student-list.tsx 9.3 KB

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