Ver Fonte

修改bug:如果当用户刷新的时候,防止购买会员单片等无效

wilhelm wong há 2 meses atrás
pai
commit
b372566d99
4 ficheiros alterados com 87 adições e 21 exclusões
  1. 10 0
      src/store/price.ts
  2. 29 0
      src/store/user.ts
  3. 2 2
      src/views/Account.vue
  4. 46 19
      src/views/VideoPlayer.vue

+ 10 - 0
src/store/price.ts

@@ -43,6 +43,8 @@ export const usePriceStore = defineStore("price", () => {
         preview_duration: "30",
       };
 
+      console.log("获取到的价格",response);
+
       response.forEach((item) => {
         if (item.name in configMap) {
           configMap[item.name as keyof PriceConfigMap] = item.value;
@@ -99,6 +101,13 @@ export const usePriceStore = defineStore("price", () => {
   // 检查是否已加载价格配置
   const isPriceConfigLoaded = computed(() => priceConfig.value !== null);
 
+  // 重置价格配置
+  const resetPriceConfig = () => {
+    priceConfig.value = null;
+    error.value = null;
+    isLoading.value = false;
+  };
+
   return {
     priceConfig,
     isLoading,
@@ -109,5 +118,6 @@ export const usePriceStore = defineStore("price", () => {
     getPreviewDuration,
     getMembershipPlans,
     isPriceConfigLoaded,
+    resetPriceConfig,
   };
 });

+ 29 - 0
src/store/user.ts

@@ -8,6 +8,7 @@ import {
 } from "@/services/api";
 import { useStorage } from "@vueuse/core";
 import { VipLevel } from "@/types/vip";
+import { usePriceStore } from "@/store/price";
 
 // 主页浏览状态接口定义
 interface HomePageState {
@@ -94,6 +95,14 @@ export const useUserStore = defineStore("user", () => {
     setToken(response.token);
     setUserInfo(response.user);
     userManuallyLoggedOut.value = false;
+    // 登录后基于新账号/团队强制刷新价格配置
+    const priceStore = usePriceStore();
+    priceStore.resetPriceConfig();
+    try {
+      await priceStore.fetchPriceConfig();
+    } catch (e) {
+      console.error("登录后加载价格失败", e);
+    }
     return response;
   };
 
@@ -108,6 +117,14 @@ export const useUserStore = defineStore("user", () => {
     setToken(response.token);
     setUserInfo(response.user);
     userManuallyLoggedOut.value = false;
+    // 注册后同样刷新价格配置(账号可能属于特定团队)
+    const priceStore = usePriceStore();
+    priceStore.resetPriceConfig();
+    try {
+      await priceStore.fetchPriceConfig();
+    } catch (e) {
+      console.error("注册后加载价格失败", e);
+    }
     return response;
   };
 
@@ -120,6 +137,10 @@ export const useUserStore = defineStore("user", () => {
     token.value = "";
     userInfo.value = {};
     userManuallyLoggedOut.value = true;
+    
+    // 重置价格配置
+    const priceStore = usePriceStore();
+    priceStore.resetPriceConfig();
   };
 
   const createGuest = async (code?: string, ref?: string) => {
@@ -128,6 +149,14 @@ export const useUserStore = defineStore("user", () => {
       setToken(response.token);
       setUserInfo(response.user);
       userManuallyLoggedOut.value = false;
+      // 游客账号也可能绑定团队,创建后刷新价格配置
+      const priceStore = usePriceStore();
+      priceStore.resetPriceConfig();
+      try {
+        await priceStore.fetchPriceConfig();
+      } catch (e) {
+        console.error("创建游客后加载价格失败", e);
+      }
       return response;
     } catch (error) {
       console.error("创建游客账号失败", error);

+ 2 - 2
src/views/Account.vue

@@ -392,7 +392,7 @@ const loadShareRecords = async (page = 0) => {
 
 // 加载价格配置的函数
 const loadPriceConfig = async () => {
-  if (isLoginUser.value && !priceStore.isPriceConfigLoaded) {
+  if (isLoginUser.value) {
     try {
       await priceStore.fetchPriceConfig();
     } catch (error) {
@@ -403,7 +403,7 @@ const loadPriceConfig = async () => {
 
 // 监听登录状态变化
 watch(
-  () => isLoginUser,
+  () => isLoginUser.value,
   (newValue) => {
     if (newValue) {
       loadPriceConfig();

+ 46 - 19
src/views/VideoPlayer.vue

@@ -678,6 +678,7 @@
 
 <script setup lang="ts">
 import { ref, onMounted, onUnmounted, computed, watch } from "vue";
+import { storeToRefs } from "pinia";
 import { useRoute, useRouter } from "vue-router";
 import {
   searchVideoByTags,
@@ -699,7 +700,7 @@ const router = useRouter();
 
 // 用户信息
 const userStore = useUserStore();
-const { isLoginUser, isVipUser } = userStore;
+const { isLoginUser, isVipUser } = storeToRefs(userStore);
 
 // 价格配置
 const priceStore = usePriceStore();
@@ -728,10 +729,10 @@ const videoInfo = ref<any>({
 // 相关视频
 const relatedVideos = ref<any[]>([]);
 
-// 开通会员,购买单片栏
-const showPurchaseButtonArea = ref(true);
-// 购买提示弹窗
-const showPurchasePrompt = ref(true);
+// 开通会员,购买单片栏(避免初始闪现,默认不显示)
+const showPurchaseButtonArea = ref(false);
+// 购买提示弹窗(避免初始闪现,默认不显示)
+const showPurchasePrompt = ref(false);
 // 会员购买弹窗
 const showMembershipPurchaseModal = ref(false);
 // 单片购买弹窗
@@ -764,6 +765,9 @@ const showSinglePaymentWaitingDialog = ref(false);
 const showShareLinkDialog = ref(false);
 const singleCurrentOrderNo = ref("");
 
+// 用户信息是否已同步完成(用于避免初次刷新时过早进行购买判断)
+const isUserSynced = ref(false);
+
 // 会员购买按钮点击
 const handleMembershipClick = () => {
   // 特殊游览器滚动
@@ -774,10 +778,21 @@ const handleMembershipClick = () => {
 };
 
 // 单片购买按钮点击
-const handleSinglePurchaseClick = () => {
+const handleSinglePurchaseClick = async () => {
   // 特殊游览器滚动
   specialBrowserScroll();
 
+  // 确保价格配置已加载(团队相关价格可能不同)
+  if (isLoginUser.value && !priceStore.isPriceConfigLoaded) {
+    try {
+      await priceStore.fetchPriceConfig();
+    } catch (error) {
+      console.error("价格配置加载失败:", error);
+      showError("价格配置加载失败,请重试");
+      return;
+    }
+  }
+
   // 已登录,显示购买弹窗
   showSinglePurchaseModal.value = true;
 };
@@ -792,7 +807,7 @@ const handleMembershipPurchase = async () => {
   isPaymentLoading.value = true;
 
   // 未登录用户先创建游客账户
-  if (!isLoginUser) {
+  if (!isLoginUser.value) {
     try {
       const guestResponse = await userStore.createGuest();
       if (!guestResponse) {
@@ -850,7 +865,7 @@ const handleSinglePurchase = async () => {
     }
 
     // 未登录用户先创建游客账户
-    if (!isLoginUser) {
+    if (!isLoginUser.value) {
       try {
         const guestResponse = await userStore.createGuest();
         if (!guestResponse) {
@@ -1277,7 +1292,7 @@ const playVideo = (video: any) => {
   }
 
   // 判断是否需要通过URL参数传递视频数据
-  const shouldIncludeVideoData = !isVipUser;
+  const shouldIncludeVideoData = !isVipUser.value;
 
   // 构建路由参数
   const routeParams = {
@@ -1302,13 +1317,12 @@ const checkVideoPurchaseStatus = async () => {
     return;
   }
 
-  if (!isLoginUser) {
+  if (!isLoginUser.value) {
     return;
   }
 
   try {
     await loadVideoInfo();
-
     console.log("showPurchasePrompt:", showPurchasePrompt.value);
   } catch (error) {
     console.error("检查视频购买状态失败:", error);
@@ -1323,6 +1337,7 @@ const loadVideoInfo = async () => {
   if (isFreeVideo.value) {
     console.log("isFreeVideo");
     showPurchaseButtonArea.value = false;
+    showPurchasePrompt.value = false;
 
     const { name, cover, m3u8 } = route.query;
     videoInfo.value = {
@@ -1336,10 +1351,10 @@ const loadVideoInfo = async () => {
       time: 0,
       taginfo: [],
     };
-  } else if (!isVipUser) {
+  } else if (!isVipUser.value) {
     // 未登录
     console.log("isNotLogin");
-    if (!isLoginUser) {
+    if (!isLoginUser.value) {
       showPurchasePrompt.value = true;
       showPurchaseButtonArea.value = true;
       const { name, cover, duration, view, like } = route.query;
@@ -1403,7 +1418,7 @@ const loadVideoInfo = async () => {
         };
       }
     }
-  } else if (isVipUser) {
+  } else if (isVipUser.value) {
     // 付费用户,从详情接口获取视频信息
     const response = await getVideoDetail(device, String(videoId));
     console.log("isVipUser");
@@ -1438,6 +1453,9 @@ const loadVideoInfo = async () => {
         taginfo: [],
       };
     }
+    // 会员用户不显示购买提示/按钮
+    showPurchaseButtonArea.value = false;
+    showPurchasePrompt.value = false;
   }
 };
 
@@ -1499,6 +1517,10 @@ const loadRecommendedVideoList = async () => {
 watch(
   [() => route.params.id, () => route.query],
   async ([newId, newQuery], [oldId, oldQuery]) => {
+    // 用户信息未同步完成时,跳过首次触发
+    if (!isUserSynced.value) {
+      return;
+    }
     // 检查路由是否真的变化了
     const idChanged = newId !== oldId;
     const queryChanged = JSON.stringify(newQuery) !== JSON.stringify(oldQuery);
@@ -1521,19 +1543,24 @@ watch(
 );
 
 onMounted(async () => {
-  // 确保用户信息已同步
-  if (!userStore.userInfo) {
+  // 确保用户信息已同步(基于 token 判断是否需要拉取)
+  if (userStore.token) {
     await userStore.sync();
   }
 
   // 加载价格配置
-  if (!priceStore.isPriceConfigLoaded) {
+  if (isLoginUser.value && !priceStore.isPriceConfigLoaded) {
     await priceStore.fetchPriceConfig();
   }
 
-  // 单片是否购买
+  // 标记用户信息已同步完成,允许路由监听逻辑运行
+  isUserSynced.value = true;
+
+  // 初次进入页面时,按顺序执行:购买判断 -> 加载视频 -> 加载相关推荐
   await checkVideoPurchaseStatus();
-  
+  await loadVideoInfo();
+  await loadRecommendedVideoList();
+
   // 处理分享链接访问
   await handleShareLinkVisit();
 });