@hlw-uni/mp-vue 2.0.2 → 2.0.6

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/app.d.ts CHANGED
@@ -10,7 +10,7 @@ export interface InterceptorOptions {
10
10
  /** Token 来源函数 */
11
11
  getToken?: () => string;
12
12
  /** 登录失效时的处理函数 */
13
- onUnauthorized?: () => void;
13
+ onLogout?: () => void;
14
14
  /** 接口业务错误码是否自动 toast */
15
15
  autoToastError?: boolean;
16
16
  }
@@ -37,24 +37,6 @@ export interface AdCloseResult {
37
37
  /** 用户是否完整观看 */
38
38
  isEnded: boolean;
39
39
  }
40
- /** showReward 的 claim 回调返回契约 */
41
- export interface AdClaimResult {
42
- /** 本次结果:true=成功 / false=失败 */
43
- ok: boolean;
44
- /** 奖励数(用于 toast 显示 +N 积分;无则不 toast) */
45
- reward?: number;
46
- /** 失败提示语;不传不弹 toast */
47
- msg?: string;
48
- /** 仅 isEnded=false 时常用:true 让 mp-core 重新 show 一次(业务方挽留确认后用) */
49
- retry?: boolean;
50
- }
51
- /**
52
- * showReward 的 claim 回调签名 —— 业务方按场景实现(领积分、解锁、抽奖等)。
53
- * 每次广告关闭后被调用一次(无论是否完整看完),业务方根据 closeRes.isEnded 决定怎么走:
54
- * - isEnded=true → 调发奖接口 → return { ok: true, reward: N }
55
- * - isEnded=false → 业务自己决定挽留:return { ok: false, retry: true } 让重新 show
56
- */
57
- export type AdClaimFn = (closeRes: AdCloseResult) => Promise<AdClaimResult>;
58
40
  /**
59
41
  * 注入业务回调,应用启动时调用一次。
60
42
  * 不调用也不会崩,但 loadConfig / showReward 会无效。
@@ -79,23 +61,23 @@ export declare function useAd(): {
79
61
  loaded: Ref<boolean, boolean>;
80
62
  loadConfig: (force?: boolean) => Promise<void>;
81
63
  getUnitId: (type: AdType) => string;
82
- showReward: (claim?: AdClaimFn) => Promise<boolean>;
64
+ showReward: (onClose?: ((res: AdCloseResult) => void | Promise<void>) | undefined) => Promise<void>;
83
65
  showPopup: () => Promise<boolean>;
66
+ confirm: typeof confirmModal;
84
67
  };
85
68
  /**
86
- * 激励视频中途关闭挽留弹窗 —— 业务方在 claim 回调里也可以复用:
69
+ * 激励视频中途关闭挽留弹窗 —— 通过 useAd().confirm 暴露给业务方:
87
70
  *
88
- * showReward(async (closeRes) => {
89
- * if (!closeRes.isEnded) {
90
- * const goon = await confirmReward();
91
- * return { ok: false, retry: goon };
92
- * }
71
+ * const { showReward, confirm } = useAd();
72
+ * showReward(async ({ isEnded }) => {
73
+ * if (!isEnded) return { ok: false, retry: await confirm() };
93
74
  * const r = await claimAdReward();
94
75
  * return r.code === 1 ? { ok: true, reward: r.data?.reward } : { ok: false, msg: r.info };
95
76
  * });
96
77
  *
97
78
  * @returns true=用户选「继续观看」 / false=放弃
98
79
  */
99
- export declare function confirmReward(): Promise<boolean>;
80
+ declare function confirmModal(): Promise<boolean>;
100
81
  /** 销毁全部广告实例并清空缓存(业务一般不用,hot reload 时调) */
101
82
  export declare function destroyAds(): void;
83
+ export {};
@@ -10,7 +10,7 @@ export { usePageMeta } from './page-meta';
10
10
  export { useStorage, type StorageInstance } from './storage';
11
11
  export { useValidate } from './validate';
12
12
  export { useFormat } from './format';
13
- export { useAd, setConfigAd, destroyAds, confirmReward, type AdType, type AdConfig, type AdError, type AdAdapter, type AdCloseResult, type AdClaimResult, type AdClaimFn, } from './ad';
13
+ export { useAd, setConfigAd, destroyAds, type AdType, type AdConfig, type AdError, type AdAdapter, type AdCloseResult, } from './ad';
14
14
  export { useShare, useShareConfig, setConfigShare, type ShareConfig, type ShareConfigResolver, type ShareFrom, type ShareAppMessageContent, type ShareTimelineContent, type ShareConfigMap, type ShareConfigAdapter, type PageShareItem, type PageShareFallback, type SharePayload, } from './share';
15
15
  export { useContact, setConfigContact, type ContactConfig, type ContactAdapter, type ContactBindProps, } from './contact';
16
16
  export { useUtils, type DownloadFileOptions, type DownloadFileResult, type TapEvent } from './utils';
@@ -18,6 +18,8 @@ export interface ModalOptions {
18
18
  cancelText?: string;
19
19
  confirmColor?: string;
20
20
  cancelColor?: string;
21
+ /** false=隐藏取消按钮(纯提示弹窗用) */
22
+ showCancel?: boolean;
21
23
  }
22
24
  export interface HlwMsg {
23
25
  toast(opts: ToastOptions | string): void;
package/dist/index.js CHANGED
@@ -352,7 +352,8 @@ var __publicField = (obj, key, value) => {
352
352
  confirmText = "确定",
353
353
  cancelText = "取消",
354
354
  confirmColor = "#3b82f6",
355
- cancelColor = "#999999"
355
+ cancelColor = "#999999",
356
+ showCancel = true
356
357
  } = opts;
357
358
  uni.showModal({
358
359
  title,
@@ -361,6 +362,7 @@ var __publicField = (obj, key, value) => {
361
362
  cancelText,
362
363
  confirmColor,
363
364
  cancelColor,
365
+ showCancel,
364
366
  success: (res) => resolve(res.confirm),
365
367
  fail: () => resolve(false)
366
368
  });
@@ -672,44 +674,19 @@ var __publicField = (obj, key, value) => {
672
674
  function getUnitId(type) {
673
675
  return store.config[`${type}_unit_id`] || "";
674
676
  }
675
- async function showReward(claim) {
677
+ async function showReward(onClose) {
676
678
  const unitId = getUnitId("reward");
677
679
  if (!unitId) {
678
680
  msg().toast("激励广告未配置");
679
- return false;
681
+ return;
680
682
  }
681
683
  if ((adapter$1 == null ? void 0 : adapter$1.isAuth) && !adapter$1.isAuth()) {
682
684
  msg().toast("请先登录");
683
- return false;
684
- }
685
- while (true) {
686
- const closeRes = await playRewardedOnce(unitId);
687
- if (!claim) {
688
- if (closeRes.isEnded)
689
- return true;
690
- const goon = await confirmReward();
691
- if (!goon)
692
- return false;
693
- continue;
694
- }
695
- try {
696
- const r = await claim(closeRes);
697
- if (r.retry)
698
- continue;
699
- if (!r.ok) {
700
- if (r.msg)
701
- msg().error(r.msg);
702
- return false;
703
- }
704
- if (r.reward && r.reward > 0)
705
- msg().success(`+${r.reward} 积分`);
706
- return true;
707
- } catch (e) {
708
- console.warn("[useAd] claim failed", e);
709
- msg().error("领取失败,请稍后再试");
710
- return false;
711
- }
685
+ return;
712
686
  }
687
+ const closeRes = await playRewardedOnce(unitId);
688
+ if (onClose)
689
+ await onClose(closeRes);
713
690
  }
714
691
  async function showPopup() {
715
692
  const unitId = getUnitId("popup");
@@ -725,7 +702,8 @@ var __publicField = (obj, key, value) => {
725
702
  loadConfig: loadConfig2,
726
703
  getUnitId,
727
704
  showReward,
728
- showPopup
705
+ showPopup,
706
+ confirm: confirmModal
729
707
  };
730
708
  }
731
709
  async function playRewardedOnce(unitId) {
@@ -744,7 +722,7 @@ var __publicField = (obj, key, value) => {
744
722
  hide();
745
723
  }
746
724
  }
747
- function confirmReward() {
725
+ function confirmModal() {
748
726
  return new Promise((resolve) => {
749
727
  uni.showModal({
750
728
  title: "提示",
@@ -1846,7 +1824,7 @@ var __publicField = (obj, key, value) => {
1846
1824
  const offError = http.onError((err) => {
1847
1825
  var _a;
1848
1826
  if (err.message.includes("401")) {
1849
- (_a = opts.onUnauthorized) == null ? void 0 : _a.call(opts);
1827
+ (_a = opts.onLogout) == null ? void 0 : _a.call(opts);
1850
1828
  }
1851
1829
  });
1852
1830
  _interceptorCleanup = [offRequest, offResponse, offError];
@@ -1900,7 +1878,6 @@ var __publicField = (obj, key, value) => {
1900
1878
  exports2.alistAdapter = alistAdapter;
1901
1879
  exports2.buildThemeStyle = buildThemeStyle;
1902
1880
  exports2.clearDeviceCache = clearDeviceCache;
1903
- exports2.confirmReward = confirmReward;
1904
1881
  exports2.cosAdapter = cosAdapter;
1905
1882
  exports2.destroyAds = destroyAds;
1906
1883
  exports2.deviceToQuery = deviceToQuery;
package/dist/index.mjs CHANGED
@@ -351,7 +351,8 @@ function useMsg() {
351
351
  confirmText = "确定",
352
352
  cancelText = "取消",
353
353
  confirmColor = "#3b82f6",
354
- cancelColor = "#999999"
354
+ cancelColor = "#999999",
355
+ showCancel = true
355
356
  } = opts;
356
357
  uni.showModal({
357
358
  title,
@@ -360,6 +361,7 @@ function useMsg() {
360
361
  cancelText,
361
362
  confirmColor,
362
363
  cancelColor,
364
+ showCancel,
363
365
  success: (res) => resolve(res.confirm),
364
366
  fail: () => resolve(false)
365
367
  });
@@ -671,44 +673,19 @@ function useAd() {
671
673
  function getUnitId(type) {
672
674
  return store.config[`${type}_unit_id`] || "";
673
675
  }
674
- async function showReward(claim) {
676
+ async function showReward(onClose) {
675
677
  const unitId = getUnitId("reward");
676
678
  if (!unitId) {
677
679
  msg().toast("激励广告未配置");
678
- return false;
680
+ return;
679
681
  }
680
682
  if ((adapter$1 == null ? void 0 : adapter$1.isAuth) && !adapter$1.isAuth()) {
681
683
  msg().toast("请先登录");
682
- return false;
683
- }
684
- while (true) {
685
- const closeRes = await playRewardedOnce(unitId);
686
- if (!claim) {
687
- if (closeRes.isEnded)
688
- return true;
689
- const goon = await confirmReward();
690
- if (!goon)
691
- return false;
692
- continue;
693
- }
694
- try {
695
- const r = await claim(closeRes);
696
- if (r.retry)
697
- continue;
698
- if (!r.ok) {
699
- if (r.msg)
700
- msg().error(r.msg);
701
- return false;
702
- }
703
- if (r.reward && r.reward > 0)
704
- msg().success(`+${r.reward} 积分`);
705
- return true;
706
- } catch (e) {
707
- console.warn("[useAd] claim failed", e);
708
- msg().error("领取失败,请稍后再试");
709
- return false;
710
- }
684
+ return;
711
685
  }
686
+ const closeRes = await playRewardedOnce(unitId);
687
+ if (onClose)
688
+ await onClose(closeRes);
712
689
  }
713
690
  async function showPopup() {
714
691
  const unitId = getUnitId("popup");
@@ -724,7 +701,8 @@ function useAd() {
724
701
  loadConfig: loadConfig2,
725
702
  getUnitId,
726
703
  showReward,
727
- showPopup
704
+ showPopup,
705
+ confirm: confirmModal
728
706
  };
729
707
  }
730
708
  async function playRewardedOnce(unitId) {
@@ -743,7 +721,7 @@ async function playRewardedOnce(unitId) {
743
721
  hide();
744
722
  }
745
723
  }
746
- function confirmReward() {
724
+ function confirmModal() {
747
725
  return new Promise((resolve) => {
748
726
  uni.showModal({
749
727
  title: "提示",
@@ -1845,7 +1823,7 @@ function setupInterceptors(options = {}) {
1845
1823
  const offError = http.onError((err) => {
1846
1824
  var _a;
1847
1825
  if (err.message.includes("401")) {
1848
- (_a = opts.onUnauthorized) == null ? void 0 : _a.call(opts);
1826
+ (_a = opts.onLogout) == null ? void 0 : _a.call(opts);
1849
1827
  }
1850
1828
  });
1851
1829
  _interceptorCleanup = [offRequest, offResponse, offError];
@@ -1900,7 +1878,6 @@ export {
1900
1878
  alistAdapter,
1901
1879
  buildThemeStyle,
1902
1880
  clearDeviceCache,
1903
- confirmReward,
1904
1881
  cosAdapter,
1905
1882
  destroyAds,
1906
1883
  deviceToQuery,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hlw-uni/mp-vue",
3
- "version": "2.0.2",
3
+ "version": "2.0.6",
4
4
  "description": "hlw-uni 小程序运行时 — Vue 组件 + composables + theme + http + 工具集(合并自原 mp-core)",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
package/src/app.ts CHANGED
@@ -20,7 +20,7 @@ export interface InterceptorOptions {
20
20
  /** Token 来源函数 */
21
21
  getToken?: () => string;
22
22
  /** 登录失效时的处理函数 */
23
- onUnauthorized?: () => void;
23
+ onLogout?: () => void;
24
24
  /** 接口业务错误码是否自动 toast */
25
25
  autoToastError?: boolean;
26
26
  }
@@ -165,7 +165,7 @@ export function setupInterceptors(options: InterceptorOptions & { sigSecret?: st
165
165
  */
166
166
  const offError = http.onError((err: Error) => {
167
167
  if (err.message.includes('401')) {
168
- opts.onUnauthorized?.();
168
+ opts.onLogout?.();
169
169
  }
170
170
  });
171
171
 
@@ -88,26 +88,6 @@ export interface AdCloseResult {
88
88
  isEnded: boolean;
89
89
  }
90
90
 
91
- /** showReward 的 claim 回调返回契约 */
92
- export interface AdClaimResult {
93
- /** 本次结果:true=成功 / false=失败 */
94
- ok: boolean;
95
- /** 奖励数(用于 toast 显示 +N 积分;无则不 toast) */
96
- reward?: number;
97
- /** 失败提示语;不传不弹 toast */
98
- msg?: string;
99
- /** 仅 isEnded=false 时常用:true 让 mp-core 重新 show 一次(业务方挽留确认后用) */
100
- retry?: boolean;
101
- }
102
-
103
- /**
104
- * showReward 的 claim 回调签名 —— 业务方按场景实现(领积分、解锁、抽奖等)。
105
- * 每次广告关闭后被调用一次(无论是否完整看完),业务方根据 closeRes.isEnded 决定怎么走:
106
- * - isEnded=true → 调发奖接口 → return { ok: true, reward: N }
107
- * - isEnded=false → 业务自己决定挽留:return { ok: false, retry: true } 让重新 show
108
- */
109
- export type AdClaimFn = (closeRes: AdCloseResult) => Promise<AdClaimResult>;
110
-
111
91
  const EMPTY: AdConfig = {
112
92
  banner_unit_id: "",
113
93
  grid_unit_id: "",
@@ -180,51 +160,27 @@ export function useAd() {
180
160
  }
181
161
 
182
162
  /**
183
- * 显示激励视频
163
+ * 显示激励视频 —— 播一次、关闭后调 onClose(带 isEnded),其它都交给业务。
184
164
  *
185
- * 流程:loading 广告 UI → 关闭 → 业务 claim 回调(按 closeRes.isEnded 决定怎么走)
165
+ * showReward(({ isEnded }) => {
166
+ * if (!isEnded) confirm().then(goon => goon && tapReward());
167
+ * else claimAdReward().then(...);
168
+ * });
186
169
  *
187
- * @param claim 关闭后业务回调;不传则纯展示,看完即返回 true、未看完返回 false
188
- * @returns true=最终成功;false=未看完且业务放弃 / 配置缺失 / claim 失败
170
+ * @param onClose 关闭回调;不传则只播。retry / toast / 发奖全在业务侧自己写。
189
171
  */
190
- async function showReward(claim?: AdClaimFn): Promise<boolean> {
172
+ async function showReward(onClose?: (res: AdCloseResult) => void | Promise<void>): Promise<void> {
191
173
  const unitId = getUnitId("reward");
192
174
  if (!unitId) {
193
175
  msg().toast("激励广告未配置");
194
- return false;
176
+ return;
195
177
  }
196
178
  if (adapter?.isAuth && !adapter.isAuth()) {
197
179
  msg().toast("请先登录");
198
- return false;
199
- }
200
-
201
- while (true) {
202
- const closeRes = await playRewardedOnce(unitId);
203
-
204
- // 没传 claim:默认行为 = 看完了 true / 没看完弹挽留 + 重试 / 放弃 false
205
- if (!claim) {
206
- if (closeRes.isEnded) return true;
207
- const goon = await confirmReward();
208
- if (!goon) return false;
209
- continue;
210
- }
211
-
212
- // 业务方决定怎么走(包括「未看完是否挽留」)
213
- try {
214
- const r = await claim(closeRes);
215
- if (r.retry) continue;
216
- if (!r.ok) {
217
- if (r.msg) msg().error(r.msg);
218
- return false;
219
- }
220
- if (r.reward && r.reward > 0) msg().success(`+${r.reward} 积分`);
221
- return true;
222
- } catch (e) {
223
- console.warn("[useAd] claim failed", e);
224
- msg().error("领取失败,请稍后再试");
225
- return false;
226
- }
180
+ return;
227
181
  }
182
+ const closeRes = await playRewardedOnce(unitId);
183
+ if (onClose) await onClose(closeRes);
228
184
  }
229
185
 
230
186
  /**
@@ -246,6 +202,7 @@ export function useAd() {
246
202
  getUnitId,
247
203
  showReward,
248
204
  showPopup,
205
+ confirm: confirmModal,
249
206
  };
250
207
  }
251
208
 
@@ -272,20 +229,18 @@ async function playRewardedOnce(unitId: string): Promise<AdCloseResult> {
272
229
  }
273
230
 
274
231
  /**
275
- * 激励视频中途关闭挽留弹窗 —— 业务方在 claim 回调里也可以复用:
232
+ * 激励视频中途关闭挽留弹窗 —— 通过 useAd().confirm 暴露给业务方:
276
233
  *
277
- * showReward(async (closeRes) => {
278
- * if (!closeRes.isEnded) {
279
- * const goon = await confirmReward();
280
- * return { ok: false, retry: goon };
281
- * }
234
+ * const { showReward, confirm } = useAd();
235
+ * showReward(async ({ isEnded }) => {
236
+ * if (!isEnded) return { ok: false, retry: await confirm() };
282
237
  * const r = await claimAdReward();
283
238
  * return r.code === 1 ? { ok: true, reward: r.data?.reward } : { ok: false, msg: r.info };
284
239
  * });
285
240
  *
286
241
  * @returns true=用户选「继续观看」 / false=放弃
287
242
  */
288
- export function confirmReward(): Promise<boolean> {
243
+ function confirmModal(): Promise<boolean> {
289
244
  return new Promise((resolve) => {
290
245
  uni.showModal({
291
246
  title: "提示",
@@ -14,14 +14,11 @@ export {
14
14
  useAd,
15
15
  setConfigAd,
16
16
  destroyAds,
17
- confirmReward,
18
17
  type AdType,
19
18
  type AdConfig,
20
19
  type AdError,
21
20
  type AdAdapter,
22
21
  type AdCloseResult,
23
- type AdClaimResult,
24
- type AdClaimFn,
25
22
  } from "./ad";
26
23
  export {
27
24
  useShare,
@@ -20,6 +20,8 @@ export interface ModalOptions {
20
20
  cancelText?: string;
21
21
  confirmColor?: string;
22
22
  cancelColor?: string;
23
+ /** false=隐藏取消按钮(纯提示弹窗用) */
24
+ showCancel?: boolean;
23
25
  }
24
26
 
25
27
  export interface HlwMsg {
@@ -87,6 +89,7 @@ export function useMsg(): HlwMsg {
87
89
  cancelText = "取消",
88
90
  confirmColor = "#3b82f6",
89
91
  cancelColor = "#999999",
92
+ showCancel = true,
90
93
  } = opts;
91
94
  uni.showModal({
92
95
  title,
@@ -95,6 +98,7 @@ export function useMsg(): HlwMsg {
95
98
  cancelText,
96
99
  confirmColor,
97
100
  cancelColor,
101
+ showCancel,
98
102
  success: (res) => resolve(res.confirm),
99
103
  fail: () => resolve(false),
100
104
  });