|
@@ -13,6 +13,7 @@
|
|
|
<a-upload
|
|
<a-upload
|
|
|
ref="upload"
|
|
ref="upload"
|
|
|
:before-upload="beforeUploadCover"
|
|
:before-upload="beforeUploadCover"
|
|
|
|
|
+ accept=".jpg,.png,.bmp,.jpeg"
|
|
|
:remove="handleRemoveCover"
|
|
:remove="handleRemoveCover"
|
|
|
:headers="headers"
|
|
:headers="headers"
|
|
|
:action="action"
|
|
:action="action"
|
|
@@ -30,22 +31,12 @@
|
|
|
}"
|
|
}"
|
|
|
/>
|
|
/>
|
|
|
</div>
|
|
</div>
|
|
|
- <!-- 文件上传状态展示 -->
|
|
|
|
|
- <ul v-if="fileList.length">
|
|
|
|
|
- <li v-for="(file, index) in fileList" :key="index" class="file-item">
|
|
|
|
|
- <span>{{ file.name }}</span>
|
|
|
|
|
- <a-progress :percent="file.percent || 0" />
|
|
|
|
|
- <span v-if="file.status === 'done'">已完成</span>
|
|
|
|
|
- <span v-else-if="file.status === 'error'">上传失败</span>
|
|
|
|
|
- <span v-else-if="file.status === 'uploading'">上传中...</span>
|
|
|
|
|
- </li>
|
|
|
|
|
- </ul>
|
|
|
|
|
</div>
|
|
</div>
|
|
|
</template>
|
|
</template>
|
|
|
|
|
|
|
|
<script setup>
|
|
<script setup>
|
|
|
import { ref, reactive, toRefs } from 'vue'
|
|
import { ref, reactive, toRefs } from 'vue'
|
|
|
- import { Modal } from 'ant-design-vue'
|
|
|
|
|
|
|
+ import { Modal, Upload, message } from 'ant-design-vue'
|
|
|
import tool from '@/utils/tool'
|
|
import tool from '@/utils/tool'
|
|
|
const emit = defineEmits(['handleRemoveCover', 'handleChangeCover'])
|
|
const emit = defineEmits(['handleRemoveCover', 'handleChangeCover'])
|
|
|
const headers = ref({
|
|
const headers = ref({
|
|
@@ -67,19 +58,31 @@
|
|
|
// 封面文件列表
|
|
// 封面文件列表
|
|
|
const coverFileList = ref([])
|
|
const coverFileList = ref([])
|
|
|
const previewImageUrl = ref('')
|
|
const previewImageUrl = ref('')
|
|
|
- const fileList = ref([])
|
|
|
|
|
const coverImageId = ref(null)
|
|
const coverImageId = ref(null)
|
|
|
// 上传封面前的钩子函数
|
|
// 上传封面前的钩子函数
|
|
|
const beforeUploadCover = (file) => {
|
|
const beforeUploadCover = (file) => {
|
|
|
- const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'
|
|
|
|
|
- if (!isJpgOrPng) {
|
|
|
|
|
- Modal.error({ content: '仅支持上传jpg/png格式文件!' })
|
|
|
|
|
|
|
+ // 允许的文件扩展名
|
|
|
|
|
+ const allowedExtensions = ['.jpg', '.png', '.bmp', '.jpeg']
|
|
|
|
|
+ // 获取文件扩展名
|
|
|
|
|
+ const fileExtension = file.name.slice(file.name.lastIndexOf('.')).toLowerCase()
|
|
|
|
|
+ console.log(file, fileExtension, 'filefilefilefile')
|
|
|
|
|
+
|
|
|
|
|
+ // 验证文件类型
|
|
|
|
|
+ const isFileTypeAllowed = allowedExtensions.includes(fileExtension)
|
|
|
|
|
+
|
|
|
|
|
+ if (!isFileTypeAllowed) {
|
|
|
|
|
+ message.error(`不支持的文件格式: ${fileExtension},请上传以下格式: ${allowedExtensions.join(', ')}`)
|
|
|
|
|
+ return false
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ // 验证文件大小(500KB限制)
|
|
|
const isLt500K = file.size / 1024 < 500
|
|
const isLt500K = file.size / 1024 < 500
|
|
|
if (!isLt500K) {
|
|
if (!isLt500K) {
|
|
|
- Modal.error({ content: '单个文件不能超过500kb!' })
|
|
|
|
|
|
|
+ message.error('文件大小不能超过500KB!')
|
|
|
|
|
+ return false
|
|
|
}
|
|
}
|
|
|
- return isJpgOrPng && isLt500K
|
|
|
|
|
|
|
+
|
|
|
|
|
+ return isFileTypeAllowed && isLt500K
|
|
|
}
|
|
}
|
|
|
// 移除封面文件
|
|
// 移除封面文件
|
|
|
const handleRemoveCover = (file) => {
|
|
const handleRemoveCover = (file) => {
|