|
|
@@ -0,0 +1,136 @@
|
|
|
+import { serviceURL } from "@/services/request";
|
|
|
+import { message } from "ant-design-vue";
|
|
|
+import { ref } from "vue";
|
|
|
+import { FileInfo, FileItem } from "./interface";
|
|
|
+
|
|
|
+function getBase64(file: File) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ const reader = new FileReader();
|
|
|
+ reader.readAsDataURL(file);
|
|
|
+ reader.onload = () => resolve(reader.result);
|
|
|
+ reader.onerror = error => reject(error);
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+function getImg(imgURl: string) {
|
|
|
+ let result = '';
|
|
|
+ if (imgURl) {
|
|
|
+ result = serviceURL.uploadUrl.replace('/upload', '') + imgURl.replace('./', '/');
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+}
|
|
|
+export function handleUplodImg(limitSize = 1) {
|
|
|
+ // 上传地址
|
|
|
+ const uploadUrl = ref<string>('')
|
|
|
+ // 图片列表
|
|
|
+ const uploadImgList = ref<FileItem[]>([])
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取上传地址,记住此方法需要在初始化数据加载完成(initData)之后 调用
|
|
|
+ */
|
|
|
+ function getUploadUrl() {
|
|
|
+ uploadUrl.value = serviceURL.uploadUrl
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 上传图片变更
|
|
|
+ * @param param0
|
|
|
+ */
|
|
|
+ function uploadChange({ file, fileList }: FileInfo) {
|
|
|
+ let resFileList = [...fileList];
|
|
|
+ // 1. Limit the number of uploaded files
|
|
|
+ resFileList = resFileList.slice(-limitSize);
|
|
|
+
|
|
|
+ // 2. 处理上传异常
|
|
|
+ if (file.status !== 'uploading') {
|
|
|
+ if (file.xhr?.status !== 200) {
|
|
|
+ message.error(`服务器返回状态不为200: ${file.xhr?.statusText}`)
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ if (file.status === 'done') {
|
|
|
+ resFileList.forEach(el => {
|
|
|
+ if (el.response) {
|
|
|
+ el.name = el.response[0].fileName
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ uploadImgList.value = resFileList;
|
|
|
+ return Promise.resolve(uploadImgList.value)
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 获取图片的filePath
|
|
|
+ * @returns
|
|
|
+ */
|
|
|
+ function getUploadResultUrl(): string[] {
|
|
|
+ const result: string[] = []
|
|
|
+ uploadImgList.value.forEach(el => {
|
|
|
+ if (Array.isArray(el.response)) {
|
|
|
+ el.response.forEach(e => {
|
|
|
+ result.push(e.filePath)
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ console.warn('ant-design获取服务返回图片的数据发生变化,请在控制台查看')
|
|
|
+ console.log(el)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ return { uploadUrl, uploadImgList, uploadChange, getUploadUrl, getUploadResultUrl, getImg }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 预览图片
|
|
|
+ */
|
|
|
+export function handlePreviewImg() {
|
|
|
+ // 是否查看附件
|
|
|
+ const previewVisible = ref<boolean>(false)
|
|
|
+ // 预览图片地址
|
|
|
+ const previewImage = ref<string | undefined>('');
|
|
|
+ /**
|
|
|
+ * 关闭查看图片
|
|
|
+ */
|
|
|
+ function cancelImg() {
|
|
|
+ previewVisible.value = false;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 预览图片
|
|
|
+ */
|
|
|
+ async function previewImg(file: FileItem) {
|
|
|
+ if (!file.url && !file.preview) {
|
|
|
+ file.preview = (await getBase64(file.originFileObj)) as string;
|
|
|
+ }
|
|
|
+ previewImage.value = file.url || file.preview;
|
|
|
+ previewVisible.value = true;
|
|
|
+
|
|
|
+ }
|
|
|
+ return { previewVisible, previewImage, cancelImg, previewImg }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * 获取上传好的图片地址
|
|
|
+ * @returns
|
|
|
+ */
|
|
|
+export function getUploadImg() {
|
|
|
+ const imgs = ref<string[]>([])
|
|
|
+ const uploadImgList = ref<FileItem[]>([])
|
|
|
+ function uploadImgAction(value: string[]) {
|
|
|
+ imgs.value = value
|
|
|
+ }
|
|
|
+ function handleImg(value: string) {
|
|
|
+ const arr = value.split(',')
|
|
|
+ return arr.map((e, i) => {
|
|
|
+ const str = JSON.parse(e);
|
|
|
+ const lastIndex = str.lastIndexOf('/') + 1;
|
|
|
+ return {
|
|
|
+ uid: i.toString(),
|
|
|
+ name: str.slice(lastIndex, str.length),
|
|
|
+ url: getImg(str),
|
|
|
+ };
|
|
|
+ });
|
|
|
+
|
|
|
+ }
|
|
|
+ return { imgs, uploadImgAction, uploadImgList, handleImg }
|
|
|
+}
|