feat(community): 友圈动态支持上传短视频,可与图片同发

- 新增 /api/upload/video 独立上传接口:仅 MP4/MOV、单条 30MB、独立限流、登录鉴权

- posts 表新增 video_url / video_cover,发布接口支持视频与图片同时落库

- 小程序发布器支持选择/预览/删除视频(≤60 秒),动态卡片与详情页支持播放

- 后台动态管理新增视频预览弹窗

- 新增短视频链路与上传鉴权回归测试,125 套件/818 用例全绿
这个提交包含在:
xcbyy
2026-10-31 10:43:43 +08:00
父节点 1681d5d086
当前提交 23f500f623
共有 11 个文件被更改,包括 478 次插入 和 36 次删除

查看文件

@@ -1008,13 +1008,15 @@ function authHeader() {
}
/**
* 真实上传本地图片至服务端持久化静态存储
* @param {string} tempFilePath 本地图片临时路径 (如 wxfile://tmp_xxx 或 blob:xxx)
* @returns {Promise<string>} 返回持久化公网图片访问路径 (形如 /uploads/img_xxx.png)
* 上传本地临时文件至服务端持久化静态存储
* @param {string} tempFilePath 本地临时路径 (如 wxfile://tmp_xxx 或 blob:xxx)
* @param {string} endpoint 上传接口路径 (图片 / 视频分别使用独立接口)
* @param {string} kindLabel 媒体类型中文名,用于错误提示
* @returns {Promise<string>} 返回持久化公网访问路径 (形如 /uploads/xxx)
*/
export function uploadImage(tempFilePath) {
function uploadMediaFile(tempFilePath, endpoint, kindLabel) {
return new Promise((resolve, reject) => {
if (!tempFilePath) return reject(new Error('图片路径为空'));
if (!tempFilePath) return reject(new Error(`${kindLabel}路径为空`));
// 微信开发者工具临时路径形如 http://tmp/ 或 http://usr/
// 微信真机临时路径形如 wxfile://tmp_xxx 或 wxfile://clientdata/xxx
@@ -1042,10 +1044,10 @@ export function uploadImage(tempFilePath) {
}
// #ifdef H5
const uploadUrl = '/api/upload';
const uploadUrl = endpoint;
// #endif
// #ifndef H5
const uploadUrl = 'https://peiww.gynhc.com/api/upload';
const uploadUrl = `https://peiww.gynhc.com${endpoint}`;
// #endif
uni.uploadFile({
@@ -1058,7 +1060,7 @@ export function uploadImage(tempFilePath) {
const data = JSON.parse(uploadRes.data);
if (data && data.code === 20000 && data.data && data.data.url) {
let finalUrl = data.data.url;
// 非H5平台(小程序端)图片访问需补全公网域名
// 非H5平台(小程序端)媒体访问需补全公网域名
// #ifndef H5
if (finalUrl.startsWith('/uploads/')) {
finalUrl = `https://peiww.gynhc.com${finalUrl}`;
@@ -1066,14 +1068,14 @@ export function uploadImage(tempFilePath) {
// #endif
resolve(finalUrl);
} else {
reject(new Error((data && data.message) || '上传解析异常'));
reject(new Error((data && data.message) || `${kindLabel}上传解析异常`));
}
} catch (e) {
reject(new Error('上传数据返回格式错误'));
}
},
fail: (err) => {
reject(new Error(err.errMsg || '图片上传网络异常'));
reject(new Error(err.errMsg || `${kindLabel}上传网络异常`));
}
});
});
@@ -1081,6 +1083,25 @@ export function uploadImage(tempFilePath) {
/**
* 客户端前端前置检测文本安全 (对接 security.msgSecCheck)
/**
* 真实上传本地图片至服务端持久化静态存储
* @param {string} tempFilePath 本地图片临时路径
* @returns {Promise<string>} 返回持久化公网图片访问路径 (形如 /uploads/img_xxx.png)
*/
export function uploadImage(tempFilePath) {
return uploadMediaFile(tempFilePath, '/api/upload', '图片');
}
/**
* 真实上传本地短视频至服务端持久化静态存储
* @param {string} tempFilePath 本地视频临时路径
* @returns {Promise<string>} 返回持久化公网视频访问路径 (形如 /uploads/video_xxx.mp4)
*/
export function uploadVideo(tempFilePath) {
return uploadMediaFile(tempFilePath, '/api/upload/video', '视频');
}
*/
export async function checkTextSecurity(content) {
try {

查看文件

@@ -71,6 +71,13 @@ export const ICONS = {
</svg>
`),
videoCamera: svgToUri(`
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="#94A3B8" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<rect x="2" y="6" width="13" height="12" rx="2"></rect>
<path d="M15 10.5l6-3.5v10l-6-3.5"></path>
</svg>
`),
plus: svgToUri(`
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="#94A3B8" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<line x1="12" y1="5" x2="12" y2="19"></line>

查看文件

@@ -76,6 +76,19 @@
<text v-for="t in post.topics" :key="t" class="detail-topic-pill">#{{ t }}</text>
</view>
<!-- 短视频播放器 -->
<view class="detail-video-wrap" v-if="post.videoUrl">
<video
class="detail-video"
:src="post.videoUrl"
:poster="post.videoCover || ''"
object-fit="contain"
controls
:show-center-play-btn="true"
:enable-progress-gesture="true"
/>
</view>
<!-- 高清图片展示 -->
<view class="detail-images-grid" v-if="post.images && post.images.length">
<image
@@ -896,6 +909,19 @@ onShow(() => {
}
}
.detail-video-wrap {
margin-bottom: 28rpx;
border-radius: 20rpx;
overflow: hidden;
background: #000000;
.detail-video {
width: 100%;
height: 420rpx;
display: block;
}
}
.detail-images-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);

查看文件

@@ -123,6 +123,20 @@
<text v-for="t in post.topics" :key="t" class="topic-item">#{{ t }}</text>
</view>
<!-- 短视频(可与图片同发) -->
<view class="post-video-wrap" v-if="post.videoUrl">
<video
class="post-video"
:src="post.videoUrl"
:poster="post.videoCover || ''"
object-fit="cover"
controls
:show-center-play-btn="true"
:enable-progress-gesture="true"
:show-fullscreen-btn="true"
/>
</view>
<!-- 图片九宫格 / 展位 -->
<view class="post-images" v-if="post.images && post.images.length">
<image
@@ -224,9 +238,25 @@
</view>
</view>
<!-- 图片九宫格选择区 (小红书/朋友圈现代画廊式) -->
<!-- 图片 / 短视频选择区 (小红书/朋友圈现代画廊式,支持视频与图片同发) -->
<view class="gallery-section">
<view class="gallery-grid">
<!-- 已选视频预览 -->
<view class="grid-img-item grid-video-item" v-if="newPostForm.video">
<video
class="thumb-video"
:src="newPostForm.video.path"
:poster="newPostForm.video.cover || ''"
object-fit="cover"
:controls="false"
/>
<view class="video-duration-badge">
<text class="video-duration-txt">{{ formatVideoDuration(newPostForm.video.duration) }}</text>
</view>
<view class="remove-btn btn-active" @click.stop="removeVideo">
<image class="remove-icon" :src="ICONS.close" mode="aspectFit" />
</view>
</view>
<view
class="grid-img-item"
v-for="(img, idx) in newPostForm.images"
@@ -239,7 +269,7 @@
</view>
<view
class="add-img-tile btn-active"
v-if="newPostForm.images.length < 9"
v-if="!newPostForm.video && newPostForm.images.length < 9"
@click="choosePostImage"
>
<view class="tile-icon-circle">
@@ -248,6 +278,17 @@
<text class="tile-label">添加图片</text>
<text class="tile-count">{{ newPostForm.images.length }}/9</text>
</view>
<view
class="add-img-tile add-video-tile btn-active"
v-if="!newPostForm.video"
@click="choosePostVideo"
>
<view class="tile-icon-circle">
<image class="cam-icon" :src="ICONS.videoCamera" mode="aspectFit" />
</view>
<text class="tile-label">添加视频</text>
<text class="tile-count">≤60秒</text>
</view>
</view>
</view>
@@ -405,7 +446,7 @@ import { ICONS } from '@/common/icons.js';
import { getCurrentThemeKey, applyThemeToNativeUI } from '@/common/theme.js';
import { ensureLoginOrRedirect } from '@/common/role.js';
import { getNavBarInfo } from '@/common/nav.js';
import { getPosts, createPost, deletePost, likePost, viewPost, getPostComments, addPostComment, applyPostParty, getPostApplies, handlePostApply, uploadImage, getPlatformConfigs, getUserProfile, normalizeUserId } from '@/common/api.js';
import { getPosts, createPost, deletePost, likePost, viewPost, getPostComments, addPostComment, applyPostParty, getPostApplies, handlePostApply, uploadImage, uploadVideo, getPlatformConfigs, getUserProfile, normalizeUserId } from '@/common/api.js';
import { useShare } from '@/common/share.js';
useShare({
@@ -997,6 +1038,7 @@ const newPostForm = ref({
content: '',
selectedTopics: [],
images: [],
video: null,
location: ''
});
@@ -1045,6 +1087,49 @@ const choosePostImage = () => {
});
};
const MAX_VIDEO_SIZE = 30 * 1024 * 1024;
const formatVideoDuration = (seconds) => {
const total = Math.max(0, Math.floor(Number(seconds) || 0));
const mm = String(Math.floor(total / 60)).padStart(2, '0');
const ss = String(total % 60).padStart(2, '0');
return `${mm}:${ss}`;
};
const choosePostVideo = () => {
if (newPostForm.value.video) {
uni.showToast({ title: '每条动态最多上传 1 个视频', icon: 'none' });
return;
}
uni.chooseVideo({
sourceType: ['album', 'camera'],
compressed: true,
maxDuration: 60,
camera: 'back',
success: (res) => {
if (res.size && res.size > MAX_VIDEO_SIZE) {
uni.showToast({ title: '视频超过 30MB,请压缩或选择更短的视频', icon: 'none' });
return;
}
newPostForm.value.video = {
path: res.tempFilePath,
cover: res.thumbTempFilePath || '',
duration: Number(res.duration) || 0,
size: Number(res.size) || 0
};
},
fail: (err) => {
// 用户主动取消不提示
if (err && String(err.errMsg || '').indexOf('cancel') > -1) return;
uni.showToast({ title: '视频选择失败,请重试', icon: 'none' });
}
});
};
const removeVideo = () => {
newPostForm.value.video = null;
};
const removeImage = (idx) => {
newPostForm.value.images.splice(idx, 1);
};
@@ -1110,16 +1195,30 @@ const submitPublishPost = async () => {
const authorAvatar = profile.avatar || '';
let finalImages = [];
let finalVideoUrl = '';
let finalVideoCover = '';
try {
if (newPostForm.value.images.length > 0) {
finalImages = await Promise.all(
newPostForm.value.images.map(img => uploadImage(img))
);
}
if (newPostForm.value.video) {
// 先传封面再传视频,封面失败不阻塞视频发布
if (newPostForm.value.video.cover) {
try {
finalVideoCover = await uploadImage(newPostForm.value.video.cover);
} catch (coverErr) {
console.warn('[Community] 视频封面上传失败,继续发布:', coverErr);
finalVideoCover = '';
}
}
finalVideoUrl = await uploadVideo(newPostForm.value.video.path);
}
} catch (uploadErr) {
console.warn('[Community] 图片上传失败:', uploadErr);
console.warn('[Community] 媒体上传失败:', uploadErr);
uni.hideLoading();
uni.showToast({ title: '图片上传失败,请重试后再发布', icon: 'none' });
uni.showToast({ title: '图片或视频上传失败,请重试后再发布', icon: 'none' });
return;
}
@@ -1133,6 +1232,8 @@ const submitPublishPost = async () => {
content: content,
topics: [...newPostForm.value.selectedTopics],
images: finalImages,
videoUrl: finalVideoUrl || undefined,
videoCover: finalVideoCover || undefined,
actionText: currentMeta.action,
category: newPostForm.value.category
};
@@ -1155,6 +1256,8 @@ const submitPublishPost = async () => {
content: content,
topics: postPayload.topics,
images: finalImages,
videoUrl: finalVideoUrl,
videoCover: finalVideoCover,
actionText: currentMeta.action,
likes: 0,
liked: false,
@@ -1175,6 +1278,7 @@ const submitPublishPost = async () => {
closePublishModal();
newPostForm.value.content = '';
newPostForm.value.images = [];
newPostForm.value.video = null;
newPostForm.value.selectedTopics = ['三里屯探店'];
uni.hideLoading();
@@ -1821,6 +1925,19 @@ const submitPublishPost = async () => {
}
}
.post-video-wrap {
margin-bottom: 18rpx;
border-radius: 18rpx;
overflow: hidden;
background: #000000;
.post-video {
width: 100%;
height: 380rpx;
display: block;
}
}
.post-footer {
display: flex;
align-items: center;
@@ -2182,6 +2299,31 @@ const submitPublishPost = async () => {
}
}
}
/* 视频预览时长角标(主题无关) */
.grid-video-item {
.thumb-video {
width: 100%;
height: 100%;
display: block;
}
.video-duration-badge {
position: absolute;
left: 10rpx;
bottom: 10rpx;
padding: 2rpx 12rpx;
border-radius: 999rpx;
background: rgba(15, 23, 42, 0.72);
z-index: 2;
.video-duration-txt {
font-size: 18rpx;
color: #FFFFFF;
font-weight: 600;
}
}
}
}
/* 定位信息微胶囊 */