diff --git a/api/smoke.js b/api/smoke.js index fec1364..62ab0bc 100644 --- a/api/smoke.js +++ b/api/smoke.js @@ -48,6 +48,14 @@ export function unlockAiAdvice(data) { return request.post('/smoke/ai/advice_unlocks', data) } +export function getAINextSmokeTime(params = {}) { + return request.get('/smoke/ai/next_smoke_time', { mode: 'ai', ...params }) +} + +export function getAIDailySummary(params = {}) { + return request.get('/smoke/ai/daily_summary', params) +} + export function getStats(params = {}) { return request.get('/smoke/stats', params) } diff --git a/pages/index/index.vue b/pages/index/index.vue index 741e978..b3c0985 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -41,12 +41,44 @@ 距上次抽烟 {{ timerDisplay }} + AI ✨ 下次建议: {{ nextSmokeTimeText }} + + + 🤖 + {{ aiLoading ? '生成中...' : 'AI 建议' }} + + + + + + 🤖 今日 AI 计划 + + {{ aiLoading ? '刷新中...' : '刷新' }} + + + + + + {{ node.time }} + + + + + {{ aiAdvice }} + + + @@ -84,6 +116,36 @@ 想抽忍住了 + + + + + 🤖 今日 AI 总结 + + {{ summaryLoading ? '刷新中...' : '刷新' }} + + + + AI 正在分析今日数据... + + + {{ parsedSummary.summary }} + + + · + {{ item }} + + + + 💡 明日建议 + {{ parsedSummary.suggestion }} + + + + + 点击生成今日 AI 总结 + + @@ -113,6 +175,8 @@ const navBarHeight = ref(0) const showDialog = ref(false) const dialogType = ref('smoke') // 'smoke' 或 'resisted' const homeData = ref(null) +const aiLoading = ref(false) +const summaryLoading = ref(false) let timerInterval = null const timerSeconds = ref(0) @@ -221,10 +285,51 @@ const nextSmokeTimeText = computed(() => { return '' }) +const suggestionSource = computed(() => { + return homeTimer.value?.suggestion_source || 'default' +}) + +const aiTimeNodes = computed(() => { + return homeTimer.value?.ai_time_nodes || [] +}) + +const aiAdvice = computed(() => { + return homeTimer.value?.ai_advice || '' +}) + +const aiTimeNodesWithStatus = computed(() => { + const now = new Date() + const nowMinutes = now.getHours() * 60 + now.getMinutes() + let foundCurrent = false + return aiTimeNodes.value.map(time => { + const [h, m] = time.split(':').map(Number) + const nodeMinutes = h * 60 + m + if (foundCurrent) return { time, status: 'future' } + if (nodeMinutes > nowMinutes) { + foundCurrent = true + return { time, status: 'current' } + } + return { time, status: 'past' } + }) +}) + const dialogTitle = computed(() => { return dialogType.value === 'smoke' ? '记录抽烟' : '想抽忍住了' }) +const dailySummaryData = computed(() => { + return homeData.value?.daily_summary?.status === 'available' ? homeData.value.daily_summary : null +}) + +const parsedSummary = computed(() => { + if (!dailySummaryData.value?.content) return {} + try { + return JSON.parse(dailySummaryData.value.content) + } catch { + return { summary: dailySummaryData.value.content, highlights: [], suggestion: '' } + } +}) + function startTimer() { stopTimer() if (timerBaseSeconds.value < 0) { @@ -346,6 +451,151 @@ function closeAiTip() { showAiTip.value = false } +async function handleAISuggest() { + if (aiLoading.value) return + + // 如果已有 AI 数据(今天已解锁),直接刷新 + if (suggestionSource.value === 'ai') { + await fetchAISuggestion() + return + } + + // 需要看广告解锁 + // #ifdef MP-WEIXIN + try { + const videoAd = wx.createRewardedVideoAd({ + adUnitId: 'adunit-fa0b2e5520a39e6a' + }) + videoAd.onClose(async (res) => { + if (res && res.isEnded) { + await unlockAndFetchAI() + } else { + uni.showToast({ title: '需要看完广告哦', icon: 'none' }) + } + }) + videoAd.onError(() => { + // 广告加载失败,直接尝试解锁(可能已是 VIP) + unlockAndFetchAI() + }) + await videoAd.show().catch(async () => { + await videoAd.load() + await videoAd.show() + }) + } catch (e) { + // 非微信环境或广告不可用,直接尝试 + await unlockAndFetchAI() + } + // #endif + // #ifndef MP-WEIXIN + await unlockAndFetchAI() + // #endif +} + +async function unlockAndFetchAI() { + try { + const today = new Date().toISOString().split('T')[0] + await api.unlockAiAdvice({ date: today }) + } catch (e) { + console.error('unlock error:', e) + } + await fetchAISuggestion() +} + +async function fetchAISuggestion() { + aiLoading.value = true + try { + const res = await api.getAINextSmokeTime() + const data = res.data || {} + // 更新 homeData 中的 timer 字段 + if (homeData.value && homeData.value.timer) { + if (data.suggested_at) { + homeData.value.timer.next_suggested_at = data.suggested_at + homeData.value.timer.suggestion_source = 'ai' + const t = new Date(data.suggested_at) + if (!isNaN(t.getTime())) { + homeData.value.timer.next_suggested_clock = `${String(t.getHours()).padStart(2, '0')}:${String(t.getMinutes()).padStart(2, '0')}` + } + } + if (data.time_nodes) { + homeData.value.timer.ai_time_nodes = data.time_nodes + } + if (data.advice) { + homeData.value.timer.ai_advice = data.advice + } + } + uni.showToast({ title: 'AI 计划已生成', icon: 'success' }) + } catch (e) { + console.error('fetchAISuggestion error:', e) + uni.showToast({ title: '生成失败,请稍后重试', icon: 'none' }) + } finally { + aiLoading.value = false + } +} + +async function handleDailySummary() { + if (summaryLoading.value) return + + // 如果已有总结数据,直接刷新(已解锁) + if (dailySummaryData.value) { + await fetchDailySummary() + return + } + + // 需要看广告解锁 + // #ifdef MP-WEIXIN + try { + const videoAd = wx.createRewardedVideoAd({ + adUnitId: 'adunit-fa0b2e5520a39e6a' + }) + videoAd.onClose(async (res) => { + if (res && res.isEnded) { + const today = new Date().toISOString().split('T')[0] + try { await api.unlockAiAdvice({ date: today }) } catch (e) { console.error('unlock error:', e) } + await fetchDailySummary() + } else { + uni.showToast({ title: '需要看完广告哦', icon: 'none' }) + } + }) + videoAd.onError(() => { + fetchDailySummary() + }) + await videoAd.show().catch(async () => { + await videoAd.load() + await videoAd.show() + }) + } catch (e) { + await fetchDailySummary() + } + // #endif + // #ifndef MP-WEIXIN + await fetchDailySummary() + // #endif +} + +async function fetchDailySummary() { + summaryLoading.value = true + try { + const today = new Date().toISOString().split('T')[0] + const res = await api.getAIDailySummary({ date: today }) + const data = res.data || {} + if (homeData.value) { + homeData.value.daily_summary = { + date: data.date || today, + content: data.content || '', + model: data.model || '', + status: 'available' + } + } + uni.showToast({ title: '总结已生成', icon: 'success' }) + } catch (e) { + console.error('fetchDailySummary error:', e) + const msg = e?.data?.message || '生成失败,请稍后重试' + uni.showToast({ title: msg, icon: 'none' }) + } finally { + summaryLoading.value = false + } +} + onMounted(() => { initPage() }) @@ -566,11 +816,48 @@ onShareAppMessage(() => { border: 2rpx solid #D1FAE5; } +.next-time-source { + font-size: 20rpx; + color: #FFFFFF; + background: linear-gradient(135deg, #34D399, #10B981); + padding: 2rpx 12rpx; + border-radius: 16rpx; + margin-right: 8rpx; + font-weight: 600; +} + .next-time-text { font-size: 24rpx; color: #059669; } +.ai-btn-wrap { + display: flex; + justify-content: center; + margin-top: 16rpx; +} + +.ai-suggest-btn { + display: inline-flex; + align-items: center; + gap: 8rpx; + background: linear-gradient(135deg, #34D399, #10B981); + color: #FFFFFF; + padding: 10rpx 28rpx; + border-radius: 32rpx; + font-size: 24rpx; + font-weight: 500; + box-shadow: 0 6rpx 16rpx rgba(16, 185, 129, 0.3); +} + +.ai-suggest-icon { + font-size: 24rpx; +} + +.ai-suggest-text { + font-size: 24rpx; +} + .stats-row { display: flex; gap: 24rpx; @@ -659,6 +946,110 @@ onShareAppMessage(() => { .stat-progress-red { background-color: #EF4444; } .stat-progress-green { background-color: #10B981; } +.ai-plan-card { + background-color: #FFFFFF; + border-radius: 24rpx; + padding: 28rpx; + margin-bottom: 24rpx; + border: 2rpx solid #D9FBE7; + box-shadow: 0 10rpx 22rpx rgba(16, 185, 129, 0.08); +} + +.ai-plan-header { + display: flex; + justify-content: space-between; + align-items: center; + margin-bottom: 24rpx; +} + +.ai-plan-title { + font-size: 28rpx; + font-weight: 600; + color: #111827; +} + +.ai-plan-refresh { + padding: 6rpx 20rpx; + border-radius: 24rpx; + background-color: #ECFDF5; + border: 2rpx solid #D1FAE5; +} + +.ai-plan-refresh-text { + font-size: 22rpx; + color: #059669; +} + +.ai-plan-timeline { + display: flex; + align-items: center; + justify-content: space-between; + padding: 0 8rpx; + margin-bottom: 20rpx; +} + +.ai-plan-node { + display: flex; + flex-direction: column; + align-items: center; + position: relative; + flex: 1; +} + +.node-dot { + width: 20rpx; + height: 20rpx; + border-radius: 50%; + background-color: #D1FAE5; + margin-bottom: 8rpx; +} + +.node-past .node-dot { + background-color: #9CA3AF; +} + +.node-current .node-dot { + background-color: #10B981; + box-shadow: 0 0 12rpx rgba(16, 185, 129, 0.5); + width: 24rpx; + height: 24rpx; +} + +.node-future .node-dot { + background-color: #D1FAE5; +} + +.node-time { + font-size: 22rpx; + color: #9CA3AF; +} + +.node-past .node-time { + color: #9CA3AF; + text-decoration: line-through; +} + +.node-current .node-time { + color: #059669; + font-weight: 600; +} + +.node-future .node-time { + color: #6B7280; +} + +.ai-plan-advice { + background-color: #F0FDF4; + border-radius: 16rpx; + padding: 16rpx 20rpx; +} + +.ai-plan-advice-text { + font-size: 24rpx; + color: #374151; + line-height: 1.6; +} + .action-buttons { display: flex; gap: 24rpx; @@ -692,4 +1083,124 @@ onShareAppMessage(() => { .action-icon { font-size: 32rpx; } + +.ai-summary-card { + background-color: #FFFFFF; + border-radius: 24rpx; + padding: 28rpx; + margin-top: 24rpx; + border: 2rpx solid #D9FBE7; + box-shadow: 0 10rpx 22rpx rgba(16, 185, 129, 0.08); +} + +.ai-summary-header { + display: flex; + justify-content: space-between; + align-items: center; + margin-bottom: 20rpx; +} + +.ai-summary-title { + font-size: 28rpx; + font-weight: 600; + color: #111827; +} + +.ai-summary-action { + padding: 6rpx 20rpx; + border-radius: 24rpx; + background-color: #ECFDF5; + border: 2rpx solid #D1FAE5; +} + +.ai-summary-action-text { + font-size: 22rpx; + color: #059669; +} + +.ai-summary-loading { + padding: 40rpx 0; + text-align: center; +} + +.ai-summary-loading-text { + font-size: 24rpx; + color: #9CA3AF; +} + +.ai-summary-body { + display: flex; + flex-direction: column; + gap: 16rpx; +} + +.ai-summary-text { + font-size: 26rpx; + color: #374151; + line-height: 1.6; +} + +.ai-summary-highlights { + display: flex; + flex-direction: column; + gap: 8rpx; +} + +.ai-summary-highlight { + display: flex; + align-items: flex-start; + gap: 8rpx; +} + +.highlight-dot { + font-size: 28rpx; + color: #10B981; + font-weight: 700; + line-height: 1.5; +} + +.highlight-text { + font-size: 24rpx; + color: #4B5563; + line-height: 1.5; + flex: 1; +} + +.ai-summary-suggestion { + background-color: #F0FDF4; + border-radius: 16rpx; + padding: 16rpx 20rpx; + margin-top: 4rpx; +} + +.suggestion-label { + font-size: 24rpx; + font-weight: 600; + color: #059669; + display: block; + margin-bottom: 6rpx; +} + +.suggestion-text { + font-size: 24rpx; + color: #374151; + line-height: 1.5; +} + +.ai-summary-empty { + display: flex; + align-items: center; + justify-content: center; + gap: 12rpx; + padding: 32rpx 0; +} + +.ai-summary-empty-icon { + font-size: 32rpx; +} + +.ai-summary-empty-text { + font-size: 26rpx; + color: #9CA3AF; +}