zhouwx
2025-06-10 c8c99bf1f753e27c4d99a0ff6058ce16f973f9c4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<template>
  <div class="notice">
    <el-dialog
        v-model="dialogVisible"
        :title="title"
        width="550px"
        :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="100px" >
        <el-form-item label="章名称:" prop="name">
          <el-input v-model.trim="state.form.name" maxlength="100" show-word-limit :disabled="!state.isFirst"></el-input>
        </el-form-item>
        <el-form-item label="节名称:" prop="chapter.name" v-if="!state.isFirst">
          <el-input v-model.trim="state.chapter.name"></el-input>
        </el-form-item>
        <el-form-item label="资源:" v-if="!state.isFirst">
          <div style="display: flex;align-items: center; width: 100%;justify-content: space-between;">
            <el-input v-model.trim="state.chapter.resourceName" disabled style="width: 80%"></el-input>
            <el-button type="primary" style="margin-left: 20px" plain size="default" @click="openResource">选择资源</el-button>
          </div>
 
        </el-form-item>
 
        <el-form-item label="排序:" prop="sort" >
          <el-input-number v-model="state.form.sort" />
        </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>
    <courseDialog ref="courseRef" @choose-res="choosedResource" ></courseDialog>
  </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} from "@/api/onlineEducation/courseClass";
import courseDialog from './chooseResource.vue'
import {
  addChapter,
  addChapterPeriod,
  checkChapterName,
  editChapter,
  editChapterPeriod
} from "@/api/onlineEducation/chapters";
 
const dialogVisible = ref(false);
const title = ref("");
const busRef = ref();
const courseRef = ref()
const length = ref()
const emit = defineEmits(["getList"]);
const startUsername = ref('');
const startUsernamePeriod = ref('');
 
 
const validateName = (rule, value, callback)=>{
  if(value === ''){
    callback(new Error('请输入章名称'))
  }else if(title.value === '编辑' && value === startUsername.value || title.value === '新增' && value === startUsername.value){
    callback()
  }else{
    let param = {}
    if(title.value === '新增') {
      param = {
        name:value,
        courseId: state.form.courseId
      }
    }else if(title.value === '编辑'){
      param = {
        name:value,
        id: state.form.id,
        courseId: state.form.courseId
      }
    }
    checkChapterName(param).then((res)=>{
      if(res.data == false){
        callback(new Error('名称已被占用,请更换其他名称'))
      }else{
        callback()
      }
    })
  }
}
const state = reactive({
  form: {
    id: '',
    name: '',
    sort: 0,
    courseId: null,
    chapterPeriods: []
  },
  formRules:{
    name: [{ required: true, trigger: "blur", validator: validateName }],
  },
  isFirst: true,
  chapter: {}
})
 
const openDialog = async (type, value) => {
  length.value = value.listLength
  state.form.courseId = value.courseId
  title.value = type === 'addFirst' || type === 'add' ? '新增' : type ==='edit' ? '编辑' : '' ;
  if(type === 'edit') {
    if(!value.chapterId){
      state.isFirst = true;
      startUsername.value = value.name;
      state.form = value;
      state.form.sort = value.sort;
    }else {
      state.isFirst = false;
      startUsernamePeriod.value = value.name;
      state.chapter = value;
      state.form.name = value.capterName;
      startUsername.value = value.capterName;
      state.chapter.resourceName = value.resource.name
    }
 
  }else if(type === 'add' && value ){
    startUsername.value = value.name;
    state.chapter.chapterId = value.id;
    state.chapter.courseId = value.courseId;
    state.form.name = value.name
    state.isFirst = false;
  }else {
    state.isFirst = true;
  }
  dialogVisible.value = true;
}
const choosedResource = (val) => {
  state.chapter.resourceName = val.name
  state.chapter.resourceId = val.id
}
 
const onSubmit = async () => {
  const valid = await busRef.value.validate();
  if(valid){
    if(title.value === '新增'){
      if(state.chapter.chapterId){
        const {id, ...data} = JSON.parse(JSON.stringify(state.chapter))
        data.sort = state.form.sort
        const res = await addChapterPeriod(data)
        if(res.code === 200){
          ElMessage({
            type: 'success',
            message: '新增成功'
          });
        }else{
          ElMessage.warning(res.message)
        }
      }else{
        const {id, ...data} = JSON.parse(JSON.stringify(state.form))
        const res = await addChapter(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 === '编辑'){
      if(state.chapter.chapterId){
        const {...data} = JSON.parse(JSON.stringify(state.chapter))
        data.sort = state.form.sort
        const res = await editChapterPeriod(data)
        if(res.code === 200){
          ElMessage({
            type: 'success',
            message: '编辑成功'
          });
        }else{
          ElMessage.warning(res.message)
        }
        emit("getList")
        busRef.value.clearValidate();
        reset();
        dialogVisible.value = false;
      }else {
        const {...data} = JSON.parse(JSON.stringify(state.form))
        const res = await editChapter(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 openResource = () => {
  courseRef.value.openDialog()
}
const reset = () => {
  state.form = {
    id: '',
    name: '',
    sort: 0,
    courseId: null,
    chapterPeriods: []
  }
  state.chapter={}
}
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>