index.tsx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. import {
  2. NButton,
  3. NCascader,
  4. NForm,
  5. NFormItem,
  6. NInput,
  7. NScrollbar,
  8. NSelect,
  9. NSpace,
  10. useMessage
  11. } from 'naive-ui';
  12. import {
  13. TransitionGroup,
  14. defineComponent,
  15. onMounted,
  16. reactive,
  17. ref
  18. } from 'vue';
  19. import styles from './index.module.less';
  20. import UploadFile from '@/components/upload-file';
  21. import { nextTick } from 'vue';
  22. import { scrollToErrorForm } from '@/utils';
  23. import { api_lessonCoursewareSave } from '../../api';
  24. import iconUpload from '../../images/icon-upload.png';
  25. import iconUpload2 from '../../images/icon-upload2.png';
  26. import iconAdd from '../../images/icon-add.png';
  27. import iconMenu from '../../images/icon-menu.png';
  28. import btnAdd from '../../images/btn-add.png';
  29. import btnDelete from '../../images/btn-delete.png';
  30. import btnUp from '../../images/btn-up.png';
  31. import btnDown from '../../images/btn-down.png';
  32. import btnRemove from '../../images/btn-remove.png';
  33. import { lessonCoursewareDetail } from '/src/views/prepare-lessons/api';
  34. import { useCatchStore } from '/src/store/modules/catchData';
  35. export const BOOK_DATA = {
  36. grades: [
  37. { label: '一年级', value: 1 },
  38. { label: '二年级', value: 2 },
  39. { label: '三年级', value: 3 },
  40. { label: '四年级', value: 4 },
  41. { label: '五年级', value: 5 },
  42. { label: '六年级', value: 6 },
  43. { label: '七年级', value: 7 },
  44. { label: '八年级', value: 8 },
  45. { label: '九年级', value: 9 }
  46. ],
  47. bookTypes: [
  48. { label: '上册', value: 'LAST' },
  49. { label: '下册', value: 'NEXT' }
  50. ]
  51. };
  52. /** 添加单元 */
  53. const createLesson = () => {
  54. return {
  55. key: 'item' + Date.now(),
  56. name: '', // 单元名称
  57. lessonTargetDesc: '', // 课时目标描述
  58. knowledgeList: [
  59. {
  60. key: Date.now() + '' + 0,
  61. name: '' // 章节名称
  62. }
  63. ] // 章节信息
  64. };
  65. };
  66. const initState = () => ({
  67. id: null, // 教材id
  68. name: '',
  69. currentGradeNum: null as any,
  70. instrumentIds: null as any,
  71. // bookType: null, // 上下册
  72. coverImg: '', // 封面
  73. enableFlag: true, // 状态
  74. type: 'COURSEWARE', // 教材类型:COURSEWARE,THEORY,可用值:COURSEWARE,THEORY
  75. lessonList: [createLesson()] // 单元列表
  76. });
  77. export default defineComponent({
  78. name: 'addNatural',
  79. props: {
  80. item: {
  81. type: Object,
  82. default: () => ({})
  83. }
  84. },
  85. emits: ['close', 'confirm'],
  86. setup(props, { emit }) {
  87. const catchStore = useCatchStore();
  88. const message = useMessage();
  89. const data = reactive({
  90. uploading: false
  91. });
  92. const formRef = ref();
  93. const form = reactive(initState());
  94. const handleSave = () => {
  95. formRef.value?.validate((err: any) => {
  96. if (err) {
  97. nextTick(scrollToErrorForm);
  98. return;
  99. }
  100. handleSubmit();
  101. });
  102. };
  103. const handleSubmit = async () => {
  104. data.uploading = true;
  105. try {
  106. const { currentGradeNum, instrumentIds, ...more } = form;
  107. await api_lessonCoursewareSave({
  108. currentGradeNum: currentGradeNum.join(','),
  109. instrumentIds: instrumentIds.join(','),
  110. ...more
  111. });
  112. Object.assign(form, initState());
  113. message.success(props.item.id ? '保存成功' : '添加成功');
  114. emit('close', true);
  115. emit('confirm');
  116. } catch {
  117. //
  118. }
  119. data.uploading = false;
  120. };
  121. onMounted(async () => {
  122. await catchStore.getSubjects();
  123. if (props.item.id) {
  124. const { data } = await lessonCoursewareDetail({ id: props.item.id });
  125. form.id = data.id;
  126. form.name = data.name;
  127. form.currentGradeNum = data.currentGradeNum
  128. ? data.currentGradeNum.split(',').map((item: any) => Number(item))
  129. : null;
  130. form.instrumentIds = data.instrumentIds
  131. ? data.instrumentIds.split(',').map((item: any) => item)
  132. : null;
  133. // form.bookType = data.bookType;
  134. form.coverImg = data.coverImg;
  135. form.lessonList = [];
  136. const lessonList = data.lessonList || [];
  137. const tempLesson: any[] = [];
  138. lessonList.forEach((item: any) => {
  139. const tmpItem: any = {
  140. id: item.id,
  141. key: 'item' + Date.now() + '-' + Math.random() * 100,
  142. name: item.name,
  143. lessonTargetDesc: item.lessonTargetDesc,
  144. knowledgeList: [] as any
  145. };
  146. if (item.knowledgeList && item.knowledgeList.length) {
  147. item.knowledgeList.forEach((knowledge: any) => {
  148. tmpItem.knowledgeList.push({
  149. id: knowledge.id,
  150. key: Date.now() + '-' + Math.random() * 100,
  151. name: knowledge.name
  152. });
  153. });
  154. }
  155. tempLesson.push(tmpItem);
  156. });
  157. form.lessonList = tempLesson;
  158. }
  159. });
  160. return () => (
  161. <div class={styles.container}>
  162. <NScrollbar style={{ 'max-height': '65vh' }}>
  163. <NForm
  164. ref={formRef}
  165. labelPlacement="left"
  166. labelWidth={120}
  167. model={form}>
  168. <div class={styles.topForms}>
  169. <NFormItem
  170. path="coverImg"
  171. rule={[
  172. {
  173. required: true,
  174. message: '请上传教材封面',
  175. trigger: ['change']
  176. }
  177. ]}>
  178. <UploadFile
  179. cropper
  180. // tips="建议尺寸:210*297, 文件大小: 5MB以内;"
  181. v-model:fileList={form.coverImg}
  182. showType="custom"
  183. size={2}
  184. accept=".jpg,jpeg,.png"
  185. options={{
  186. autoCropWidth: 210,
  187. autoCropHeight: 297,
  188. fixedBox: true
  189. }}>
  190. {{
  191. custom: () => (
  192. <div class={styles.uploadContent}>
  193. <img src={iconUpload2} class={styles.iconUpload} />
  194. <p>请上传教材封面</p>
  195. </div>
  196. )
  197. }}
  198. </UploadFile>
  199. </NFormItem>
  200. <div class={styles.topFormInput}>
  201. <NFormItem
  202. style={{ minWidth: '360px' }}
  203. path="name"
  204. rule={[
  205. {
  206. required: true,
  207. message: '请输入教材名称',
  208. trigger: ['blur', 'change']
  209. }
  210. ]}>
  211. <NInput
  212. placeholder="请输入教材名称"
  213. maxlength={25}
  214. v-model:value={form.name}
  215. clearable></NInput>
  216. </NFormItem>
  217. <NFormItem
  218. path="currentGradeNum"
  219. rule={{
  220. required: true,
  221. message: '请选择年级',
  222. trigger: 'change',
  223. type: 'array'
  224. }}>
  225. <NSelect
  226. style={{ minWidth: '360px' }}
  227. placeholder="请选择年级"
  228. options={BOOK_DATA.grades}
  229. v-model:value={form.currentGradeNum}
  230. clearable
  231. multiple
  232. filterable
  233. maxTagCount={3}
  234. />
  235. </NFormItem>
  236. <NFormItem
  237. path="instrumentIds"
  238. style={{ width: '360px' }}
  239. rule={{
  240. required: true,
  241. message: '请选择乐器',
  242. trigger: 'change',
  243. type: 'array'
  244. }}>
  245. <NCascader
  246. placeholder="请选择乐器"
  247. options={catchStore.getSubjectList}
  248. v-model:value={form.instrumentIds}
  249. checkStrategy="child"
  250. showPath={false}
  251. childrenField="instruments"
  252. expandTrigger="hover"
  253. labelField="name"
  254. valueField="id"
  255. clearable
  256. filterable
  257. multiple
  258. maxTagCount="responsive"
  259. style={{ width: '400px' }}
  260. />
  261. </NFormItem>
  262. {/* <NFormItem
  263. path="bookType"
  264. style={{ width: '360px' }}
  265. rule={{
  266. required: true,
  267. message: '请选择册别',
  268. trigger: 'change'
  269. }}>
  270. <NSelect
  271. placeholder="请选择册别"
  272. options={BOOK_DATA.bookTypes}
  273. v-model:value={form.bookType}
  274. clearable
  275. />
  276. </NFormItem> */}
  277. </div>
  278. </div>
  279. <div class={styles.menuTitle}>
  280. <img src={iconMenu} class={styles.iconMenu} />
  281. 目录
  282. </div>
  283. <TransitionGroup name="list" tag="div">
  284. {form.lessonList.map((item, index) => {
  285. return (
  286. <NSpace
  287. class={styles.lessonItem}
  288. wrap={false}
  289. wrapItem={false}
  290. align="start"
  291. key={item.key}>
  292. <NFormItem
  293. label="单元名称"
  294. labelPlacement="top"
  295. path={`lessonList[${index}].name`}
  296. rule={{
  297. required: true,
  298. message: '填写单元名称',
  299. trigger: ['blur', 'change']
  300. }}>
  301. <NInput
  302. placeholder="填写单元名称"
  303. maxlength={25}
  304. v-model:value={item.name}
  305. clearable></NInput>
  306. </NFormItem>
  307. <TransitionGroup name="list" tag="div">
  308. {item.knowledgeList.map((know, knowIndex) => {
  309. return (
  310. <NFormItem
  311. style={{
  312. '--n-label-height': knowIndex === 0 ? '26px' : '0'
  313. }}
  314. labelPlacement="top"
  315. label={knowIndex === 0 ? '章节名称' : ''}
  316. key={know.key}
  317. path={`lessonList[${index}].knowledgeList[${knowIndex}].name`}
  318. rule={{
  319. required: true,
  320. message: '填写章节名称',
  321. trigger: ['blur', 'change']
  322. }}>
  323. <NSpace
  324. wrap={false}
  325. align="center"
  326. class={styles.btnGroupAll}
  327. wrapItem={false}>
  328. <NInput
  329. maxlength={25}
  330. placeholder="填写章节名称"
  331. v-model:value={know.name}
  332. clearable></NInput>
  333. <NButton
  334. quaternary
  335. circle
  336. onClick={() => {
  337. item.knowledgeList.splice(knowIndex + 1, 0, {
  338. name: '',
  339. key: Date.now() + '' + knowIndex
  340. });
  341. }}>
  342. {{
  343. icon: () => (
  344. <img src={btnAdd} class={styles.btnImg} />
  345. )
  346. }}
  347. </NButton>
  348. <NButton
  349. quaternary
  350. circle
  351. disabled={item.knowledgeList.length < 2}
  352. onClick={() => {
  353. item.knowledgeList.splice(knowIndex, 1);
  354. }}>
  355. {{
  356. icon: () => (
  357. <img
  358. src={btnDelete}
  359. class={styles.btnImg}
  360. />
  361. )
  362. }}
  363. </NButton>
  364. <NButton
  365. quaternary
  366. circle
  367. disabled={knowIndex === 0}
  368. onClick={() => {
  369. if (knowIndex === 0) return;
  370. const tmp = item.knowledgeList[knowIndex - 1];
  371. item.knowledgeList[knowIndex - 1] =
  372. item.knowledgeList[knowIndex];
  373. item.knowledgeList[knowIndex] = tmp;
  374. }}>
  375. {{
  376. icon: () => (
  377. <img src={btnUp} class={styles.btnImg} />
  378. )
  379. }}
  380. </NButton>
  381. <NButton
  382. quaternary
  383. circle
  384. disabled={
  385. knowIndex === item.knowledgeList.length - 1
  386. }
  387. onClick={() => {
  388. if (
  389. knowIndex ===
  390. item.knowledgeList.length - 1
  391. )
  392. return;
  393. const tmp = item.knowledgeList[knowIndex + 1];
  394. item.knowledgeList[knowIndex + 1] =
  395. item.knowledgeList[knowIndex];
  396. item.knowledgeList[knowIndex] = tmp;
  397. }}>
  398. {{
  399. icon: () => (
  400. <img src={btnDown} class={styles.btnImg} />
  401. )
  402. }}
  403. </NButton>
  404. </NSpace>
  405. </NFormItem>
  406. );
  407. })}
  408. </TransitionGroup>
  409. <NButton
  410. class={styles.closeBtn}
  411. secondary
  412. circle
  413. size="small"
  414. disabled={form.lessonList.length < 2}
  415. onClick={() => {
  416. form.lessonList.splice(index, 1);
  417. }}>
  418. <img src={btnRemove} />
  419. </NButton>
  420. </NSpace>
  421. );
  422. })}
  423. </TransitionGroup>
  424. <div class={styles.line}></div>
  425. <NButton
  426. block
  427. class={styles.addUnitBtn}
  428. ghost
  429. color="#198CFE"
  430. onClick={() => {
  431. form.lessonList.push(createLesson());
  432. }}>
  433. {{
  434. icon: () => <img src={iconAdd} />,
  435. default: () => '新增单元'
  436. }}
  437. </NButton>
  438. </NForm>
  439. </NScrollbar>
  440. <NSpace class={styles.btnGroup} justify="center">
  441. <NButton round onClick={() => emit('close')}>
  442. 取消
  443. </NButton>
  444. <NButton
  445. round
  446. loading={data.uploading}
  447. type="primary"
  448. onClick={() => handleSave()}>
  449. 保存
  450. </NButton>
  451. </NSpace>
  452. </div>
  453. );
  454. }
  455. });