部门批量添加课程

main
king 1 year ago
parent c9afe27986
commit 14cbc8584f
  1. 30
      src/pages/manage/addCur/addCur.module.scss
  2. 110
      src/pages/manage/addCur/addCur.tsx

@ -0,0 +1,30 @@
.curBox {
display: flex;
align-items: center;
padding: 30rpx 0;
width: 100%;
border-bottom: 1px solid #F5F8F7;
}
.image {
width: 280rpx;
height: 164rpx;
border-radius: 10px;
margin-right: 10rpx;
}
.fixed {
position: fixed;
bottom: 0;
left: 0;
background: #f1f8f6;
width: 100%;
}
.addAll {
padding: 20rpx;
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: space-between;
}

@ -1,19 +1,16 @@
import {View} from "@tarojs/components"; import {Image, Radio, View} from "@tarojs/components";
import {getCurrentInstance} from "@tarojs/runtime"; import {getCurrentInstance} from "@tarojs/runtime";
import {ManageApi} from "@/api/manage"; import {ManageApi} from "@/api/manage";
import React, {FC, useCallback, useEffect, useState} from "react"; import React, {FC, useCallback, useEffect, useState} from "react";
import Taro from "@tarojs/taro"; import Taro from "@tarojs/taro";
import VideoCover from "@/components/videoCover/videoCover"; import styles from './addCur.module.scss'
import Empty from "@/components/empty/empty"; import Empty from "@/components/empty/empty";
import MyButton from "@/components/button/MyButton";
interface AddProps {
cur_id: number,
name: string,
index: number
}
const AddCur = () => { const AddCur: FC = () => {
const {id} = getCurrentInstance()?.router?.params as { id: string } const {id} = getCurrentInstance()?.router?.params as { id: string }
const [courseId, setCourseId] = useState<number[]>([])
const [data, setData] = useState<Curriculum[]>([]) const [data, setData] = useState<Curriculum[]>([])
const [page] = useState(1) const [page] = useState(1)
@ -33,50 +30,77 @@ const AddCur = () => {
getData() getData()
}, []) }, [])
const Add: FC<AddProps> = ({cur_id, name, index}) => {
function addCur() {
Taro.showModal({
title: "请选择" + name + '课程类型',
cancelText: '选修',
confirmText: '必修',
async success({confirm}) {
try {
const is_required = confirm ? 1 : 0
Taro.showLoading()
await ManageApi.addCur({course_id: [cur_id], dep_id: [Number(id)], is_required:[is_required]})
const oldData: Curriculum[] = JSON.parse(JSON.stringify(data))
oldData.splice(index, 1)
setData(oldData)
Taro.showToast({title: "添加成功"})
} catch (e) {
}
Taro.hideLoading()
}
})
event?.stopImmediatePropagation() function addCur(id: number) {
const index = courseId.indexOf(id)
if (index === -1) {
setCourseId([...courseId, id])
} else {
const old: number[] = JSON.parse(JSON.stringify(courseId))
old.splice(index, 1)
setCourseId(old)
} }
}
return ( function addAll() {
<View className='text-center mt-1' onClick={addCur}></View> if (courseId.length === data.length) {
) setCourseId([])
} else {
const courseIds = data.map(d => d.id)
setCourseId(courseIds)
}
}
function addCourses() {
console.log(courseId)
if (!courseId.length) return;
Taro.showModal({
title: "请选择课程类型",
cancelText: '选修',
confirmText: '必修',
async success({confirm}) {
try {
Taro.showLoading()
const is_required = confirm ? 1 : 0
await ManageApi.addCur({course_id: courseId, dep_id: [Number(id)], is_required: [is_required]})
const newData = data.reduce((pre, cur) => {
if (!courseId.includes(cur.id)) {
pre.push(cur)
}
return pre
}, [] as Curriculum[])
setData(newData)
Taro.showToast({title: "添加成功"})
} catch (e) {
}
Taro.hideLoading()
}
})
} }
return ( return (
<> <>
{ {
data.length ? data.length ?
<View className='flex flex-wrap'> <View className='flex flex-wrap bg-white'>
{data?.map((d, index) => ( {data?.map((d) =>
<VideoCover <View key={d.id} className={styles.curBox} onClick={() => addCur(d.id)}>
key={d.id} <Radio checked={courseId.includes(d.id)} onClick={() => addCur(d.id)}/>
thumb={d.thumb} <View className='flex'>
title={<Add cur_id={d.id} name={d.title} index={index}/>} <Image src={d.thumb} className={styles.image}/>
id={d.id} <View>{d.title}</View>
depId={Number(id)} </View>
content={d.title} </View>
/> )}
))}
<View className={styles.fixed}>
<View className={styles.addAll}>
<Radio checked={courseId.length === data.length} onClick={addAll}></Radio>
<MyButton size='mini' onClick={addCourses}>{courseId.length}</MyButton>
</View>
</View>
</View> </View>
: <Empty name='无更多课程'/> : <Empty name='无更多课程'/>
} }

Loading…
Cancel
Save