Blame view

src/views/system/xttz/components/addDialog.vue 10.3 KB
蔡俊立 committed
1
<template>
2 3 4 5 6 7 8 9 10 11 12 13
  <el-form ref="ruleForm" :model="ruleForm" label-width="100px" :rules="rules">
    <el-row>
      <el-col :span="24">
        <el-form-item label="通知标题:" prop="noticeTitle">
          <el-input v-model="ruleForm.noticeTitle" class="width100"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="24">
        <el-form-item label="通知来源:" prop="noticeSource">
          <el-input v-model="ruleForm.noticeSource" class="width100"></el-input>
任超 committed
14
        </el-form-item>
15 16 17
      </el-col>
    </el-row>
    <el-row>
赵千 committed
18 19 20 21 22 23 24
      <el-col :span="24">
        <el-form-item label="有效时长:">
          <el-input v-model="ruleForm.validDays" class="width100"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
25 26 27 28 29 30 31
      <el-form-item label="通知内容:" prop="noticeContent">
        <quill-editor v-model="ruleForm.noticeContent" class="editor ql-editor" ref="myQuillEditor"
          :options="editorOption" @blur="onEditorBlur($event)" @focus="onEditorFocus($event)"
          @change="onEditorChange($event)" @ready="onEditorReady($event)"></quill-editor>
      </el-form-item>
    </el-row>
    <!-- <el-row>
蔡俊立 committed
32
        <el-col :span="8">
任超 committed
33
          <el-form-item label="附件:" prop="noticeFileUrl">
任超 committed
34 35
            <el-upload action multiple :auto-upload="false" :limit="1" :on-change="handleChange"
              :before-upload="beforeUpload">
任超 committed
36 37 38 39
              <el-button icon="el-icon-upload" type="primary">上传</el-button>
              <div slot="tip" class="el-upload__tip">文件大小不超过20MB</div>
            </el-upload>
          </el-form-item>
蔡俊立 committed
40
        </el-col>
蔡俊立 committed
41
      </el-row> -->
42 43 44 45 46 47 48 49 50 51 52 53 54
    <!-- 隐藏上传按钮 用于代替触发富文本上传事件 -->
    <div v-show="false">
      <el-upload action multiple :auto-upload="false" :on-change="RichTexthandleChange" :before-upload="beforeUpload"
        class="richUpload">
        <el-button icon="el-icon-upload" type="primary">上传</el-button>
        <div slot="tip" class="el-upload__tip">文件大小不超过20MB</div>
      </el-upload>
    </div>
    <el-form-item v-if="formData.isButtonFlag" class="text-center">
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="submitForm" plain>确定</el-button>
    </el-form-item>
  </el-form>
蔡俊立 committed
55 56
</template>
<script>
57
import { addSysNotice, updateSysNotice } from "@/api/sysNotice.js";
58
import { upload } from "@/api/file.js";
蔡俊立 committed
59
import { quillEditor } from "vue-quill-editor";
蔡俊立 committed
60 61
export default {
  props: {
62 63 64 65
    formData: {
      type: Object,
      default: () => { }
    }
蔡俊立 committed
66
  },
蔡俊立 committed
67
  components: { quillEditor },
任超 committed
68
  data () {
蔡俊立 committed
69 70
    return {
      ruleForm: {
蔡俊立 committed
71 72 73 74
        noticeTitle: "",
        noticeContent: "",
        noticeFileUrl: "",
        noticeSource: "",
赵千 committed
75
        validDays: 14,
蔡俊立 committed
76
        noticeType: "1"
蔡俊立 committed
77 78 79
      },
      rules: {
        noticeTitle: [
蔡俊立 committed
80
          { required: true, message: "请输入通知标题", trigger: "blur" }
蔡俊立 committed
81 82
        ],
        noticeContent: [
蔡俊立 committed
83
          { required: true, message: "请输入通知内容", trigger: "blur" }
蔡俊立 committed
84 85
        ],
        noticeSource: [
蔡俊立 committed
86
          { required: true, message: "请输入通知来源", trigger: "blur" }
蔡俊立 committed
87 88
        ]
      },
蔡俊立 committed
89
      // 富文本编辑器配置
蔡俊立 committed
90 91 92 93 94 95 96 97 98 99 100 101
      editorOption: {
        theme: "snow", // or 'bubble'
        modules: {
          toolbar: {
            container: [
              ["bold", "italic", "underline", "strike"], // 加粗 斜体 下划线 删除线
              ["blockquote", "code-block"], // 引用  代码块
              [{ header: 1 }, { header: 2 }], // 1、2 级标题
              [{ list: "ordered" }, { list: "bullet" }], // 有序、无序列表
              [{ script: "sub" }, { script: "super" }], // 上标/下标
              [{ indent: "-1" }, { indent: "+1" }], // 缩进
              [{ direction: "rtl" }], // 文本方向
蔡俊立 committed
102
              [{ size: ["small", false, "large", "huge"] }], // 字体大小
蔡俊立 committed
103 104 105 106 107
              [{ header: [1, 2, 3, 4, 5, 6] }], // 标题
              [{ color: [] }, { background: [] }], // 字体颜色、字体背景颜色
              // [{ font: ['songti'] }], // 字体种类
              [{ align: [] }], // 对齐方式
              ["clean"], // 清除文本格式
蔡俊立 committed
108
              ["image", "video"] // 链接、图片、视频
蔡俊立 committed
109
            ],
蔡俊立 committed
110
            handlers: {
任超 committed
111
              image: function (value) {
蔡俊立 committed
112 113 114 115 116
                if (value) {
                  // 用upload的点击事件代替文本编辑器的图片上传事件
                  document.querySelector(".richUpload input").click();
                } else {
                  this.quill.format("image", false);
蔡俊立 committed
117
                }
蔡俊立 committed
118
              }
蔡俊立 committed
119 120 121
            }
          }
        },
蔡俊立 committed
122 123 124
        placeholder: "请输入正文"
      }
    };
蔡俊立 committed
125
  },
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
  mounted () {
    this.$nextTick(() => {
      //创建富文本粘贴事件监听
      let quill = this.$refs.myQuillEditor.quill;
      this.$forceUpdate();
      quill.root.addEventListener(
        "paste",
        evt => {
          if (
            evt.clipboardData &&
            evt.clipboardData.files &&
            evt.clipboardData.files.length
          ) {
            evt.preventDefault();
            [].forEach.call(evt.clipboardData.files, file => {
              if (!file.type.match(/^image\/(gif|jpe?g|a?png|bmp)/i)) {
                return;
蔡俊立 committed
143
              }
144 145 146 147 148 149 150
              that.clipboardPictureChange(file);
            });
          }
        },
        false
      )
    })
赵千 committed
151 152 153 154
    if (!this.formData.isButtonFlag) {
      this.getDetailInfo(this.formData)
    }
    if (this.formData.edit) {
155
      this.getDetailInfo(this.formData)
蔡俊立 committed
156
    }
蔡俊立 committed
157
  },
蔡俊立 committed
158
  methods: {
yuanbo committed
159 160 161 162
    /**
     * @description: submitForm
     * @author: renchao
     */
任超 committed
163
    submitForm () {
任超 committed
164 165 166
      let that = this;
      that.$refs.ruleForm.validate(valid => {
        if (valid) {
任超 committed
167
          if (that.ruleForm.bsmNotice) {
蔡俊立 committed
168
            that.editNotice();
任超 committed
169
          } else {
蔡俊立 committed
170 171
            that.addNotice();
          }
任超 committed
172 173 174 175
        } else {
          return false;
        }
      });
蔡俊立 committed
176 177
    },
    //关闭窗口
yuanbo committed
178 179 180 181
    /**
     * @description: 关闭窗口
     * @author: renchao
     */
任超 committed
182
    closeDialog () {
183
      this.$popupCacel()
蔡俊立 committed
184 185
      this.resetRuleForm();
    },
蔡俊立 committed
186
    //新增通知
yuanbo committed
187 188 189 190
    /**
     * @description: 新增通知
     * @author: renchao
     */
任超 committed
191
    addNotice () {
赵千 committed
192 193
      // 解决报错
      // this.ruleForm.noticeType = "1"
蔡俊立 committed
194 195 196
      addSysNotice(this.ruleForm).then(res => {
        if (res.code == 200) {
          this.$message.success("保存成功");
197
          this.$popupCacel()
蔡俊立 committed
198 199 200 201 202 203 204 205
          this.resetRuleForm();
          this.$parent.queryClick();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    //编辑通知
yuanbo committed
206 207 208 209
    /**
     * @description: 编辑通知
     * @author: renchao
     */
任超 committed
210
    editNotice () {
蔡俊立 committed
211 212 213
      updateSysNotice(this.ruleForm).then(res => {
        if (res.code == 200) {
          this.$message.success("编辑成功");
214
          this.$popupCacel()
蔡俊立 committed
215 216 217 218 219 220 221 222
          this.resetRuleForm();
          this.$parent.queryClick();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    //重置表单
yuanbo committed
223 224 225 226
    /**
     * @description: 重置表单
     * @author: renchao
     */
任超 committed
227
    resetRuleForm () {
蔡俊立 committed
228 229 230 231 232
      this.ruleForm = {
        noticeTitle: "",
        noticeContent: "",
        noticeFileUrl: "",
        noticeSource: "",
赵千 committed
233
        validDays: 14,
蔡俊立 committed
234 235
        noticeType: "1"
      }
蔡俊立 committed
236
    },
yuanbo committed
237 238 239 240 241
    /**
     * @description: beforeUpload
     * @param {*} file
     * @author: renchao
     */
任超 committed
242
    beforeUpload (file) {
蔡俊立 committed
243
      return true;
蔡俊立 committed
244
    },
蔡俊立 committed
245
    //附件上传事件
yuanbo committed
246 247 248 249 250
    /**
     * @description: 附件上传事件
     * @param {*} file
     * @author: renchao
     */
任超 committed
251
    async handleChange (file) {
蔡俊立 committed
252 253 254
      var formdata = new FormData();
      formdata.append("file", file.raw);
      upload(formdata).then(res => {
蔡俊立 committed
255 256
        this.ruleForm.noticeFileUrl = res.message;
      });
蔡俊立 committed
257
    },
蔡俊立 committed
258
    //富文本图片上传事件
yuanbo committed
259 260 261 262 263
    /**
     * @description: 富文本图片上传事件
     * @param {*} file
     * @author: renchao
     */
任超 committed
264
    RichTexthandleChange (file) {
蔡俊立 committed
265 266 267
      this.uploadPicture(file.raw)
    },
    //图片粘贴事件
yuanbo committed
268 269 270 271 272
    /**
     * @description: 图片粘贴事件
     * @param {*} file
     * @author: renchao
     */
任超 committed
273
    clipboardPictureChange (file) {
蔡俊立 committed
274 275
      this.uploadPicture(file)
    },
yuanbo committed
276 277 278 279 280
    /**
     * @description: getDetailInfo
     * @param {*} item
     * @author: renchao
     */
任超 committed
281
    getDetailInfo (item) {
蔡俊立 committed
282 283
      this.ruleForm = item
    },
yuanbo committed
284 285 286 287 288
    /**
     * @description: uploadPicture
     * @param {*} file
     * @author: renchao
     */
任超 committed
289
    uploadPicture (file) {
蔡俊立 committed
290 291
      let that = this;
      var formdata = new FormData();
蔡俊立 committed
292
      formdata.append("file", file);
蔡俊立 committed
293 294 295 296 297 298 299
      upload(formdata).then(res => {
        //editor对象
        const quill = that.$refs.myQuillEditor.quill;
        // 如果上传成功
        // 获取光标所在位置
        const length = quill.selection.savedRange.index;
        // 插入图片
蔡俊立 committed
300
        quill.insertEmbed(length, "image", res.message);
蔡俊立 committed
301 302
        // 调整光标到最后
        quill.setSelection(length + 1);
蔡俊立 committed
303
      });
蔡俊立 committed
304
    },
yuanbo committed
305 306 307 308
    /**
     * @description: onSubmit
     * @author: renchao
     */
任超 committed
309
    onSubmit () {
蔡俊立 committed
310 311 312
      //console.log("submit!");
    },
    // 失去焦点事件
yuanbo committed
313 314 315 316 317
    /**
     * @description: 失去焦点事件
     * @param {*} quill
     * @author: renchao
     */
任超 committed
318
    onEditorBlur (quill) {
蔡俊立 committed
319 320 321
      //console.log("editor blur!", quill);
    },
    // 获得焦点事件
yuanbo committed
322 323 324 325 326
    /**
     * @description: 获得焦点事件
     * @param {*} quill
     * @author: renchao
     */
任超 committed
327
    onEditorFocus (quill) {
蔡俊立 committed
328 329 330
      //console.log("editor focus!", quill);
    },
    // 准备富文本编辑器
yuanbo committed
331 332 333 334 335
    /**
     * @description: 准备富文本编辑器
     * @param {*} quill
     * @author: renchao
     */
任超 committed
336
    onEditorReady (quill) {
蔡俊立 committed
337 338 339
      //console.log("editor ready!", quill);
    },
    // 内容改变事件
yuanbo committed
340 341 342 343 344 345 346
    /**
     * @description: 内容改变事件
     * @param {*} quill
     * @param {*} html
     * @param {*} text
     * @author: renchao
     */
任超 committed
347
    onEditorChange ({ quill, html, text }) {
蔡俊立 committed
348 349
      //console.log("editor change!", quill, html, text);
      //this.content = html;
蔡俊立 committed
350
    }
蔡俊立 committed
351
  }
蔡俊立 committed
352
};
蔡俊立 committed
353 354
</script>
<style scoped lang="scss">
xiaomiao committed
355
@import "~@/styles/dialogBoxheader.scss";
356

任超 committed
357 358 359 360 361 362 363 364 365 366 367 368 369
.quill-editor {
  padding: 0;
}

.ql-editor {
  height: auto !important;
}

/deep/.ql-container {
  padding: 0;
  max-height: 400px;
  min-height: 160px;
  overflow-y: scroll;
370
}</style>