index.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import OHeader from '@/components/o-header'
  2. import OPopup from '@/components/o-popup'
  3. import OSticky from '@/components/o-sticky'
  4. import request from '@/helpers/request'
  5. import { Button, Cell, CellGroup, Field, showToast } from 'vant'
  6. import { defineComponent, onMounted } from 'vue'
  7. import styles from '../index.module.less'
  8. import { state as baseState } from '@/state'
  9. import StudentList from '../modal/student-list'
  10. import SubjectList from '../modal/subject-list'
  11. import { createState as state, resestState } from './create'
  12. import { useRouter } from 'vue-router'
  13. import deepClone from '@/helpers/deep-clone'
  14. export default defineComponent({
  15. name: 'create-orchestra',
  16. setup() {
  17. const router = useRouter()
  18. // const state = reactive({
  19. // subjectStatus: false,
  20. // subjectList: [] as any, // 声部列表
  21. // selectSubjectIds: [] as any,
  22. // selectSubjects: [] as any, // 选中的声部
  23. // studentStatus: false,
  24. // teacherStatus: false,
  25. // orchestraList: [] as any, // 乐团列表
  26. // selectSubjectStudents: {} as any
  27. // })
  28. // 获取声部
  29. const getSubjects = async () => {
  30. try {
  31. const { data } = await request.post('/api-school/subjectBasicConfig/page', {
  32. data: {
  33. page: 1,
  34. rows: 50
  35. }
  36. })
  37. state.subjectList = data.rows || []
  38. } catch {
  39. //
  40. }
  41. }
  42. // 获取乐团列表
  43. const getOrchestras = async () => {
  44. try {
  45. const { data } = await request.post('/api-school/orchestra/page', {
  46. data: {
  47. page: 1,
  48. rows: 100,
  49. schoolId: baseState.user.data.school.id
  50. }
  51. })
  52. const temps = data.rows || []
  53. const s = [] as any
  54. temps.forEach((item: any) => {
  55. s.push({
  56. text: item.name,
  57. value: item.id
  58. })
  59. })
  60. state.orchestraList = [...s]
  61. } catch {
  62. //
  63. }
  64. }
  65. // 初始化选择声部
  66. const onSelectSubject = (ids: any) => {
  67. state.selectSubjectIds = [...ids]
  68. const temps: any = []
  69. console.log(state.selectSubjects, '1212', state.subjectList)
  70. state.subjectList.forEach((item: any) => {
  71. const index = state.selectSubjects.findIndex((select: any) => select.id === item.subjectId)
  72. if (ids.includes(item.subjectId)) {
  73. // 判断是否在数据里,如果在则直接添加,不能重置数据
  74. if (index < 0) {
  75. temps.push({
  76. id: item.subjectId,
  77. name: item.subjectName,
  78. code: item.code,
  79. type: null,
  80. teacher: {}, // 老师信息
  81. students: [] as any // 选中的数据数
  82. })
  83. } else {
  84. temps.push(state.selectSubjects.find((select: any) => select.id === item.subjectId))
  85. }
  86. }
  87. })
  88. state.selectSubjects = [...temps]
  89. }
  90. const onSubmit = () => {
  91. if (!state.orchestraName) {
  92. showToast('请输入乐团名称')
  93. return
  94. }
  95. if (state.selectSubjects && state.selectSubjects.length <= 0) {
  96. showToast('请选择声部')
  97. return
  98. }
  99. const selectSubjects = state.selectSubjects || []
  100. let isSelect = false
  101. selectSubjects.forEach((item: any) => {
  102. if (!item.students || (item.students && item.students.length <= 0)) {
  103. isSelect = true
  104. }
  105. })
  106. if (isSelect) {
  107. showToast('请选择学生')
  108. return
  109. }
  110. // 初始化班级
  111. const tempSelect: any = []
  112. // 添加所在学生
  113. const tempStudents: any = []
  114. let largeUpSubject: any = {} // 上低音号和大号合集,目前根据编码处理,待定
  115. state.selectSubjects.forEach((item: any) => {
  116. console.log(item, 'item.name')
  117. tempStudents.push(...item.students)
  118. if (item.code !== 'BARITONE' && item.code !== 'TUBA') {
  119. tempSelect.push(item)
  120. } else {
  121. // 获取学生
  122. const temps = largeUpSubject.students ? largeUpSubject.students : []
  123. largeUpSubject = {
  124. id: largeUpSubject.id ? largeUpSubject.id + ',' + item.id : item.id,
  125. name: largeUpSubject.name ? largeUpSubject.name + '-' + item.name : item.name,
  126. type: null,
  127. teacher: {},
  128. students: [...temps, ...item.students]
  129. }
  130. }
  131. })
  132. state.selectLastTeacherSubjects = deepClone(tempSelect)
  133. // 判断是否有大号或者上低音号, 如果有则添加
  134. if (largeUpSubject.id) {
  135. state.selectLastTeacherSubjects.push(largeUpSubject)
  136. }
  137. // 默认添加两个班级
  138. state.selectLastTeacherSubjects.push(
  139. {
  140. id: null,
  141. name: '乐理班',
  142. type: 'MUSIC_THEORY',
  143. teacher: {}, // 老师信息
  144. students: [...tempStudents] as any // 选中的数据数
  145. },
  146. {
  147. id: null,
  148. name: '合奏班',
  149. type: 'INSTRUMENTAL_ENSEMBLE',
  150. teacher: {}, // 老师信息
  151. students: [...tempStudents] as any // 选中的数据数
  152. }
  153. )
  154. // 选择老师
  155. router.push({
  156. path: '/create-orchestra-teacher'
  157. })
  158. }
  159. onMounted(() => {
  160. resestState()
  161. getSubjects()
  162. getOrchestras()
  163. })
  164. return () => (
  165. <div class={styles['create-orchestra']}>
  166. <OHeader />
  167. <CellGroup inset>
  168. <Field
  169. label="乐团名称"
  170. v-model={state.orchestraName}
  171. placeholder="请输入乐团名称"
  172. inputAlign="right"
  173. maxlength={30}
  174. />
  175. <Field
  176. label="乐团声部"
  177. readonly
  178. placeholder={
  179. state.selectSubjects.length > 0 ? `已选${state.selectSubjects.length}个` : '选择声部'
  180. }
  181. isLink
  182. inputAlign="right"
  183. onClick={() => (state.subjectStatus = true)}
  184. />
  185. {state.selectSubjects.map((item: any) => (
  186. <Cell
  187. title={item.name}
  188. isLink
  189. onClick={() => {
  190. state.studentStatus = true
  191. state.selectSubjectStudents = item
  192. }}
  193. >
  194. {{
  195. value: () => (
  196. <>
  197. 已选{' '}
  198. <span style={{ color: 'var(--van-primary-color)' }}>
  199. {item.students?.length || 0}
  200. </span>{' '}
  201. 名学生
  202. </>
  203. )
  204. }}
  205. </Cell>
  206. ))}
  207. </CellGroup>
  208. <OSticky position="bottom">
  209. <div class={['btnGroup']}>
  210. <Button type="primary" block round onClick={onSubmit}>
  211. 下一步
  212. </Button>
  213. </div>
  214. </OSticky>
  215. {/* 选择声部 */}
  216. <OPopup v-model:modelValue={state.subjectStatus} style="background: #F8F8F8;">
  217. {state.subjectStatus && (
  218. <SubjectList
  219. onClose={() => (state.subjectStatus = false)}
  220. subjectList={state.subjectList}
  221. selectSubjects={state.selectSubjectIds}
  222. onSelect={onSelectSubject}
  223. />
  224. )}
  225. </OPopup>
  226. {/* 选择学生 */}
  227. <OPopup v-model:modelValue={state.studentStatus} style="background: #f8f8f8;">
  228. {state.studentStatus && (
  229. <StudentList
  230. orchestraList={state.orchestraList}
  231. subjectId={state.selectSubjectStudents.id}
  232. selectStudentIds={state.selectSubjectStudents.students}
  233. onClose={() => (state.studentStatus = false)}
  234. onSelect={(item: any) => {
  235. // console.log(item, 'select student')
  236. state.selectSubjectStudents.students = [...item]
  237. }}
  238. />
  239. )}
  240. </OPopup>
  241. </div>
  242. )
  243. }
  244. })