<template>
|
<div class="notice">
|
<el-dialog
|
v-model="dialogVisible"
|
:title="title"
|
width="500px"
|
:before-close="handleClose"
|
:close-on-press-escape="false"
|
:close-on-click-modal="false"
|
>
|
<el-form :model="state.form" size="default" ref="busRef" :rules="state.formRules" label-width="150px" >
|
<el-form-item label="课程名称:" prop="name">
|
<el-input v-model.trim="state.form.name" placeholder="请输入课程名称"></el-input>
|
</el-form-item>
|
<el-form-item label="课程分类:" prop="categoryId" >
|
<!-- <el-select clearable v-model="state.form.categoryId" style="width: 100%" placeholder="请选择课程分类" @change="changeType">-->
|
<!-- <el-option v-for="item in state.classifyList" :key="item" :label="item.name" :value="item.id" />-->
|
<!-- </el-select>-->
|
<el-cascader
|
ref="classifyRef"
|
style="width: 100%"
|
v-model="state.form.categoryId"
|
:options="state.classifyList"
|
:props="state.props"
|
clearable
|
:show-all-levels="false"
|
@change="handleChange"
|
/>
|
</el-form-item>
|
<!-- <el-form-item label="要求课时:" prop="period">-->
|
<!-- <el-input v-model="state.form.period" placeholder="请输入要求课时">-->
|
<!-- <template #append>分钟</template>-->
|
<!-- </el-input>-->
|
<!-- </el-form-item>-->
|
<el-form-item label="提交单位:" prop="companyName" >
|
<el-input v-model="state.form.companyName" disabled/>
|
</el-form-item>
|
<el-form-item label="封面:">
|
<el-upload accept="image/*" :action="state.uploadUrl" :headers="state.header" method="post" :on-success="(res, uploadFile)=>handleAvatarSuccess(res, uploadFile)" :on-exceed="showTip" :limit='state.imgLimit' v-model:file-list="state.imgList" list-type="picture-card" :before-upload="picSize" :on-remove="(file, uploadFiles)=>handleRemove(file, uploadFiles)" >
|
<el-icon><Plus /></el-icon>
|
<template #tip>
|
<div class="el-upload__tip">上传jpg/png图片尺寸小于5M,最多可上传1张</div>
|
</template>
|
</el-upload>
|
</el-form-item>
|
</el-form>
|
<template #footer>
|
<span class="dialog-footer">
|
<el-button @click="handleClose" size="default">取 消</el-button>
|
<el-button type="primary" @click="onSubmit" size="default" v-preReClick>确认</el-button>
|
</span>
|
</template>
|
</el-dialog>
|
</div>
|
</template>
|
<script setup>
|
import {reactive, ref, toRefs} from 'vue'
|
import Editor from "@/components/Editor/index.vue";
|
import {ElMessage} from "element-plus";
|
import {addNotice} from "@/api/backManage/notice";
|
import {addDict, editDict, getDictDetail} from "@/api/backManage/evaluate";
|
import {addCompany, checkName, distributeCompany, editCompany} from "@/api/onlineEducation/company";
|
import {verifyPhone} from "@/utils/validate";
|
import {
|
addClassification,
|
checkClassName,
|
editClassification,
|
getClassification
|
} from "@/api/onlineEducation/courseClass";
|
import {addCourse, checkCourseName, editCourse, getCourseById} from "@/api/onlineEducation/courseManage";
|
import {getToken} from "@/utils/auth";
|
import {delPic, getBannerById} from "@/api/onlineEducation/banner";
|
import Cookies from "js-cookie";
|
|
const dialogVisible = ref(false);
|
const title = ref("");
|
const busRef = ref();
|
const length = ref()
|
const emit = defineEmits(["getList"]);
|
const startUsername = ref('');
|
const classifyRef = ref(null)
|
|
const validateName = (rule, value, callback)=>{
|
if(value === ''){
|
callback(new Error('请输入课程名称'))
|
}else if(title.value === '编辑' && value === startUsername.value){
|
callback()
|
}else{
|
let param = {}
|
if(title.value === '新增') {
|
param = {
|
name:value
|
}
|
}else if(title.value === '编辑'){
|
param = {
|
name:value,
|
id: state.form.id
|
}
|
}
|
checkCourseName(param).then((res)=>{
|
if(res.data == false){
|
callback(new Error('课程名称已被占用,请更换其他名称'))
|
}else{
|
callback()
|
}
|
})
|
}
|
}
|
const state = reactive({
|
form: {
|
id: '',
|
name: '',
|
categoryId: null,
|
period: null,
|
logo: '',
|
companyId: null
|
},
|
formRules: {
|
name: [{required: true, trigger: "blur", validator: validateName}],
|
categoryId: [{required: true, message: '请选择课程分类', trigger: 'blur'}],
|
period: [{required: true, message: '请输入要求课时', trigger: 'blur'}],
|
},
|
classifyList: [],
|
uploadUrl: import.meta.env.VITE_APP_BASE_API + '/system/common/uploadFile',
|
header: {
|
Authorization: getToken()
|
},
|
imgLimit: 1,
|
imgList: [],
|
isAdmin: false,
|
props: {
|
checkStrictly: true,
|
}
|
})
|
|
const openDialog = async (type, value) => {
|
await getClassifyList();
|
const userInfo = JSON.parse(Cookies.get('userInfo'))
|
console.log("userInfo",userInfo)
|
if(userInfo.userType === 0){
|
state.isAdmin = true;
|
state.form.companyName = '公开课'
|
state.form.companyId = null
|
}else {
|
state.isAdmin = false;
|
state.form.companyName = userInfo.companyName
|
state.form.companyId = userInfo.companyId
|
}
|
title.value = type === 'addFirst' || type === 'add' ? '新增' : type ==='edit' ? '编辑' : '' ;
|
if(type === 'edit') {
|
const res = await getCourseById(value.id);
|
if(res.code === 200){
|
state.form = res.data
|
state.form.companyName = res.data.companyName ? res.data.companyName : '公开课'
|
console.log("11",res.data)
|
if(res.data.logo) {
|
const obj = {
|
url: import.meta.env.VITE_APP_BASE_API + "/" + res.data.logo,
|
name: ''
|
}
|
state.imgList = [obj]
|
}
|
}else{
|
ElMessage.warning(res.message)
|
}
|
startUsername.value = value.username;
|
}else if(type === 'add' && value ){
|
state.form.parentId = value.id
|
}
|
dialogVisible.value = true;
|
}
|
const getClassifyList = async () => {
|
const res = await getClassification();
|
if(res.code === 200){
|
state.classifyList = recursion(res.data)
|
}else{
|
ElMessage.warning(res.message)
|
}
|
}
|
const recursion = (data) => {
|
let tmp = []
|
for (let i = 0; i < data.length; i++) {
|
let item = data[i]
|
// children为空
|
if (item.children&& item.children.length==0) {
|
tmp.push({
|
value: item.id,
|
label: item.name
|
})
|
// 有children
|
} else {
|
tmp.push({
|
value: item.id,
|
label: item.name,
|
children:recursion(item.children)
|
})
|
}
|
}
|
return tmp;
|
}
|
const handleAvatarSuccess = (res, uploadFile) => {
|
if(res.code == 200){
|
state.form.logo = res.data.path
|
}else{
|
state.imgList = []
|
ElMessage({
|
type: 'warning',
|
message: '文件上传失败'
|
})
|
}
|
}
|
const handleChange = ()=> {
|
console.log("label====",classifyRef.value.getCheckedNodes()[0].value)
|
state.form.categoryId = classifyRef.value.getCheckedNodes()[0].value
|
// 我这里只是打印了一下label的值哦,需要赋值的话自己去赋值哦
|
if (classifyRef.value.popperVisible) {
|
classifyRef.value.togglePopperVisible()
|
}
|
}
|
|
|
const showTip =()=>{
|
ElMessage({
|
type: 'warning',
|
message: '超出文件上传数量'
|
});
|
}
|
const picSize = async (rawFile) => {
|
if(rawFile.size / 1024 / 1024 > 5){
|
ElMessage({
|
type: 'warning',
|
message: '文件大小不能超过5M'
|
});
|
return false
|
}
|
};
|
const handleRemove = async (file, uploadFiles) => {
|
let path = state.form.logo;
|
await delPic({path: path}).then(res => {
|
if(res.code == 200){
|
// ElMessage({
|
// type: 'success',
|
// message: '文件已删除'
|
// })
|
state.form.logo = ''
|
}else{
|
ElMessage({
|
type: 'warning',
|
message: res.message
|
})
|
}
|
}).catch(() => {
|
state.form.imgUrl = ''
|
});
|
}
|
const onSubmit = async () => {
|
const valid = await busRef.value.validate();
|
if(valid){
|
if(title.value === '新增'){
|
const {id, ...data} = JSON.parse(JSON.stringify(state.form))
|
const res = await addCourse(data)
|
if(res.code === 200){
|
ElMessage({
|
type: 'success',
|
message: '新增成功'
|
});
|
}else{
|
ElMessage.warning(res.message)
|
}
|
emit("getList")
|
busRef.value.clearValidate();
|
reset();
|
dialogVisible.value = false;
|
}else if(title.value === '编辑'){
|
const {...data} = JSON.parse(JSON.stringify(state.form))
|
const res = await editCourse(data)
|
if(res.code === 200){
|
ElMessage({
|
type: 'success',
|
message: '编辑成功'
|
});
|
}else{
|
ElMessage.warning(res.message)
|
}
|
emit("getList")
|
busRef.value.clearValidate();
|
reset();
|
dialogVisible.value = false;
|
}
|
}
|
}
|
|
const handleClose = () => {
|
busRef.value.clearValidate();
|
reset();
|
dialogVisible.value = false;
|
emit("getList")
|
|
}
|
const reset = () => {
|
state.form = {
|
id: '',
|
name: '',
|
categoryId: null,
|
period: null,
|
logo: '',
|
companyId: null
|
}
|
state.imgList = []
|
}
|
defineExpose({
|
openDialog
|
});
|
|
</script>
|
|
<style scoped lang="scss">
|
.notice{
|
:deep(.el-form .el-form-item__label) {
|
font-size: 15px;
|
}
|
.file {
|
display: flex;
|
flex-direction: column;
|
align-items: flex-start;
|
}
|
}
|
</style>
|