@jolibox/implement 1.1.11-beta.5 → 1.1.11-beta.7

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.
@@ -0,0 +1,152 @@
1
+ import { context } from '@/common/context';
2
+ import { invokeNative, subscribe } from '../bootstrap/bridge';
3
+ import { Deferred } from '@jolibox/common';
4
+ import { createRecommendModal, IGame, IRecommendationButton, RecommendModalOnCloseParams } from '@jolibox/ui';
5
+ import { innerFetch as fetch } from '../network';
6
+
7
+ let exitRecommendationsCache: {
8
+ code: string;
9
+ data: {
10
+ gameListInfo: {
11
+ games: IGame[];
12
+ };
13
+ title: string;
14
+ buttons: IRecommendationButton[];
15
+ };
16
+ } | null = null;
17
+
18
+ /**
19
+ * Fetches exit recommendations data from the API
20
+ */
21
+ async function fetchExitRecommendations() {
22
+ if (exitRecommendationsCache) {
23
+ return exitRecommendationsCache;
24
+ }
25
+ const host = context.testMode ? `https://stg-game.jolibox.com` : `https://game.jolibox.com`;
26
+ const url = `${host}/api/user-retention/exit-recommendations?objectId=${context.mpId}&from=GAME_DETAIL`;
27
+ const {
28
+ response: { data }
29
+ } = await fetch<{
30
+ code: string;
31
+ data: {
32
+ gameListInfo: {
33
+ games: IGame[];
34
+ };
35
+ title: string;
36
+ buttons: IRecommendationButton[];
37
+ };
38
+ }>(url);
39
+
40
+ exitRecommendationsCache = data;
41
+ return data;
42
+ }
43
+
44
+ //prefetch
45
+ fetchExitRecommendations();
46
+
47
+ const openGameSchema = (game: IGame) => {
48
+ const { data } = invokeNative('envSync');
49
+
50
+ // Parse the original URL to preserve its structure
51
+
52
+ const url = new URL(data.schema);
53
+ const originalPath = url.pathname;
54
+ const originalSearch = new URLSearchParams(url.search);
55
+
56
+ // Set or replace gameId and joliSource parameters
57
+ originalSearch.set('gameId', context.mpId);
58
+ originalSearch.set(
59
+ 'joliSource',
60
+ context.encodeJoliSourceQuery({
61
+ __mpType: 'game',
62
+ __orientation: game.orientation ?? 'VERTICAL'
63
+ })
64
+ );
65
+
66
+ const host = `https://${game.gameId}.content.jolibox.com/`;
67
+ // Construct the final schema URL
68
+ const schema = `${host}${originalPath}?${originalSearch.toString()}`;
69
+
70
+ invokeNative('openSchemaSync', {
71
+ schema
72
+ });
73
+ };
74
+
75
+ export async function openRetentionSchema() {
76
+ // const { data } = invokeNative('envSync');
77
+ // const { orientation, webviewId } = data;
78
+ // let joliPayload: Record<string, unknown> = {
79
+ // __mpType: 'miniApp',
80
+ // __transparent: true,
81
+ // // set entryPath
82
+ // __orientation: orientation ?? 'VERTICAL', // 默认竖屏
83
+ // __showStatusBar: false,
84
+ // __shouldInterupt: false,
85
+ // __showCapsuleButton: false
86
+ // };
87
+ // if (webviewId) {
88
+ // joliPayload = {
89
+ // ...joliPayload,
90
+ // __from: webviewId
91
+ // };
92
+ // }
93
+
94
+ // const joliSource = context.encodeJoliSourceQuery(joliPayload);
95
+
96
+ // const host = context.testMode
97
+ // ? `https://G32115508989327465281365749294.app.jolibox.com`
98
+ // : `https://G32115508989327465281365749294.app.jolibox.com`;
99
+ // const retentionSchema = `${host}/recommended-guide/${context.mpId}?appId=${context.mpId}&joliSource=${joliSource}&navigationStyle=present`;
100
+
101
+ const quitResultDeffer = new Deferred<boolean>();
102
+ // 小程序不走挽留逻辑
103
+ if (context.mpType !== 'game') {
104
+ quitResultDeffer.resolve(false);
105
+ return quitResultDeffer.promise;
106
+ }
107
+
108
+ const data = await fetchExitRecommendations();
109
+
110
+ if (data.code !== 'SUCCESS') {
111
+ quitResultDeffer.resolve(false);
112
+ return quitResultDeffer.promise;
113
+ }
114
+
115
+ const { gameListInfo, title, buttons } = data.data;
116
+
117
+ const modal = createRecommendModal({
118
+ games: gameListInfo.games,
119
+ title,
120
+ buttons,
121
+ onClose: (params: RecommendModalOnCloseParams) => {
122
+ switch (params.type) {
123
+ case 'quit':
124
+ quitResultDeffer.resolve(false);
125
+ break;
126
+ case 'dismiss':
127
+ quitResultDeffer.resolve(true);
128
+ break;
129
+ case 'navigate':
130
+ // TODO: 跳转游戏
131
+ if (params.data?.game) {
132
+ openGameSchema(params.data.game);
133
+ } else {
134
+ quitResultDeffer.resolve(true);
135
+ }
136
+ modal.hide();
137
+ break;
138
+ default:
139
+ // 关闭弹框,留在当前游戏
140
+ quitResultDeffer.resolve(true);
141
+ break;
142
+ }
143
+ }
144
+ });
145
+ // // 异步
146
+ // setTimeout(() => {
147
+ // invokeNative('openSchemaSync', {
148
+ // schema: retentionSchema
149
+ // });
150
+ // }, 0);
151
+ return quitResultDeffer.promise;
152
+ }
@@ -1,44 +0,0 @@
1
- import { context } from '@/common/context';
2
- import { invokeNative, subscribe } from './bridge';
3
- import { Deferred } from '@jolibox/common';
4
- export async function openRetentionSchema() {
5
- const { data } = invokeNative('envSync');
6
- const { orientation, webviewId } = data;
7
- let joliPayload: Record<string, unknown> = {
8
- __mpType: 'miniApp',
9
- __transparent: true,
10
- // set entryPath
11
- __orientation: orientation ?? 'VERTICAL', // 默认竖屏
12
- __showStatusBar: false,
13
- __shouldInterupt: false,
14
- __showCapsuleButton: false
15
- };
16
- if (webviewId) {
17
- joliPayload = {
18
- ...joliPayload,
19
- __from: webviewId
20
- };
21
- }
22
-
23
- const joliSource = context.encodeJoliSourceQuery(joliPayload);
24
-
25
- const host = context.testMode
26
- ? `https://G32115508989327465281365749294.app.jolibox.com`
27
- : `https://G32115508989327465281365749294.app.jolibox.com`;
28
- const retentionSchema = `${host}/recommended-guide/${context.mpId}?appId=${context.mpId}&joliSource=${joliSource}&navigationStyle=present`;
29
-
30
- const quitResultDeffer = new Deferred<boolean>();
31
-
32
- subscribe('onRetentionResult', ({ shouldStay }) => {
33
- quitResultDeffer.resolve(shouldStay);
34
- });
35
- // 异步
36
- setTimeout(() => {
37
- invokeNative('openSchemaSync', {
38
- schema: retentionSchema
39
- });
40
- }, 0);
41
- return quitResultDeffer.promise;
42
- }
43
-
44
- // 添加锁变量,防止多次执行
File without changes