Auto merge base into spa
This commit is contained in:
commit
d56ba37c6d
@ -36,7 +36,21 @@ export default function useUpload<T extends TfileType>(options: TOptions<T> = {}
|
||||
const chooseFileOptions = {
|
||||
count: 1,
|
||||
success: (res: any) => {
|
||||
handleFileChoose({ tempFilePath: res.tempFilePaths[0], tempFiles: res.tempFiles[0] })
|
||||
console.log('File selected successfully:', res)
|
||||
// h5中res:{errMsg: "chooseImage:ok", tempFilePaths: "blob:http://localhost:9000/f74ab6b8-a14d-4cb6-a10d-fcf4511a0de5", tempFiles: [File]}
|
||||
// h5的File有一下字段:{name: "girl.jpeg", size: 48976, type: "image/jpeg"}
|
||||
// 小程序中res:{errMsg: "chooseImage:ok", tempFiles: [{fileType: "image", size: 48976, tempFilePath: "http://tmp/5iG1WpIxTaJf3ece38692a337dc06df7eb69ecb49c6b.jpeg"}]}
|
||||
let tempFilePath = ''
|
||||
let size = 0
|
||||
// #ifdef H5
|
||||
tempFilePath = res.tempFilePaths[0]
|
||||
size = res.tempFiles[0].size
|
||||
// #endif
|
||||
// #ifdef MP-WEIXIN
|
||||
tempFilePath = res.tempFiles[0].tempFilePath
|
||||
size = res.tempFiles[0].size
|
||||
// #endif
|
||||
handleFileChoose({ tempFilePath, size })
|
||||
},
|
||||
fail: (err: any) => {
|
||||
console.error('File selection failed:', err)
|
||||
@ -64,11 +78,8 @@ export default function useUpload<T extends TfileType>(options: TOptions<T> = {}
|
||||
}
|
||||
}
|
||||
|
||||
const handleFileChoose = (file: {
|
||||
tempFilePath: string
|
||||
tempFiles?: { size?: number; name?: string }
|
||||
}) => {
|
||||
if (file?.tempFiles?.size && file.tempFiles.size > maxSize) {
|
||||
const handleFileChoose = ({ tempFilePath, size }: { tempFilePath: string; size: number }) => {
|
||||
if (size > maxSize) {
|
||||
uni.showToast({
|
||||
title: `文件大小不能超过 ${maxSize / 1024 / 1024}MB`,
|
||||
icon: 'none',
|
||||
@ -76,20 +87,20 @@ export default function useUpload<T extends TfileType>(options: TOptions<T> = {}
|
||||
return
|
||||
}
|
||||
|
||||
const fileExtension = file?.tempFiles?.name?.split('.').pop()?.toLowerCase()
|
||||
const isTypeValid = accept.some((type) => type === '*' || type.toLowerCase() === fileExtension)
|
||||
// const fileExtension = file?.tempFiles?.name?.split('.').pop()?.toLowerCase()
|
||||
// const isTypeValid = accept.some((type) => type === '*' || type.toLowerCase() === fileExtension)
|
||||
|
||||
if (!isTypeValid) {
|
||||
uni.showToast({
|
||||
title: `仅支持 ${accept.join(', ')} 格式的文件`,
|
||||
icon: 'none',
|
||||
})
|
||||
return
|
||||
}
|
||||
// if (!isTypeValid) {
|
||||
// uni.showToast({
|
||||
// title: `仅支持 ${accept.join(', ')} 格式的文件`,
|
||||
// icon: 'none',
|
||||
// })
|
||||
// return
|
||||
// }
|
||||
|
||||
loading.value = true
|
||||
uploadFile({
|
||||
tempFilePath: file.tempFilePath,
|
||||
tempFilePath: tempFilePath,
|
||||
formData,
|
||||
onSuccess: (res) => {
|
||||
data.value = res
|
||||
|
@ -88,7 +88,7 @@
|
||||
<script lang="ts" setup>
|
||||
import { useUserStore } from '@/store'
|
||||
import { useToast } from 'wot-design-uni'
|
||||
import { uploadFileUrl, useUpload } from '@/utils/uploadFile'
|
||||
import { useUpload } from '@/utils/uploadFile'
|
||||
import { storeToRefs } from 'pinia'
|
||||
import { IUploadSuccessInfo } from '@/api/login.typings'
|
||||
|
||||
@ -106,10 +106,13 @@ onShow((options) => {
|
||||
// #ifndef MP-WEIXIN
|
||||
// 上传头像
|
||||
const { run } = useUpload<IUploadSuccessInfo>(
|
||||
uploadFileUrl.USER_AVATAR,
|
||||
import.meta.env.VITE_UPLOAD_BASEURL,
|
||||
{},
|
||||
{
|
||||
onSuccess: (res) => useUserStore().getUserInfo(),
|
||||
onSuccess: (res: string) => {
|
||||
console.log('头像上传成功', res)
|
||||
useUserStore().setUserAvatar(res)
|
||||
},
|
||||
},
|
||||
)
|
||||
// #endif
|
||||
@ -134,10 +137,13 @@ const onChooseAvatar = (e: any) => {
|
||||
console.log('选择头像', e.detail)
|
||||
const { avatarUrl } = e.detail
|
||||
const { run } = useUpload<IUploadSuccessInfo>(
|
||||
uploadFileUrl.USER_AVATAR,
|
||||
import.meta.env.VITE_UPLOAD_BASEURL,
|
||||
{},
|
||||
{
|
||||
onSuccess: (res) => useUserStore().getUserInfo(),
|
||||
onSuccess: (res) => {
|
||||
console.log('头像上传成功', res)
|
||||
// 更新用户信息
|
||||
},
|
||||
},
|
||||
avatarUrl,
|
||||
)
|
||||
@ -162,7 +168,7 @@ const handlePassword = () => {
|
||||
// 消息通知
|
||||
const handleInform = () => {
|
||||
// uni.navigateTo({ url: `/pages/mine/inform/index` })
|
||||
toast.success('功能开发中')
|
||||
toast.show('功能开发中')
|
||||
}
|
||||
// 应用更新
|
||||
const handleAppUpdate = () => {
|
||||
@ -173,9 +179,9 @@ const handleAppUpdate = () => {
|
||||
// 请求完新版本信息的回调
|
||||
// console.log(res.hasUpdate)
|
||||
if (res.hasUpdate) {
|
||||
toast.success('检测到新版本,正在下载中...')
|
||||
toast.show('检测到新版本,正在下载中...')
|
||||
} else {
|
||||
toast.success('已是最新版本')
|
||||
toast.show('已是最新版本')
|
||||
}
|
||||
})
|
||||
updateManager.onUpdateReady(function (res) {
|
||||
@ -198,7 +204,7 @@ const handleAppUpdate = () => {
|
||||
// #endif
|
||||
|
||||
// #ifndef MP
|
||||
toast.success('功能开发中')
|
||||
toast.show('功能开发中')
|
||||
// #endif
|
||||
}
|
||||
// 关于我们
|
||||
@ -217,7 +223,7 @@ const handleClearCache = () => {
|
||||
uni.clearStorageSync()
|
||||
// 清除用户信息并跳转到登录页
|
||||
useUserStore().logout()
|
||||
toast.success('清除缓存成功')
|
||||
toast.show('清除缓存成功')
|
||||
} catch (err) {
|
||||
console.error('清除缓存失败:', err)
|
||||
toast.error('清除缓存失败')
|
||||
@ -237,7 +243,7 @@ const handleLogout = () => {
|
||||
useUserStore().logout()
|
||||
hasLogin.value = false
|
||||
// 执行退出登录逻辑
|
||||
toast.success('退出登录成功')
|
||||
toast.show('退出登录成功')
|
||||
// #ifdef MP-WEIXIN
|
||||
// 微信小程序,去首页
|
||||
// uni.reLaunch({ url: '/pages/index/index' })
|
||||
|
@ -34,6 +34,9 @@ export const useUserStore = defineStore(
|
||||
}
|
||||
userInfo.value = val
|
||||
}
|
||||
const setUserAvatar = (avatar: string) => {
|
||||
userInfo.value.avatar = avatar
|
||||
}
|
||||
// 删除用户信息
|
||||
const removeUserInfo = () => {
|
||||
userInfo.value = { ...userInfoState }
|
||||
@ -94,6 +97,7 @@ export const useUserStore = defineStore(
|
||||
login,
|
||||
wxLogin,
|
||||
getUserInfo,
|
||||
setUserAvatar,
|
||||
logout,
|
||||
}
|
||||
},
|
||||
|
@ -61,7 +61,7 @@ export interface UploadOptions {
|
||||
/** 上传进度回调函数 */
|
||||
onProgress?: (progress: number) => void
|
||||
/** 上传成功回调函数 */
|
||||
onSuccess?: (res: UniApp.UploadFileSuccessCallbackResult) => void
|
||||
onSuccess?: (res: string) => void
|
||||
/** 上传失败回调函数 */
|
||||
onError?: (err: Error | UniApp.GeneralCallbackResult) => void
|
||||
/** 上传完成回调函数(无论成功失败) */
|
||||
@ -248,7 +248,7 @@ interface UploadFileOptions<T> {
|
||||
/** 上传进度回调 */
|
||||
onProgress?: (progress: number) => void
|
||||
/** 上传成功回调 */
|
||||
onSuccess?: (res: UniApp.UploadFileSuccessCallbackResult) => void
|
||||
onSuccess?: (res: string) => void
|
||||
/** 上传失败回调 */
|
||||
onError?: (err: Error | UniApp.GeneralCallbackResult) => void
|
||||
/** 上传完成回调 */
|
||||
@ -288,19 +288,13 @@ function uploadFile<T>({
|
||||
},
|
||||
// 确保文件名称合法
|
||||
success: (uploadFileRes) => {
|
||||
console.log('上传文件成功:', uploadFileRes)
|
||||
try {
|
||||
// 解析响应数据
|
||||
const result = JSON.parse(uploadFileRes.data)
|
||||
if (result.code === 1) {
|
||||
// 上传成功
|
||||
data.value = result.data as T
|
||||
onSuccess?.(uploadFileRes)
|
||||
} else {
|
||||
// 业务错误
|
||||
const err = new Error(result.message || '上传失败')
|
||||
error.value = true
|
||||
onError?.(err)
|
||||
}
|
||||
const result = uploadFileRes.data
|
||||
// 上传成功
|
||||
data.value = result as T
|
||||
onSuccess?.(result)
|
||||
} catch (err) {
|
||||
// 响应解析错误
|
||||
console.error('解析上传响应失败:', err)
|
||||
|
Loading…
x
Reference in New Issue
Block a user