feat: 针对不同场景,配置2个不同的上传图片函数

This commit is contained in:
菲鸽 2024-05-03 14:26:00 +08:00
parent ae24b9d598
commit 6a6030642e
6 changed files with 96 additions and 5 deletions

View File

@ -88,6 +88,7 @@
"watchPostEffect": true, "watchPostEffect": true,
"watchSyncEffect": true, "watchSyncEffect": true,
"useRequest": true, "useRequest": true,
"useUpload": true "useUpload": true,
"useUpload2": true
} }
} }

2
env/.env vendored
View File

@ -10,3 +10,5 @@ VITE_WX_APPID = 'wxa2abb91f64032a2b'
# 非h5端只能使用完整的baseurl否则无法请求本地proxy只支持h5端 # 非h5端只能使用完整的baseurl否则无法请求本地proxy只支持h5端
# VITE_SERVER_BASEURL = '/api' # VITE_SERVER_BASEURL = '/api'
VITE_SERVER_BASEURL = 'https://ukw0y1.laf.run' VITE_SERVER_BASEURL = 'https://ukw0y1.laf.run'
VITE_UPLOAD_BASEURL = 'https://ukw0y1.laf.run/upload'

6
src/env.d.ts vendored
View File

@ -9,9 +9,15 @@ declare module '*.vue' {
} }
interface ImportMetaEnv { interface ImportMetaEnv {
/** 网站标题,应用名称 */
readonly VITE_APP_TITLE: string readonly VITE_APP_TITLE: string
/** 服务端口号 */
readonly VITE_SERVER_PORT: string readonly VITE_SERVER_PORT: string
/** 后台接口地址 */
readonly VITE_SERVER_BASEURL: string readonly VITE_SERVER_BASEURL: string
/** 上传图片地址 */
readonly VITE_UPLOAD_BASEURL: string
/** 是否清除console */
readonly VITE_DELETE_CONSOLE: string readonly VITE_DELETE_CONSOLE: string
// 更多环境变量... // 更多环境变量...
} }

View File

@ -1,14 +1,13 @@
/** /**
* useUpload * useUpload
* @param url https://ukw0y1.laf.run/upload。
* @param formData {name: '菲鸽'} * @param formData {name: '菲鸽'}
* @returns {loading, error, data, run} * @returns {loading, error, data, run}
*/ */
export default function useUpload<T>(url: string, formData: Record<string, any> = {}) { export default function useUpload<T>(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>()
const url = import.meta.env.VITE_UPLOAD_BASEURL
const run = () => { const run = () => {
// #ifdef MP-WEIXIN // #ifdef MP-WEIXIN
// 微信小程序从基础库 2.21.0 开始, wx.chooseImage 停止维护,请使用 uni.chooseMedia 代替。 // 微信小程序从基础库 2.21.0 开始, wx.chooseImage 停止维护,请使用 uni.chooseMedia 代替。

82
src/hooks/useUpload2.ts Normal file
View File

@ -0,0 +1,82 @@
/**
* useUpload
* @param url https://ukw0y1.laf.run/upload。
* .env
* @param formData {name: '菲鸽'}
* @returns {loading, error, data, run}
*/
export default function useUpload<T>(url: string, formData: Record<string, any> = {}) {
const loading = ref(false)
const error = ref(false)
const data = ref<T>()
const run = () => {
// #ifdef MP-WEIXIN
// 微信小程序从基础库 2.21.0 开始, wx.chooseImage 停止维护,请使用 uni.chooseMedia 代替。
// 微信小程序在2023年10月17日之后使用本API需要配置隐私协议
uni.chooseMedia({
count: 1,
mediaType: ['image'],
success: (res) => {
console.log(res)
loading.value = true
const tempFilePath = res.tempFiles[0].tempFilePath
uni.uploadFile({
url,
filePath: tempFilePath,
name: 'file',
formData,
success: (uploadFileRes) => {
console.log(uploadFileRes.data)
data.value = uploadFileRes.data as T
},
fail: (err) => {
console.log('uni.uploadFile err->', err)
error.value = true
},
complete: () => {
loading.value = false
},
})
},
fail: (err) => {
console.log('uni.chooseMedia err->', err)
error.value = true
},
})
// #endif
// #ifndef MP-WEIXIN
uni.chooseImage({
count: 1,
success: (res) => {
console.log(res)
loading.value = true
const tempFilePath = res.tempFilePaths[0]
uni.uploadFile({
url,
filePath: tempFilePath,
name: 'file',
formData,
success: (uploadFileRes) => {
console.log(uploadFileRes.data)
data.value = uploadFileRes.data as T
},
fail: (err) => {
console.log('uni.uploadFile err->', err)
error.value = true
},
complete: () => {
loading.value = false
},
})
},
fail: (err) => {
console.log('uni.chooseImage err->', err)
error.value = true
},
})
// #endif
}
return { loading, error, data, run }
}

View File

@ -22,7 +22,8 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
const { loading, data, run } = useUpload<string>('https://ukw0y1.laf.run/upload', { user: '菲鸽' }) // const { loading, data, run } = useUpload2<string>('https://ukw0y1.laf.run/upload', { user: '' })
const { loading, data, run } = useUpload<string>({ user: '菲鸽' })
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>