index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. <template>
  2. <div v-loading="listLoading" class="app-container page-component__scroll">
  3. <div class="filter-container">
  4. <div v-if="selectedCount" class="filter-control">
  5. <el-button
  6. type="primary"
  7. size="small"
  8. icon="el-icon-edit"
  9. plain
  10. :disabled="selectedCount > 1"
  11. @click="onEditFile"
  12. >编辑</el-button>
  13. <el-button type="primary" size="small" icon="el-icon-rank" plain @click="onMoveFile">移动</el-button>
  14. <el-button type="primary" size="small" icon="el-icon-download" plain @click="onDownload">下载</el-button>
  15. <el-button type="primary" size="small" icon="el-icon-delete" plain @click="onDelete">删除</el-button>
  16. </div>
  17. <template v-else>
  18. <div class="filter-control">
  19. <OssUpload
  20. ref="ossUpload"
  21. :limit="999"
  22. :delay-upload="delayUpload"
  23. :multiple="true"
  24. :button-group="buttonGroup"
  25. :on-success="onSuccess"
  26. :on-error="onError"
  27. @typeChange="onOssUploadTypeChange"
  28. />
  29. </div>
  30. <div class="filter-control">
  31. <el-button
  32. type="primary"
  33. size="small"
  34. icon="el-icon-folder-add"
  35. plain
  36. @click="onCreateDir"
  37. >新建文件夹</el-button>
  38. <el-button
  39. type="primary"
  40. size="small"
  41. icon="el-icon-s-promotion"
  42. plain
  43. @click="onPublishArticle"
  44. >发布文章</el-button>
  45. <el-button
  46. type="primary"
  47. size="small"
  48. icon="el-icon-alarm-clock"
  49. plain
  50. @click="onUploadHistory"
  51. >上传记录</el-button>
  52. </div>
  53. </template>
  54. </div>
  55. <!-- 面包屑 -->
  56. <el-breadcrumb separator-class="el-icon-arrow-right">
  57. <el-breadcrumb-item :to="{ path: '/docs/0/list' }">全部文件</el-breadcrumb-item>
  58. <template v-for="(item, index) in crumbList">
  59. <template v-if="index === crumbList.length - 1">
  60. <el-breadcrumb-item :key="item.id">
  61. <span class="cell">{{ item.fileName }}</span>
  62. </el-breadcrumb-item>
  63. </template>
  64. <template v-else>
  65. <el-breadcrumb-item :key="item.id" :to="{ path: `/docs/${item.id}/list` }">
  66. <span>{{ item.fileName | crumbFormat }}</span>
  67. </el-breadcrumb-item>
  68. </template>
  69. </template>
  70. </el-breadcrumb>
  71. <!-- 列表 -->
  72. <el-table
  73. :data="list"
  74. style="width: 100%"
  75. :row-style="{ cursor: 'pointer' }"
  76. @selection-change="onSelectionChange"
  77. @row-dblclick="onRowClick"
  78. >
  79. <el-table-column type="selection" align="center" width="50" />
  80. <el-table-column label="文件名">
  81. <template slot-scope="{ row }">
  82. <doc-icon v-if="!listLoading" :type="row.fileType" :src="row.screenshot" />
  83. <span v-if="row.fileType === 'article'" class="file-name" @click.stop="onRowClick(row)">{{
  84. row.fileName | fileNameFormat
  85. }}</span>
  86. <span v-else class="file-name" @click.stop="onRowClick(row)">{{ row.fileName }}</span>
  87. </template>
  88. </el-table-column>
  89. <el-table-column prop="createTime" label="时间" width="160">
  90. <template slot-scope="{ row }">{{ row.saveTime | formatTime }}</template>
  91. </el-table-column>
  92. <el-table-column label="大小" width="120">
  93. <template slot-scope="{ row }">
  94. <span v-if="row.packageType > 0">{{ row.fileSize | fileSize }}</span>
  95. <span v-else>-</span>
  96. </template>
  97. </el-table-column>
  98. </el-table>
  99. <!-- 创建文件夹弹窗 -->
  100. <el-dialog :title="editText" :visible.sync="editDialog" width="30%" :close-on-click-modal="false" @close="onCancel">
  101. <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="80">
  102. <el-form-item prop="fileName" label="名称:">
  103. <el-input v-model="formData.fileName" placeholder="请输入文件名" @keyup.enter.native="onSubmit" />
  104. </el-form-item>
  105. </el-form>
  106. <span slot="footer" class="dialog-footer">
  107. <el-button @click="editDialog = false">取 消</el-button>
  108. <el-button type="primary" @click="onSubmit">确 定</el-button>
  109. </span>
  110. </el-dialog>
  111. <!-- 移动文件 -->
  112. <el-dialog title="选择移动位置" :visible.sync="moveDialog" width="60%" :close-on-click-modal="false">
  113. <el-button icon="el-icon-arrow-left" :disabled="lastDisabled" size="mini" @click="currentIndex--" />
  114. <el-button icon="el-icon-arrow-right" :disabled="nextDisabled" size="mini" @click="currentIndex++" />
  115. <el-table
  116. :data="filterSameDir(moveCurrentDir.childList)"
  117. :row-style="{ cursor: 'pointer' }"
  118. style="width: 100%"
  119. :height="300"
  120. empty-text="该目录为空"
  121. @row-dblclick="onMoveRowClick"
  122. >
  123. <el-table-column label="文件名">
  124. <template slot-scope="{ row }">
  125. <doc-icon v-if="!listLoading" :type="row.fileType" />
  126. <span class="file-name" @click.stop="onMoveRowClick(row)">{{ row.fileName }}</span>
  127. </template>
  128. </el-table-column>
  129. </el-table>
  130. <span slot="footer" class="dialog-footer">
  131. <el-button @click="onMoveCancel">取 消</el-button>
  132. <el-button type="primary" @click="onSubmitMove">确 定</el-button>
  133. </span>
  134. </el-dialog>
  135. </div>
  136. </template>
  137. <script>
  138. import DocIcon from '@/views/components/DocIcon'
  139. import { OssUpload } from '@/components/OssUpload'
  140. import {
  141. createPackage,
  142. fetchDocsList,
  143. removePackage,
  144. saveFile,
  145. updatePackage,
  146. fetchPathCumbs,
  147. fetchDirTree,
  148. movePackage,
  149. saveFileWithDir
  150. } from '@/api/doc'
  151. import { downloadWithUrl } from '@/utils/tools'
  152. export default {
  153. name: 'DocsAllList',
  154. components: { DocIcon, OssUpload },
  155. filters: {
  156. fileSize(size) {
  157. size = Math.round(size / 1024)
  158. if (size < 1024) return size + 'KB'
  159. size = Math.round(size / 1024)
  160. if (size < 1024) return size + 'MB'
  161. return Math.round(size / 1024) + 'GB'
  162. },
  163. crumbFormat(name) {
  164. if (name.length < 12) return name
  165. return name.substring(0, 10) + '…'
  166. },
  167. fileNameFormat(name) {
  168. return name.replace(/(.+)(.txt$)/, (match, $1, $2) => {
  169. return $1
  170. })
  171. }
  172. },
  173. data() {
  174. const fileNameValidate = (rule, value, callback) => {
  175. if (/[\\\/\[\]\|^%&',;=?$\x22]+/g.test(value)) {
  176. callback(new Error("文件名不能包含|\/\\^%&',;=?$等特殊字符"))
  177. } else {
  178. callback()
  179. }
  180. }
  181. return {
  182. selectionList: [], // 选中列表
  183. list: [],
  184. fileId: 0,
  185. editDialog: false,
  186. moveDialog: false,
  187. listLoading: false,
  188. formData: {
  189. fileName: '',
  190. fileId: ''
  191. },
  192. rules: {
  193. fileName: [
  194. { required: true, message: '文件名不能为空', trigger: ['blur'] },
  195. { validator: fileNameValidate, trigger: ['blur'] }
  196. ]
  197. },
  198. editType: '',
  199. current: null,
  200. crumbList: [],
  201. currentDirStack: [],
  202. currentIndex: 0,
  203. message: null,
  204. buttonGroup: [
  205. {
  206. type: 'file',
  207. color: 'primary',
  208. text: '上传文件',
  209. size: 'small',
  210. plain: true,
  211. icon: 'el-icon-upload2'
  212. },
  213. {
  214. type: 'folder',
  215. color: 'primary',
  216. text: '上传文件夹',
  217. size: 'small',
  218. plain: true,
  219. icon: 'el-icon-upload2'
  220. }
  221. ],
  222. uploadType: 'record',
  223. delayUpload: false
  224. }
  225. },
  226. computed: {
  227. selectedCount() {
  228. return this.selectionList.length
  229. },
  230. editText() {
  231. const texts = {
  232. editType: '创建文件夹',
  233. editFile: '修改文件'
  234. }
  235. return texts[this.editType]
  236. },
  237. moveCurrentDir() {
  238. return this.currentDirStack[this.currentIndex] || []
  239. },
  240. lastDisabled() {
  241. return this.currentIndex === 0 || this.currentDirStack.length === 0
  242. },
  243. nextDisabled() {
  244. return this.currentIndex === this.currentDirStack.length - 1 || this.currentDirStack.length === 0
  245. }
  246. },
  247. created() {
  248. this.init()
  249. },
  250. activated() {
  251. this.init()
  252. },
  253. methods: {
  254. // 排除相同文件夹
  255. filterSameDir(dirList) {
  256. if (!dirList) dirList = []
  257. return dirList.filter((item) => {
  258. return !this.selectionList.find((file) => file.id === item.id)
  259. })
  260. },
  261. // 获取文件夹结构
  262. async fetchDirTree() {
  263. try {
  264. const res = await fetchDirTree()
  265. res.data.fileName = '根目录'
  266. this.currentDirStack.push(res.data)
  267. this.currentIndex = this.currentDirStack.length - 1
  268. } catch (error) {
  269. console.log(error)
  270. }
  271. },
  272. // 处理面包屑
  273. generateCrumb(node) {
  274. const list = []
  275. if (!node) return list
  276. function recursive(node) {
  277. list.push(node)
  278. if (node.childNode) {
  279. recursive(node.childNode)
  280. }
  281. }
  282. recursive(node)
  283. return list
  284. },
  285. // 页面初始化
  286. init() {
  287. this.fileId = parseInt(this.$route.params.fileId)
  288. if (!this.fileId && this.fileId !== 0) return
  289. this.fetchDocsList()
  290. this.fetchPathCumbs()
  291. },
  292. // 获取面包屑导航
  293. async fetchPathCumbs() {
  294. try {
  295. const res = await fetchPathCumbs({ fileId: this.fileId })
  296. this.crumbList = this.generateCrumb(res.data)
  297. } catch (error) {
  298. console.log(error)
  299. }
  300. },
  301. // 获取资料库列表
  302. async fetchDocsList() {
  303. try {
  304. this.listLoading = true
  305. const res = await fetchDocsList({ fileId: this.fileId })
  306. this.list = res.data || []
  307. this.listLoading = false
  308. } catch (error) {
  309. console.log(error)
  310. }
  311. },
  312. // 保存上传文件
  313. async saveUploadFile(data) {
  314. try {
  315. await saveFile(data)
  316. this.fetchDocsList()
  317. if (this.message) this.message.close()
  318. this.message = this.$message.success('文件上传成功')
  319. } catch (error) {
  320. console.log(error)
  321. }
  322. },
  323. // 保存上传文件夹
  324. async saveUploadDir(fileList) {
  325. try {
  326. await saveFileWithDir({ params: fileList })
  327. this.fetchDocsList()
  328. if (this.message) this.message.close()
  329. this.message = this.$message.success('文件上传成功')
  330. } catch (error) {
  331. console.log(error)
  332. }
  333. },
  334. // 取消保存
  335. onCancel() {
  336. this.formData.fileId = ''
  337. this.formData.fileName = ''
  338. this.$refs.ruleForm.clearValidate()
  339. },
  340. // 保存
  341. async onSubmit() {
  342. const actions = {
  343. createDir: this.onSaveDir,
  344. editFile: this.onUpdateFile
  345. }
  346. try {
  347. await this.$refs.ruleForm.validate()
  348. actions[this.editType]()
  349. } catch (error) {
  350. console.log(error)
  351. }
  352. },
  353. // 保存文件夹
  354. async onSaveDir() {
  355. try {
  356. this.formData.fileId = this.fileId
  357. await createPackage(this.formData)
  358. this.fetchDocsList()
  359. this.editDialog = false
  360. this.$message.success('文件夹创建成功')
  361. } catch (error) {
  362. console.log(error)
  363. }
  364. },
  365. // 修改文件名
  366. async onUpdateFile() {
  367. try {
  368. await updatePackage(this.formData)
  369. this.fetchDocsList()
  370. this.editDialog = false
  371. this.$message.success('文件名修改成功')
  372. } catch (error) {
  373. console.log(error)
  374. }
  375. },
  376. // 选择文件并上传
  377. onChooseFile() {
  378. this.$refs.ossUpload.chooseFile()
  379. },
  380. // 上传成功
  381. onSuccess(res, file, fileList) {
  382. if (this.uploadType === 'folder') {
  383. this.onUploadWithDir(fileList)
  384. } else {
  385. this.onUploadWithFile(file)
  386. }
  387. },
  388. formatFileData(file) {
  389. const strList = file.name.split('.')
  390. const ossName = file.uuid + '.' + strList[strList.length - 1]
  391. return {
  392. fileName: file.name,
  393. ossName: ossName,
  394. ossUrl: file.ossUrl,
  395. fileSize: file.size,
  396. parentId: this.fileId,
  397. mime: file.raw.type,
  398. filePath: file.raw.webkitRelativePath || ''
  399. }
  400. },
  401. // 文件上传
  402. onUploadWithFile(file) {
  403. this.saveUploadFile(this.formatFileData(file))
  404. },
  405. // 文件夹上传
  406. onUploadWithDir(fileList) {
  407. const uploadFiles = fileList.map((file) => this.formatFileData(file))
  408. console.log(uploadFiles)
  409. this.saveUploadDir(uploadFiles)
  410. },
  411. // 上传错误
  412. onError(error) {
  413. console.log(error)
  414. },
  415. // 上传方式修改
  416. onOssUploadTypeChange(type) {
  417. this.uploadType = type
  418. this.delayUpload = type === 'folder'
  419. },
  420. // 查看历史记录
  421. onUploadHistory() {
  422. this.$refs.ossUpload.showRecord()
  423. },
  424. // 创建文件夹
  425. onCreateDir() {
  426. this.editType = 'createDir'
  427. this.editDialog = true
  428. },
  429. // 发布文章
  430. onPublishArticle() {
  431. const url = `/docs/article-edit?parentId=${this.fileId}`
  432. this.$router.push(url)
  433. },
  434. // 编辑文件
  435. onEditFile() {
  436. this.current = this.selectionList[0]
  437. if (this.current.fileType === 'article') {
  438. // 文章
  439. const url = `/docs/article-edit?parentId=${this.current.parentId}&articleId=${this.current.articleId}`
  440. this.$router.push(url)
  441. } else {
  442. this.editType = 'editFile'
  443. this.editDialog = true
  444. this.formData.fileName = this.current.fileName
  445. this.formData.fileId = this.current.id
  446. }
  447. },
  448. // 移动文件
  449. onMoveFile() {
  450. this.currentDirStack = []
  451. this.currentIndex = 0
  452. this.fetchDirTree()
  453. this.moveDialog = true
  454. },
  455. // 移动取消
  456. onMoveCancel() {
  457. this.moveDialog = false
  458. this.fetchDocsList()
  459. },
  460. // 移动确认
  461. async onSubmitMove() {
  462. const current = this.currentDirStack[this.currentIndex]
  463. const filedIds = this.selectionList.map((file) => file.id).join(',')
  464. try {
  465. await movePackage({ fileId: filedIds, parentId: current.id })
  466. this.moveDialog = false
  467. this.fetchDocsList()
  468. this.$message.success('文件移动成功')
  469. } catch (error) {
  470. console.log(error)
  471. }
  472. },
  473. // 下载文件
  474. async onDownload() {
  475. const confirmText = '文件'
  476. let fileName = '文件'
  477. const filedIds = this.selectionList.map((file) => file.id).join(',')
  478. const text = await this.$confirm(`确认下载所选${confirmText}?`, '提示', {
  479. confirmButtonText: '确定',
  480. cancelButtonText: '取消',
  481. type: 'warning'
  482. }).catch(() => {
  483. this.exportClubList = []
  484. this.$message.info('已取消操作')
  485. })
  486. if (text !== 'confirm') return
  487. let notification = null
  488. notification = this.$notify({
  489. title: '提示',
  490. message: `正在下载${confirmText},请勿重复操作!`,
  491. duration: 0
  492. })
  493. // 下载链接
  494. let downUrl = ''
  495. if (this.selectionList.length === 1) {
  496. const target = this.selectionList[0]
  497. fileName = target.fileName
  498. if (target.packageType > 0) {
  499. downUrl = `${process.env.VUE_APP_BASE_API}/download/file?ossName=${
  500. target.ossName
  501. }&fileName=${encodeURIComponent(target.fileName)}`
  502. } else {
  503. downUrl = `${process.env.VUE_APP_BASE_API}/database/path/package/zip?fileId=${
  504. target.id
  505. }&fileName=${encodeURIComponent(target.fileName)}`
  506. }
  507. } else {
  508. // 使用a链接下载
  509. downUrl = `${process.env.VUE_APP_BASE_API}/database/path/chose/zip?fileId=${filedIds}`
  510. }
  511. downloadWithUrl(downUrl, fileName, {
  512. headers: {
  513. 'X-Token': this.$store.getters.token
  514. }
  515. })
  516. .then((res) => {
  517. this.fetchDocsList()
  518. })
  519. .catch((err) => {
  520. console.log(err)
  521. this.$message.error(`下载${confirmText}失败`)
  522. })
  523. .finally(() => {
  524. notification.close()
  525. })
  526. },
  527. // 删除文件
  528. async onDelete(file) {
  529. const tip = file.packageType > 0 ? '确定删除该文件夹及其文件夹内的全部文件?' : '确定删除该文件?'
  530. let confirm = ''
  531. try {
  532. confirm = await this.$confirm(tip, '提示', {
  533. confirmButtonText: '确定',
  534. cancelButtonText: '取消',
  535. type: 'warning'
  536. })
  537. } catch (error) {
  538. console.log(error)
  539. this.$message.info('已取消操作')
  540. }
  541. if (confirm !== 'confirm') return false
  542. // 获取要删除的文件id
  543. const filedIds = this.selectionList.map((file) => file.id).join(',')
  544. try {
  545. this.listLoading = true
  546. await removePackage({ fileId: filedIds })
  547. this.fetchDocsList()
  548. this.$message.success('删除成功')
  549. this.listLoading = false
  550. } catch (error) {
  551. console.log(error)
  552. }
  553. },
  554. // 表格内容勾选
  555. onSelectionChange(val) {
  556. this.selectionList = val
  557. },
  558. // 文件点击
  559. onRowClick(row) {
  560. if (row.fileType === 'article') {
  561. // 文章
  562. const url = `/docs/article-edit?parentId=${row.parentId}&articleId=${row.articleId}`
  563. this.$router.push(url)
  564. } else if (row.packageType === 1) {
  565. // 其它文件
  566. console.log('详情')
  567. this.$router.push(`/docs/detail?id=${row.id}`)
  568. } else {
  569. this.$router.push(`/docs/${row.id}/list`)
  570. this.$store.dispatch('tagsView/delView', this.$route)
  571. }
  572. },
  573. // 移动位置点击
  574. onMoveRowClick(row) {
  575. if (this.currentIndex === this.currentDirStack.length - 1) {
  576. this.currentDirStack.push(row)
  577. this.currentIndex = this.currentDirStack.length - 1
  578. } else {
  579. this.currentDirStack.splice(this.currentIndex + 1, this.currentDirStack.length)
  580. this.currentDirStack.push(row)
  581. this.currentIndex = this.currentDirStack.length - 1
  582. }
  583. }
  584. }
  585. }
  586. </script>
  587. <style scoped lang="scss">
  588. .file-name {
  589. font-size: 14px;
  590. color: #666666;
  591. margin-left: 25px;
  592. cursor: pointer;
  593. line-height: 32px;
  594. }
  595. .file-name:hover {
  596. color: #1890ff;
  597. }
  598. .el-breadcrumb {
  599. margin: 16px 0;
  600. .cell {
  601. color: #999;
  602. }
  603. }
  604. </style>