student-list.tsx 10.0 KB

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