feat: 打卡详情页一键分享到社区功能
- 新增后端接口 POST /api/front/tool/checkin/{checkinId}/share-to-community
- 实现 shareCheckinToCommunity 方法,复制打卡数据到社区帖子表
- 前端修改 handleCopyCheckin 方法,直接调用后端接口创建帖子
- 支持将打卡图片、描述、营养分析数据复制到社区
- 添加重复分享检查,防止重复创建帖子
- 创建成功后显示提示并可跳转到社区详情页
This commit is contained in:
@@ -177,6 +177,14 @@ export function learnCheckin(sourcePostId, data) {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 分享打卡记录到社区
|
||||
* @param {Number} checkinId - 打卡记录ID
|
||||
*/
|
||||
export function shareCheckinToCommunity(checkinId) {
|
||||
return request.post('tool/checkin/' + checkinId + '/share-to-community');
|
||||
}
|
||||
|
||||
// ==================== 食物百科相关 ====================
|
||||
|
||||
/**
|
||||
|
||||
@@ -101,7 +101,7 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { getCheckinDetail } from '@/api/tool.js';
|
||||
import { getCheckinDetail, shareCheckinToCommunity } from '@/api/tool.js';
|
||||
import { mapGetters } from 'vuex';
|
||||
|
||||
export default {
|
||||
@@ -220,18 +220,50 @@ export default {
|
||||
};
|
||||
return map[type] || '🍽️';
|
||||
},
|
||||
handleCopyCheckin() {
|
||||
// 将数据存储到本地缓存,避免URL参数过长
|
||||
uni.setStorageSync('checkin_copy_data', {
|
||||
images: this.checkinData.images,
|
||||
mealType: this.checkinData.mealType,
|
||||
notes: this.checkinData.notes
|
||||
});
|
||||
|
||||
// 跳转到发布页
|
||||
uni.navigateTo({
|
||||
url: '/pages/tool/checkin-publish?mode=copy'
|
||||
async handleCopyCheckin() {
|
||||
if (!this.checkinData.id) {
|
||||
uni.showToast({
|
||||
title: '打卡记录ID不存在',
|
||||
icon: 'none'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
uni.showLoading({
|
||||
title: '分享中...',
|
||||
mask: true
|
||||
});
|
||||
|
||||
try {
|
||||
const res = await shareCheckinToCommunity(this.checkinData.id);
|
||||
uni.hideLoading();
|
||||
|
||||
if (res && res.code === 200) {
|
||||
const postId = res.data && res.data.postId;
|
||||
uni.showModal({
|
||||
title: '分享成功',
|
||||
content: '您的打卡记录已成功分享到社区',
|
||||
confirmText: '去查看',
|
||||
cancelText: '知道了',
|
||||
success: (modalRes) => {
|
||||
if (modalRes.confirm && postId) {
|
||||
uni.navigateTo({
|
||||
url: '/pages/tool/community-detail?id=' + postId
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
throw new Error(res.message || '分享失败');
|
||||
}
|
||||
} catch (e) {
|
||||
uni.hideLoading();
|
||||
console.error('分享到社区失败:', e);
|
||||
uni.showToast({
|
||||
title: e.message || '分享失败,请重试',
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user