zhouwenxuan
2023-11-29 88d7869b422a1070a0ea8efd22613997e9dbe2fe
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
<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">
                    <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";
 
const dialogVisible = ref(false);
const title = ref("");
const noticeRef = ref();
const fileList = ref([]);
const myQuillEditor = ref();
const isReview = ref(false);
const state = reactive({
    noticeForm: {
        noticeTitle: '',
        noticeContent: '',
        fileList: []
    },
    formRules:{
        noticeTitle: [{ required: true, message: '请填写公告标题', trigger: 'blur' }],
        noticeContent: [{ required: true, message: '请输入公告内容', trigger: 'change' }],
    },
 
})
 
const openDialog = (type, value) => {
    isReview.value = false;
    title.value = type === 'add' ? '新增' : type ==='edit' ? '编辑' : '查看' ;
    if(type === 'edit') {
        state.noticeForm = value;
    }
    if(type === 'review') {
        isReview.value = true;
        state.noticeForm = value;
    }
    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;
        }
        console.log('file, fileList',state.noticeForm.noticeContent);
        noticeRef.value.clearValidate();
        reset();
        dialogVisible.value = false;
 
    }
}
 
const handleClose = () => {
    noticeRef.value.clearValidate();
    reset();
    dialogVisible.value = false;
 
}
const reset = () => {
    state.noticeForm = {
        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>