Explorar o código

上传路径及回调提取到配置文件

tongshangming hai 1 ano
pai
achega
9fbe8c7d18

+ 3 - 2
src/components/ImageUpload.vue

@@ -3,6 +3,7 @@ import { useUserStore } from '@/stores/user'
 import { ACCESS_TOKEN } from '@/utils/constants'
 import { ElMessage } from 'element-plus'
 import type { UploadProps } from 'element-plus'
+import config from '@/config/defaultSetting'
 
 interface Props {
   modelValue: any
@@ -23,7 +24,7 @@ const headers = reactive({
 // 图片上传
 const handleUploadSuccess: UploadProps['onSuccess'] = response => {
   if (response.success || response.code === 200) {
-    modelValue.value = import.meta.env.VITE_BASE_PATH + response.data
+    modelValue.value = config.uploadSuccessCb(response)
   } else {
     ElMessage.error(response.msg)
   }
@@ -34,7 +35,7 @@ const handleUploadSuccess: UploadProps['onSuccess'] = response => {
   <el-upload
     class="avatar-uploader"
     :on-success="handleUploadSuccess"
-    :action="baseApi + '/file/upload'"
+    :action="baseApi + config.uploadApi"
     :headers="headers"
     :show-file-list="false"
   >

+ 4 - 3
src/components/form/FormComp.vue

@@ -1,9 +1,10 @@
 <!-- eslint-disable vue/no-mutating-props -->
 <script setup lang="ts">
 import type { BasicFormItem } from '@/types/form'
-import type { UploadProps } from 'element-plus'
+// import type { UploadProps } from 'element-plus'
 import { useUserStore } from '@/stores/user'
 import { ACCESS_TOKEN } from '@/utils/constants'
+import config from '@/config/defaultSetting'
 
 interface Props {
   modelValue: any
@@ -39,7 +40,7 @@ const headers = reactive({
 })
 // 图片上传
 // const handleUploadSuccess: UploadProps['onSuccess'] = response => {
-//   modelValue.value = import.meta.env.VITE_BASE_PATH + response.data
+//   modelValue.value = config.uploadSuccessCb(response)
 // }
 </script>
 
@@ -71,7 +72,7 @@ const headers = reactive({
   <el-upload
     v-else-if="item.type === 'upload'"
     v-model:file-list="modelValue"
-    :action="baseApi + '/file/upload'"
+    :action="baseApi + config.uploadApi"
     :headers="headers"
     v-bind="item.props"
     v-on="item.events || {}"

+ 3 - 1
src/config/defaultSetting.ts

@@ -4,5 +4,7 @@ export default {
   homeRouteName: 'home',
   themeColor: '#1890ff',
   themeStyle: 'nav-light', // nav-dark  nav-light  header-dark
-  themeNav: 'left' // left | top
+  themeNav: 'left', // left | top
+  uploadApi: '/file/upload',
+  uploadSuccessCb: (res: any) => import.meta.env.VITE_BASE_PATH + res.data
 }