FileTable.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. <template>
  2. <div class="file-table-wrapper">
  3. <!-- 文件表格 -->
  4. <a-table
  5. class="file-table"
  6. :class="['file-type-' + fileType, routeName === 'Share' ? 'share' : '']"
  7. ref="multipleTableRef"
  8. :loading="loading"
  9. :data-source="fileList"
  10. :columns="columns"
  11. :row-key="(record) => record.userFileId"
  12. :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
  13. :pagination="false"
  14. :custom-row="customRow"
  15. @change="handleTableChange"
  16. >
  17. <template #bodyCell="{ column, record, index }">
  18. <template v-if="column.key === 'isDir'">
  19. <video
  20. style="width: 30px; max-height: 30px; cursor: pointer"
  21. v-if="$file.isVideoFile(record)"
  22. :src="$file.setFileImg(record)"
  23. ></video>
  24. <img
  25. :src="$file.setFileImg(record)"
  26. :title="`${record.isDir || fileType == '6' ? '' : '点击预览'}`"
  27. style="width: 30px; max-height: 30px; cursor: pointer"
  28. @click="$file.handleFileNameClick(record, index, sortedFileList)"
  29. v-else
  30. />
  31. </template>
  32. <template v-else-if="column.key === 'fileName'">
  33. <span @click="$file.handleFileNameClick(record, index, sortedFileList)">
  34. <span
  35. class="file-name"
  36. style="cursor: pointer"
  37. :title="`${record.isDir || fileType == '6' ? '' : '点击预览'}`"
  38. v-html="$file.getFileNameComplete(record, true)"
  39. ></span>
  40. <div class="file-info" v-if="screenWidth <= 768">
  41. {{ record.uploadTime }}
  42. <span class="file-size">
  43. {{ record.isDir === 0 ? $file.calculateFileSize(record.fileSize) : '' }}
  44. </span>
  45. </div>
  46. </span>
  47. </template>
  48. <template v-else-if="column.key === 'filePath'">
  49. <span
  50. style="cursor: pointer"
  51. title="点击跳转"
  52. @click="
  53. () => {
  54. // router.push({
  55. // query: { filePath: record.filePath, fileType: 0 }
  56. // })
  57. myResourceStore.changeQuery({ filePath: record.filePath, fileType: 0 })
  58. }
  59. "
  60. >{{ record.filePath }}</span
  61. >
  62. </template>
  63. <template v-else-if="column.key === 'extendName'">
  64. <span>{{ $file.getFileType(record) }}</span>
  65. </template>
  66. <template v-else-if="column.key === 'fileSize'">
  67. {{ record.isDir === 0 ? $file.calculateFileSize(record.fileSize) : '' }}
  68. </template>
  69. <template v-else-if="column.key === 'shareType'">
  70. {{ record.shareType === 1 ? '私密' : '公共' }}
  71. </template>
  72. <template v-else-if="column.key === 'endTime'">
  73. <div>
  74. <WarningOutlined v-if="$file.getFileShareStatus(record.endTime)" />
  75. <ClockCircleOutlined v-else />
  76. {{ record.endTime }}
  77. </div>
  78. </template>
  79. <template v-else-if="column.key === 'operation'">
  80. <MoreOutlined
  81. class="file-operate"
  82. :class="`operate-more-${index}`"
  83. @click="handleClickMore(record, $event)"
  84. />
  85. </template>
  86. </template>
  87. </a-table>
  88. </div>
  89. </template>
  90. <script setup>
  91. import { ref, computed, watch, onMounted } from 'vue'
  92. import { useRoute, useRouter } from 'vue-router'
  93. import { storeToRefs } from 'pinia'
  94. import { useMyResourceStore } from '@/store/myResource'
  95. import {
  96. WarningOutlined,
  97. ClockCircleOutlined,
  98. MoreOutlined,
  99. PlusCircleOutlined,
  100. MinusCircleOutlined
  101. } from '@ant-design/icons-vue'
  102. const props = defineProps({
  103. // 文件类型
  104. fileType: {
  105. required: true,
  106. type: Number
  107. },
  108. // 文件路径
  109. filePath: {
  110. required: true,
  111. type: String
  112. },
  113. // 文件列表
  114. fileList: {
  115. required: true,
  116. type: Array
  117. },
  118. // 文件加载状态
  119. loading: {
  120. required: true,
  121. type: Boolean
  122. }
  123. })
  124. const emit = defineEmits(['getTableDataByType'])
  125. const route = useRoute()
  126. const router = useRouter()
  127. const myResourceStore = useMyResourceStore()
  128. const { proxy } = getCurrentInstance()
  129. const multipleTableRef = ref(null)
  130. const officeFileType = ref(['ppt', 'pptx', 'doc', 'docx', 'xls', 'xlsx'])
  131. const sortedFileList = ref([]) // 排序后的表格数据
  132. const selectedRowKeys = ref([])
  133. const { getSelectedColumnList } = storeToRefs(myResourceStore)
  134. const selectedColumnList = computed({
  135. get: () => getSelectedColumnList.value
  136. })
  137. // 路由名称
  138. const routeName = computed(() => route.name)
  139. // 屏幕宽度
  140. const screenWidth = computed(() => myResourceStore.screenWidth)
  141. // 定义表格列
  142. const columns = computed(() => {
  143. const cols = [
  144. {
  145. dataIndex: 'isDir',
  146. key: 'isDir',
  147. width: screenWidth.value <= 768 ? 40 : 56,
  148. align: 'center',
  149. className: 'file-icon-column'
  150. },
  151. {
  152. title: '文件名',
  153. dataIndex: 'fileName',
  154. key: 'fileName',
  155. sorter: (a, b) => {
  156. if (a.isDir !== b.isDir) return b.isDir - a.isDir
  157. return a.fileName.localeCompare(b.fileName)
  158. },
  159. showOverflowTooltip: true
  160. }
  161. ]
  162. console.log(
  163. 'selectedColumnList: ',
  164. selectedColumnList.value,
  165. ' screenWidth ',
  166. screenWidth.value,
  167. ' screenWidth.value > 768 ',
  168. screenWidth.value > 768
  169. )
  170. // if (![0, 8].includes(Number(route.query.fileType)) && routeName.value !== 'Share' && screenWidth.value > 768) {
  171. if (
  172. ![0, 8].includes(Number(myResourceStore.getQuery.fileType ? myResourceStore.getQuery.fileType : 0)) &&
  173. routeName.value !== 'Share' &&
  174. screenWidth.value > 768
  175. ) {
  176. cols.push({
  177. title: props.fileType === 6 ? '原路径' : '路径',
  178. dataIndex: 'filePath',
  179. key: 'filePath',
  180. showOverflowTooltip: true
  181. })
  182. }
  183. if (selectedColumnList.value.includes('extendName') && screenWidth.value > 768) {
  184. cols.push({
  185. title: '类型',
  186. dataIndex: 'extendName',
  187. key: 'extendName',
  188. width: 80,
  189. sorter: (a, b) => {
  190. if (a.isDir !== b.isDir) return b.isDir - a.isDir
  191. return a.extendName.localeCompare(b.extendName)
  192. },
  193. showOverflowTooltip: true
  194. })
  195. }
  196. if (selectedColumnList.value.includes('fileSize') && screenWidth.value > 768) {
  197. cols.push({
  198. title: '大小',
  199. dataIndex: 'fileSize',
  200. key: 'fileSize',
  201. width: 100,
  202. align: 'right',
  203. sorter: (a, b) => {
  204. if (a.isDir !== b.isDir) return b.isDir - a.isDir
  205. return a.fileSize - b.fileSize
  206. }
  207. })
  208. }
  209. if (
  210. selectedColumnList.value.includes('uploadTime') &&
  211. ![7, 8].includes(props.fileType) &&
  212. !['Share'].includes(routeName.value) &&
  213. screenWidth.value > 768
  214. ) {
  215. cols.push({
  216. title: '修改日期',
  217. dataIndex: 'uploadTime',
  218. key: 'uploadTime',
  219. width: 160,
  220. align: 'center',
  221. sorter: (a, b) => {
  222. if (a.isDir !== b.isDir) return b.isDir - a.isDir
  223. return new Date(a.uploadTime).getTime() - new Date(b.uploadTime).getTime()
  224. }
  225. })
  226. }
  227. if (props.fileType === 6 && selectedColumnList.value.includes('deleteTime') && screenWidth.value > 768) {
  228. cols.push({
  229. title: '删除日期',
  230. dataIndex: 'deleteTime',
  231. key: 'deleteTime',
  232. width: 160,
  233. align: 'center',
  234. sorter: (a, b) => {
  235. if (a.isDir !== b.isDir) return b.isDir - a.isDir
  236. return new Date(a.deleteTime).getTime() - new Date(b.deleteTime).getTime()
  237. }
  238. })
  239. }
  240. if (props.fileType === 8 && screenWidth.value > 768) {
  241. cols.push({
  242. title: '分享类型',
  243. dataIndex: 'shareType',
  244. key: 'shareType',
  245. width: 100,
  246. align: 'center'
  247. })
  248. cols.push({
  249. title: '分享时间',
  250. dataIndex: 'shareTime',
  251. key: 'shareTime',
  252. width: 160,
  253. showOverflowTooltip: true,
  254. align: 'center',
  255. sorter: (a, b) => {
  256. if (a.isDir !== b.isDir) return b.isDir - a.isDir
  257. return new Date(a.shareTime).getTime() - new Date(b.shareTime).getTime()
  258. }
  259. })
  260. cols.push({
  261. title: '过期时间',
  262. dataIndex: 'endTime',
  263. key: 'endTime',
  264. width: 190,
  265. showOverflowTooltip: true,
  266. align: 'center',
  267. sorter: (a, b) => {
  268. if (a.isDir !== b.isDir) return b.isDir - a.isDir
  269. return new Date(a.endTime).getTime() - new Date(b.endTime).getTime()
  270. }
  271. })
  272. }
  273. if (screenWidth.value <= 768) {
  274. cols.push({
  275. title: '',
  276. dataIndex: 'operation',
  277. key: 'operation',
  278. width: 48
  279. })
  280. }
  281. return cols
  282. })
  283. watch(
  284. () => props.filePath,
  285. () => {
  286. clearSelectedTable()
  287. // Ant Design Vue Table 没有 clearSort 方法,需要手动处理排序状态
  288. // multipleTableRef.value.clearSort();
  289. }
  290. )
  291. watch(
  292. () => props.fileType,
  293. () => {
  294. clearSelectedTable()
  295. // multipleTableRef.value.clearSort();
  296. }
  297. )
  298. watch(
  299. () => props.fileList,
  300. () => {
  301. clearSelectedTable()
  302. // multipleTableRef.value.clearSort();
  303. sortedFileList.value = props.fileList
  304. }
  305. )
  306. // 当表格的排序条件发生变化的时候会触发该事件
  307. const handleTableChange = (pagination, filters, sorter) => {
  308. // Ant Design Vue 的 sorter 返回的是当前排序的列信息,需要根据实际情况更新 sortedFileList
  309. // 这里简化处理,如果需要前端排序,可以根据 sorter.field 和 sorter.order 对 fileList 进行排序
  310. sortedFileList.value = multipleTableRef.value.tableData
  311. }
  312. // 表格某一行右键事件
  313. const customRow = (record, index) => {
  314. return {
  315. onContextmenu: (event) => {
  316. // 阻止右键事件冒泡
  317. event.cancelBubble = true
  318. // xs 以上的屏幕
  319. if (screenWidth.value > 768) {
  320. event.preventDefault()
  321. // Ant Design Vue Table 没有 setCurrentRow 方法,需要手动处理选中状态
  322. // multipleTableRef.value.setCurrentRow(record);
  323. proxy.$openBox
  324. .contextMenu({
  325. selectedFile: record,
  326. domEvent: event,
  327. serviceEl: proxy
  328. })
  329. .then((res) => {
  330. // multipleTableRef.value.setCurrentRow(); // 取消当前选中行
  331. if (res === 'confirm') {
  332. emit('getTableDataByType') // 刷新文件列表
  333. myResourceStore.showStorage() // 刷新存储容量
  334. }
  335. })
  336. }
  337. }
  338. }
  339. }
  340. // 清空表格已选行
  341. const clearSelectedTable = () => {
  342. selectedRowKeys.value = []
  343. myResourceStore.changeSelectedFiles([])
  344. myResourceStore.changeIsBatchOperation(false)
  345. }
  346. const onSelectChange = (changableRowKeys) => {
  347. console.log('selectedRowKeys changed: ', changableRowKeys)
  348. selectedRowKeys.value = changableRowKeys
  349. const list = changableRowKeys.map((id) => props.fileList.find((file) => file.userFileId === id)).filter(Boolean)
  350. console.log('list', list)
  351. myResourceStore.changeSelectedFiles(list)
  352. myResourceStore.changeIsBatchOperation(changableRowKeys.length !== 0)
  353. }
  354. // 更多图标点击事件
  355. const handleClickMore = (record, event) => {
  356. // multipleTableRef.value.setCurrentRow(record); // 选中当前行
  357. proxy.$openBox
  358. .contextMenu({
  359. selectedFile: record,
  360. domEvent: event
  361. })
  362. .then((res) => {
  363. // multipleTableRef.value.setCurrentRow(); // 取消当前选中行
  364. if (res === 'confirm') {
  365. emit('getTableDataByType') // 刷新文件列表
  366. myResourceStore.showStorage() // 刷新存储容量
  367. }
  368. })
  369. }
  370. // 暴露方法给父组件
  371. defineExpose({
  372. clearSelectedTable
  373. })
  374. </script>
  375. <style lang="less" scoped>
  376. @import '@/style/myResource/varibles.less';
  377. @import '@/style/myResource/mixins.less';
  378. .file-table-wrapper {
  379. margin-top: 2px;
  380. .file-type-0 {
  381. height: calc(100vh - 206px) !important;
  382. :deep(.ant-table-body) {
  383. height: calc(100vh - 262px) !important;
  384. }
  385. }
  386. .file-type-6 {
  387. height: calc(100vh - 211px) !important;
  388. :deep(.ant-table-body) {
  389. height: calc(100vh - 263px) !important;
  390. }
  391. }
  392. .file-table.share {
  393. height: calc(100vh - 109px) !important;
  394. :deep(.ant-table-body) {
  395. height: calc(100vh - 161px) !important;
  396. }
  397. }
  398. .file-table {
  399. width: 100% !important;
  400. height: calc(100vh - 203px);
  401. :deep(.ant-table-thead) {
  402. th {
  403. // background: @tabBackColor; // 需要在 varibles.less 中定义
  404. padding: 4px 0;
  405. color: @RegularText; // 需要在 varibles.less 中定义
  406. }
  407. .anticon-plus-circle,
  408. .anticon-minus-circle {
  409. margin-left: 6px;
  410. cursor: pointer;
  411. font-size: 16px;
  412. &:hover {
  413. color: @Primary; // 需要在 varibles.less 中定义
  414. }
  415. }
  416. }
  417. :deep(.ant-table-body) {
  418. height: calc(100vh - 255px);
  419. overflow-y: auto;
  420. // setScrollbar(6px, transparent, #C0C4CC); // 需要在 mixins.less 中定义
  421. td {
  422. padding: 8px 0;
  423. .file-name {
  424. .keyword {
  425. color: @Danger; // 需要在 varibles.less 中定义
  426. }
  427. }
  428. }
  429. .anticon-warning {
  430. font-size: 16px;
  431. color: @Warning; // 需要在 varibles.less 中定义
  432. }
  433. .anticon-clock-circle {
  434. font-size: 16px;
  435. color: @Success; // 需要在 varibles.less 中定义
  436. }
  437. }
  438. }
  439. }
  440. .right-menu-list {
  441. position: fixed;
  442. display: flex;
  443. flex-direction: column;
  444. background: #fff;
  445. border: 1px solid @BorderLighter; // 需要在 varibles.less 中定义
  446. border-radius: 4px;
  447. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  448. z-index: 2;
  449. padding: 4px 0;
  450. color: @RegularText; // 需要在 varibles.less 中定义
  451. .right-menu-item,
  452. .unzip-item {
  453. padding: 0 16px;
  454. height: 36px;
  455. line-height: 36px;
  456. cursor: pointer;
  457. &:hover {
  458. background: @PrimaryHover; // 需要在 varibles.less 中定义
  459. color: @Primary; // 需要在 varibles.less 中定义
  460. }
  461. i {
  462. margin-right: 8px;
  463. }
  464. }
  465. .unzip-menu-item {
  466. position: relative;
  467. &:hover {
  468. .unzip-list {
  469. display: block;
  470. }
  471. }
  472. .unzip-list {
  473. position: absolute;
  474. display: none;
  475. .unzip-item {
  476. width: 200px;
  477. // setEllipsis(1); // 需要在 mixins.less 中定义
  478. }
  479. }
  480. }
  481. }
  482. .right-menu-list,
  483. .unzip-list {
  484. background: #fff;
  485. border: 1px solid @BorderLighter; // 需要在 varibles.less 中定义
  486. border-radius: 4px;
  487. box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  488. z-index: 2;
  489. padding: 4px 0;
  490. color: @RegularText; // 需要在 varibles.less 中定义
  491. }
  492. </style>