提交 8ba26b13 authored 作者: 王鹏飞's avatar 王鹏飞

fix: #3200

上级 a593ff3f
...@@ -18,13 +18,14 @@ interface Chapter { ...@@ -18,13 +18,14 @@ interface Chapter {
id: string id: string
name: string name: string
} }
const courses = ref<Course[]>([])
export function useGetCourseList() { export function useGetCourseList() {
const courseValue = ref('') const courseValue = ref('')
const courses = ref<Course[]>([])
const chapters = ref<Chapter[]>([]) const chapters = ref<Chapter[]>([])
// 获取课程列表 // 获取课程列表
function getCourses() { function getCourses() {
if (courses.value.length) return
getCourseList().then(res => { getCourseList().then(res => {
courses.value = res.data.items courses.value = res.data.items
}) })
......
...@@ -6,6 +6,7 @@ import { createPost } from '../api' ...@@ -6,6 +6,7 @@ import { createPost } from '../api'
import { useMapStore } from '@/stores/map' import { useMapStore } from '@/stores/map'
import { useGetCourseList } from '@/composables/useGetCourseList' import { useGetCourseList } from '@/composables/useGetCourseList'
import { isVideo } from '@/utils/index' import { isVideo } from '@/utils/index'
import { useStorage } from '@vueuse/core'
interface Props { interface Props {
courseId?: string courseId?: string
...@@ -23,7 +24,8 @@ const { courses, courseValue, chapters } = useGetCourseList() ...@@ -23,7 +24,8 @@ const { courses, courseValue, chapters } = useGetCourseList()
const types = useMapStore().getMapValuesByKey('learning_discussion_type') const types = useMapStore().getMapValuesByKey('learning_discussion_type')
const formRef = $ref<FormInstance>() const formRef = $ref<FormInstance>()
const form = reactive({ const form = $(
useStorage('bbsPost', {
semester_id: props.semesterId || '', semester_id: props.semesterId || '',
course_id: props.courseId || '', course_id: props.courseId || '',
chapter_id: '', chapter_id: '',
...@@ -31,7 +33,8 @@ const form = reactive({ ...@@ -31,7 +33,8 @@ const form = reactive({
title: '', title: '',
content: '', content: '',
files: [] files: []
}) })
)
const rules = ref<FormRules>({ const rules = ref<FormRules>({
course_id: [{ required: true, message: '请选择课程', trigger: 'change' }], course_id: [{ required: true, message: '请选择课程', trigger: 'change' }],
chapter_id: [{ required: true, message: '请选择章节', trigger: 'change' }], chapter_id: [{ required: true, message: '请选择章节', trigger: 'change' }],
...@@ -42,9 +45,11 @@ const rules = ref<FormRules>({ ...@@ -42,9 +45,11 @@ const rules = ref<FormRules>({
watchEffect(() => { watchEffect(() => {
courseValue.value = form.course_id courseValue.value = form.course_id
const course = courses.value.find(item => item.course_id === form.course_id) const course = courses.value.find(item => item.course_id === form.course_id)
form.chapter_id = ''
form.semester_id = course ? course.semester.id : '' form.semester_id = course ? course.semester.id : ''
}) })
function handleCourseChange() {
form.chapter_id = ''
}
// 提交 // 提交
function handleSubmit() { function handleSubmit() {
formRef?.validate().then(create) formRef?.validate().then(create)
...@@ -56,6 +61,8 @@ const create = () => { ...@@ -56,6 +61,8 @@ const create = () => {
ElMessage({ message: '发布成功', type: 'success' }) ElMessage({ message: '发布成功', type: 'success' })
emit('update') emit('update')
emit('update:modelValue', false) emit('update:modelValue', false)
// 清空本地存储
localStorage.removeItem('bbsPost')
}) })
} }
...@@ -84,10 +91,10 @@ function onChange(file: any, files: any, uploadRef: any) { ...@@ -84,10 +91,10 @@ function onChange(file: any, files: any, uploadRef: any) {
width="800px" width="800px"
:close-on-click-modal="false" :close-on-click-modal="false"
@update:modelValue="$emit('update:modelValue')"> @update:modelValue="$emit('update:modelValue')">
<el-form ref="formRef" :model="form" :rules="rules" hide-required-asterisk label-position="top"> <el-form ref="formRef" :model="form" :rules="rules" label-position="top">
<el-row justify="space-between"> <el-row justify="space-between">
<el-form-item label="相关课程" prop="course_id" v-if="!courseId"> <el-form-item label="相关课程" prop="course_id" v-if="!courseId">
<el-select filterable v-model="form.course_id"> <el-select filterable v-model="form.course_id" @change="handleCourseChange">
<el-option v-for="item in courses" :key="item.id" :label="item.name" :value="item.course_id"></el-option> <el-option v-for="item in courses" :key="item.id" :label="item.name" :value="item.course_id"></el-option>
</el-select> </el-select>
</el-form-item> </el-form-item>
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论