Browse Source

调整上传文件接口

王飞 1 year ago
parent
commit
9d2507021b

+ 1 - 0
package.json

@@ -32,6 +32,7 @@
     "@wangeditor/editor": "^5.1.23",
     "@wangeditor/editor-for-vue": "^5.1.10",
     "@zxcvbn-ts/core": "^3.0.3",
+    "ali-oss": "^6.18.1",
     "animate.css": "^4.1.1",
     "axios": "^1.4.0",
     "clipboard": "^2.0.11",

+ 24 - 3
src/api/common/index.ts

@@ -1,5 +1,5 @@
 import request from '@/config/axios'
-
+import OSS from 'ali-oss'
 // 获取所有字典
 export const getDictApi = () => {
   return request.get({ url: '/dict/list' })
@@ -10,6 +10,27 @@ export const getDictOneApi = async () => {
   return request.get({ url: '/dict/one' })
 }
 
-export const uploadFile = async (data: any) => {
-  return request.post({ url: '/api/sysApk/uploadFile', headersType: 'multipart/form-data', data })
+export const uploadFile = async (file: any) => {
+  const res = (await getParam()) as any
+  const resData = res.data as any
+  console.log(file)
+  if (file.size > 1024 * 1000) {
+    const client = new OSS({
+      region: 'oss-cn-nanjing', // OSS地域节点
+      accessKeyId: resData.keyId, // 访问密钥ID
+      accessKeySecret: resData.keySecret, // 访问密钥Secret
+      bucket: resData.bucketName // 存储空间名称
+    })
+    return client.put(resData.uploadPath + '/' + file.name, file)
+  } else {
+    return request.post({
+      url: '/api/sysApk/uploadFile',
+      headersType: 'multipart/form-data',
+      data: file
+    })
+  }
+}
+
+export const getParam = async () => {
+  return request.post({ url: '/api/sysApk/getParam' })
 }

+ 19 - 3
src/views/Manage/Company/CompanyPage.vue

@@ -218,12 +218,28 @@ const crudSchemas: CrudSchema[] = [
       component: 'Upload',
       componentProps: {
         httpRequest: (data: any) => {
+          // uploadFile(formData).then((response) => {
+          //   wechatImg.value = response.data.virtualPath
+          //   currentRow.value.wechatImg = response.data.virtualPath
+          // })
           let file = data.file
           let formData = new FormData()
           formData.append('file', file)
-          uploadFile(formData).then((response) => {
-            wechatImg.value = response.data.virtualPath
-            currentRow.value.wechatImg = response.data.virtualPath
+          uploadFile(file.size <= 1024 * 1000 ? formData : file).then((response) => {
+            console.log(response)
+            let resUrl = ''
+            if (file.size <= 1024 * 1000) {
+              resUrl = response.data.virtualPath
+            } else {
+              resUrl = response.url
+            }
+            wechatImg.value = resUrl
+            currentRow.value.wechatImg = resUrl
+            const write = unref(writeRef)
+            write?.setValues({
+              bannerUrl: resUrl
+            })
+            delLoading.value = false
           })
         },
         limit: 1,

+ 22 - 2
src/views/Manage/File/FilePage.vue

@@ -144,17 +144,37 @@ const crudSchemas: CrudSchema[] = [
       component: 'Upload',
       componentProps: {
         httpRequest: (data: any) => {
+
           delLoading.value = true
           let file = data.file
           let formData = new FormData()
           formData.append('file', file)
-          uploadFile(formData).then((response) => {
+          uploadFile(file.size <= 1024 * 1000 ? formData : file).then((response) => {
+            console.log(response)
+            let resUrl = ''
+            if (file.size <= 1024 * 1000) {
+              resUrl = response.data.virtualPath
+            } else {
+              resUrl = response.url
+            }
             currentRow.value = {
               ...currentRow.value,
-              ...response.data
+              virtualPath: resUrl,
+              fileName: file.name
             }
+            const write = unref(writeRef)
+            write?.setValues({
+              bannerUrl: resUrl
+            })
             delLoading.value = false
           })
+          // uploadFile(formData).then((response) => {
+          //   currentRow.value = {
+          //     ...currentRow.value,
+          //     ...response.data
+          //   }
+          //   delLoading.value = false
+          // })
         },
         class: 'filePageUploader',
         fileList: currentRow.value.virtualPath

+ 14 - 4
src/views/Manage/Img/ImgPage.vue

@@ -86,6 +86,7 @@ getDict({
 const { t } = useI18n()
 const appStore = usePageStore()
 const dialogVisible = ref(false)
+
 // const fileList = ref([])
 const crudSchemas: CrudSchema[] = [
   {
@@ -135,11 +136,20 @@ const crudSchemas: CrudSchema[] = [
           let file = data.file
           let formData = new FormData()
           formData.append('file', file)
-          uploadFile(formData).then((response) => {
-            currentRow.value.bannerUrl = response.data.virtualPath
+          uploadFile(file.size <= 1024 * 1000 ? formData : file).then((response) => {
+            console.log(file)
+            console.log(response)
+            let resUrl = ''
+            if (file.size <= 1024 * 1000) {
+              resUrl = response.data.virtualPath
+            } else {
+              resUrl = response.url
+            }
+            console.log(resUrl)
+            currentRow.value.bannerUrl = resUrl
             const write = unref(writeRef)
             write?.setValues({
-              bannerUrl: response.data.virtualPath
+              bannerUrl: resUrl
             })
             delLoading.value = false
           })
@@ -156,7 +166,7 @@ const crudSchemas: CrudSchema[] = [
           token: getStorage('token')
         },
         onSuccess: (response) => {
-          currentRow.value.bannerUrl = response.data.virtualPath
+          // currentRow.value.bannerUrl = response.url
         },
         slots: {
           default: () => <ElButton type="primary">上传文件</ElButton>

+ 16 - 3
src/views/Manage/News/components/Write.vue

@@ -110,12 +110,25 @@ const schema = reactive<FormSchema[]>([
         let file = data.file
         let formData = new FormData()
         formData.append('file', file)
-        uploadFile(formData).then((response) => {
+        uploadFile(file.size <= 1024 * 1000 ? formData : file).then((response) => {
+          console.log(response)
+          let resUrl = ''
+          if (file.size <= 1024 * 1000) {
+            resUrl = response.data.virtualPath
+          } else {
+            resUrl = response.url
+          }
           setValues({
-            articlePic: response.data.virtualPath
+            articlePic: resUrl
           })
-          articlePic.value = response.data.virtualPath
+          articlePic.value = response.data.resUrl
         })
+        // uploadFile(formData).then((response) => {
+        //   setValues({
+        //     articlePic: response.data.virtualPath
+        //   })
+        //   articlePic.value = response.data.virtualPath
+        // })
       },
       showFileList: false,
       class: 'NewsUploader',

+ 16 - 3
src/views/Manage/Product/components/Write.vue

@@ -87,12 +87,25 @@ const schema = reactive<FormSchema[]>([
         let file = data.file
         let formData = new FormData()
         formData.append('file', file)
-        uploadFile(formData).then((response) => {
+        uploadFile(file.size <= 1024 * 1000 ? formData : file).then((response) => {
+          console.log(response)
+          let resUrl = ''
+          if (file.size <= 1024 * 1000) {
+            resUrl = response.data.virtualPath
+          } else {
+            resUrl = response.url
+          }
           setValues({
-            productPic: response.data.virtualPath
+            productPic: resUrl
           })
-          productPic.value = response.data.virtualPath
+          productPic.value = response.data.resUrl
         })
+        // uploadFile(formData).then((response) => {
+        //   setValues({
+        //     productPic: response.data.virtualPath
+        //   })
+        //   productPic.value = response.data.virtualPath
+        // })
       },
       showFileList: false,
       class: 'productUploader',

+ 6 - 1
vite.config.ts

@@ -121,7 +121,12 @@ export default ({ command, mode }: ConfigEnv): UserConfig => {
       proxy: {
         // 选项写法
         '^/api': {
-          target: 'http://api.dacundianzi.com/',
+          target: 'http://api.dacundianzi.com/',  // http://api.dacundianzi.com/
+          changeOrigin: true,
+          rewrite: path => path
+        },
+        'http://xn-back.oss-cn-nanjing.aliyuncs.com/back': {
+          target: 'http://xn-back.oss-cn-nanjing.aliyuncs.com/back',  // http://api.dacundianzi.com/
           changeOrigin: true,
           rewrite: path => path
         }