houzhongjian
2024-07-23 8501060c4f921d1e744c477e4dc08eb47b52693c
提交 | 用户 | 时间
850106 1 <template>
H 2   <el-dialog
3       :title="!dataForm.id ? `新增` : '编辑'"
4       :close-on-click-modal="false"
5       :visible.sync="visible">
6     <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="120px">
7       <el-row>
8         <el-col :span="12">
9           <el-form-item label="类型" prop="typeId" :rules="dataRule.typeId">
10             <dict-select-tag style="width: 100%;" v-model="dataForm.typeId" dictCode="knowledge-type" placeholder="类型"></dict-select-tag>
11           </el-form-item>
12         </el-col>
13         <el-col :span="12">
14           <el-form-item label="目录" prop="treeId" :rules="dataRule.treeId">
15             <el-cascader
16                 ref="cascader"
17                 style="width: 100%"
18                 v-model="dataForm.values"
19                 :options="options"
20                 :props="{ expandTrigger: 'hover', checkStrictly: true, emitPath: false }"
21                 @change="cascaderChange">
22             </el-cascader>
23           </el-form-item>
24         </el-col>
25       </el-row>
26       <el-form-item label="标题" prop="title" :rules="dataRule.title">
27         <el-input v-model="dataForm.title" placeholder="标题"></el-input>
28       </el-form-item>
29       <el-form-item v-if="dataForm.typeId === '1'" label="网址" prop="website" :rules="dataRule.website">
30         <el-input v-model="dataForm.website" placeholder="网址"></el-input>
31       </el-form-item>
32       <el-form-item label="关键词" prop="keyWords" :rules="dataRule.keyWords">
33         <el-input v-model="dataForm.keyWords" placeholder="关键词"></el-input>
34       </el-form-item>
35       <el-form-item label="摘要" prop="content" :rules="dataRule.content">
36         <el-input type="textarea" v-model="dataForm.content" placeholder="摘要"></el-input>
37       </el-form-item>
38       <el-form-item v-if="dataForm.typeId !== '1' && dataForm.typeId" label="上传文件" prop="chooseFile" :rules="dataRule.chooseFile">
39         <el-upload
40             multiple
41             ref="upload"
42             :action="action"
43             :on-success="successHandle"
44             :file-list="fileList"
45             :auto-upload="true"
46             :on-remove="handleRemove"
47             :on-change="changeFile"
48             :limit="1"
49             :on-exceed="handleExceed"
50             :disabled="dataForm.id !== ''">
51           <el-button size="small" :disabled="dataForm.id !== ''" type="primary" :loading="uploadLoading">点击上传</el-button>
52         </el-upload>
53       </el-form-item>
54     </el-form>
55     <span slot="footer" class="dialog-footer">
56       <el-button @click="visible = false" :loading="uploadLoading">取消</el-button>
57       <el-button type="primary" @click="dataFormSubmit()" :loading="uploadLoading">确定</el-button>
58     </span>
59   </el-dialog>
60 </template>
61
62 <script>
63   import DictSelectTag from "@/components/dict/dict-select-tag";
64   import Cookies from 'js-cookie'
65   export default {
66     components: {
67       DictSelectTag
68     },
69     data () {
70       return {
71         visible: false,
72         roleList: [],
73         successFlg: false,
74         dataForm: {
75           id: '',
76           title: '',
77           treeId: '',
78           values: [],
79           typeId: '',
80           website: '',
81           keyWords: '',
82           content: '',
83           chooseFile: [],
84           fileName: '',
85           filePostfix: '',
86           url: '',
87           host: ''
88         },
89         action: '',
90         fileList: [],
91         options: [],
92         uploadLoading: false,
93         dataRule: {
94           title: [
95             { required: true, message: '不能为空', trigger: 'blur' }
96           ],
97           typeId: [
98             { required: true, message: '不能为空', trigger: 'blur' }
99           ],
100           website: [
101             { required: true, message: '不能为空', trigger: 'blur' }
102           ],
103           chooseFile: [
104             { required: true, message: '不能为空', trigger: 'blur' }
105           ],
106           treeId: [
107             { required: true, message: '不能为空', trigger: 'blur' }
108           ]
109         }
110       }
111     },
112     methods: {
113       // 初始化
114       init (id, chooseTreeId, data) {
115         this.dataForm.chooseFile = []
116         this.successFlg = false
117         this.fileList = []
118         this.action = `/proxyApi/iailab-ntt-model/oss/file/upload?token=${Cookies.get('token')}`
119         this.options = this.deleteChildren(data)
120         this.dataForm.id = id || ''
121         this.dataForm.values = ''
122         if (chooseTreeId !== '') {
123           this.dataForm.values = chooseTreeId
124         }
125         this.visible = true
126         this.$nextTick(() => {
127           this.$refs['dataForm'].resetFields()
128           this.dataForm.treeId = this.dataForm.values
129           if (this.dataForm.id) {
130             this.getInfo()
131           }
132         })
133       },
134       // 获取详情
135       getInfo() {
136         this.uploadLoading = true
137         this.$http.get(`/iailab-ntt-model/knowledge/det/${this.dataForm.id}`).then(({data: res}) => {
138           this.uploadLoading = false
139           if (res.code !== 0) {
140             return this.$message.error(res.msg)
141           }
142           this.dataForm.typeId = res.data.typeId
143           this.dataForm.title = res.data.title
144           this.dataForm.keyWords = res.data.keyWords
145           this.dataForm.content = res.data.content
146           this.dataForm.treeId = res.data.treeId
147           this.dataForm.website = res.data.website
148           this.dataForm.values = this.dataForm.treeId
149           this.dataForm.chooseFile = this.fileList
150           this.dataForm.id = id || ''
151
152           // this.fileList.push({
153           //   name: `${res.data.fileName}.${res.data.filePostfix}`,
154           //   url: res.data.url
155           // })
156         }).catch(() => {
157         })
158       },
159       // 把树形图数据里空的children删除
160       deleteChildren (arr) {
161         let list = arr
162         for (const item of arr) {
163           if (item.children) {
164             if (item.children.length > 0) {
165               this.deleteChildren(item.children)
166             } else {
167               delete item.children
168             }
169           }
170         }
171         return list
172       },
173       // 表单提交
174       dataFormSubmit () {
175         this.$refs['dataForm'].validate((valid) => {
176           if (valid) {
177             this.uploadLoading = true
178             this.$http[!this.dataForm.id ? 'post' : 'put']('/iailab-ntt-model/knowledge/det', {
179               ...this.dataForm
180             }).then(({ data: res }) => {
181               this.uploadLoading = false
182               if (res.code !== 0) {
183                 return this.$message.error(res.msg)
184               }
185               this.$message({
186                 message: this.$t('prompt.success'),
187                 type: 'success',
188                 duration: 500,
189                 onClose: () => {
190                   this.visible = false
191                   this.$emit('refreshDataList')
192                 }
193               })
194             }).catch(() => {})
195           }
196         })
197       },
198       changeFile (file, fileList) {
199         this.dataForm.chooseFile = fileList
200       },
201       handleRemove (file, fileList) {
202         this.dataForm.chooseFile = fileList
203       },
204       // 上传成功
205       successHandle (response, file, fileList) {
206         this.uploadLoading = false
207         this.fileList = fileList
208         if (response && response.code === 0) {
209           this.successFlg = true
210           this.dataForm.fileName = file.name.substring(0, file.name.lastIndexOf('.'))
211           this.dataForm.filePostfix = file.name.substring(file.name.lastIndexOf('.') + 1)
212           this.dataForm.url = response.data.url
213         } else {
214           this.$message.error(response.msg)
215         }
216       },
217       handleExceed () {
218         this.$message.warning(`当前限制选择 1 个文件,如果需要更换文件,请删除当前文件并再次选择`)
219       },
220       cascaderChange (values) {
221         this.$refs.cascader.toggleDropDownVisible()
222         this.dataForm.treeId = values
223       }
224     }
225   }
226 </script>
227
228 <style>
229   .el-cascader-panel .el-radio {
230     width: 132px;
231     height: 34px;
232     line-height: 34px;
233     padding: 0 10px;
234     z-index: 10;
235     position: absolute;
236   }
237   .el-cascader-panel .el-radio__input {
238     visibility: hidden;
239   }
240   .el-cascader-panel .el-cascader-node__postfix {
241     top: 10px;
242   }
243 </style>