Skip to content

Commit

Permalink
refactor(upload): 抽离上传本地服务器方法到utils里,可被单独调用 (#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
kanyxmo authored Nov 26, 2024
1 parent 1b73f61 commit b323488
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 46 deletions.
26 changes: 3 additions & 23 deletions web/src/components/ma-upload-file/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ zh_TW:

<script setup lang="tsx">
import { useLocalTrans } from '@/hooks/useLocalTrans.ts'
import type { UploadRequestOptions, UploadUserFile } from 'element-plus'
import type { UploadUserFile } from 'element-plus'
import { isArray, uid } from 'radash'
import { useMessage } from '@/hooks/useMessage.ts'
import { uploadLocal } from '@/utils/uploadLocal.ts'

defineOptions({ name: 'MaUploadFile' })

Expand Down Expand Up @@ -50,27 +51,6 @@ const t = useLocalTrans()

const fileList = ref<UploadUserFile[]>([])

function upload(formData: FormData) {
return useHttp().post('/admin/attachment/upload', formData)
}

function handleUpload(options: UploadRequestOptions): any {
return new Promise((resolve, reject) => {
const formData = new FormData()
formData.append('file', options.file)
upload(formData).then((res: Record<string, any>) => {
if (res.code === 200) {
resolve(res)
}
else {
reject(res)
}
}).catch((err) => {
reject(err)
})
})
}

function updateModelValue() {
emit(
'update:modelValue',
Expand Down Expand Up @@ -156,7 +136,7 @@ watch(
<el-upload
v-model:file-list="fileList"
:before-upload="beforeUpload"
:http-request="handleUpload"
:http-request="uploadLocal"
:on-success="handleSuccess"
:on-exceed="handleExceed"
:on-error="handleError"
Expand Down
26 changes: 3 additions & 23 deletions web/src/components/ma-upload-image/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ zh_TW:

<script setup lang="tsx">
import { useLocalTrans } from '@/hooks/useLocalTrans.ts'
import type { UploadRequestOptions, UploadUserFile } from 'element-plus'
import type { UploadUserFile } from 'element-plus'
import { isArray, uid } from 'radash'
import { useDebounceFn } from '@vueuse/core'
import { useMessage } from '@/hooks/useMessage.ts'
import { uploadLocal } from '@/utils/uploadLocal.ts'

defineOptions({ name: 'MaUploadImage' })

Expand Down Expand Up @@ -90,27 +91,6 @@ function btnRender() {

const fileList = ref<UploadUserFile[]>([])

function upload(formData: FormData) {
return useHttp().post('/admin/attachment/upload', formData)
}

function handleUpload(options: UploadRequestOptions): any {
return new Promise((resolve, reject) => {
const formData = new FormData()
formData.append('file', options.file)
upload(formData).then((res: Record<string, any>) => {
if (res.code === 200) {
resolve(res)
}
else {
reject(res)
}
}).catch((err) => {
reject(err)
})
})
}

function updateModelValue() {
emit(
'update:modelValue',
Expand Down Expand Up @@ -202,7 +182,7 @@ watch(
v-model:file-list="fileList"
:class="`ma-upload-${id}`"
:before-upload="beforeUpload"
:http-request="handleUpload"
:http-request="uploadLocal"
:on-success="handleSuccess"
:on-exceed="handleExceed"
:on-error="handleError"
Expand Down
26 changes: 26 additions & 0 deletions web/src/utils/uploadLocal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type {UploadRequestOptions} from "element-plus";

/**
* MineAdmin is committed to providing solutions for quickly building web applications
* Please view the LICENSE file that was distributed with this source code,
* For the full copyright and license information.
* Thank you very much for using MineAdmin.
*
* @Author X.Mo<[email protected]>
* @Link https://github.com/mineadmin
*/
export function uploadLocal(options: UploadRequestOptions, url?: string = '/admin/attachment/upload', key?: string = 'file') {
const upload = (formData: FormData) => {
return useHttp().post(url, formData)
}

return new Promise((resolve, reject) => {
const formData = new FormData()
formData.append(key, options.file)
upload(formData).then((res: Record<string, any>) => {
res.code === 200 ? resolve(res) : reject(res)
}).catch((err) => {
reject(err)
})
})
}

0 comments on commit b323488

Please sign in to comment.