feat(上传功能): 添加媒体上传组件和弹窗封装
实现完整的媒体上传功能,包括: 1. 新增 PopupWrapper 作为通用弹窗组件 2. 新增 UploadMediaPopup 作为上传弹窗容器 3. 新增 UploadMediaComponent 实现具体上传逻辑 4. 在 FamilyAlbum 中集成上传功能 5. 更新组件类型声明 支持图片和视频上传,包含预览、删除和提交功能
Showing
6 changed files
with
796 additions
and
3 deletions
| ... | @@ -29,6 +29,7 @@ declare module 'vue' { | ... | @@ -29,6 +29,7 @@ declare module 'vue' { |
| 29 | NutToast: typeof import('@nutui/nutui-taro')['Toast'] | 29 | NutToast: typeof import('@nutui/nutui-taro')['Toast'] |
| 30 | Picker: typeof import('./src/components/time-picker-data/picker.vue')['default'] | 30 | Picker: typeof import('./src/components/time-picker-data/picker.vue')['default'] |
| 31 | PointsCollector: typeof import('./src/components/PointsCollector.vue')['default'] | 31 | PointsCollector: typeof import('./src/components/PointsCollector.vue')['default'] |
| 32 | + PopupWrapper: typeof import('./src/components/PopupWrapper.vue')['default'] | ||
| 32 | PosterBuilder: typeof import('./src/components/PosterBuilder/index.vue')['default'] | 33 | PosterBuilder: typeof import('./src/components/PosterBuilder/index.vue')['default'] |
| 33 | PrimaryButton: typeof import('./src/components/PrimaryButton.vue')['default'] | 34 | PrimaryButton: typeof import('./src/components/PrimaryButton.vue')['default'] |
| 34 | RankingCard: typeof import('./src/components/RankingCard.vue')['default'] | 35 | RankingCard: typeof import('./src/components/RankingCard.vue')['default'] |
| ... | @@ -37,6 +38,8 @@ declare module 'vue' { | ... | @@ -37,6 +38,8 @@ declare module 'vue' { |
| 37 | ShareButton: typeof import('./src/components/ShareButton/index.vue')['default'] | 38 | ShareButton: typeof import('./src/components/ShareButton/index.vue')['default'] |
| 38 | TabBar: typeof import('./src/components/TabBar.vue')['default'] | 39 | TabBar: typeof import('./src/components/TabBar.vue')['default'] |
| 39 | TotalPointsDisplay: typeof import('./src/components/TotalPointsDisplay.vue')['default'] | 40 | TotalPointsDisplay: typeof import('./src/components/TotalPointsDisplay.vue')['default'] |
| 41 | + UploadMediaComponent: typeof import('./src/components/UploadMediaComponent.vue')['default'] | ||
| 42 | + UploadMediaPopup: typeof import('./src/components/UploadMediaPopup.vue')['default'] | ||
| 40 | WeRunAuth: typeof import('./src/components/WeRunAuth.vue')['default'] | 43 | WeRunAuth: typeof import('./src/components/WeRunAuth.vue')['default'] |
| 41 | } | 44 | } |
| 42 | } | 45 | } | ... | ... |
| ... | @@ -109,6 +109,15 @@ | ... | @@ -109,6 +109,15 @@ |
| 109 | @fullscreenchange="handleFullscreenChange" | 109 | @fullscreenchange="handleFullscreenChange" |
| 110 | /> | 110 | /> |
| 111 | </view> | 111 | </view> |
| 112 | + | ||
| 113 | + <!-- 上传媒体弹窗 --> | ||
| 114 | + <UploadMediaPopup | ||
| 115 | + v-model:visible="uploadPopupVisible" | ||
| 116 | + from="family" | ||
| 117 | + :id="''" | ||
| 118 | + @success="handleUploadSuccess" | ||
| 119 | + @close="closeUploadPopup" | ||
| 120 | + /> | ||
| 112 | </view> | 121 | </view> |
| 113 | </template> | 122 | </template> |
| 114 | 123 | ||
| ... | @@ -118,12 +127,16 @@ import Taro, { useDidShow } from '@tarojs/taro'; | ... | @@ -118,12 +127,16 @@ import Taro, { useDidShow } from '@tarojs/taro'; |
| 118 | import { Close, Photograph, IconFont } from '@nutui/icons-vue-taro'; | 127 | import { Close, Photograph, IconFont } from '@nutui/icons-vue-taro'; |
| 119 | 128 | ||
| 120 | import { getPhotoListAPI } from '@/api/photo'; | 129 | import { getPhotoListAPI } from '@/api/photo'; |
| 130 | +import UploadMediaPopup from '@/components/UploadMediaPopup.vue'; | ||
| 121 | 131 | ||
| 122 | // 视频播放相关状态 | 132 | // 视频播放相关状态 |
| 123 | const videoVisible = ref(false); | 133 | const videoVisible = ref(false); |
| 124 | const currentVideo = ref(null); | 134 | const currentVideo = ref(null); |
| 125 | const videoId = ref(0); | 135 | const videoId = ref(0); |
| 126 | 136 | ||
| 137 | +// 上传弹窗相关状态 | ||
| 138 | +const uploadPopupVisible = ref(false); | ||
| 139 | + | ||
| 127 | /** | 140 | /** |
| 128 | * 处理媒体项点击事件 | 141 | * 处理媒体项点击事件 |
| 129 | * @param {Object} item - 媒体项 | 142 | * @param {Object} item - 媒体项 |
| ... | @@ -239,10 +252,35 @@ const openAlbumList = () => { | ... | @@ -239,10 +252,35 @@ const openAlbumList = () => { |
| 239 | }; | 252 | }; |
| 240 | 253 | ||
| 241 | /** | 254 | /** |
| 242 | - * 跳转到上传媒体页面 | 255 | + * 打开上传弹窗 |
| 243 | */ | 256 | */ |
| 244 | const navigateToUpload = () => { | 257 | const navigateToUpload = () => { |
| 245 | - Taro.navigateTo({ url: '/pages/UploadMedia/index' }); | 258 | + uploadPopupVisible.value = true; |
| 259 | +}; | ||
| 260 | + | ||
| 261 | +/** | ||
| 262 | + * 关闭上传弹窗 | ||
| 263 | + */ | ||
| 264 | +const closeUploadPopup = () => { | ||
| 265 | + uploadPopupVisible.value = false; | ||
| 266 | +}; | ||
| 267 | + | ||
| 268 | +/** | ||
| 269 | + * 上传成功回调 | ||
| 270 | + * @param {Object} data - 上传成功的数据 | ||
| 271 | + */ | ||
| 272 | +const handleUploadSuccess = (data) => { | ||
| 273 | + console.log('上传成功:', data); | ||
| 274 | + // 刷新相册数据 | ||
| 275 | + refreshData(); | ||
| 276 | + // 关闭弹窗 | ||
| 277 | + closeUploadPopup(); | ||
| 278 | + // 显示成功提示 | ||
| 279 | + Taro.showToast({ | ||
| 280 | + title: '上传成功', | ||
| 281 | + icon: 'success', | ||
| 282 | + duration: 2000 | ||
| 283 | + }); | ||
| 246 | }; | 284 | }; |
| 247 | 285 | ||
| 248 | // 组件挂载时获取数据 | 286 | // 组件挂载时获取数据 | ... | ... |
| ... | @@ -126,7 +126,7 @@ const sourceTypeMap = { | ... | @@ -126,7 +126,7 @@ const sourceTypeMap = { |
| 126 | 'CHECK_IN_COUNT': '完成活动', | 126 | 'CHECK_IN_COUNT': '完成活动', |
| 127 | 'FAMILY_SIZE': '家庭成员', | 127 | 'FAMILY_SIZE': '家庭成员', |
| 128 | 'COMPANION_PHOTO': '陪伴拍照', | 128 | 'COMPANION_PHOTO': '陪伴拍照', |
| 129 | - 'WHEELCHAIR_COMPANION': '陪伴轮椅' | 129 | + 'WHEELCHAIR_COMPANION': '特殊陪伴' |
| 130 | } | 130 | } |
| 131 | 131 | ||
| 132 | /** | 132 | /** | ... | ... |
src/components/PopupWrapper.vue
0 → 100644
| 1 | +<template> | ||
| 2 | +<nut-popup | ||
| 3 | + :visible="visibleModel" | ||
| 4 | + :position="position" | ||
| 5 | + :style="popupStyle" | ||
| 6 | + :closeable="closeable" | ||
| 7 | + :close-on-click-overlay="closeOnClickOverlay" | ||
| 8 | + :round="round" | ||
| 9 | + :z-index="zIndex" | ||
| 10 | + @close="handleClose" | ||
| 11 | + @open="handleOpen" | ||
| 12 | + @opened="handleOpened" | ||
| 13 | + @closed="handleClosed" | ||
| 14 | +> | ||
| 15 | + <!-- 自定义头部 --> | ||
| 16 | + <view v-if="showHeader" class="popup-header"> | ||
| 17 | + <view class="flex items-center justify-between p-4 border-b border-gray-200"> | ||
| 18 | + <view class="text-lg font-medium">{{ title }}</view> | ||
| 19 | + <view v-if="showCloseButton" @tap="handleClose" class="w-8 h-8 flex items-center justify-center"></view> | ||
| 20 | + </view> | ||
| 21 | + </view> | ||
| 22 | + | ||
| 23 | + <!-- 内容区域 --> | ||
| 24 | + <view class="popup-content" :style="contentStyle"> | ||
| 25 | + <slot></slot> | ||
| 26 | + </view> | ||
| 27 | +</nut-popup> | ||
| 28 | +</template> | ||
| 29 | + | ||
| 30 | +<script setup> | ||
| 31 | +import { ref, computed, defineProps, defineEmits } from 'vue'; | ||
| 32 | +import { Close } from '@nutui/icons-vue-taro'; | ||
| 33 | + | ||
| 34 | +// 定义props | ||
| 35 | +const props = defineProps({ | ||
| 36 | + // 控制弹窗显示/隐藏 | ||
| 37 | + visible: { | ||
| 38 | + type: Boolean, | ||
| 39 | + default: false | ||
| 40 | + }, | ||
| 41 | + // 弹窗位置 | ||
| 42 | + position: { | ||
| 43 | + type: String, | ||
| 44 | + default: 'center', | ||
| 45 | + validator: (value) => ['center', 'top', 'bottom', 'left', 'right'].includes(value) | ||
| 46 | + }, | ||
| 47 | + // 弹窗标题 | ||
| 48 | + title: { | ||
| 49 | + type: String, | ||
| 50 | + default: '' | ||
| 51 | + }, | ||
| 52 | + // 是否显示头部 | ||
| 53 | + showHeader: { | ||
| 54 | + type: Boolean, | ||
| 55 | + default: true | ||
| 56 | + }, | ||
| 57 | + // 是否显示关闭按钮 | ||
| 58 | + showCloseButton: { | ||
| 59 | + type: Boolean, | ||
| 60 | + default: true | ||
| 61 | + }, | ||
| 62 | + // 是否可关闭 | ||
| 63 | + closeable: { | ||
| 64 | + type: Boolean, | ||
| 65 | + default: true | ||
| 66 | + }, | ||
| 67 | + // 点击遮罩是否关闭 | ||
| 68 | + closeOnClickOverlay: { | ||
| 69 | + type: Boolean, | ||
| 70 | + default: false | ||
| 71 | + }, | ||
| 72 | + // 是否圆角 | ||
| 73 | + round: { | ||
| 74 | + type: Boolean, | ||
| 75 | + default: false | ||
| 76 | + }, | ||
| 77 | + // 层级 | ||
| 78 | + zIndex: { | ||
| 79 | + type: Number, | ||
| 80 | + default: 2000 | ||
| 81 | + }, | ||
| 82 | + // 自定义宽度 | ||
| 83 | + width: { | ||
| 84 | + type: String, | ||
| 85 | + default: '100%' | ||
| 86 | + }, | ||
| 87 | + // 自定义高度 | ||
| 88 | + height: { | ||
| 89 | + type: String, | ||
| 90 | + default: '100%' | ||
| 91 | + }, | ||
| 92 | + // 是否全屏 | ||
| 93 | + fullscreen: { | ||
| 94 | + type: Boolean, | ||
| 95 | + default: false | ||
| 96 | + } | ||
| 97 | +}); | ||
| 98 | + | ||
| 99 | +// 定义emits | ||
| 100 | +const emit = defineEmits(['update:visible', 'close', 'open', 'opened', 'closed']); | ||
| 101 | + | ||
| 102 | +// 计算属性处理双向绑定 | ||
| 103 | +const visibleModel = computed({ | ||
| 104 | + get() { | ||
| 105 | + return props.visible; | ||
| 106 | + }, | ||
| 107 | + set(value) { | ||
| 108 | + emit('update:visible', value); | ||
| 109 | + } | ||
| 110 | +}); | ||
| 111 | + | ||
| 112 | +// 计算弹窗样式 | ||
| 113 | +const popupStyle = computed(() => { | ||
| 114 | + const style = {}; | ||
| 115 | + | ||
| 116 | + if (props.fullscreen) { | ||
| 117 | + style.width = '100%'; | ||
| 118 | + style.height = '100%'; | ||
| 119 | + } else { | ||
| 120 | + if (props.width) style.width = props.width; | ||
| 121 | + if (props.height) style.height = props.height; | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + return style; | ||
| 125 | +}); | ||
| 126 | + | ||
| 127 | +// 计算内容区域样式 | ||
| 128 | +const contentStyle = computed(() => { | ||
| 129 | + const style = {}; | ||
| 130 | + | ||
| 131 | + if (props.showHeader) { | ||
| 132 | + style.height = 'calc(100% - 60px)'; | ||
| 133 | + } else { | ||
| 134 | + style.height = '100%'; | ||
| 135 | + } | ||
| 136 | + | ||
| 137 | + return style; | ||
| 138 | +}); | ||
| 139 | + | ||
| 140 | +/** | ||
| 141 | + * 处理关闭事件 | ||
| 142 | + */ | ||
| 143 | +const handleClose = () => { | ||
| 144 | + emit('update:visible', false); | ||
| 145 | + emit('close'); | ||
| 146 | +}; | ||
| 147 | + | ||
| 148 | +/** | ||
| 149 | + * 处理打开事件 | ||
| 150 | + */ | ||
| 151 | +const handleOpen = () => { | ||
| 152 | + emit('open'); | ||
| 153 | +}; | ||
| 154 | + | ||
| 155 | +/** | ||
| 156 | + * 处理已打开事件 | ||
| 157 | + */ | ||
| 158 | +const handleOpened = () => { | ||
| 159 | + emit('opened'); | ||
| 160 | +}; | ||
| 161 | + | ||
| 162 | +/** | ||
| 163 | + * 处理已关闭事件 | ||
| 164 | + */ | ||
| 165 | +const handleClosed = () => { | ||
| 166 | + emit('closed'); | ||
| 167 | +}; | ||
| 168 | +</script> | ||
| 169 | + | ||
| 170 | +<style lang="less" scoped> | ||
| 171 | +.popup-header { | ||
| 172 | + background-color: #fff; | ||
| 173 | + border-bottom: 1px solid #e5e7eb; | ||
| 174 | +} | ||
| 175 | + | ||
| 176 | +.popup-content { | ||
| 177 | + background-color: #f8f9fa; | ||
| 178 | + overflow-y: auto; | ||
| 179 | +} | ||
| 180 | +</style> |
src/components/UploadMediaComponent.vue
0 → 100644
| 1 | +<template> | ||
| 2 | + <view class="upload-media-component"> | ||
| 3 | + <!-- Upload Area --> | ||
| 4 | + <view class="p-4"> | ||
| 5 | + <!-- Upload Button --> | ||
| 6 | + <view | ||
| 7 | + v-if="!uploadedFile" | ||
| 8 | + class="border border-dashed border-gray-300 rounded-lg p-8 flex flex-col items-center justify-center mb-4 bg-white" | ||
| 9 | + @tap="chooseMedia" | ||
| 10 | + > | ||
| 11 | + <view class="text-gray-400 mb-4"> | ||
| 12 | + <Photograph size="48" /> | ||
| 13 | + </view> | ||
| 14 | + <view class="text-center text-gray-600 mb-2 text-sm">选择图片或视频</view> | ||
| 15 | + <view class="text-center text-gray-400 text-sm"> | ||
| 16 | + 支持图片格式(jpg、png)最大10MB或60秒内视频 | ||
| 17 | + </view> | ||
| 18 | + </view> | ||
| 19 | + | ||
| 20 | + <!-- Preview Area --> | ||
| 21 | + <view v-if="uploadedFile" class="mb-4"> | ||
| 22 | + <!-- Image Preview --> | ||
| 23 | + <view v-if="uploadedFile.type === 'image'" class="relative rounded-lg overflow-hidden bg-white shadow-sm"> | ||
| 24 | + <image | ||
| 25 | + :src="uploadedFile.url" | ||
| 26 | + class="w-full h-64 object-cover cursor-pointer" | ||
| 27 | + mode="widthFix" | ||
| 28 | + @tap="previewImage" | ||
| 29 | + /> | ||
| 30 | + <view | ||
| 31 | + @tap="removeFile" | ||
| 32 | + class="absolute top-2 right-2 w-8 h-8 bg-black bg-opacity-50 rounded-full flex items-center justify-center" | ||
| 33 | + > | ||
| 34 | + <Close size="16" class="text-white" /> | ||
| 35 | + </view> | ||
| 36 | + </view> | ||
| 37 | + | ||
| 38 | + <!-- Video Preview --> | ||
| 39 | + <view v-if="uploadedFile.type === 'video'" class="relative rounded-lg overflow-hidden bg-white shadow-sm"> | ||
| 40 | + <view | ||
| 41 | + class="relative w-full h-64 bg-black rounded-lg flex items-center justify-center" | ||
| 42 | + @tap="playVideo" | ||
| 43 | + > | ||
| 44 | + <image | ||
| 45 | + v-if="uploadedFile.thumbnail" | ||
| 46 | + :src="uploadedFile.thumbnail" | ||
| 47 | + class="w-full h-full object-cover" | ||
| 48 | + mode="widthFix" | ||
| 49 | + /> | ||
| 50 | + <view class="absolute inset-0 flex items-center justify-center"> | ||
| 51 | + <view class="w-16 h-16 bg-black bg-opacity-60 rounded-full flex items-center justify-center"> | ||
| 52 | + <image :src="playIcon" class="w-6 h-6" /> | ||
| 53 | + </view> | ||
| 54 | + </view> | ||
| 55 | + </view> | ||
| 56 | + <view | ||
| 57 | + @tap="removeFile" | ||
| 58 | + class="absolute top-2 right-2 w-8 h-8 bg-black bg-opacity-50 rounded-full flex items-center justify-center" | ||
| 59 | + > | ||
| 60 | + <Close size="16" class="text-white" /> | ||
| 61 | + </view> | ||
| 62 | + </view> | ||
| 63 | + | ||
| 64 | + <!-- File Info --> | ||
| 65 | + <view class="mt-3 p-3 bg-white rounded-lg"> | ||
| 66 | + <view class="text-sm text-gray-600">文件大小: {{ formatFileSize(uploadedFile.size) }}</view> | ||
| 67 | + <view v-if="uploadedFile.type === 'video'" class="text-sm text-gray-600 mt-1"> | ||
| 68 | + 时长: {{ formatDuration(uploadedFile.duration) }} | ||
| 69 | + </view> | ||
| 70 | + </view> | ||
| 71 | + </view> | ||
| 72 | + | ||
| 73 | + <!-- Action Buttons --> | ||
| 74 | + <view class="flex gap-3"> | ||
| 75 | + <view | ||
| 76 | + @tap="chooseMedia" | ||
| 77 | + class="flex-1 bg-gray-100 text-gray-700 py-3 rounded-lg text-center text-sm" | ||
| 78 | + > | ||
| 79 | + {{ uploadedFile ? '重新选择' : '选择文件' }} | ||
| 80 | + </view> | ||
| 81 | + <view | ||
| 82 | + v-if="uploadedFile" | ||
| 83 | + @tap="saveMedia" | ||
| 84 | + class="flex-1 bg-blue-500 text-white py-3 rounded-lg text-center text-sm" | ||
| 85 | + > | ||
| 86 | + 提交 | ||
| 87 | + </view> | ||
| 88 | + </view> | ||
| 89 | + <view class="mt-6 text-sm text-gray-500"> | ||
| 90 | + <view class="mb-2">积分注意事项:</view> | ||
| 91 | + <view class="text-xs mb-1">• 每张图片或视频积分100分</view> | ||
| 92 | + <view class="text-xs mb-1">• 每天最多积分100分</view> | ||
| 93 | + </view> | ||
| 94 | + </view> | ||
| 95 | + | ||
| 96 | + <!-- Video Player Modal --> | ||
| 97 | + <view | ||
| 98 | + v-if="videoVisible" | ||
| 99 | + class="fixed inset-0 bg-black" | ||
| 100 | + style="z-index: 9999;" | ||
| 101 | + @tap="closeVideo" | ||
| 102 | + > | ||
| 103 | + <!-- Close Button --> | ||
| 104 | + <view | ||
| 105 | + @tap.stop="closeVideo" | ||
| 106 | + class="absolute top-4 right-4 w-10 h-10 bg-black bg-opacity-50 rounded-full flex items-center justify-center" | ||
| 107 | + style="z-index: 10000;" | ||
| 108 | + > | ||
| 109 | + <Close size="24" class="text-white" /> | ||
| 110 | + </view> | ||
| 111 | + | ||
| 112 | + <!-- Video Player --> | ||
| 113 | + <video | ||
| 114 | + v-if="uploadedFile && uploadedFile.type === 'video'" | ||
| 115 | + :id="'upload-video-' + videoId" | ||
| 116 | + :src="uploadedFile.url" | ||
| 117 | + :poster="uploadedFile.thumbnail" | ||
| 118 | + :controls="true" | ||
| 119 | + :autoplay="false" | ||
| 120 | + :show-center-play-btn="true" | ||
| 121 | + :show-play-btn="true" | ||
| 122 | + :object-fit="'contain'" | ||
| 123 | + :show-fullscreen-btn="true" | ||
| 124 | + style="width: 100vw; height: 50vh; position: absolute; top: 20vh; left: 0;" | ||
| 125 | + @tap.stop | ||
| 126 | + @play="handleVideoPlay" | ||
| 127 | + @pause="handleVideoPause" | ||
| 128 | + @error="handleVideoError" | ||
| 129 | + @fullscreenchange="handleFullscreenChange" | ||
| 130 | + /> | ||
| 131 | + </view> | ||
| 132 | + | ||
| 133 | + <!-- 图片预览 --> | ||
| 134 | + <nut-image-preview | ||
| 135 | + v-model:show="previewVisible" | ||
| 136 | + :images="previewImages" | ||
| 137 | + :init-no="previewIndex" | ||
| 138 | + :show-index="false" | ||
| 139 | + @close="closePreview" | ||
| 140 | + /> | ||
| 141 | + </view> | ||
| 142 | +</template> | ||
| 143 | + | ||
| 144 | +<script setup> | ||
| 145 | +import { ref, defineProps, defineEmits } from 'vue'; | ||
| 146 | +import Taro from '@tarojs/taro'; | ||
| 147 | +import { Photograph, Close } from '@nutui/icons-vue-taro'; | ||
| 148 | +import BASE_URL from '@/utils/config'; | ||
| 149 | +import { savePhotoAPI } from '@/api/photo'; | ||
| 150 | + | ||
| 151 | +// 定义props | ||
| 152 | +const props = defineProps({ | ||
| 153 | + from: { | ||
| 154 | + type: String, | ||
| 155 | + default: '' | ||
| 156 | + }, | ||
| 157 | + id: { | ||
| 158 | + type: String, | ||
| 159 | + default: '' | ||
| 160 | + } | ||
| 161 | +}); | ||
| 162 | + | ||
| 163 | +// 定义emits | ||
| 164 | +const emit = defineEmits(['success', 'close']); | ||
| 165 | + | ||
| 166 | +// 播放图标 | ||
| 167 | +const playIcon = 'https://cdn.ipadbiz.cn/lls_prog/icon/play.svg'; | ||
| 168 | +const callbackUrl = BASE_URL + '/srv/?f=walk&a=media&t=qiniu_audit_notify' | ||
| 169 | + | ||
| 170 | +// 响应式数据 | ||
| 171 | +const uploadedFile = ref(null); | ||
| 172 | +const videoVisible = ref(false); | ||
| 173 | +const videoId = ref(Date.now()); | ||
| 174 | + | ||
| 175 | +// 图片预览相关 | ||
| 176 | +const previewVisible = ref(false); | ||
| 177 | +const previewImages = ref([]); | ||
| 178 | +const previewIndex = ref(0); | ||
| 179 | + | ||
| 180 | +/** | ||
| 181 | + * 选择媒体文件(图片或视频) | ||
| 182 | + */ | ||
| 183 | +const chooseMedia = () => { | ||
| 184 | + Taro.chooseMedia({ | ||
| 185 | + count: 1, | ||
| 186 | + mediaType: ['image', 'video'], | ||
| 187 | + sourceType: ['album', 'camera'], | ||
| 188 | + maxDuration: 60, | ||
| 189 | + sizeType: ['compressed'], | ||
| 190 | + camera: 'back', | ||
| 191 | + success: async (res) => { | ||
| 192 | + const file = res.tempFiles[0]; | ||
| 193 | + | ||
| 194 | + // 检查文件大小(仅对图片进行10MB限制,视频不检查大小) | ||
| 195 | + if (file.fileType === 'image' && file.size > 10 * 1024 * 1024) { | ||
| 196 | + Taro.showToast({ | ||
| 197 | + title: '图片大小不能超过10MB', | ||
| 198 | + icon: 'none', | ||
| 199 | + duration: 2000 | ||
| 200 | + }); | ||
| 201 | + return; | ||
| 202 | + } | ||
| 203 | + | ||
| 204 | + // 检查视频长度(仅对视频进行60秒限制) | ||
| 205 | + if (file.fileType === 'video' && file.duration > 60) { | ||
| 206 | + Taro.showToast({ | ||
| 207 | + title: '视频时长不能超过60秒', | ||
| 208 | + icon: 'none', | ||
| 209 | + duration: 2000 | ||
| 210 | + }); | ||
| 211 | + return; | ||
| 212 | + } | ||
| 213 | + | ||
| 214 | + // 显示上传进度 | ||
| 215 | + Taro.showLoading({ title: '上传中...' }); | ||
| 216 | + | ||
| 217 | + try { | ||
| 218 | + // 立即上传文件到服务器 | ||
| 219 | + const serverUrl = await uploadFileToServer(file.tempFilePath, file.fileType); | ||
| 220 | + | ||
| 221 | + // 根据文件类型设置不同的信息,包含服务器URL | ||
| 222 | + if (file.fileType === 'image') { | ||
| 223 | + uploadedFile.value = { | ||
| 224 | + type: 'image', | ||
| 225 | + url: file.tempFilePath, | ||
| 226 | + serverUrl: serverUrl.src, | ||
| 227 | + qiniu_audit: serverUrl.qiniu_audit, | ||
| 228 | + size: file.size, | ||
| 229 | + name: `image_${Date.now()}.jpg` | ||
| 230 | + }; | ||
| 231 | + } else if (file.fileType === 'video') { | ||
| 232 | + uploadedFile.value = { | ||
| 233 | + type: 'video', | ||
| 234 | + url: file.tempFilePath, | ||
| 235 | + serverUrl: serverUrl.src, | ||
| 236 | + qiniu_audit: serverUrl.qiniu_audit, | ||
| 237 | + thumbnail: file.thumbTempFilePath, | ||
| 238 | + duration: Math.floor(file.duration), | ||
| 239 | + size: file.size, | ||
| 240 | + name: `video_${Date.now()}.mp4`, | ||
| 241 | + }; | ||
| 242 | + } | ||
| 243 | + | ||
| 244 | + Taro.hideLoading(); | ||
| 245 | + Taro.showToast({ | ||
| 246 | + title: '上传成功', | ||
| 247 | + icon: 'success', | ||
| 248 | + duration: 1500 | ||
| 249 | + }); | ||
| 250 | + } catch (error) { | ||
| 251 | + console.error('上传失败:', error); | ||
| 252 | + Taro.hideLoading(); | ||
| 253 | + Taro.showToast({ | ||
| 254 | + title: '上传失败,请重试', | ||
| 255 | + icon: 'none', | ||
| 256 | + duration: 2000 | ||
| 257 | + }); | ||
| 258 | + } | ||
| 259 | + }, | ||
| 260 | + fail: (err) => { | ||
| 261 | + console.error('选择媒体文件失败:', err); | ||
| 262 | + Taro.showToast({ | ||
| 263 | + title: '选择文件失败', | ||
| 264 | + icon: 'error', | ||
| 265 | + duration: 2000 | ||
| 266 | + }); | ||
| 267 | + } | ||
| 268 | + }); | ||
| 269 | +}; | ||
| 270 | + | ||
| 271 | +/** | ||
| 272 | + * 移除文件 | ||
| 273 | + */ | ||
| 274 | +const removeFile = () => { | ||
| 275 | + uploadedFile.value = null; | ||
| 276 | +}; | ||
| 277 | + | ||
| 278 | +/** | ||
| 279 | + * 播放视频 | ||
| 280 | + */ | ||
| 281 | +const playVideo = () => { | ||
| 282 | + if (uploadedFile.value && uploadedFile.value.type === 'video') { | ||
| 283 | + videoId.value = Date.now(); | ||
| 284 | + videoVisible.value = true; | ||
| 285 | + } | ||
| 286 | +}; | ||
| 287 | + | ||
| 288 | +/** | ||
| 289 | + * 关闭视频播放 | ||
| 290 | + */ | ||
| 291 | +const closeVideo = () => { | ||
| 292 | + videoVisible.value = false; | ||
| 293 | +}; | ||
| 294 | + | ||
| 295 | +/** | ||
| 296 | + * 预览图片 | ||
| 297 | + */ | ||
| 298 | +const previewImage = () => { | ||
| 299 | + if (!uploadedFile.value || uploadedFile.value.type !== 'image') { | ||
| 300 | + Taro.showToast({ | ||
| 301 | + title: '暂无图片可预览', | ||
| 302 | + icon: 'error', | ||
| 303 | + duration: 2000 | ||
| 304 | + }); | ||
| 305 | + return; | ||
| 306 | + } | ||
| 307 | + previewImages.value = [{ src: uploadedFile.value.url }]; | ||
| 308 | + previewIndex.value = 0; | ||
| 309 | + previewVisible.value = true; | ||
| 310 | +}; | ||
| 311 | + | ||
| 312 | +/** | ||
| 313 | + * 关闭图片预览 | ||
| 314 | + */ | ||
| 315 | +const closePreview = () => { | ||
| 316 | + previewVisible.value = false; | ||
| 317 | +}; | ||
| 318 | + | ||
| 319 | +/** | ||
| 320 | + * 处理视频播放 | ||
| 321 | + */ | ||
| 322 | +const handleVideoPlay = () => { | ||
| 323 | + console.log('视频开始播放'); | ||
| 324 | +}; | ||
| 325 | + | ||
| 326 | +/** | ||
| 327 | + * 处理视频暂停 | ||
| 328 | + */ | ||
| 329 | +const handleVideoPause = () => { | ||
| 330 | + console.log('视频暂停播放'); | ||
| 331 | +}; | ||
| 332 | + | ||
| 333 | +/** | ||
| 334 | + * 处理全屏状态变化 | ||
| 335 | + * @param {Event} event - 全屏事件 | ||
| 336 | + */ | ||
| 337 | +const handleFullscreenChange = (event) => { | ||
| 338 | + console.log('全屏状态变化:', event.detail); | ||
| 339 | +}; | ||
| 340 | + | ||
| 341 | +/** | ||
| 342 | + * 处理视频播放错误 | ||
| 343 | + * @param {Event} error - 错误事件 | ||
| 344 | + */ | ||
| 345 | +const handleVideoError = (error) => { | ||
| 346 | + console.error('视频播放错误:', error); | ||
| 347 | + Taro.showToast({ | ||
| 348 | + title: '视频播放失败', | ||
| 349 | + icon: 'error', | ||
| 350 | + duration: 2000 | ||
| 351 | + }); | ||
| 352 | + closeVideo(); | ||
| 353 | +}; | ||
| 354 | + | ||
| 355 | +/** | ||
| 356 | + * 格式化文件大小 | ||
| 357 | + * @param {number} bytes - 字节数 | ||
| 358 | + * @returns {string} 格式化后的文件大小 | ||
| 359 | + */ | ||
| 360 | +const formatFileSize = (bytes) => { | ||
| 361 | + if (bytes === 0) return '0 B'; | ||
| 362 | + const k = 1024; | ||
| 363 | + const sizes = ['B', 'KB', 'MB', 'GB']; | ||
| 364 | + const i = Math.floor(Math.log(bytes) / Math.log(k)); | ||
| 365 | + return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; | ||
| 366 | +}; | ||
| 367 | + | ||
| 368 | +/** | ||
| 369 | + * 格式化视频时长 | ||
| 370 | + * @param {number} seconds - 秒数 | ||
| 371 | + * @returns {string} 格式化后的时长 | ||
| 372 | + */ | ||
| 373 | +const formatDuration = (seconds) => { | ||
| 374 | + const minutes = Math.floor(seconds / 60); | ||
| 375 | + const remainingSeconds = seconds % 60; | ||
| 376 | + return `${minutes}:${remainingSeconds.toString().padStart(2, '0')}`; | ||
| 377 | +}; | ||
| 378 | + | ||
| 379 | +/** | ||
| 380 | + * 上传文件到服务器 | ||
| 381 | + * @param {string} filePath - 文件路径 | ||
| 382 | + * @param {string} fileType - 文件类型 | ||
| 383 | + */ | ||
| 384 | +const uploadFileToServer = (filePath, fileType) => { | ||
| 385 | + return new Promise((resolve, reject) => { | ||
| 386 | + // 视频上传需要判断回调 | ||
| 387 | + let video_params = '' | ||
| 388 | + if (fileType === 'video') { | ||
| 389 | + video_params = '&callback_rul=' + encodeURIComponent(callbackUrl) | ||
| 390 | + } | ||
| 391 | + Taro.uploadFile({ | ||
| 392 | + url: BASE_URL + '/admin/?m=srv&a=upload&image_audit=1' + video_params, | ||
| 393 | + filePath, | ||
| 394 | + name: 'file', | ||
| 395 | + header: { | ||
| 396 | + 'content-type': 'multipart/form-data', | ||
| 397 | + }, | ||
| 398 | + success: function (res) { | ||
| 399 | + try { | ||
| 400 | + const upload_data = JSON.parse(res.data); | ||
| 401 | + if (upload_data.code === 0 && upload_data.data) { | ||
| 402 | + resolve({ src: upload_data.data.src, qiniu_audit: upload_data.data.audit_result}); | ||
| 403 | + } else { | ||
| 404 | + reject(new Error(upload_data.msg || '服务器错误')); | ||
| 405 | + } | ||
| 406 | + } catch (error) { | ||
| 407 | + reject(new Error('解析响应数据失败')); | ||
| 408 | + } | ||
| 409 | + }, | ||
| 410 | + fail: function (error) { | ||
| 411 | + reject(error); | ||
| 412 | + } | ||
| 413 | + }); | ||
| 414 | + }); | ||
| 415 | +}; | ||
| 416 | + | ||
| 417 | +/** | ||
| 418 | + * 保存媒体文件 | ||
| 419 | + */ | ||
| 420 | +const saveMedia = async () => { | ||
| 421 | + if (!uploadedFile.value) { | ||
| 422 | + Taro.showToast({ | ||
| 423 | + title: '请先选择文件', | ||
| 424 | + icon: 'error', | ||
| 425 | + duration: 2000 | ||
| 426 | + }); | ||
| 427 | + return; | ||
| 428 | + } | ||
| 429 | + | ||
| 430 | + if (!uploadedFile.value.serverUrl) { | ||
| 431 | + Taro.showToast({ | ||
| 432 | + title: '文件上传未完成,请重新选择', | ||
| 433 | + icon: 'error', | ||
| 434 | + duration: 2000 | ||
| 435 | + }); | ||
| 436 | + return; | ||
| 437 | + } | ||
| 438 | + | ||
| 439 | + Taro.showLoading({ | ||
| 440 | + title: '保存中...', | ||
| 441 | + mask: true | ||
| 442 | + }); | ||
| 443 | + | ||
| 444 | + try { | ||
| 445 | + // 调用后端接口保存媒体信息 | ||
| 446 | + const saveData = { | ||
| 447 | + media_type: uploadedFile.value.type === 'image' ? 'IMAGE' : 'VIDEO', | ||
| 448 | + media_url: uploadedFile.value.serverUrl, | ||
| 449 | + source_type: props.from === 'checkin' ? 'CHECK_IN' : 'COMPANION', | ||
| 450 | + source_id: props.id || '0', | ||
| 451 | + qiniu_audit: uploadedFile.value.qiniu_audit || '', | ||
| 452 | + }; | ||
| 453 | + | ||
| 454 | + const result = await savePhotoAPI(saveData); | ||
| 455 | + | ||
| 456 | + if (result.code) { | ||
| 457 | + Taro.hideLoading(); | ||
| 458 | + Taro.showToast({ | ||
| 459 | + title: '保存成功', | ||
| 460 | + icon: 'success', | ||
| 461 | + duration: 2000 | ||
| 462 | + }); | ||
| 463 | + | ||
| 464 | + // 触发成功事件,传递保存结果 | ||
| 465 | + emit('success', { | ||
| 466 | + ...saveData, | ||
| 467 | + result: result | ||
| 468 | + }); | ||
| 469 | + | ||
| 470 | + // 清空上传的文件 | ||
| 471 | + uploadedFile.value = null; | ||
| 472 | + } else { | ||
| 473 | + throw new Error(result.msg || '保存失败'); | ||
| 474 | + } | ||
| 475 | + } catch (error) { | ||
| 476 | + console.error('保存失败:', error); | ||
| 477 | + Taro.hideLoading(); | ||
| 478 | + Taro.showToast({ | ||
| 479 | + title: error.message || '保存失败,请重试', | ||
| 480 | + icon: 'error', | ||
| 481 | + duration: 2000 | ||
| 482 | + }); | ||
| 483 | + } | ||
| 484 | +}; | ||
| 485 | +</script> | ||
| 486 | + | ||
| 487 | +<style lang="less" scoped> | ||
| 488 | +.upload-media-component { | ||
| 489 | + height: 100%; | ||
| 490 | + background-color: #f8f9fa; | ||
| 491 | + overflow-y: auto; | ||
| 492 | +} | ||
| 493 | +</style> | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
src/components/UploadMediaPopup.vue
0 → 100644
| 1 | +<template> | ||
| 2 | + <PopupWrapper | ||
| 3 | + v-model:visible="popupVisible" | ||
| 4 | + position="right" | ||
| 5 | + title="上传照片/视频" | ||
| 6 | + :fullscreen="true" | ||
| 7 | + :close-on-click-overlay="false" | ||
| 8 | + @close="handleClose" | ||
| 9 | + > | ||
| 10 | + <UploadMediaComponent | ||
| 11 | + :from="from" | ||
| 12 | + :id="id" | ||
| 13 | + @success="handleSuccess" | ||
| 14 | + @close="handleClose" | ||
| 15 | + /> | ||
| 16 | + </PopupWrapper> | ||
| 17 | +</template> | ||
| 18 | + | ||
| 19 | +<script setup> | ||
| 20 | +import { ref, defineProps, defineEmits, watch } from 'vue'; | ||
| 21 | +import PopupWrapper from '@/components/PopupWrapper.vue'; | ||
| 22 | +import UploadMediaComponent from '@/components/UploadMediaComponent.vue'; | ||
| 23 | + | ||
| 24 | +// 定义props | ||
| 25 | +const props = defineProps({ | ||
| 26 | + // 控制弹窗显示/隐藏 | ||
| 27 | + visible: { | ||
| 28 | + type: Boolean, | ||
| 29 | + default: false | ||
| 30 | + }, | ||
| 31 | + // 来源标识 | ||
| 32 | + from: { | ||
| 33 | + type: String, | ||
| 34 | + default: '' | ||
| 35 | + }, | ||
| 36 | + // ID参数 | ||
| 37 | + id: { | ||
| 38 | + type: String, | ||
| 39 | + default: '' | ||
| 40 | + } | ||
| 41 | +}); | ||
| 42 | + | ||
| 43 | +// 定义emits | ||
| 44 | +const emit = defineEmits(['update:visible', 'success', 'close']); | ||
| 45 | + | ||
| 46 | +// 内部弹窗状态 | ||
| 47 | +const popupVisible = ref(false); | ||
| 48 | + | ||
| 49 | +// 监听外部visible变化 | ||
| 50 | +watch(() => props.visible, (newVal) => { | ||
| 51 | + popupVisible.value = newVal; | ||
| 52 | +}, { immediate: true }); | ||
| 53 | + | ||
| 54 | +// 监听内部弹窗状态变化 | ||
| 55 | +watch(popupVisible, (newVal) => { | ||
| 56 | + emit('update:visible', newVal); | ||
| 57 | +}); | ||
| 58 | + | ||
| 59 | +/** | ||
| 60 | + * 处理上传成功 | ||
| 61 | + * @param {Object} data - 上传成功的数据 | ||
| 62 | + */ | ||
| 63 | +const handleSuccess = (data) => { | ||
| 64 | + emit('success', data); | ||
| 65 | + handleClose(); | ||
| 66 | +}; | ||
| 67 | + | ||
| 68 | +/** | ||
| 69 | + * 处理关闭弹窗 | ||
| 70 | + */ | ||
| 71 | +const handleClose = () => { | ||
| 72 | + popupVisible.value = false; | ||
| 73 | + emit('close'); | ||
| 74 | +}; | ||
| 75 | +</script> | ||
| 76 | + | ||
| 77 | +<style lang="less" scoped> | ||
| 78 | +// 组件样式由PopupWrapper和UploadMediaComponent提供 | ||
| 79 | +</style> | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment