index.tsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 } 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/subject/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. state.subjectList.forEach((item: any) => {
  70. const index = state.selectSubjects.findIndex((select: any) => select.id === item.id)
  71. if (ids.includes(item.id)) {
  72. // 判断是否在数据里,如果在则直接添加,不能重置数据
  73. if (index < 0) {
  74. temps.push({
  75. id: item.id,
  76. name: item.name,
  77. type: null,
  78. teacher: {}, // 老师信息
  79. students: [] as any // 选中的数据数
  80. })
  81. } else {
  82. temps.push(state.selectSubjects.find((select: any) => select.id === item.id))
  83. }
  84. }
  85. })
  86. state.selectSubjects = [...temps]
  87. }
  88. const onSubmit = () => {
  89. if (!state.orchestraName) {
  90. showToast('请输入乐团名称')
  91. return
  92. }
  93. if (state.selectSubjects && state.selectSubjects.length <= 0) {
  94. showToast('请选择声部')
  95. return
  96. }
  97. const selectSubjects = state.selectSubjects || []
  98. let isSelect = false
  99. selectSubjects.forEach((item: any) => {
  100. if (!item.students || (item.students && item.students.length <= 0)) {
  101. isSelect = true
  102. }
  103. })
  104. if (isSelect) {
  105. showToast('请选择学生')
  106. return
  107. }
  108. // 初始化班级
  109. state.selectLastTeacherSubjects = deepClone(state.selectSubjects)
  110. // 添加所在学生
  111. const tempStudents: any = []
  112. state.selectSubjects.forEach((item: any) => {
  113. tempStudents.push(...item.students)
  114. })
  115. // 默认添加两个班级
  116. state.selectLastTeacherSubjects.push(
  117. {
  118. id: null,
  119. name: '乐理班',
  120. type: 'MUSIC_THEORY',
  121. teacher: {}, // 老师信息
  122. students: [...tempStudents] as any // 选中的数据数
  123. },
  124. {
  125. id: null,
  126. name: '合奏班',
  127. type: 'INSTRUMENTAL_ENSEMBLE',
  128. teacher: {}, // 老师信息
  129. students: [...tempStudents] as any // 选中的数据数
  130. }
  131. )
  132. // 选择老师
  133. router.push({
  134. path: '/create-orchestra-teacher'
  135. })
  136. }
  137. onMounted(() => {
  138. getSubjects()
  139. getOrchestras()
  140. })
  141. return () => (
  142. <div class={styles['create-orchestra']}>
  143. <OHeader />
  144. <CellGroup inset>
  145. <Field
  146. label="乐团名称"
  147. v-model={state.orchestraName}
  148. placeholder="请输入乐团名称"
  149. inputAlign="right"
  150. maxlength={30}
  151. />
  152. <Field
  153. label="乐团声部"
  154. readonly
  155. placeholder="选择声部"
  156. isLink
  157. inputAlign="right"
  158. onClick={() => (state.subjectStatus = true)}
  159. />
  160. {state.selectSubjects.map((item: any) => (
  161. <Cell
  162. title={item.name}
  163. isLink
  164. onClick={() => {
  165. state.studentStatus = true
  166. state.selectSubjectStudents = item
  167. }}
  168. >
  169. {{
  170. value: () => (
  171. <>
  172. 已选
  173. <span style={{ color: 'var(--van-primary-color)' }}>
  174. {item.students?.length || 0}
  175. </span>
  176. 名学生
  177. </>
  178. )
  179. }}
  180. </Cell>
  181. ))}
  182. </CellGroup>
  183. <OSticky position="bottom">
  184. <div class={['btnGroup']}>
  185. <Button type="primary" block round onClick={onSubmit}>
  186. 下一步
  187. </Button>
  188. </div>
  189. </OSticky>
  190. {/* 选择声部 */}
  191. <OPopup v-model:modelValue={state.subjectStatus} style="background: #F8F8F8;">
  192. <SubjectList
  193. onClose={() => (state.subjectStatus = false)}
  194. subjectList={state.subjectList}
  195. selectSubjects={state.selectSubjects}
  196. onSelect={onSelectSubject}
  197. />
  198. </OPopup>
  199. {/* 选择学生 */}
  200. <OPopup v-model:modelValue={state.studentStatus} style="background: #f8f8f8;">
  201. <StudentList
  202. orchestraList={state.orchestraList}
  203. subjectId={state.selectSubjectStudents.id}
  204. selectStudentIds={state.selectSubjectStudents.students}
  205. onClose={() => (state.studentStatus = false)}
  206. onSelect={(item: any) => {
  207. console.log(item, 'select student')
  208. state.selectSubjectStudents.students = [...item]
  209. }}
  210. />
  211. </OPopup>
  212. </div>
  213. )
  214. }
  215. })