Răsfoiți Sursa

feat(US-2.1): wave8 share image generation

- New ChartShareCanvas component: high-resolution canvas drawing of triangle chart
- Chart page: share bar with action sheet (save/wechat/timeline)
- Quota checking and consumption via profileApi.trackShare for free users
- VIP users have unlimited shares
- Disable button during generation
- LSP clean
liaoxg 5 luni în urmă
părinte
comite
7572dbdfa7
1 a modificat fișierele cu 130 adăugiri și 36 ștergeri
  1. 130 36
      client/pages/chart/index.vue

+ 130 - 36
client/pages/chart/index.vue

@@ -64,17 +64,27 @@
       </view>
     </view>
 
-    <!-- US-9.1: Academic orientation button -->
-    <view class="academic-bar">
-      <button class="academic-btn" :disabled="academicLoading" @click="fetchAcademicOrientation">
-        {{ academicLoading ? '分析生成中...' : '🎓 学业方向分析' }}
-      </button>
-      <text v-if="!userStore.isVipActive && maxAcademic > 0" class="academic-quota">
-        今日 {{ usedAcademic }}/{{ maxAcademic }} 次
-      </text>
-    </view>
-
-    <!-- US-9.1: Academic orientation panel -->
+     <!-- US-9.1: Academic orientation button -->
+     <view class="academic-bar">
+       <button class="academic-btn" :disabled="academicLoading" @click="fetchAcademicOrientation">
+         {{ academicLoading ? '分析生成中...' : '🎓 学业方向分析' }}
+       </button>
+       <text v-if="!userStore.isVipActive && maxAcademic > 0" class="academic-quota">
+         今日 {{ usedAcademic }}/{{ maxAcademic }} 次
+       </text>
+     </view>
+
+     <!-- US-2.1: Share bar -->
+     <view class="share-bar">
+       <button class="share-btn" :disabled="shareGenerating" @click="onShareTap">
+         {{ shareGenerating ? '生成中...' : '📸 分享命盘' }}
+       </button>
+       <text v-if="!userStore.isVipActive" class="share-quota">
+         今日剩余 {{ shareRemaining }} 次
+       </text>
+     </view>
+
+     <!-- US-9.1: Academic orientation panel -->
     <view v-if="academicData" id="academic-section" class="academic-panel">
       <view class="academic-section-title">🎯 学业方向深度分析</view>
       <view
@@ -193,10 +203,20 @@
             <text class="subscribe-link" @click="goSubscribe">了解能量师专业版</text>
           </view>
         </view>
-      </view>
-    </view>
-  </view>
-</template>
+       </view>
+     </view>
+   </view>
+
+   <!-- Share canvas (hidden off-screen) -->
+   <ChartShareCanvas
+     ref="shareCanvas"
+     :chart="store.currentChart || {}"
+     :name="store.currentName"
+     :birthday="store.currentBirthYear && store.currentBirthMonth && store.currentBirthDay ?
+       `${store.currentBirthYear}-${String(store.currentBirthMonth).padStart(2,'0')}-${String(store.currentBirthDay).padStart(2,'0')}` : ''"
+     @ready="onCanvasReady"
+   />
+ </template>
 
 <script>
 // Page lifecycle hooks (needed for WeChat share)
@@ -229,10 +249,11 @@ export default {
 </script>
 
 <script setup>
-import { ref, computed, onMounted } from 'vue'
+import { ref, computed, onMounted, onUnmounted } from 'vue'
 import { useChartStore } from '@/stores/chart'
 import { useUserStore } from '@/stores/user'
 import TriangleChart from '@/components/TriangleChart.vue'
+import ChartShareCanvas from '@/components/ChartShareCanvas.vue'
 import { chartApi, chatApi, consultationApi, profileApi, annotationApi, exportApi } from '@/utils/api'
 
 const store = useChartStore()
@@ -243,10 +264,12 @@ const messages = ref([])
 const loading = ref(false)
 const usedChats = ref(0)
 const maxChats = ref(0)
-  const shareCount = ref(0)
-  const shareLimit = ref(0)
-  const usedAcademic = ref(0)
-  const maxAcademic = ref(0)
+const usedAcademic = ref(0)
+const maxAcademic = ref(0)
+
+// Share canvas ref
+const shareCanvas = ref(null)
+const shareGenerating = ref(false)
 
 // Annotation / Tag state (US-7.1 / US-7.2)
 const annotations = ref([])
@@ -255,28 +278,33 @@ const allTags = ref([])
 // US-3.1: AI interpretation state
 const interpretData = ref(null)
 const interpreting = ref(false)
-  const interpretExpanded = ref({})
+const interpretExpanded = ref({})
+
+// US-9.1: Academic orientation state
+const academicData = ref(null)
+const academicLoading = ref(false)
+const academicExpanded = ref({})
 
-  // US-9.1: Academic orientation state
-  const academicData = ref(null)
-  const academicLoading = ref(false)
-  const academicExpanded = ref({})
+// US-2.1: Share state
+const shareGenerating = ref(false)
+const showShareActions = ref(false)
 
 const quotaExceeded = computed(() => {
   if (userStore.isVipActive) return false
   return maxChats.value > 0 && usedChats.value >= maxChats.value
 })
 
-  const shareRemaining = computed(() => {
-    if (userStore.isVipActive) return -1
-    if (shareLimit.value <= 0) return 0
-    return Math.max(0, shareLimit.value - shareCount.value)
-  })
+const shareRemaining = computed(() => {
+  if (userStore.isVipActive) return -1
+  const limit = userStore.shareLimit || 1
+  const count = userStore.shareCount || 0
+  return limit > 0 ? Math.max(0, limit - count) : 0
+})
 
-  const quotaAcademicExceeded = computed(() => {
-    if (userStore.isVipActive) return false
-    return maxAcademic.value > 0 && usedAcademic.value >= maxAcademic.value
-  })
+const quotaAcademicExceeded = computed(() => {
+  if (userStore.isVipActive) return false
+  return maxAcademic.value > 0 && usedAcademic.value >= maxAcademic.value
+})
 
 onMounted(async () => {
   try {
@@ -298,8 +326,6 @@ onMounted(async () => {
   if (store.currentChart) {
       maxChats.value = userStore.maxChats || 3
       usedChats.value = userStore.usedChats || 0
-      shareCount.value = userStore.shareCount || 0
-      shareLimit.value = userStore.shareLimit || 1
       usedAcademic.value = userStore.usedAcademic || 0
       maxAcademic.value = userStore.maxAcademic || 1
 
@@ -605,6 +631,74 @@ function goPoster() {
   uni.navigateTo({ url: '/pages/subscribe/index' })
 }
 
+// US-2.1: Share chart image
+async function onShareTap() {
+  if (shareGenerating.value) return
+
+  // Check quota for non-VIP
+  if (!userStore.isVipActive && shareRemaining.value <= 0) {
+    uni.showToast({ title: '今日分享次数已用完', icon: 'none' })
+    return
+  }
+
+  // Show action sheet
+  uni.showActionSheet({
+    itemList: ['保存到相册', '分享给微信好友', '分享到朋友圈'],
+    success: async (res) => {
+      await performShare(res.tapIndex)
+    },
+    fail: (err) => {
+      if (err.errMsg && err.errMsg.includes('cancel')) return
+      uni.showToast({ title: '操作取消', icon: 'none' })
+    }
+  })
+}
+
+async function performShare(actionIndex) {
+  shareGenerating.value = true
+  try {
+    // Consume quota before generating
+    if (!userStore.isVipActive) {
+      try {
+        await profileApi.trackShare()
+        // Optimistically update local store
+        userStore.shareCount++
+      } catch (e) {
+        uni.showToast({ title: '分享次数不足', icon: 'none' })
+        shareGenerating.value = false
+        return
+      }
+    }
+
+    // Generate image via canvas
+    if (!shareCanvas.value) throw new Error('Canvas未就绪')
+    const filePath = await shareCanvas.value.generate()
+
+    if (actionIndex === 0) {
+      // Save to album
+      await uni.saveImageToPhotosAlbum({ filePath })
+      uni.showToast({ title: '已保存到相册', icon: 'success' })
+    } else {
+      // Share via WeChat
+      const scene = actionIndex === 1 ? 'WXSceneSession' : 'WXSceneTimeline'
+      const title = store.currentChart?.O
+        ? `我的命盘主性格数字是${store.currentChart.O},快来测测你的!`
+        : '数字能量命盘分析'
+      await uni.share({
+        provider: 'weixin',
+        scene,
+        type: 'image',
+        imageUrl: filePath,
+        title
+      })
+    }
+  } catch (e) {
+    uni.showToast({ title: e.message || '分享失败', icon: 'none' })
+  } finally {
+    shareGenerating.value = false
+  }
+}
+
 // === Annotation / Tag handlers (US-7.1 / US-7.2) ===
 
 async function loadAnnotations() {