ff2db1cc07
- 修改 AI 页面标题为 "AI 建议" 和 "AI 总结" - 在个人资料页面添加 AI 建议和总结的导航项 - 优化 AI 页面结构,移除不必要的加载状态和元素 - 更新样式以提升用户体验
420 lines
9.0 KiB
Vue
420 lines
9.0 KiB
Vue
<template>
|
||
<view class="page">
|
||
<view class="status-bar" :style="{ height: statusBarHeight + 'px' }"></view>
|
||
<view class="container">
|
||
<view class="hero-card">
|
||
<text class="hero-label">AI 建议</text>
|
||
<text class="hero-title">今日控烟节奏</text>
|
||
<text class="hero-desc">基于最近 3 天记录、作息和默认控烟节奏生成建议。</text>
|
||
</view>
|
||
|
||
<view class="section-card">
|
||
<view class="section-header">
|
||
<view>
|
||
<text class="section-title">下次建议</text>
|
||
<text class="section-subtitle">首页会同步显示这里的最新结果</text>
|
||
</view>
|
||
<view class="primary-btn" @tap="handleAISuggest">
|
||
<text class="primary-btn-text">{{ aiLoading ? '生成中...' : actionText }}</text>
|
||
</view>
|
||
</view>
|
||
|
||
<view v-if="suggestedClock" class="suggested-pill">
|
||
<text class="suggested-pill-label">✨ 下次建议</text>
|
||
<text class="suggested-pill-value">{{ suggestedClock }}</text>
|
||
</view>
|
||
|
||
<view v-if="aiTimeNodes.length" class="timeline">
|
||
<view
|
||
v-for="(node, idx) in aiTimeNodesWithStatus"
|
||
:key="idx"
|
||
class="timeline-node"
|
||
:class="{
|
||
'timeline-node-past': node.status === 'past',
|
||
'timeline-node-current': node.status === 'current',
|
||
'timeline-node-future': node.status === 'future'
|
||
}"
|
||
>
|
||
<view class="timeline-dot"></view>
|
||
<text class="timeline-time">{{ node.time }}</text>
|
||
<view class="timeline-line" v-if="idx < aiTimeNodesWithStatus.length - 1"></view>
|
||
</view>
|
||
</view>
|
||
|
||
<view v-if="aiAdvice" class="advice-card">
|
||
<text class="advice-title">AI 文案</text>
|
||
<text class="advice-text">{{ aiAdvice }}</text>
|
||
</view>
|
||
|
||
<view v-if="!aiTimeNodes.length && !aiAdvice && !aiLoading" class="empty-card">
|
||
<text class="empty-title">还没有生成今日 AI 建议</text>
|
||
<text class="empty-desc">点击右上角生成,系统会结合近 3 天记录给出建议时间点。</text>
|
||
</view>
|
||
</view>
|
||
</view>
|
||
</view>
|
||
</template>
|
||
|
||
<script setup>
|
||
import { computed, ref } from 'vue'
|
||
import { onShow } from '@dcloudio/uni-app'
|
||
import * as api from '@/api'
|
||
import { useLogin } from '@/hooks/useLogin'
|
||
|
||
const { waitForLogin } = useLogin()
|
||
const rewardAdUnitId = 'adunit-36e13d77e185f757'
|
||
|
||
const statusBarHeight = ref(0)
|
||
const homeData = ref(null)
|
||
const aiLoading = ref(false)
|
||
|
||
const homeTimer = computed(() => homeData.value?.timer || {})
|
||
const aiTimeNodes = computed(() => homeTimer.value?.ai_time_nodes || [])
|
||
const aiAdvice = computed(() => homeTimer.value?.ai_advice || '')
|
||
const actionText = computed(() => (aiTimeNodes.value.length > 0 ? '刷新' : '生成'))
|
||
|
||
const suggestedClock = computed(() => {
|
||
const timer = homeTimer.value
|
||
if (timer.next_suggested_clock) return timer.next_suggested_clock
|
||
if (timer.next_suggested_at) {
|
||
const date = new Date(timer.next_suggested_at)
|
||
if (!isNaN(date.getTime())) {
|
||
return `${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}`
|
||
}
|
||
}
|
||
return ''
|
||
})
|
||
|
||
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' }
|
||
})
|
||
})
|
||
|
||
function formatLocalDate(date = new Date()) {
|
||
const y = date.getFullYear()
|
||
const m = String(date.getMonth() + 1).padStart(2, '0')
|
||
const d = String(date.getDate()).padStart(2, '0')
|
||
return `${y}-${m}-${d}`
|
||
}
|
||
|
||
async function fetchHomeData() {
|
||
const res = await api.getHome()
|
||
homeData.value = res.data || {}
|
||
}
|
||
|
||
async function runRewardedUnlock(onUnlocked) {
|
||
// #ifdef MP-WEIXIN
|
||
try {
|
||
const videoAd = wx.createRewardedVideoAd({
|
||
adUnitId: rewardAdUnitId
|
||
})
|
||
videoAd.onClose(async (res) => {
|
||
if (res && res.isEnded) {
|
||
await onUnlocked()
|
||
} else {
|
||
uni.showToast({ title: '需要看完广告哦', icon: 'none' })
|
||
}
|
||
})
|
||
videoAd.onError(async () => {
|
||
await onUnlocked()
|
||
})
|
||
await videoAd.show().catch(async () => {
|
||
await videoAd.load()
|
||
await videoAd.show()
|
||
})
|
||
return
|
||
} catch (e) {
|
||
await onUnlocked()
|
||
return
|
||
}
|
||
// #endif
|
||
// #ifndef MP-WEIXIN
|
||
await onUnlocked()
|
||
// #endif
|
||
}
|
||
|
||
async function unlockToday() {
|
||
try {
|
||
await api.unlockAiAdvice({ date: formatLocalDate() })
|
||
} catch (e) {
|
||
console.error('unlockToday error:', e)
|
||
}
|
||
}
|
||
|
||
async function fetchAISuggestion() {
|
||
aiLoading.value = true
|
||
try {
|
||
const res = await api.getAINextSmokeTime()
|
||
const data = res.data || {}
|
||
if (!homeData.value) {
|
||
homeData.value = {}
|
||
}
|
||
if (!homeData.value.timer) {
|
||
homeData.value.timer = {}
|
||
}
|
||
homeData.value.timer.suggestion_source = 'ai'
|
||
homeData.value.timer.ai_time_nodes = data.time_nodes || []
|
||
homeData.value.timer.ai_advice = data.advice || ''
|
||
homeData.value.timer.next_suggested_at = data.suggested_at || ''
|
||
if (data.suggested_at) {
|
||
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')}`
|
||
}
|
||
}
|
||
uni.showToast({ title: 'AI 计划已生成', icon: 'success' })
|
||
} catch (e) {
|
||
console.error('fetchAISuggestion error:', e)
|
||
const msg = e?.data?.message || '生成失败,请稍后重试'
|
||
uni.showToast({ title: msg, icon: 'none' })
|
||
} finally {
|
||
aiLoading.value = false
|
||
}
|
||
}
|
||
|
||
async function handleAISuggest() {
|
||
if (aiLoading.value) return
|
||
if (aiTimeNodes.value.length > 0) {
|
||
await fetchAISuggestion()
|
||
return
|
||
}
|
||
|
||
await runRewardedUnlock(async () => {
|
||
await unlockToday()
|
||
await fetchAISuggestion()
|
||
})
|
||
}
|
||
|
||
onShow(async () => {
|
||
try {
|
||
const systemInfo = uni.getSystemInfoSync()
|
||
statusBarHeight.value = systemInfo.statusBarHeight || 0
|
||
await waitForLogin()
|
||
await fetchHomeData()
|
||
} catch (e) {
|
||
console.error('ai suggest onShow error:', e)
|
||
}
|
||
})
|
||
</script>
|
||
|
||
<style scoped>
|
||
.page {
|
||
min-height: 100vh;
|
||
background: linear-gradient(to bottom, #D1FAE5 0%, #F0FDF4 45%, #FFFFFF 100%);
|
||
}
|
||
|
||
.status-bar {
|
||
background: linear-gradient(to bottom, #D1FAE5, #E9FDF2);
|
||
}
|
||
|
||
.container {
|
||
padding: 24rpx 32rpx 80rpx;
|
||
}
|
||
|
||
.hero-card,
|
||
.section-card,
|
||
.advice-card,
|
||
.empty-card {
|
||
background-color: #FFFFFF;
|
||
border-radius: 28rpx;
|
||
border: 2rpx solid #D9FBE7;
|
||
box-shadow: 0 10rpx 24rpx rgba(16, 185, 129, 0.08);
|
||
}
|
||
|
||
.hero-card {
|
||
padding: 32rpx;
|
||
margin-bottom: 24rpx;
|
||
}
|
||
|
||
.hero-label {
|
||
font-size: 22rpx;
|
||
color: #059669;
|
||
display: block;
|
||
margin-bottom: 12rpx;
|
||
}
|
||
|
||
.hero-title {
|
||
font-size: 42rpx;
|
||
font-weight: 700;
|
||
color: #111827;
|
||
display: block;
|
||
margin-bottom: 12rpx;
|
||
}
|
||
|
||
.hero-desc {
|
||
font-size: 24rpx;
|
||
color: #6B7280;
|
||
line-height: 1.6;
|
||
}
|
||
|
||
.section-card {
|
||
padding: 28rpx;
|
||
}
|
||
|
||
.section-header {
|
||
display: flex;
|
||
align-items: flex-start;
|
||
justify-content: space-between;
|
||
gap: 24rpx;
|
||
margin-bottom: 24rpx;
|
||
}
|
||
|
||
.section-title {
|
||
font-size: 30rpx;
|
||
font-weight: 700;
|
||
color: #111827;
|
||
display: block;
|
||
}
|
||
|
||
.section-subtitle {
|
||
font-size: 22rpx;
|
||
color: #6B7280;
|
||
display: block;
|
||
margin-top: 8rpx;
|
||
}
|
||
|
||
.primary-btn {
|
||
padding: 10rpx 24rpx;
|
||
border-radius: 999rpx;
|
||
background: linear-gradient(135deg, #34D399, #10B981);
|
||
}
|
||
|
||
.primary-btn-text {
|
||
font-size: 22rpx;
|
||
font-weight: 600;
|
||
color: #FFFFFF;
|
||
}
|
||
|
||
.suggested-pill {
|
||
display: inline-flex;
|
||
align-items: center;
|
||
gap: 12rpx;
|
||
padding: 14rpx 22rpx;
|
||
border-radius: 999rpx;
|
||
background: #ECFDF5;
|
||
border: 2rpx solid #D1FAE5;
|
||
margin-bottom: 24rpx;
|
||
}
|
||
|
||
.suggested-pill-label {
|
||
font-size: 22rpx;
|
||
color: #059669;
|
||
}
|
||
|
||
.suggested-pill-value {
|
||
font-size: 26rpx;
|
||
font-weight: 700;
|
||
color: #111827;
|
||
}
|
||
|
||
.timeline {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
padding: 0 8rpx;
|
||
margin-bottom: 24rpx;
|
||
}
|
||
|
||
.timeline-node {
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
position: relative;
|
||
flex: 1;
|
||
}
|
||
|
||
.timeline-dot {
|
||
width: 20rpx;
|
||
height: 20rpx;
|
||
border-radius: 50%;
|
||
background-color: #D1FAE5;
|
||
margin-bottom: 8rpx;
|
||
}
|
||
|
||
.timeline-line {
|
||
position: absolute;
|
||
top: 10rpx;
|
||
left: calc(50% + 18rpx);
|
||
right: -50%;
|
||
height: 2rpx;
|
||
background-color: #D1FAE5;
|
||
}
|
||
|
||
.timeline-time {
|
||
font-size: 22rpx;
|
||
color: #9CA3AF;
|
||
}
|
||
|
||
.timeline-node-past .timeline-dot {
|
||
background-color: #9CA3AF;
|
||
}
|
||
|
||
.timeline-node-past .timeline-time {
|
||
text-decoration: line-through;
|
||
}
|
||
|
||
.timeline-node-current .timeline-dot {
|
||
background-color: #10B981;
|
||
width: 24rpx;
|
||
height: 24rpx;
|
||
box-shadow: 0 0 12rpx rgba(16, 185, 129, 0.5);
|
||
}
|
||
|
||
.timeline-node-current .timeline-time {
|
||
color: #059669;
|
||
font-weight: 600;
|
||
}
|
||
|
||
.timeline-node-future .timeline-time {
|
||
color: #6B7280;
|
||
}
|
||
|
||
.advice-card {
|
||
padding: 22rpx;
|
||
background-color: #F0FDF4;
|
||
}
|
||
|
||
.advice-title {
|
||
font-size: 24rpx;
|
||
font-weight: 600;
|
||
color: #059669;
|
||
display: block;
|
||
margin-bottom: 8rpx;
|
||
}
|
||
|
||
.advice-text {
|
||
font-size: 24rpx;
|
||
color: #374151;
|
||
line-height: 1.7;
|
||
}
|
||
|
||
.empty-card {
|
||
padding: 36rpx 28rpx;
|
||
}
|
||
|
||
.empty-title {
|
||
font-size: 28rpx;
|
||
font-weight: 600;
|
||
color: #111827;
|
||
display: block;
|
||
margin-bottom: 12rpx;
|
||
}
|
||
|
||
.empty-desc {
|
||
font-size: 24rpx;
|
||
color: #6B7280;
|
||
line-height: 1.6;
|
||
}
|
||
</style>
|