@hlw-uni/mp-vue 2.2.8 → 2.3.1

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
@@ -410,6 +410,15 @@ var __publicField = (obj, key, value) => {
410
410
  });
411
411
  return current;
412
412
  }
413
+ function destroyRewardAd(adId) {
414
+ adInstances.delete(adId);
415
+ if (activeRewardId === adId) {
416
+ activeRewardId = "";
417
+ rewardCallback = void 0;
418
+ rewardResolve = null;
419
+ rewardPromise = null;
420
+ }
421
+ }
413
422
  function confirmRewardAd() {
414
423
  return new Promise((resolve) => {
415
424
  uni.showModal({
@@ -434,7 +443,8 @@ var __publicField = (obj, key, value) => {
434
443
  showPopupAd,
435
444
  setRewardAd,
436
445
  showRewardAd,
437
- confirmRewardAd
446
+ confirmRewardAd,
447
+ destroyRewardAd
438
448
  };
439
449
  }
440
450
  function withQuery(url, qs) {
@@ -1163,6 +1173,7 @@ var __publicField = (obj, key, value) => {
1163
1173
  exports2.confirmRewardAd = confirmRewardAd;
1164
1174
  exports2.copy = copy;
1165
1175
  exports2.cosAdapter = cosAdapter;
1176
+ exports2.destroyRewardAd = destroyRewardAd;
1166
1177
  exports2.download = download;
1167
1178
  exports2.fontFamilyPresets = fontFamilyPresets;
1168
1179
  exports2.fontSizePresets = fontSizePresets;
package/dist/index.mjs CHANGED
@@ -409,6 +409,15 @@ function showRewardAd(onShowSuccess) {
409
409
  });
410
410
  return current;
411
411
  }
412
+ function destroyRewardAd(adId) {
413
+ adInstances.delete(adId);
414
+ if (activeRewardId === adId) {
415
+ activeRewardId = "";
416
+ rewardCallback = void 0;
417
+ rewardResolve = null;
418
+ rewardPromise = null;
419
+ }
420
+ }
412
421
  function confirmRewardAd() {
413
422
  return new Promise((resolve) => {
414
423
  uni.showModal({
@@ -433,7 +442,8 @@ function useHlwAd() {
433
442
  showPopupAd,
434
443
  setRewardAd,
435
444
  showRewardAd,
436
- confirmRewardAd
445
+ confirmRewardAd,
446
+ destroyRewardAd
437
447
  };
438
448
  }
439
449
  function withQuery(url, qs) {
@@ -1163,6 +1173,7 @@ export {
1163
1173
  confirmRewardAd,
1164
1174
  copy,
1165
1175
  cosAdapter,
1176
+ destroyRewardAd,
1166
1177
  download,
1167
1178
  fontFamilyPresets,
1168
1179
  fontSizePresets,
@@ -43,6 +43,12 @@ export declare function setRewardAd(adId: string, done?: (res: AdRes) => void):
43
43
  * @returns 返回 Promise<AdRes>,指示广告是否正常播放完毕
44
44
  */
45
45
  export declare function showRewardAd(onShowSuccess?: () => void): Promise<AdRes>;
46
+ /**
47
+ * 销毁指定 Unit ID 的激励视频广告实例,释放内存与原生渲染层绑定并清除缓存。
48
+ *
49
+ * @param adId 广告单元 ID
50
+ */
51
+ export declare function destroyRewardAd(adId: string): void;
46
52
  /**
47
53
  * 弹窗提示需要看完广告才有奖励。
48
54
  * 提供“继续观看”和“取消”按钮。
@@ -58,4 +64,5 @@ export declare function useHlwAd(): {
58
64
  setRewardAd: typeof setRewardAd;
59
65
  showRewardAd: typeof showRewardAd;
60
66
  confirmRewardAd: typeof confirmRewardAd;
67
+ destroyRewardAd: typeof destroyRewardAd;
61
68
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hlw-uni/mp-vue",
3
- "version": "2.2.8",
3
+ "version": "2.3.1",
4
4
  "description": "hlw-uni 小程序运行时 — Vue 组件 + composables + theme + http + 工具集(合并自原 mp-core)",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -5,8 +5,8 @@
5
5
  </template>
6
6
 
7
7
  <script setup lang="ts">
8
- import { ref, onMounted, watch } from "vue";
9
- import { setRewardAd, showRewardAd, confirmRewardAd } from "../../utils/ad";
8
+ import { ref, onMounted, watch, onUnmounted } from "vue";
9
+ import { setRewardAd, showRewardAd, confirmRewardAd, destroyRewardAd } from "../../utils/ad";
10
10
 
11
11
  defineOptions({ name: "HlwRewardAd" });
12
12
 
@@ -18,7 +18,7 @@ interface Props {
18
18
  const props = defineProps<Props>();
19
19
 
20
20
  const emit = defineEmits<{
21
- (e: "onHandle", res: { success: boolean; isEnded: boolean; err?: any }): void;
21
+ (e: "onHandle", res: { success: boolean; isEnded: boolean; loadFailed?: boolean; err?: any }): void;
22
22
  }>();
23
23
 
24
24
  // 点击锁定状态,防止连续多次点击重复触发广告
@@ -84,6 +84,7 @@ async function playRewardAdFlow(): Promise<void> {
84
84
  emit("onHandle", {
85
85
  success: false,
86
86
  isEnded: false,
87
+ loadFailed: true,
87
88
  err: res.err,
88
89
  });
89
90
  } else {
@@ -105,6 +106,7 @@ async function playRewardAdFlow(): Promise<void> {
105
106
  emit("onHandle", {
106
107
  success: false,
107
108
  isEnded: false,
109
+ loadFailed: true,
108
110
  err: e,
109
111
  });
110
112
  }
@@ -146,6 +148,10 @@ defineExpose({
146
148
  }
147
149
  }
148
150
  });
151
+
152
+ onUnmounted(() => {
153
+ destroyRewardAd(props.unitId);
154
+ });
149
155
  </script>
150
156
 
151
157
  <style scoped>
@@ -196,6 +196,24 @@ export function showRewardAd(onShowSuccess?: () => void): Promise<AdRes> {
196
196
  return current;
197
197
  }
198
198
 
199
+ /**
200
+ * 销毁指定 Unit ID 的激励视频广告实例,释放内存与原生渲染层绑定并清除缓存。
201
+ *
202
+ * @param adId 广告单元 ID
203
+ */
204
+ export function destroyRewardAd(adId: string) {
205
+ // 仅从本地缓存 Map 中移除该广告单元的实例引用,并清理相关状态,严禁调用 ad.destroy()
206
+ // 因为微信小程序的 wx.createRewardedVideoAd 在整个应用生命周期中为全局单例,
207
+ // 一旦销毁 (destroy) 后,后续再次 create 该广告位 ID 将永远返回已销毁的实例,导致报错 "video-ad has been destroyed"
208
+ adInstances.delete(adId);
209
+ if (activeRewardId === adId) {
210
+ activeRewardId = "";
211
+ rewardCallback = undefined;
212
+ rewardResolve = null;
213
+ rewardPromise = null;
214
+ }
215
+ }
216
+
199
217
  /**
200
218
  * 弹窗提示需要看完广告才有奖励。
201
219
  * 提供“继续观看”和“取消”按钮。
@@ -230,5 +248,6 @@ export function useHlwAd() {
230
248
  setRewardAd,
231
249
  showRewardAd,
232
250
  confirmRewardAd,
251
+ destroyRewardAd,
233
252
  };
234
253
  }