refine: upload 还是返回一个url吧
This commit is contained in:
parent
5b16ebe1fe
commit
ef83c31b8b
@ -6,7 +6,7 @@ const VITE_UPLOAD_BASEURL = import.meta.env.VITE_UPLOAD_BASEURL
|
|||||||
* @param formData 额外传递给后台的数据,如{name: '菲鸽'}。
|
* @param formData 额外传递给后台的数据,如{name: '菲鸽'}。
|
||||||
* @returns 返回一个对象{loading, error, data, run},包含请求的加载状态、错误信息、响应数据和手动触发请求的函数。
|
* @returns 返回一个对象{loading, error, data, run},包含请求的加载状态、错误信息、响应数据和手动触发请求的函数。
|
||||||
*/
|
*/
|
||||||
export default function useUpload<T>(formData: Record<string, any> = {}) {
|
export default function useUpload<T = string>(formData: Record<string, any> = {}) {
|
||||||
const loading = ref(false)
|
const loading = ref(false)
|
||||||
const error = ref(false)
|
const error = ref(false)
|
||||||
const data = ref<T>()
|
const data = ref<T>()
|
||||||
@ -18,7 +18,6 @@ export default function useUpload<T>(formData: Record<string, any> = {}) {
|
|||||||
count: 1,
|
count: 1,
|
||||||
mediaType: ['image'],
|
mediaType: ['image'],
|
||||||
success: (res) => {
|
success: (res) => {
|
||||||
console.log(res)
|
|
||||||
loading.value = true
|
loading.value = true
|
||||||
const tempFilePath = res.tempFiles[0].tempFilePath
|
const tempFilePath = res.tempFiles[0].tempFilePath
|
||||||
uploadFile<T>({ tempFilePath, formData, data, error, loading })
|
uploadFile<T>({ tempFilePath, formData, data, error, loading })
|
||||||
@ -33,7 +32,6 @@ export default function useUpload<T>(formData: Record<string, any> = {}) {
|
|||||||
uni.chooseImage({
|
uni.chooseImage({
|
||||||
count: 1,
|
count: 1,
|
||||||
success: (res) => {
|
success: (res) => {
|
||||||
console.log(res)
|
|
||||||
loading.value = true
|
loading.value = true
|
||||||
const tempFilePath = res.tempFilePaths[0]
|
const tempFilePath = res.tempFilePaths[0]
|
||||||
uploadFile<T>({ tempFilePath, formData, data, error, loading })
|
uploadFile<T>({ tempFilePath, formData, data, error, loading })
|
||||||
@ -56,8 +54,7 @@ function uploadFile<T>({ tempFilePath, formData, data, error, loading }) {
|
|||||||
name: 'file',
|
name: 'file',
|
||||||
formData,
|
formData,
|
||||||
success: (uploadFileRes) => {
|
success: (uploadFileRes) => {
|
||||||
console.log(uploadFileRes, typeof uploadFileRes.data)
|
data.value = uploadFileRes.data as T
|
||||||
data.value = JSON.parse(uploadFileRes.data) as T
|
|
||||||
},
|
},
|
||||||
fail: (err) => {
|
fail: (err) => {
|
||||||
console.log('uni.uploadFile err->', err)
|
console.log('uni.uploadFile err->', err)
|
||||||
|
@ -13,16 +13,16 @@
|
|||||||
<view v-if="loading" class="text-blue h-10">上传...</view>
|
<view v-if="loading" class="text-blue h-10">上传...</view>
|
||||||
<template v-else>
|
<template v-else>
|
||||||
<view class="m-2">上传后返回的接口数据:</view>
|
<view class="m-2">上传后返回的接口数据:</view>
|
||||||
<view class="m-2">{{ JSON.stringify(data) }}</view>
|
<view class="m-2">{{ data }}</view>
|
||||||
<view class="h-80 w-full">
|
<view class="h-80 w-full">
|
||||||
<image v-if="data" :src="data.url || data" mode="scaleToFill" />
|
<image v-if="data" :src="data || data" mode="scaleToFill" />
|
||||||
</view>
|
</view>
|
||||||
</template>
|
</template>
|
||||||
</view>
|
</view>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
const { loading, data, run } = useUpload<any>({ user: '菲鸽' })
|
const { loading, data, run } = useUpload({ user: '菲鸽' })
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
<style lang="scss" scoped>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user