AddDialog.vue 9.2 KB
<!-- 新增/编辑弹框 -->
<template>
  <div class="add">
    <gl-dialog
      :title="title"
      :visible.sync="isVisible"
      :before-close="handleClose"
      :width="diaWidth"
      :close-on-click-modal="false"
    >
      <gl-form
        label-width="120px"
        :model="noticeForm"
        :rules="rules"
      >
        <gl-form-item
          label="标题:"
          prop="title"
        >
          <gl-input
            v-model="noticeForm.titile"
            placeholder="请输入标题"
            name="title"
            data-vv-as="标题"
            v-validate="'required|max:25'"
            clearable
          ></gl-input>
          <span
            v-show="errorBags.has('title')"
            class="error"
          >
            {{ errorBags.first('title') }}
          </span>
        </gl-form-item>
        <gl-form-item
          label="内容详情:"
          prop="context"
        >
          <div>
            <editor
              :isClear="isClear"
              name="context"
              data-vv-as="内容"
              v-validate="'required'"
              v-model="noticeForm.context"
            ></editor>
          </div>
          <span
            v-show="errorBags.has('context')"
            class="error"
          >
            {{ errorBags.first('context') }}
          </span>
        </gl-form-item>
        <gl-form-item
          label="发送对象:"
          prop="sendObj"
        >
          <gl-checkbox-group
            v-model="noticeForm.merchants"
            name="sendObj"
            data-vv-as="发送对象"
            v-validate="'required'"
          >
            <gl-checkbox
              v-for="item in sendObjList"
              :label="item.id"
              :key="item.id"
            >{{item.groupName}}</gl-checkbox>
          </gl-checkbox-group>
          <span
            v-show="errorBags.has('sendObj')"
            class="error"
          >
            {{ errorBags.first('sendObj') }}
          </span>
        </gl-form-item>
        <gl-form-item
          label="上传附件:"
          prop="attachment"
        >
          <gl-upload
            class="upload-demo"
            name="file"
            :action="uploadAction"
            :limit="2"
            :before-upload="beforeUpload"
            :on-success="handleSuccess"
            :on-error="handleError"
            :on-remove="handleRemove"
            :file-list="fileList"
          >
            <gl-button
              size="small"
              type="primary"
            >点击上传</gl-button>
            <div
              slot="tip"
              class="el-upload__tip"
            >请上传小于20MB的文件</div>
          </gl-upload>
        </gl-form-item>
        <gl-form-item
          label="展示方式:"
          prop="showType"
        >
          <gl-radio-group 
              v-model="noticeForm.showType" 
              name="showType"
              data-vv-as="展示方式"
              v-validate="'required'">
            <gl-radio
              v-for="item in showTypeList"
              :label="item.value"
              :key="item.value"
            >{{item.label}}</gl-radio>
          </gl-radio-group>
          <!-- <p>说明:没有勾选则为默认形式,即在消息中心中展示</p> -->
          <span
            v-show="errorBags.has('showType')"
            class="error"
          >
            {{ errorBags.first('showType') }}
          </span>
        </gl-form-item>
      </gl-form>
      <span slot="footer">
        <el-button
          type="primary"
          @click="handleDraft"
        >保存草稿</el-button>
        <el-button
          type="primary"
          @click="handleComfrim"
        >审核并发布</el-button>
        <el-button
          type="primary"
          @click="preview"
        >预览</el-button>
        <el-button @click="handleClose">取消发布</el-button>
      </span>
      <gl-dialog title="通知详情" :visible.sync="previewVisible" append-to-body>
        <div class="inmodal" >
            <!-- <div class="modal-header">
                <h4 class="modal-title">通知详情</h4>
                <button type="button" class="close" data-dismiss="modal" ng-click="$dismiss()">
                    <span aria-hidden="true">×</span> <span class="sr-only">Close</span>
                </button>
            </div> -->
            <div class="modal-body ofy-visible">
                <div class="detail_title">{{noticeForm.titile}}</div>
                <div>
                    <span class="subTitle">发布时间:{{new Date().format('yyyy/MM/dd HH:mm:ss')}}</span>
                    <span class="subTitle f-r">消息来自:广联</span>
                </div>
                <div class="content" ng-bind-html="content | htmlTrusted" v-html="noticeForm.context"></div>
                <div>附件:<a class="btn btn-link text-info btn-xs" :href="noticeForm.attachment" v-show="noticeForm.attachment">点击下载资料</a></div>
            </div>
        </div>
      </gl-dialog>
    </gl-dialog>
  </div>
</template>

<script>
import Editor from './Editor'
import { nmMethods, request } from '@/api'
export default {
  name: 'AddDialog',
  components: {
    Editor
  },
  data() {
    return {
      previewVisible: false,
      isClear: false,
      title: '创建通知',
      fileList: [],
      uploadAction: '',
      sendObjList: [],
      showTypeList: [{
        label: '弹出框',
        value: 1
      }, {
        label: '置顶消息',
        value: 2
      }],
      rules: {
        title: [{ required: true }],
        context: [{ required: true }],
        sendObj: [{ required: true }],
        showType: [{ required: true }]
      }
    }
  },
  props: {
    isVisible: {
      type: Boolean,
      default: false
    },
    diaWidth: {
      type: String,
      default: '870px'
    },
    noticeForm: Object
  },
  watch: {
    'noticeForm.context'(val) {
      if (val === '<p><br></p>') {
        this.noticeForm.context = ''
        this.$validator.validate('context', this.noticeForm.context)
      } else {
        this.errorBags.remove('context')
      }
    },
    isVisible(val) {
      !val && (this.fileList = [])
      this.noticeForm.recordId && (this.title = '编辑通知')
      this.$validator.errors.clear()
    }
  },
  mounted() {
    this.getGroupList()
    this.uploadAction = `${
      this.$get_session_common_config().axios.fileUploadURL
    }/gps/common/uploadFile`
  },
  methods: {
    // 获取分组列表
    getGroupList() {
      const params = {}
      params.pageCount = 1000
      params.pageSize = 1
      params.method = nmMethods.MerchantGroup
      request
        .req(params)
        .then(res => {
          this.sendObjList = res.groupDicts
        })
        .catch(err => {
          this.tableLoading = false
          console.log(err)
        })
    },
    // 上传附件,限制用户上传的附件格式和大小
    beforeUpload(file) {
      const isLT20M = file.size / 1024 / 1024 < 20
      !isLT20M && this.$message.error('上传附件大小不能超过20M')
      return isLT20M
    },
    handleSuccess(res, file, fileList) {
      this.fileList = [{ name: file.name, url: file.response.data }]
      this.noticeForm.attachment = file.response.data
      // this.noticeForm.attachment = 'https://dz.80txt.com/57199/极品全能学生.zip'
      // 移除验证
      // this.errorBags.remove('attachment')
    },
    handleError() {
      this.$message.error('上传失败,请重新上传!')
    },
    handleRemove(file, fileList) {
      this.noticeForm.attachment = ''
      // 添加验证
      // this.$validator.validate('attachment', this.noticeForm.attachment)
    },
    handleClose() {
      this.$emit('hideDialog')
    },
    // 保存草稿
    handleDraft() {
      if (this.noticeForm.titile !== '') {
        this.noticeForm.bulletinType = 1 // 通知
        this.noticeForm.publishStatus = 1 // 未发布
        this.noticeForm.checkStatus = 0 // 草稿
        this.$emit('handleAddComfirm')
      } else {
        // 添加验证
        this.$validator.validate('title', this.noticeForm.titile)
      }
    },
    // 审核并发布
    handleComfrim() {
      this.$validator.validateAll().then((result) => {
        if (result) {
          if (this.noticeForm.publishStatus === 3) { // 当此通知为取消发布时,审核发布操作应新建一个通知
            this.noticeForm.recordId && (this.noticeForm.recordId = '')
          }
          this.noticeForm.bulletinType = 1 // 通知
          this.noticeForm.publishStatus = 2 // 已发布
          this.noticeForm.checkStatus = 2 // 已审核
          this.$emit('handleAddComfirm', true)
        }
      })
    },
    preview() {
      this.previewVisible = !this.previewVisible
    }
  }
}

</script>
<style>
.add .el-dialog__body {
  padding: 0 20px;
}
.add .el-select {
  width: 100%;
}
.add .el-checkbox-group {
  display: flex;
  overflow-x: auto;
}
.merchant .btn {
  width: 100%
}
.merchantSelect .ui-select-search {
  width: 100%
}
.detail_title {
    text-align: center;
    font-size: 20px;
    color: #6C6F71;
    padding: 5px 0 10px;
}
.subTitle {
    font-size: 12px;
    color: #A1A1A1;
    letter-spacing: -0.3px;
    text-align: center;
}
.f-r {
    float: right;
}
.content {
    padding: 12px 0 20px;
}
</style>