inleft
2022-03-01 88f419df77ade235ea5e5e24be204842a24b7b33
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
<template>
  <a-modal
    title="编辑通知公告"
    :width="1000"
    :footer="null"
    :visible="visible"
    @cancel="handleCancel"
  >
    <a-spin :spinning="formLoading">
      <a-form :form="form">
        <a-form-item v-show="false">
          <a-input v-decorator="['id']" />
        </a-form-item>
        <a-form-item
          label="标题"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input placeholder="请输入标题" v-decorator="['title', {rules: [{required: true, message: '请输入标题!'}]}]" />
        </a-form-item>
        <a-form-item
          label="类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-radio-group v-decorator="['type',{rules: [{ required: true, message: '请选择类型!' }]}]" >
            <a-radio-button v-for="(item,index) in typeDictTypeDropDown" :key="index" :value="item.code">{{ item.value }}</a-radio-button>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="内容"
        >
          <antd-editor :uploadConfig="editorUploadConfig" v-model="editorContent" @onchange="changeEditor" @oninit="getEditor" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="通知到的人"
        >
          <a-transfer
            :data-source="mockData"
            show-search
            :list-style="{
              width: '40%',
              height: '300px',
            }"
            :filter-option="filterOption"
            :target-keys="targetKeys"
            :render="item => item.title"
            @change="handleChange"
          />
        </a-form-item>
        <a-divider />
        <a-form-item class="subForm-item">
          <a-button type="primary" class="subButton" @click="handleSubmit('1')">发布</a-button>
          <a-button type="danger" class="subButton" @click="handleSubmit('0')">存为草稿</a-button>
          <a-button class="subButton" @click="handleCancel">取消</a-button>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
  import { sysNoticeEdit, sysNoticeDetail } from '@/api/modular/system/noticeManage'
  import { sysDictTypeDropDown } from '@/api/modular/system/dictManage'
  import { sysFileInfoUpload } from '@/api/modular/system/fileManage'
  import { AntdEditor } from '@/components'
  import { sysUserSelector } from '@/api/modular/system/userManage'
  export default {
    name: 'AddForm',
    components: {
      AntdEditor
    },
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 3 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 18 }
        },
        visible: false,
        form: this.$form.createForm(this),
        typeDictTypeDropDown: [], // 0通知 1公告
        editorContent: '',
        editorContentText: '',
        editorUploadConfig: {
          method: 'http',
          callback: this.editorUploadImage
        },
        mockData: [],
        targetKeys: [],
        noticeDetail: [],
        formLoading: true
      }
    },
    methods: {
      // 初始化方法
      edit (record) {
        this.visible = true
        this.sysNoticeDetail(record.id)
        this.sysDictTypeDropDown()
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              title: record.title,
              type: record.type.toString()
            }
          )
          this.editor.txt.html(record.content)
          this.editorContent = record.content
        }, 100)
      },
      /**
       * 获取字典数据
       */
      sysDictTypeDropDown () {
        sysDictTypeDropDown({ code: 'notice_type' }).then((res) => {
          this.typeDictTypeDropDown = res.data
        })
      },
      /**
       * 编辑器回调上传及回传图片url
       */
      editorUploadImage (files, insert) {
        const formData = new FormData()
        files.forEach(file => {
          formData.append('file', file)
        })
        sysFileInfoUpload(formData).then((res) => {
          if (res.success) {
            insert(process.env.VUE_APP_API_BASE_URL + '/sysFileInfo/preview?id=' + res.data)
          } else {
            this.$message.error('编辑器上传图片失败:' + res.message)
          }
        })
      },
      getEditor (editor) {
        this.editor = editor
      },
      changeEditor (html, ele) {
        this.editorContent = html
        this.editorContentText = ele.text()
      },
      /**
       * 编辑时获取全部信息
       */
      sysNoticeDetail (id) {
        sysNoticeDetail({ id: id }).then((res) => {
          this.noticeDetail = res.data
          this.getMock(this.noticeDetail)
        })
      },
      /**
       * 穿梭框
       */
      getMock (noticeDetail) {
        const targetKeys = []
        const mockData = []
          sysUserSelector().then((res) => {
            this.formLoading = false
            for (let i = 0; i < res.data.length; i++) {
              const data = {
                key: res.data[i].id.toString(),
                title: res.data[i].name,
                description: `description of ${res.data[i].name}`
              }
              for (let j = 0; j < noticeDetail.noticeUserIdList.length; j++) {
                if (data.key === noticeDetail.noticeUserIdList[j]) {
                  targetKeys.push(noticeDetail.noticeUserIdList[j])
                }
              }
              mockData.push(data)
            }
          })
        this.mockData = mockData
        this.targetKeys = targetKeys
      },
      filterOption (inputValue, option) {
        return option.description.indexOf(inputValue) > -1
      },
      handleChange (targetKeys, direction, moveKeys) {
        this.targetKeys = targetKeys
      },
      handleSubmit (types) {
        const { form: { validateFields } } = this
        // eslint-disable-next-line eqeqeq
        if (this.editorContent == '') {
          this.$message.error('请填写内容')
          return
        }
        if (this.targetKeys.length < 1) {
          this.$message.error('请选择通知到的人')
          return
        }
        validateFields((errors, values) => {
          if (!errors) {
            this.formLoading = true
            values.content = this.editorContent
            values.status = types
            values.noticeUserIdList = this.targetKeys
            sysNoticeEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.visible = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败:' + res.message)
              }
            }).finally((res) => {
              this.formLoading = false
            })
          }
        })
      },
      handleCancel () {
        this.editor.txt.clear()
        this.targetKeys = []
        this.editorContent = ''
        this.form.resetFields()
        this.visible = false
        this.formLoading = true
      }
    }
  }
</script>
<style>
  .subButton{
    float: right;
  }
  .subForm-item{
    margin-bottom: 0px;
  }
</style>