uniapp vue3 封装上传文件的hook

2025-04-27  本文已影响0人  燕自浩
import useGizoneConfig from '../utils/useGizoneConfig';
import useAccessToken from '../auth/useAccessToken';

// 上传文件并保留文件名
export function useUploadKeepName() {
    const gizoneConfig = useGizoneConfig();
    const token = useAccessToken();
    const url = `${gizoneConfig.apiBaseUrl}/operation/file/uploadKeepName`;

    return (name: string, filePath: string) => {
        return new Promise<string>((resolve, reject) => {
            // @ts-ignore
            uni.uploadFile({
                url,
                name,
                filePath,
                header: {
                    Authorization: `Bearer ${token.value}`,
                },
                // @ts-ignore
                success: res => {
                    resolve(JSON.parse(res.data)?.data.fileUrl);
                },
                // @ts-ignore
                fail: err => {
                    reject(err);
                }
            })
        })
    }
}

使用

 const uploadKeepName = useUploadKeepName();
 uploadKeepName (‘file’, url)
上一篇 下一篇

猜你喜欢

热点阅读