index.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import { serviceURL } from "@/services/request";
  2. import { message } from "ant-design-vue";
  3. import { ref } from "vue";
  4. import { FileInfo, FileItem } from "./interface";
  5. function getBase64(file: File) {
  6. return new Promise((resolve, reject) => {
  7. const reader = new FileReader();
  8. reader.readAsDataURL(file);
  9. reader.onload = () => resolve(reader.result);
  10. reader.onerror = error => reject(error);
  11. });
  12. }
  13. function getImg(imgURl: string) {
  14. let result = '';
  15. if (imgURl) {
  16. result = serviceURL.uploadUrl.replace('/upload', '') + imgURl.replace('./', '/');
  17. }
  18. return result;
  19. }
  20. function getImgName(value: string) {
  21. let result = '--'
  22. if (value) {
  23. const str = JSON.parse(value).replace(/\"/g, "");
  24. const lastIndex = str.lastIndexOf('/') + 1;
  25. result = str.slice(lastIndex, str.length)
  26. }
  27. return result
  28. }
  29. export function handleUplodImg(limitSize = 1) {
  30. // 上传地址
  31. const uploadUrl = ref<string>('')
  32. // 图片列表
  33. const uploadImgList = ref<FileItem[]>([])
  34. /**
  35. * 获取上传地址,记住此方法需要在初始化数据加载完成(initData)之后 调用
  36. */
  37. function getUploadUrl() {
  38. uploadUrl.value = serviceURL.uploadUrl
  39. }
  40. /**
  41. * 上传图片变更
  42. * @param param0
  43. */
  44. function uploadChange({ file, fileList }: FileInfo) {
  45. let resFileList = [...fileList];
  46. // 1. Limit the number of uploaded files
  47. resFileList = resFileList.slice(-limitSize);
  48. // 2. 处理上传异常
  49. if (file.status !== 'uploading') {
  50. if (file.xhr?.status !== 200) {
  51. message.error(`服务器返回状态不为200: ${file.xhr?.statusText}`)
  52. }
  53. }
  54. if (file.status === 'done') {
  55. resFileList.forEach(el => {
  56. if (el.response) {
  57. el.name = el.response[0].fileName
  58. }
  59. })
  60. }
  61. uploadImgList.value = resFileList;
  62. return Promise.resolve(uploadImgList.value)
  63. }
  64. /**
  65. * 获取图片的filePath
  66. * @returns
  67. */
  68. function getUploadResultUrl(): string[] {
  69. const result: string[] = []
  70. uploadImgList.value.forEach(el => {
  71. if (Array.isArray(el.response)) {
  72. el.response.forEach(e => {
  73. result.push(e.filePath)
  74. })
  75. } else {
  76. console.warn('ant-design获取服务返回图片的数据发生变化,请在控制台查看')
  77. console.log(el)
  78. }
  79. })
  80. return result;
  81. }
  82. return { uploadUrl, uploadImgList, uploadChange, getUploadUrl, getUploadResultUrl }
  83. }
  84. /**
  85. * 预览图片
  86. */
  87. export function handlePreviewImg() {
  88. // 是否查看附件
  89. const previewVisible = ref<boolean>(false)
  90. // 预览图片地址
  91. const previewImage = ref<string | undefined>('');
  92. /**
  93. * 关闭查看图片
  94. */
  95. function cancelImg() {
  96. previewVisible.value = false;
  97. }
  98. /**
  99. * 预览图片
  100. */
  101. async function previewImg(file: FileItem | string) {
  102. if (typeof file === 'string') {
  103. if (file && file !== '--') {
  104. const str = JSON.parse(file).replace(/\"/g, "")
  105. previewImage.value = getImg(str)
  106. previewVisible.value = true;
  107. }
  108. } else {
  109. if (!file.url && !file.preview) {
  110. file.preview = (await getBase64(file.originFileObj)) as string;
  111. }
  112. previewImage.value = file.url || file.preview;
  113. previewVisible.value = true;
  114. }
  115. }
  116. return { previewVisible, previewImage, cancelImg, previewImg, getImgName }
  117. }
  118. /**
  119. * 获取上传好的图片地址
  120. * @returns
  121. */
  122. export function getUploadImg() {
  123. // 存储上传的图片
  124. const imgs = ref<string[]>([])
  125. // 存储从服务获取的图片信息,用于展示
  126. const uploadImgList = ref<FileItem[]>([])
  127. // 上传图片
  128. function uploadImgAction(value: string[]) {
  129. imgs.value = value
  130. }
  131. // 用于处理从服务获取的图片
  132. function handleImg(value: string) {
  133. const arr = value.split(',')
  134. return arr.map((e, i) => {
  135. const str = JSON.parse(e);
  136. return {
  137. uid: i.toString(),
  138. name: getImgName(e),
  139. url: getImg(str),
  140. };
  141. });
  142. }
  143. // 获取上传的第一张图片
  144. function getFirstImg() {
  145. return imgs.value.length ? imgs.value[0] : ''
  146. }
  147. return { imgs, uploadImgAction, uploadImgList, handleImg, getFirstImg }
  148. }