zhouwenxuan
2023-11-30 25fa992e12447e535b801da2cc497bdafe0b7b9b
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
<template>
    <div class="notice">
        <el-dialog
            v-model="dialogVisible"
            :title="title"
            width="50%"
            :before-close="handleClose"
        >
            <el-form :model="state.noticeForm" size="default" ref="noticeRef" :rules="title === '新增' || title === '编辑' ? state.formRules : {}" label-width="110px" >
                <el-form-item label="公告标题:" prop="noticeTitle">
                    <el-input v-model.trim="state.noticeForm.noticeTitle" v-if="!isReview" ></el-input>
                    <span v-else>{{state.noticeForm.noticeTitle}}</span>
                </el-form-item>
                <div style="margin: 0 0 15px 30px" v-if="!isReview">
                    <el-upload
                        v-model:file-list="fileList"
                        class="upload-demo"
                        action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
                        multiple
                        :on-preview="handlePreview"
                        :on-remove="handleRemove"
                        :before-remove="beforeRemove"
                        :limit="3"
                    >
                        <el-button type="primary">附件上传</el-button>
                    </el-upload>
                </div>
                <el-form-item v-else label="附件:" >
                    <div class="file">
                        <el-link v-for="(item,index) in state.noticeForm.fileList"  type="primary" :key="index" :underline="false">
                            <i class="el-icon-paperclip" style="margin-right: 5px;color: #1e6abc"></i>{{item.fileName}}
                        </el-link>
                    </div>
                </el-form-item>
                <el-form-item v-if="!isReview" style="margin-left: -80px" prop="noticeContent">
<!--                    <tinymce v-model="state.noticeForm.noticeContent"></tinymce>-->
                    <editor  ref="myQuillEditor" v-model="state.noticeForm.noticeContent" :height="300"></editor>
 
                </el-form-item>
                <el-form-item label="公告内容:" v-else>
                    <div class="ql-container ql-snow" style="height: 300px;width: 100%;margin-top: 10px;" >
                        <div class="ql-editor">
                            <div v-html="state.noticeForm.noticeContent"></div>
                        </div>
                    </div>
                </el-form-item>
            </el-form>
            <template #footer v-if="!isReview">
                    <span class="dialog-footer">
                        <el-button @click="handleClose" size="default">取 消</el-button>
                        <el-button type="primary"  @click="onSubmit" size="default">确认发布</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, editNotice, getNoticeDetail} from "@/api/backManage/notice";
const emit = defineEmits(["getList"]);
 
const dialogVisible = ref(false);
const title = ref("");
const noticeRef = ref();
const fileList = ref([]);
const myQuillEditor = ref();
const isReview = ref(false);
const state = reactive({
    noticeForm: {
        id: '',
        noticeTitle: '',
        noticeContent: '',
        fileList: []
    },
    formRules:{
        noticeTitle: [{ required: true, message: '请填写公告标题', trigger: 'blur' }],
        noticeContent: [{ required: true, message: '请输入公告内容', trigger: 'blur' }],
    },
 
})
 
const openDialog = async (type, value) => {
    isReview.value = false;
    title.value = type === 'add' ? '新增' : type ==='edit' ? '编辑' : '查看' ;
    if(type === 'edit' || type === 'review') {
        const param = {
            noticeId: value.id
        }
        const res = await getNoticeDetail(param);
        if(res.code === 200){
            state.noticeForm.id = res.data.id
            state.noticeForm.noticeTitle = res.data.title
            state.noticeForm.noticeContent = res.data.content
        }else{
            ElMessage.warning(res.message)
        }
    }
    if(type === 'review') {
        isReview.value = true;
    }
    if(type === 'add'){
        reset()
    }
    dialogVisible.value = true;
}
 
const onSubmit = async () => {
    const valid = await noticeRef.value.validate();
    if(valid){
        if(state.noticeForm.noticeContent === "<p><br></p>"){
            ElMessage({
                type: 'warning',
                message: '请输入公告内容'
            });
            return;
        }
        if(title.value === '新增'){
            const param = {
                content: state.noticeForm.noticeContent,
                title: state.noticeForm.noticeTitle
            }
            const res = await addNotice(param)
            if(res.code === 200){
                ElMessage({
                    type: 'success',
                    message: '新增成功'
                });
            }else{
                ElMessage.warning(res.message)
            }
            emit("getList")
            reset();
            noticeRef.value.clearValidate();
            dialogVisible.value = false;
        }else if(title.value === '编辑') {
            const nowDate = new Date()
            const param = {
                id: state.noticeForm.id,
                content: state.noticeForm.noticeContent,
                title: state.noticeForm.noticeTitle
            }
            const res = await editNotice(param)
            if(res.code === 200){
                ElMessage({
                    type: 'success',
                    message: '编辑成功'
                });
            }else{
                ElMessage.warning(res.message)
            }
            emit("getList")
            reset();
            noticeRef.value.clearValidate();
            dialogVisible.value = false;
        }
    }
}
 
const handleClose = () => {
    noticeRef.value.clearValidate();
    dialogVisible.value = false;
}
const reset = () => {
    state.noticeForm = {
        id: '',
        noticeTitle: '',
        noticeContent: ''
    }
}
const handleRemove = (file, fileList) => {
    console.log(file, fileList);
}
const handlePreview = (uploadFile) => {
    console.log(uploadFile)
}
const beforeRemove = (file, fileList) => {
    return this.$confirm(`确定移除 ${ file.name }?`);
}
 
 
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>