UploadImageFormItem.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { StringUtils } from "@imengyu/imengyu-utils";
  2. import { message, type UploadProps } from "ant-design-vue";
  3. export interface UploadImageFormItemProps {
  4. /**
  5. * 是否禁用
  6. */
  7. disabled?: boolean;
  8. /**
  9. * 上传工厂类
  10. */
  11. uploadCo: UploadCoInterface;
  12. /**
  13. * 上传之前的自定义检查回调
  14. * 如果返回false,将停止上传
  15. */
  16. beforeUpload?: (file: FileItem) => boolean;
  17. /**
  18. * 是否限制单图上传
  19. */
  20. single?: boolean;
  21. /**
  22. * single 为false时,限制最多上传图片的数量
  23. */
  24. maxCount?: number
  25. /**
  26. * 类样式
  27. */
  28. uploadClass?: unknown;
  29. /**
  30. * single 模式下图片显示大小
  31. */
  32. singleImageSize?: { width: number, height: number },
  33. /**
  34. * 参数,可以是单张 string,多张 string[]
  35. */
  36. value?: unknown;
  37. /**
  38. * a-upload 其他自定义参数
  39. */
  40. customProps?: UploadProps;
  41. }
  42. /**
  43. * 上传工厂接口
  44. */
  45. export interface UploadCoInterface {
  46. /**
  47. * 请求上传Token
  48. */
  49. requestUploadToken : (key: string, bucketNameDa: string, expire ?:number) => Promise<string>,
  50. /**
  51. * 上传主函数。由 ant-upload 调用。
  52. */
  53. uploadRequest: (requestOption: AntUploadRequestOption) => void,
  54. /**
  55. * 获取上传返回后的URL函数。
  56. */
  57. getUrlByUploadResponse: (response: unknown) => string,
  58. }
  59. export interface FileItem {
  60. uid: string;
  61. name?: string;
  62. status?: string;
  63. response?: string;
  64. url: string;
  65. type: string;
  66. size: number;
  67. originFileObj?: unknown;
  68. }
  69. export interface FileInfo {
  70. file: FileItem;
  71. fileList: FileItem[];
  72. }
  73. export interface AntUploadRequestOption {
  74. action: string|Promise<string>;
  75. filename: string;
  76. data : unknown;
  77. file: File;
  78. headers: { [index: string]: string; };
  79. withCredentials: boolean;
  80. method: string;
  81. onProgress: (e: number) => void;
  82. onSuccess: (ret : { url: string, key: string }, xhr : XMLHttpRequest|null) => void;
  83. onError: (err : Error|null|undefined, ret : unknown) => void;
  84. }
  85. /**
  86. * 上传图片大小检查组合代码。
  87. * @param limitSizeMB 限制大小MB.
  88. * @returns
  89. */
  90. export function useBeforeUploadImageChecker(limitSizeMB = 8) : (file: FileItem) => boolean {
  91. return (file) => {
  92. const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
  93. if (!isJpgOrPng)
  94. message.error('请选择图片文件!');
  95. const isLt2M = file.size / 1024 / 1024 < limitSizeMB;
  96. if (!isLt2M)
  97. message.error(`图片大小不能大于${limitSizeMB}MB!`);
  98. return isJpgOrPng && isLt2M;
  99. };
  100. }
  101. /**
  102. * 上传视频大小检查组合代码。
  103. * @param limitSizeMB 限制大小MB.
  104. * @returns
  105. */
  106. export function useBeforeUploadVideoChecker(limitSizeMB = 256) : (file: FileItem) => boolean {
  107. return (file) => {
  108. const isVideo = file.type.startsWith('video/');
  109. if (!isVideo)
  110. message.error('请选择视频文件!');
  111. const isLt2M = file.size / 1024 / 1024 < limitSizeMB;
  112. if (!isLt2M)
  113. message.error(`视频大小不能大于${limitSizeMB}MB!`);
  114. return isVideo && isLt2M;
  115. };
  116. }
  117. /**
  118. * 把字符串URL数组转为a-upload已上传的条目
  119. * @param arr URL数组
  120. */
  121. export function stringUrlsToUploadedItems(arr: string[]) : FileItem[] {
  122. return arr.map((i, k) => {
  123. return {
  124. uid: k.toString(),
  125. name: StringUtils.path.getFileName(i),
  126. status: 'done',
  127. url: i,
  128. size: 0,
  129. type: '',
  130. }
  131. });
  132. }