data.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import type { BasicColumn, ActionItem } from '/@/components/Table';
  2. import { FileItem, PreviewFileItem, UploadResultStatus } from './types';
  3. import { checkImgType, isImgTypeByName } from './helper';
  4. import { Progress, Tag } from 'ant-design-vue';
  5. import TableAction from '/@/components/Table/src/components/TableAction.vue';
  6. import ThumbUrl from './ThumbUrl.vue';
  7. import { useI18n } from '/@/hooks/web/useI18n';
  8. const { t } = useI18n();
  9. // 文件上传列表
  10. export function createTableColumns(): BasicColumn[] {
  11. return [
  12. {
  13. dataIndex: 'thumbUrl',
  14. title: t('component.upload.legend'),
  15. width: 100,
  16. customRender: ({ record }) => {
  17. const { thumbUrl } = (record as FileItem) || {};
  18. return thumbUrl && <ThumbUrl fileUrl={thumbUrl} />;
  19. },
  20. },
  21. {
  22. dataIndex: 'name',
  23. title: t('component.upload.fileName'),
  24. align: 'left',
  25. customRender: ({ text, record }) => {
  26. const { percent, status: uploadStatus } = (record as FileItem) || {};
  27. let status: 'normal' | 'exception' | 'active' | 'success' = 'normal';
  28. if (uploadStatus === UploadResultStatus.ERROR) {
  29. status = 'exception';
  30. } else if (uploadStatus === UploadResultStatus.UPLOADING) {
  31. status = 'active';
  32. } else if (uploadStatus === UploadResultStatus.SUCCESS) {
  33. status = 'success';
  34. }
  35. return (
  36. <span>
  37. <p class="ellipsis mb-1" title={text}>
  38. {text}
  39. </p>
  40. <Progress percent={percent} size="small" status={status} />
  41. </span>
  42. );
  43. },
  44. },
  45. {
  46. dataIndex: 'size',
  47. title: t('component.upload.fileSize'),
  48. width: 100,
  49. customRender: ({ text = 0 }) => {
  50. return text && (text / 1024).toFixed(2) + 'KB';
  51. },
  52. },
  53. // {
  54. // dataIndex: 'type',
  55. // title: '文件类型',
  56. // width: 100,
  57. // },
  58. {
  59. dataIndex: 'status',
  60. title: t('component.upload.fileStatue'),
  61. width: 100,
  62. customRender: ({ text }) => {
  63. if (text === UploadResultStatus.SUCCESS) {
  64. return <Tag color="green">{() => t('component.upload.uploadSuccess')}</Tag>;
  65. } else if (text === UploadResultStatus.ERROR) {
  66. return <Tag color="red">{() => t('component.upload.uploadError')}</Tag>;
  67. } else if (text === UploadResultStatus.UPLOADING) {
  68. return <Tag color="blue">{() => t('component.upload.uploading')}</Tag>;
  69. }
  70. return text;
  71. },
  72. },
  73. ];
  74. }
  75. export function createActionColumn(handleRemove: Function, handlePreview: Function): BasicColumn {
  76. return {
  77. width: 120,
  78. title: t('component.upload.operating'),
  79. dataIndex: 'action',
  80. fixed: false,
  81. customRender: ({ record }) => {
  82. const actions: ActionItem[] = [
  83. {
  84. label: t('component.upload.del'),
  85. color: 'error',
  86. onClick: handleRemove.bind(null, record),
  87. },
  88. ];
  89. if (checkImgType(record)) {
  90. actions.unshift({
  91. label: t('component.upload.preview'),
  92. onClick: handlePreview.bind(null, record),
  93. });
  94. }
  95. return <TableAction actions={actions} />;
  96. },
  97. };
  98. }
  99. // 文件预览列表
  100. export function createPreviewColumns(): BasicColumn[] {
  101. return [
  102. {
  103. dataIndex: 'url',
  104. title: t('component.upload.legend'),
  105. width: 100,
  106. customRender: ({ record }) => {
  107. const { url } = (record as PreviewFileItem) || {};
  108. return isImgTypeByName(url) && <ThumbUrl fileUrl={url} />;
  109. },
  110. },
  111. {
  112. dataIndex: 'name',
  113. title: t('component.upload.fileName'),
  114. align: 'left',
  115. },
  116. ];
  117. }
  118. export function createPreviewActionColumn({
  119. handleRemove,
  120. handlePreview,
  121. handleDownload,
  122. }: {
  123. handleRemove: Fn;
  124. handlePreview: Fn;
  125. handleDownload: Fn;
  126. }): BasicColumn {
  127. return {
  128. width: 160,
  129. title: t('component.upload.operating'),
  130. dataIndex: 'action',
  131. fixed: false,
  132. customRender: ({ record }) => {
  133. const { url } = (record || {}) as PreviewFileItem;
  134. const actions: ActionItem[] = [
  135. {
  136. label: t('component.upload.del'),
  137. color: 'error',
  138. onClick: handleRemove.bind(null, record),
  139. },
  140. {
  141. label: t('component.upload.download'),
  142. onClick: handleDownload.bind(null, record),
  143. },
  144. ];
  145. if (isImgTypeByName(url)) {
  146. actions.unshift({
  147. label: t('component.upload.preview'),
  148. onClick: handlePreview.bind(null, record),
  149. });
  150. }
  151. return <TableAction actions={actions} />;
  152. },
  153. };
  154. }