@hlw-uni/mp-vue 2.3.0 → 2.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -411,17 +411,7 @@ var __publicField = (obj, key, value) => {
411
411
  return current;
412
412
  }
413
413
  function destroyRewardAd(adId) {
414
- var _a;
415
- const ad = adInstances.get(adId);
416
- if (ad) {
417
- try {
418
- (_a = ad.destroy) == null ? void 0 : _a.call(ad);
419
- console.log(`[Ad] Rewarded video destroyed: ${adId}`);
420
- } catch (e) {
421
- console.error("[Ad] Rewarded video destroy error:", e);
422
- }
423
- adInstances.delete(adId);
424
- }
414
+ adInstances.delete(adId);
425
415
  if (activeRewardId === adId) {
426
416
  activeRewardId = "";
427
417
  rewardCallback = void 0;
package/dist/index.mjs CHANGED
@@ -410,17 +410,7 @@ function showRewardAd(onShowSuccess) {
410
410
  return current;
411
411
  }
412
412
  function destroyRewardAd(adId) {
413
- var _a;
414
- const ad = adInstances.get(adId);
415
- if (ad) {
416
- try {
417
- (_a = ad.destroy) == null ? void 0 : _a.call(ad);
418
- console.log(`[Ad] Rewarded video destroyed: ${adId}`);
419
- } catch (e) {
420
- console.error("[Ad] Rewarded video destroy error:", e);
421
- }
422
- adInstances.delete(adId);
423
- }
413
+ adInstances.delete(adId);
424
414
  if (activeRewardId === adId) {
425
415
  activeRewardId = "";
426
416
  rewardCallback = void 0;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@hlw-uni/mp-vue",
3
- "version": "2.3.0",
4
- "description": "hlw-uni 小程序运行时 — Vue 组件 + composables + theme + http + 工具集(合并自原 mp-core)",
3
+ "version": "2.3.2",
4
+ "description": "hlw-uni工具集",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
7
7
  "types": "dist/index.d.ts",
@@ -5,7 +5,7 @@
5
5
  </template>
6
6
 
7
7
  <script setup lang="ts">
8
- import { ref, onMounted, watch, onUnmounted } from "vue";
8
+ import { ref, onUnmounted } from "vue";
9
9
  import { setRewardAd, showRewardAd, confirmRewardAd, destroyRewardAd } from "../../utils/ad";
10
10
 
11
11
  defineOptions({ name: "HlwRewardAd" });
@@ -24,30 +24,6 @@ const emit = defineEmits<{
24
24
  // 点击锁定状态,防止连续多次点击重复触发广告
25
25
  const isClicked = ref(false);
26
26
 
27
- /**
28
- * 提前初始化并静默加载广告实例的辅助方法。
29
- */
30
- function preloadAd(id: string) {
31
- if (!id) return;
32
- // 异步静默加载,不使用 await 阻塞组件渲染或生命周期
33
- setRewardAd(id).catch((err) => {
34
- console.warn("[HlwRewardAd] Preload error:", err);
35
- });
36
- }
37
-
38
- // 组件挂载时,提前执行激励视频广告的初始化与加载
39
- onMounted(() => {
40
- preloadAd(props.unitId);
41
- });
42
-
43
- // 监听 unitId 的变化,确保广告单元 ID 动态改变时能够重新静默加载
44
- watch(
45
- () => props.unitId,
46
- (newId) => {
47
- preloadAd(newId);
48
- },
49
- );
50
-
51
27
  async function playRewardAdFlow(): Promise<void> {
52
28
  // 弹出全局模态 Loading 状态
53
29
  let hidden = false;
@@ -75,6 +51,9 @@ async function playRewardAdFlow(): Promise<void> {
75
51
  // 播放流结束后(不管成功或退出),立刻关闭 Loading 状态
76
52
  hide();
77
53
 
54
+ // 播放结束后,立即清理本地缓存引用,以防状态重叠
55
+ destroyRewardAd(props.unitId);
56
+
78
57
  if (res.success && res.isEnded) {
79
58
  emit("onHandle", {
80
59
  success: true,
@@ -102,6 +81,7 @@ async function playRewardAdFlow(): Promise<void> {
102
81
  }
103
82
  } catch (e) {
104
83
  hide();
84
+ destroyRewardAd(props.unitId);
105
85
  console.error("[HlwRewardAd] Failed to show reward ad:", e);
106
86
  emit("onHandle", {
107
87
  success: false,
@@ -202,16 +202,10 @@ export function showRewardAd(onShowSuccess?: () => void): Promise<AdRes> {
202
202
  * @param adId 广告单元 ID
203
203
  */
204
204
  export function destroyRewardAd(adId: string) {
205
- const ad = adInstances.get(adId);
206
- if (ad) {
207
- try {
208
- ad.destroy?.();
209
- console.log(`[Ad] Rewarded video destroyed: ${adId}`);
210
- } catch (e) {
211
- console.error("[Ad] Rewarded video destroy error:", e);
212
- }
213
- adInstances.delete(adId);
214
- }
205
+ // 仅从本地缓存 Map 中移除该广告单元的实例引用,并清理相关状态,严禁调用 ad.destroy()
206
+ // 因为微信小程序的 wx.createRewardedVideoAd 在整个应用生命周期中为全局单例,
207
+ // 一旦销毁 (destroy) 后,后续再次 create 该广告位 ID 将永远返回已销毁的实例,导致报错 "video-ad has been destroyed"
208
+ adInstances.delete(adId);
215
209
  if (activeRewardId === adId) {
216
210
  activeRewardId = "";
217
211
  rewardCallback = undefined;