提交 42cbcc3c authored 作者: 王鹏飞's avatar 王鹏飞

updates

上级 9db4d825
...@@ -11,3 +11,15 @@ declare module 'vue' { ...@@ -11,3 +11,15 @@ declare module 'vue' {
Avatar: typeof Avatar Avatar: typeof Avatar
} }
} }
// declare module '@vue/runtime-core' {
// export interface GlobalComponents {
// AppCard: typeof import('./src/components/base/AppCard.vue')['default']
// AppContainer: typeof import('./src/components/base/AppContainer.vue')['default']
// Avatar: typeof import('./src/components/base/Avatar.vue')['default']
// RouterLink: typeof import('vue-router')['RouterLink']
// RouterView: typeof import('vue-router')['RouterView']
// }
// }
// export {}
<script setup lang="ts">
import md5 from 'blueimp-md5'
import { ref, withDefaults, watch } from 'vue'
import { getSignature, uploadFile } from '@/api/base'
import type { UploaderFileListItem } from 'vant'
const props = withDefaults(defineProps<{ modelValue: string[]; prefix?: string }>(), { prefix: 'upload/psp/' })
const emit = defineEmits(['update:modelValue'])
const fileList = ref<UploaderFileListItem[]>([])
watch(props.modelValue, files => {
fileList.value = files.map((url: string) => ({ url }))
})
// 上传
const afterRead = async (file: any) => {
// 获取签名
const signature: any = await getSignature()
const rawFile = file.file
const rawFileName = rawFile?.name || ''
const key = props.prefix + md5(rawFileName + new Date().getTime()) + rawFileName.substr(rawFileName.lastIndexOf('.'))
file.status = 'uploading'
file.message = '上传中...'
file.url = `${signature.host}/${key}`
// 上传文件
const params = {
key,
OSSAccessKeyId: signature.accessid,
policy: signature.policy,
signature: signature.signature,
success_action_status: '200',
file: rawFile
}
uploadFile(params)
.then(() => {
file.status = 'done'
file.message = '上传成功'
emit(
'update:modelValue',
fileList.value.map((item: any) => item.url)
)
})
.catch(() => {
file.status = 'failed'
file.message = '上传失败'
})
}
</script>
<template>
<van-uploader :after-read="afterRead"></van-uploader>
</template>
...@@ -4,24 +4,35 @@ import { ref, withDefaults, watch, computed } from 'vue' ...@@ -4,24 +4,35 @@ import { ref, withDefaults, watch, computed } from 'vue'
import { getSignature, uploadFile } from '@/api/base' import { getSignature, uploadFile } from '@/api/base'
import type { UploaderFileListItem } from 'vant' import type { UploaderFileListItem } from 'vant'
const props = withDefaults(defineProps<{ modelValue?: string[]; prefix?: string }>(), { prefix: 'upload/psp/' }) const props = withDefaults(defineProps<{ modelValue?: string | string[]; prefix?: string }>(), {
prefix: 'upload/psp/'
})
const emit = defineEmits(['update:modelValue']) const emit = defineEmits(['update:modelValue'])
const fileList = ref<UploaderFileListItem[]>([]) const fileList = ref<UploaderFileListItem[]>([])
// 是否为列表
const isFileList = computed(() => { const isFileList = computed(() => {
return Array.isArray(props.modelValue) return Array.isArray(props.modelValue)
}) })
// 文件上传数量限制
const maxCount = computed(() => {
return isFileList.value ? Infinity : 1
})
watch( watch(
() => props.modelValue, () => props.modelValue,
files => { value => {
if (Array.isArray(files)) { fileList.value = Array.isArray(value) ? value.map((url: string) => ({ url })) : (value && [{ url: value }]) || []
fileList.value = files.map((url: string) => ({ url }))
}
} }
) )
console.log(isFileList)
// 更新modelValue
const handleEmitUpdate = () => {
const value = isFileList.value ? fileList.value.map((item: any) => item.url) : fileList.value[0]?.url
emit('update:modelValue', value)
}
// 上传 // 上传
const afterRead = async (file: any) => { const afterRead = async (file: any) => {
// 获取签名 // 获取签名
...@@ -45,25 +56,20 @@ const afterRead = async (file: any) => { ...@@ -45,25 +56,20 @@ const afterRead = async (file: any) => {
.then(() => { .then(() => {
file.status = 'done' file.status = 'done'
file.message = '上传成功' file.message = '上传成功'
emit( handleEmitUpdate()
'update:modelValue',
fileList.value.map((item: any) => item.url)
)
}) })
.catch(() => { .catch(() => {
file.status = 'failed' file.status = 'failed'
file.message = '上传失败' file.message = '上传失败'
}) })
} }
// 删除
const onDelete = () => {
emit(
'update:modelValue',
fileList.value.map((item: any) => item.url)
)
}
</script> </script>
<template> <template>
<van-uploader v-model="fileList" :after-read="afterRead" @delete="onDelete"></van-uploader> <van-uploader
v-model="fileList"
:after-read="afterRead"
:max-count="maxCount"
@delete="handleEmitUpdate"
></van-uploader>
</template> </template>
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import { reactive } from 'vue' import { reactive } from 'vue'
import { useRouter, useRoute } from 'vue-router' import { useRouter, useRoute } from 'vue-router'
import { createCourseRecord } from '../api' import { createCourseRecord } from '../api'
import UploadFileList from '@/components/UploadFileList.vue' import AppUpload from '@/components/base/AppUpload.vue'
import { Toast } from 'vant' import { Toast } from 'vant'
const router = useRouter() const router = useRouter()
const route = useRoute() const route = useRoute()
...@@ -31,7 +31,7 @@ function onSubmit() { ...@@ -31,7 +31,7 @@ function onSubmit() {
/> />
<van-field :rules="[{ validator: pictureValidator, message: '请上传图片' }]"> <van-field :rules="[{ validator: pictureValidator, message: '请上传图片' }]">
<template #input> <template #input>
<UploadFileList v-model="form.picture"></UploadFileList> <AppUpload v-model="form.picture"></AppUpload>
</template> </template>
</van-field> </van-field>
<van-button block round native-type="submit" class="my-button">发表</van-button> <van-button block round native-type="submit" class="my-button">发表</van-button>
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
import { reactive } from 'vue' import { reactive } from 'vue'
import { useRouter } from 'vue-router' import { useRouter } from 'vue-router'
import { createQuestion } from '../api' import { createQuestion } from '../api'
import UploadFileList from '@/components/UploadFileList.vue' import AppUpload from '@/components/base/AppUpload.vue'
import { Toast } from 'vant' import { Toast } from 'vant'
const router = useRouter() const router = useRouter()
...@@ -34,7 +34,7 @@ function onSubmit() { ...@@ -34,7 +34,7 @@ function onSubmit() {
/> />
<van-field> <van-field>
<template #input> <template #input>
<UploadFileList v-model="form.picture"></UploadFileList> <AppUpload v-model="form.picture"></AppUpload>
</template> </template>
</van-field> </van-field>
<van-button block round native-type="submit" class="my-button">发布</van-button> <van-button block round native-type="submit" class="my-button">发布</van-button>
......
<script setup lang="ts"> <script setup lang="ts">
import { reactive } from 'vue' import { reactive } from 'vue'
import { createTeam } from '../api' import { createTeam } from '../api'
import UploadFileList from '@/components/UploadFileList.vue' import AppUpload from '@/components/base/AppUpload.vue'
import { Toast } from 'vant' import { Toast } from 'vant'
const form = reactive({ name: '', slogan: '', logo: '', brief: '' }) const form = reactive({ name: '', slogan: '', logo: '', brief: '' })
...@@ -25,7 +25,7 @@ function onSubmit() { ...@@ -25,7 +25,7 @@ function onSubmit() {
/> />
<van-field :rules="[{ required: true, message: '请上传图片' }]"> <van-field :rules="[{ required: true, message: '请上传图片' }]">
<template #input> <template #input>
<UploadFileList></UploadFileList> <AppUpload v-model="form.logo"></AppUpload>
</template> </template>
</van-field> </van-field>
<van-button block round native-type="submit" class="my-button">立即创建</van-button> <van-button block round native-type="submit" class="my-button">立即创建</van-button>
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论