data.tsx 4.7 KB

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