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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
<template>
  <div>
    <div v-if="currentView === 'list'">
      <!-- 创建知识库按钮 -->
      <div class="header-container">
        <el-input style="width: 200px;margin-right: 50px;" v-model="TreeDataPool.searchInput" placeholder="搜索" clearable @input="querySearchAsync('camera')">
          <i class="el-icon-search el-input__icon" style="color: #dcdfe6" slot="prefix" @click="searchAreaData"></i>
        </el-input>
        <el-button type="primary" @click="openDialog('create')">
          <i class="el-icon-plus"></i> 创建知识库
        </el-button>
      </div>
 
      <!-- 通用弹窗组件 -->
      <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="600px" custom-class="custom-dialog"
        @open="handleDialogOpen" @closed="handleDialogClose">
        <el-form label-width="100px" :disabled="isViewMode" ref="form" :rules="formRules" :model="currentFile">
          <el-form-item label="名称" prop="title">
            <el-input v-model="currentFile.title" placeholder="请输入知识库名称" />
          </el-form-item>
 
          <el-form-item label="描述" prop="content">
            <el-input type="textarea" :rows="6" v-model="currentFile.content" resize="none">
            </el-input>
          </el-form-item>
        </el-form>
 
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleConfirm" v-if="!isViewMode">
            确 定
          </el-button>
        </span>
      </el-dialog>
 
      <div class="file-manager-container">
 
        <el-row v-if="files.length > 0" :gutter="20">
          <el-col v-for="(file, index) in files" :key="file.checkId" :span="5" class="file-col">
            <el-card class="file-card" @click.native="handleView(file)">
              <!-- 文件标题部分 -->
              <div class="file-header">
                <span class="file-index" hidden>{{ index + 1 }}</span>
                <i class="el-icon-document file-icon"></i>
                <div class="file-title">{{ file.title }}</div>
              </div>
 
              <!-- 文件描述 -->
              <div class="file-desc">
                <!-- {{ file.content }} -->
              </div>
 
              <!-- 操作按钮组 -->
              <div class="file-actions">
                <el-button type="text" @click.stop="openDialog('edit', file)">
                  <i class="el-icon-edit"></i> 编辑
                </el-button>|
                <el-button type="text" @click.stop="handleView(file)"> <!-- 修改查看按钮事件 -->
                  <i class="el-icon-view"></i> 查看
                </el-button>|
                <el-button type="text" @click.stop="handleDelete(file)">
                  <i class="el-icon-delete"></i> 删除
                </el-button>
              </div>
            </el-card>
          </el-col>
        </el-row>
 
        <!-- 新增空状态显示 -->
        <el-empty v-else description="暂无数据"></el-empty>
 
      </div>
      <div class="footer-container">
        <el-pagination @current-change="handleCurrentChange" :page-size="16" layout="prev, pager, next"
          :total="this.pagination.total">
        </el-pagination>
      </div>
    </div>
    <!-- 知识库详情视图 -->
    <KnowLedgeFilesView v-if="currentView === 'detail'" :knowledge-id="currentKnowledgeId" @back="handleBackToList" />
  </div>
</template>
 
<script>
import FileAPI from '@/api/KnowledgeView.ts'
import KnowLedgeFilesView from './KnowLedgeFilesView.vue' // 引入组件
 
export default {
  components: {
    KnowLedgeFilesView
  },
  data() {
    return {
      currentView: 'list', // 'list' 或 'detail'
      currentKnowledgeId: null,
      formRules: {},
      pagination: {
        page: 1,
        pageSize: 16,
        total: 0,
        totalPage: 1
      },
      dialogVisible: false,
      dialogType: 'create',
      currentFile: this.initFile(),
      form: {
        fileName: '',
        checkContent: '',
        rangeValue: 0
      },
      files: []
    }
  },
  mounted() {
    this.fetchFiles()
  },
  computed: {
    dialogTitle() {
      return {
        create: '创建知识库',
        edit: '编辑知识库',
        view: '知识库详情'
      }[this.dialogType]
    },
    isViewMode() {
      return this.dialogType === 'view'
    }
  },
  methods: {
    handleDialogOpen() {
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    },
    handleDialogClose() {
      this.$refs.form.resetFields()
      this.formRules = {}
    },
    async fetchFiles() {
      try {
        const res = await FileAPI.getKnowledges({
          page: this.pagination.page,
          pageSize: this.pagination.pageSize
        })
        const totalPage = res.data.pagination.totalPage
        const currentPage = this.pagination.page > totalPage
          ? totalPage
          : res.data.pagination.page
        this.pagination = {
          ...this.pagination,
          page: currentPage,
          total: res.data.pagination.total,
          totalPage: totalPage
        }
        this.files = res.data.list
        this.pagination.total = res.data.pagination.total
        this.pagination.totalPage = res.data.pagination.totalPage
      } catch (error) {
        this.$message.error('获取列表失败')
        console.error('API Error:', error)
      }
    },
    initFile() {
      return {
        id: null,
        title: '',
        content: ''
      }
    },
    openDialog(type, file = null) {
      if (type != 'view') {
        this.formRules = {
          title: [
            { required: true, message: '文件名称不能为空', trigger: 'blur' }
          ],
          content: [
            { required: true, message: '检测内容不能为空', trigger: 'blur' }
          ]
        }
      }
      this.dialogType = type
      this.currentFile = file ? { ...file } : this.initFile()
      this.dialogVisible = true
    },
    handleConfirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const operation = {
            create: this.createFile,
            edit: this.updateFile
          }[this.dialogType]
 
          operation && operation(this.currentFile)
          this.dialogVisible = false
        }
      })
    },
    async createFile(file) {
      try {
        await FileAPI.createKnowledge({
          title: file.title,
          content: file.content
        })
        this.$message.success('添加成功')
        this.fetchFiles()
      } catch (error) {
        this.$message.error('添加失败')
        console.error('API Error:', error)
      }
    },
    async updateFile(file) {
      try {
        await FileAPI.updateKnowledge({
          id: file.id,
          title: file.title,
          content: file.content
        })
        this.$message.success('编辑成功')
        this.fetchFiles()
      } catch (error) {
        this.$message.error('编辑失败')
        console.error('API Error:', error)
      }
    },
    handleCurrentChange(page) {
      this.pagination.page = page
      this.fetchFiles()
    },
    async handleDelete(file) {
      this.$confirm('即将删除该知识库及其所有文件,所有内容将无法找回, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await FileAPI.deleteKnowledgee({
            id: file.id
          }).then((rsp) => {
            if (rsp && rsp.status === 200) {
              // 删除成功后自动修正页码
              if (this.files.length === 1 && this.pagination.page > 1) {
                this.pagination.page -= 1
              }
              this.fetchFiles()
 
              this.$message({
                type: "success",
                message: "删除成功"
              })
            } else {
              this.$message({
                type: "error",
                message: rsp.msg
              })
            }
          })
        } catch (error) {
          this.$message.error('删除失败')
          console.error('API Error:', error)
        }
      }).catch(() => {
        // this.$message({
        //   type: 'info',
        //   message: '已取消删除'
        // });
      });
    },
    // 新增:处理查看知识库详情
    handleView(file) {
      this.currentKnowledgeId = file.id
      this.currentView = 'detail'
      // this.$router.push({
      //   name: 'KnowledgeFiles',
      //   params: { id: file.id }
      // })
    },
    // 返回列表方法
    handleBackToList() {
      this.currentView = 'list'
    },
  }
}
</script>
<style lang="scss" scoped>
/* 新增按钮容器样式 */
.header-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
  margin-right: 20px;
 
 
}
 
.footer-container {}
 
.file-manager-container {
  min-height: 750px;
  padding: 20px;
  background: #fff;
  position: relative;
  /* 新增定位基准 */
 
  .new-task-btn {
    /* 移除绝对定位 */
    position: absolute;
    right: 30px;
    top: 20px;
  }
 
  .file-col {
    margin-bottom: 20px;
    width: 300px;
  }
 
  .file-card {
    cursor: pointer;
    transition: all 0.3s;
    border: 2px solid transparent;
 
    // &.selected {
    //   border-color: #409EFF;
    // }
 
    .file-header {
      display: flex;
      align-items: center;
      margin-bottom: 12px;
 
      .file-index {
        width: 24px;
        height: 24px;
        background: #409EFF;
        color: white;
        border-radius: 50%;
        text-align: center;
        line-height: 24px;
        margin-right: 8px;
      }
 
      .file-icon {
        color: #409EFF;
        font-size: 24px;
        margin-right: 8px;
      }
 
      .file-title {
        font-weight: bold;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
 
    .file-desc {
      color: #666;
      font-size: 12px;
      line-height: 1.5; // 根据实际字体大小调整
      height: 3em; // 2行 x 1.5行高 = 3em(推荐使用相对单位)
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      margin-bottom: 12px;
      text-align: left;
      // 新增以下两行
      word-wrap: break-word;
      white-space: normal; // 或使用 pre-line
 
    }
 
    .file-actions {
      width: 250px;
      border-top: 1px solid #eee;
      padding-top: 12px;
      // text-align: right;
 
      .el-button {
        padding: 0 8px;
        color: #666;
 
        i {
          margin-right: 4px;
        }
      }
    }
  }
}
 
/* 弹窗样式 */
:deep(.custom-dialog) {
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
 
  .el-dialog__header {
    border-bottom: 1px solid #ebeef5;
    padding: 16px 20px;
 
    .el-dialog__title {
      font-size: 16px;
      color: #303133;
    }
  }
 
  .el-dialog__body {
    padding: 20px 30px;
  }
 
  .dialog-input {
    width: 80%;
 
    .el-input__inner {
      border-color: #dcdfe6;
    }
  }
 
  .dialog-textarea {
    .el-textarea__inner {
      font-family: 'Microsoft YaHei', sans-serif;
      line-height: 1.6;
      color: #606266;
      border-color: #dcdfe6;
    }
  }
 
  .dialog-slider {
    width: 70%;
    margin-right: 20px;
    flex: 1; // 滑块自适应宽度
  }
 
  .score-value {
    color: rgb(4, 8, 12);
    font-weight: bold;
    margin-right: 15px;
  }
 
  .dialog-footer {
    .el-button {
      padding: 10px 20px;
 
      &--primary {
        background: #409EFF;
        border-color: #409EFF;
      }
    }
  }
}
 
.content-pre {
  white-space: pre-wrap;
  word-break: break-word;
  margin: 0;
  font-size: 13px;
  line-height: 1.6;
}
</style>