feat(detail): 描述 Tab 头部改「拆约推荐+主题价值按钮」+灯泡内联价值文字+下接描述;后端新增 theme_analysis_cache 缓存表与 GET 端点,进 Tab 静默拉取
This commit is contained in:
@@ -47,6 +47,15 @@ const getThemeAnalysis = (activityId) => {
|
||||
return post(`/ai/theme-analysis/${activityId}`, {}, { timeout: 30000, showLoading: false });
|
||||
};
|
||||
|
||||
/**
|
||||
* 主题价值缓存读取(描述 Tab 进入时静默拉取)
|
||||
* @param {string} activityId - 活动 ID
|
||||
* @returns {Promise}
|
||||
*/
|
||||
const getThemeAnalysisCache = (activityId) => {
|
||||
return get(`/ai/theme-analysis/${activityId}`, { timeout: 10000, showLoading: false });
|
||||
};
|
||||
|
||||
/**
|
||||
* 合作可能性分析
|
||||
* @param {string} targetUserId - 目标用户 ID
|
||||
@@ -63,4 +72,5 @@ module.exports = {
|
||||
refreshAIRecommendations,
|
||||
getCollaborationAnalysis,
|
||||
getThemeAnalysis,
|
||||
getThemeAnalysisCache,
|
||||
};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// pages/activity-detail/index.js
|
||||
const { getActivityDetail, joinActivity, cancelJoin, shareActivity } = require('../../api/activity');
|
||||
const { getAIRecommendations, refreshAIRecommendations, getThemeAnalysis } = require('../../api/ai');
|
||||
const { getAIRecommendations, refreshAIRecommendations, getThemeAnalysis, getThemeAnalysisCache } = require('../../api/ai');
|
||||
const { getFullImageUrl, formatDateTime, formatDuration } = require('../../utils/helpers');
|
||||
const { isLoggedIn, getIsAdmin } = require('../../utils/auth');
|
||||
|
||||
@@ -159,20 +159,33 @@ Page({
|
||||
const tab = e.currentTarget.dataset.tab;
|
||||
if (tab && tab !== this.data.activeTab) {
|
||||
this.setData({ activeTab: tab });
|
||||
// 进入描述 Tab:静默拉取主题价值缓存(不弹 loading、不报错)
|
||||
if (tab === 'description') {
|
||||
this.loadThemeAnalysisQuietly();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
// 组织者卡片上的「主题价值」入口
|
||||
// 静默加载主题价值缓存(进 Tab 时;无缓存不生成,等用户点按钮)
|
||||
async loadThemeAnalysisQuietly() {
|
||||
if (this.data.themeAnalysis || this.data.themeLoading) return;
|
||||
try {
|
||||
const res = await getThemeAnalysisCache(this.data.activityId);
|
||||
if (res.success && res.data && res.data.analysis) {
|
||||
this.setData({ themeAnalysis: res.data.analysis });
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('主题价值缓存读取失败:', err);
|
||||
}
|
||||
},
|
||||
|
||||
// 「主题价值」按钮:强制重新生成并弹窗
|
||||
onThemeTap() {
|
||||
this.showThemeAnalysis();
|
||||
},
|
||||
|
||||
// 打开主题价值弹窗(有缓存用缓存,无则请求)
|
||||
// 生成主题价值(强制重新生成,成功后弹窗+内联更新)
|
||||
async showThemeAnalysis() {
|
||||
if (this.data.themeAnalysis) {
|
||||
this.setData({ themeModalVisible: true });
|
||||
return;
|
||||
}
|
||||
if (this.data.themeLoading) return;
|
||||
this.setData({ themeLoading: true });
|
||||
wx.showLoading({ title: '生成中...', mask: true });
|
||||
|
||||
@@ -131,18 +131,26 @@
|
||||
|
||||
<!-- Tab:活动描述 -->
|
||||
<view class="tab-panel" wx:if="{{activeTab === 'description'}}">
|
||||
<view class="section desc-section">
|
||||
<text class="section-content">{{activity.description}}</text>
|
||||
</view>
|
||||
|
||||
<!-- 主题价值 -->
|
||||
<view class="section theme-section">
|
||||
<view class="ai-section-header">
|
||||
<text class="ai-header-caption">主题价值</text>
|
||||
<text class="ai-header-caption">拆约推荐</text>
|
||||
<view class="ai-header-actions">
|
||||
<button class="theme-btn" bindtap="onThemeTap" disabled="{{themeLoading}}">主题价值</button>
|
||||
<button class="theme-btn" bindtap="onThemeTap" disabled="{{themeLoading}}">
|
||||
<text wx:if="{{!themeLoading}}">主题价值</text>
|
||||
<text wx:else>生成中...</text>
|
||||
</button>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 主题价值:灯泡+简明说明 -->
|
||||
<view class="ai-total-suggestion" wx:if="{{themeAnalysis}}">
|
||||
<text class="ai-total-icon">💡</text>
|
||||
<text class="ai-total-text">{{themeAnalysis}}</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="section desc-section">
|
||||
<text class="section-content">{{activity.description}}</text>
|
||||
</view>
|
||||
|
||||
<!-- 活动图片 -->
|
||||
|
||||
@@ -262,7 +262,15 @@ function initDatabase() {
|
||||
db.prepare('DELETE FROM ai_recommendations').run();
|
||||
console.log('已清空旧版AI推荐缓存');
|
||||
}
|
||||
db.exec('CREATE INDEX IF NOT EXISTS idx_ai_rec_activity_viewer ON ai_recommendations(activity_id, viewer_id)');
|
||||
db.exec('CREATE INDEX IF NOT EXISTS idx_ai_rec_activity_viewer ON ai_recommendations(activity_id, viewer_id)');
|
||||
|
||||
// 迁移:theme_analysis 缓存表(主题价值按活动维度缓存)
|
||||
db.exec(`CREATE TABLE IF NOT EXISTS theme_analysis_cache (
|
||||
activity_id TEXT PRIMARY KEY,
|
||||
analysis TEXT NOT NULL,
|
||||
generated_at TEXT NOT NULL
|
||||
)`);
|
||||
|
||||
|
||||
// 迁移:activities 表添加 fee / organizer 字段
|
||||
const activityInfo = db.prepare('PRAGMA table_info(activities)').all();
|
||||
@@ -2067,7 +2075,19 @@ app.get('/ai/recommendations/:activityId', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// 主题分析(活动详情页组织者「主题分析」按钮)
|
||||
// 主题价值缓存读取(活动详情描述 Tab 进入时静默拉取)
|
||||
app.get('/ai/theme-analysis/:activityId', (req, res) => {
|
||||
try {
|
||||
const { activityId } = req.params;
|
||||
const row = db.prepare('SELECT analysis, generated_at FROM theme_analysis_cache WHERE activity_id = ?').get(activityId);
|
||||
res.json({ success: true, data: row ? { analysis: row.analysis } : null });
|
||||
} catch (error) {
|
||||
console.error('读取主题价值缓存失败:', error);
|
||||
res.json({ success: false, message: '读取失败' });
|
||||
}
|
||||
});
|
||||
|
||||
// 主题分析(活动详情页「主题价值」按钮:强制重新生成)
|
||||
app.post('/ai/theme-analysis/:activityId', async (req, res) => {
|
||||
try {
|
||||
const { activityId } = req.params;
|
||||
@@ -2097,6 +2117,9 @@ app.post('/ai/theme-analysis/:activityId', async (req, res) => {
|
||||
const tagLine = tags.length ? `围绕${tags.join('、')}` : '围绕活动主题';
|
||||
analysis = `${tagLine}的一次线下共拆。区别于单向分享:现场以原文为底、轮流领拆,人人有角色。两小时带走一套拆书方法、几个可复用的案例,以及可能继续组队共读的人。`;
|
||||
}
|
||||
// 写入缓存(活动维度,覆盖旧值)
|
||||
db.prepare('INSERT OR REPLACE INTO theme_analysis_cache (activity_id, analysis, generated_at) VALUES (?, ?, ?)')
|
||||
.run(activityId, analysis, new Date().toISOString());
|
||||
res.json({ success: true, data: { analysis } });
|
||||
} catch (error) {
|
||||
console.error('主题分析失败:', error);
|
||||
|
||||
Reference in New Issue
Block a user