@taole/dev-helper 0.1.22 → 0.1.24

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.
@@ -8,6 +8,8 @@
8
8
  const appInfoMap = {
9
9
  'wx6cd4c28b58e8737f': { // 点点开黑
10
10
  app_name: '点点开黑',
11
+ openinstall_app_key: 'qrsz57',
12
+ openinstall_script: 'https://res.opstatistics.com/openinstall-qrsz57.js',
11
13
  link_ios: "com.tuwan.diandian://openPage",
12
14
  link_android: "intent://diandain:8888/splash#Intent;scheme=taole;package=com.tuwan.yw;S.browser_fallback_url=https://sj.qq.com/myapp/detail.htm?apkName=com.tuwan.yw;end",
13
15
  link_harmony: "taole://diandain:8888/splash",
@@ -33,6 +35,39 @@
33
35
  fallback_fin_5s: 'https://sj.qq.com/appdetail/com.tuwan.game.werewolf',
34
36
  },
35
37
  };
38
+ // OpenInstall 初始化对应页面访问,多个组件共用同一个实例。
39
+ const openInstallInstances = new Map();
40
+ function resolveOpenInstall(appInfo) {
41
+ const key = appInfo && appInfo.openinstall_app_key;
42
+ if (!key || !appInfo.openinstall_script) {
43
+ return Promise.reject(new Error('OpenInstall is not configured for this app'));
44
+ }
45
+ if (openInstallInstances.has(key)) return openInstallInstances.get(key);
46
+ const promise = new Promise((resolve, reject) => {
47
+ const timer = setTimeout(() => reject(new Error('OpenInstall initialization timed out')), 10000);
48
+ const fail = error => { clearTimeout(timer); reject(error); };
49
+ const initialize = () => {
50
+ try {
51
+ if (!window.OpenInstall) throw new Error('OpenInstall SDK is unavailable');
52
+ new window.OpenInstall({ appKey: key, onready: function () { clearTimeout(timer); resolve(this); } },
53
+ window.OpenInstall.parseUrlParams());
54
+ } catch (error) { fail(error); }
55
+ };
56
+ if (window.OpenInstall) {
57
+ initialize();
58
+ return;
59
+ }
60
+ const script = document.createElement('script');
61
+ script.src = appInfo.openinstall_script;
62
+ script.charset = 'UTF-8';
63
+ script.onload = initialize;
64
+ script.onerror = () => { script.remove(); fail(new Error('load OpenInstall error')); };
65
+ document.head.appendChild(script);
66
+ });
67
+ openInstallInstances.set(key, promise);
68
+ promise.catch(() => { if (openInstallInstances.get(key) === promise) openInstallInstances.delete(key); });
69
+ return promise;
70
+ }
36
71
  function resolveTwKit() {
37
72
  if (window.twKit) {
38
73
  return;
@@ -56,7 +91,7 @@
56
91
  return class extends HTMLElement {
57
92
  // fallback-time: 3000, 跳转fallback链接的延迟时间,单位毫秒 不传或者传0等价于3000ms
58
93
  static get observedAttributes() {
59
- return ['appid', 'extinfo', 'debug', 'scene', 'fallback-time'];
94
+ return ['appid', 'extinfo', 'debug', 'scene', 'fallback-time', 'openinstall'];
60
95
  }
61
96
  constructor() {
62
97
  super();
@@ -84,6 +119,10 @@
84
119
  this._clipboardText = '';
85
120
  this._clipboardTextPs = null;
86
121
  this._clipboardTextTimer = null;
122
+ this._openInstall = null;
123
+ this._openInstallPromise = null;
124
+ this._openInstallKey = '';
125
+ this._openInstallPending = false;
87
126
  }
88
127
  _log(...args) {
89
128
  if (this._debug) {
@@ -97,6 +136,8 @@
97
136
  connectedCallback() {
98
137
  this._log("connectedCallback");
99
138
  this._isConnected = true;
139
+ this._updateTagAttr();
140
+ this._initOpenInstall();
100
141
  if (this._isWechat) {
101
142
  this._genTemplate();
102
143
  if (!this._openAppTag) {
@@ -130,6 +171,10 @@
130
171
  bubbles: true,
131
172
  composed: true,
132
173
  }));
174
+ if (this._useOpenInstall()) {
175
+ this._wakeupOpenInstall();
176
+ return;
177
+ }
133
178
  if (this._isHarmony || this._isSceneLink()) {
134
179
  this._showOpenInBrowserHint();
135
180
  return;
@@ -216,7 +261,12 @@
216
261
  this._initClipboardText();
217
262
  }
218
263
  }
219
- _handleAlinkClick() {
264
+ _handleAlinkClick(event) {
265
+ if (this._useOpenInstall()) {
266
+ event.preventDefault();
267
+ this._wakeupOpenInstall();
268
+ return;
269
+ }
220
270
  this._writeClickboard();
221
271
  if (this._alink_link != this._alink_fallback) {
222
272
  this._log("this._alink is clicked");
@@ -237,6 +287,7 @@
237
287
  disconnectedCallback() {
238
288
  this._log("disconnectedCallback");
239
289
  this._isConnected = false;
290
+ this._openInstallPending = false;
240
291
  if (this._openAppTag) {
241
292
  this._openAppTag.remove();
242
293
  this._openAppTag = null;
@@ -257,6 +308,40 @@
257
308
  this._fin5sVisHandler = null;
258
309
  }
259
310
  }
311
+ _useOpenInstall() {
312
+ return this.hasAttribute('openinstall') && !!(this._appInfo && this._appInfo.openinstall_app_key);
313
+ }
314
+ _initOpenInstall() {
315
+ if (!this._isConnected || this._isApp || !this._isMobile || !this._useOpenInstall()) return;
316
+ const key = this._appInfo.openinstall_app_key;
317
+ if (this._openInstallKey === key && this._openInstallPromise) return;
318
+ this._openInstallKey = key;
319
+ this._openInstall = null;
320
+ const promise = resolveOpenInstall(this._appInfo);
321
+ this._openInstallPromise = promise;
322
+ promise.then(instance => {
323
+ if (this._openInstallPromise !== promise) return;
324
+ this._openInstall = instance;
325
+ if (this._openInstallPending && this._isConnected && this._useOpenInstall()) {
326
+ this._openInstallPending = false;
327
+ instance.wakeupOrInstall();
328
+ }
329
+ }).catch(error => {
330
+ if (this._openInstallPromise !== promise) return;
331
+ this._openInstallPromise = null;
332
+ this._openInstallPending = false;
333
+ this._log('OpenInstall initialization failed', error);
334
+ this.dispatchEvent(new CustomEvent('openinstallerror', { detail: { error }, bubbles: true, composed: true }));
335
+ });
336
+ }
337
+ _wakeupOpenInstall() {
338
+ if (this._openInstall) {
339
+ this._openInstall.wakeupOrInstall();
340
+ } else {
341
+ this._openInstallPending = true;
342
+ this._initOpenInstall();
343
+ }
344
+ }
260
345
  _initClipboardText() {
261
346
  // 延迟20ms执行,如果20ms后卸载了,直接不执行了
262
347
  this._clipboardTextTimer = setTimeout(() => {
@@ -274,7 +359,13 @@
274
359
  this._clipboardTextPs = new Promise(async (resolve, reject) => {
275
360
  try {
276
361
  await resolveTwKit();
277
- const urlText = `https://${twKit.isTest ? 'y-test.tuwan.com' : 'y.tuwan.com'}/events/app_transfer_page?extinfo=` + encodeURIComponent(extinfo);
362
+ let urlText = `https://${twKit.isTest ? 'y-test.tuwan.com' : 'y.tuwan.com'}/events/app_transfer_page?extinfo=` + encodeURIComponent(extinfo);
363
+ try {
364
+ const info = JSON.parse(extinfo);
365
+ if (info && info.openType == 3 && info.isPop && info.url) {
366
+ urlText = info.url;
367
+ }
368
+ } catch (e) {}
278
369
  const shorUrl = await twKit.genShortUrl(urlText);
279
370
  this._clipboardText = `【${this._appInfo.app_name}】 ${shorUrl} `;
280
371
  this._log("initClipboardText success", this._clipboardText);
@@ -369,6 +460,12 @@
369
460
  this._debug = this.getAttribute('debug') !== null;
370
461
  this._appid = this.getAttribute('appid') || this._defaultAppid;
371
462
  this._appInfo = appInfoMap[this._appid];
463
+ if (this._openInstallKey && (!this._appInfo || this._openInstallKey !== this._appInfo.openinstall_app_key)) {
464
+ this._openInstallKey = '';
465
+ this._openInstall = null;
466
+ this._openInstallPromise = null;
467
+ this._openInstallPending = false;
468
+ }
372
469
  var fallbackTime = this.getAttribute('fallback-time');
373
470
  if (fallbackTime == null || fallbackTime === '' || isNaN(Number(fallbackTime))) {
374
471
  this._fallbackTime = 3000;
@@ -380,6 +477,10 @@
380
477
  console.error('open-diandian: appid not supported', this._appid);
381
478
  return;
382
479
  }
480
+ if (this._isConnected) {
481
+ if (!this._useOpenInstall()) this._openInstallPending = false;
482
+ this._initOpenInstall();
483
+ }
383
484
  this._log('open-diandian: appInfo', this._appInfo);
384
485
  if (this._isIOS) {
385
486
  this._alink_fallback = this._appInfo.fallback_link_ios;
@@ -3,6 +3,8 @@
3
3
  const appInfoMap = {
4
4
  'wx6cd4c28b58e8737f': { // 点点开黑
5
5
  app_name: '点点开黑',
6
+ openinstall_app_key: 'qrsz57',
7
+ openinstall_script: 'https://res.opstatistics.com/openinstall-qrsz57.js',
6
8
  link_ios: "com.tuwan.diandian://openPage",
7
9
  link_android: "intent://diandain:8888/splash#Intent;scheme=taole;package=com.tuwan.yw;S.browser_fallback_url=https://sj.qq.com/myapp/detail.htm?apkName=com.tuwan.yw;end",
8
10
  link_harmony: "taole://diandain:8888/splash",
@@ -28,6 +30,39 @@ const appInfoMap = {
28
30
  fallback_fin_5s: 'https://sj.qq.com/appdetail/com.tuwan.game.werewolf',
29
31
  },
30
32
  }
33
+ // OpenInstall 初始化对应页面访问,多个组件共用同一个实例。
34
+ const openInstallInstances = new Map();
35
+ function resolveOpenInstall(appInfo) {
36
+ const key = appInfo && appInfo.openinstall_app_key;
37
+ if (!key || !appInfo.openinstall_script) {
38
+ return Promise.reject(new Error('OpenInstall is not configured for this app'));
39
+ }
40
+ if (openInstallInstances.has(key)) return openInstallInstances.get(key);
41
+ const promise = new Promise((resolve, reject) => {
42
+ const timer = setTimeout(() => reject(new Error('OpenInstall initialization timed out')), 10000);
43
+ const fail = error => { clearTimeout(timer); reject(error); };
44
+ const initialize = () => {
45
+ try {
46
+ if (!window.OpenInstall) throw new Error('OpenInstall SDK is unavailable');
47
+ new window.OpenInstall({ appKey: key, onready: function () { clearTimeout(timer); resolve(this); } },
48
+ window.OpenInstall.parseUrlParams());
49
+ } catch (error) { fail(error); }
50
+ };
51
+ if (window.OpenInstall) {
52
+ initialize();
53
+ return;
54
+ }
55
+ const script = document.createElement('script');
56
+ script.src = appInfo.openinstall_script;
57
+ script.charset = 'UTF-8';
58
+ script.onload = initialize;
59
+ script.onerror = () => { script.remove(); fail(new Error('load OpenInstall error')); };
60
+ document.head.appendChild(script);
61
+ });
62
+ openInstallInstances.set(key, promise);
63
+ promise.catch(() => { if (openInstallInstances.get(key) === promise) openInstallInstances.delete(key); });
64
+ return promise;
65
+ }
31
66
  function resolveTwKit() {
32
67
  if (window.twKit) {
33
68
  return;
@@ -51,7 +86,7 @@ function createOpenDiandianElementClass() {
51
86
  return class extends HTMLElement {
52
87
  // fallback-time: 3000, 跳转fallback链接的延迟时间,单位毫秒 不传或者传0等价于3000ms
53
88
  static get observedAttributes() {
54
- return ['appid', 'extinfo', 'debug', 'scene', 'fallback-time'];
89
+ return ['appid', 'extinfo', 'debug', 'scene', 'fallback-time', 'openinstall'];
55
90
  }
56
91
  constructor() {
57
92
  super();
@@ -79,6 +114,10 @@ function createOpenDiandianElementClass() {
79
114
  this._clipboardText = '';
80
115
  this._clipboardTextPs = null;
81
116
  this._clipboardTextTimer = null;
117
+ this._openInstall = null;
118
+ this._openInstallPromise = null;
119
+ this._openInstallKey = '';
120
+ this._openInstallPending = false;
82
121
  }
83
122
  _log(...args) {
84
123
  if (this._debug) {
@@ -92,6 +131,8 @@ function createOpenDiandianElementClass() {
92
131
  connectedCallback() {
93
132
  this._log("connectedCallback")
94
133
  this._isConnected = true;
134
+ this._updateTagAttr();
135
+ this._initOpenInstall();
95
136
  if (this._isWechat) {
96
137
  this._genTemplate();
97
138
  if (!this._openAppTag) {
@@ -125,6 +166,10 @@ function createOpenDiandianElementClass() {
125
166
  bubbles: true,
126
167
  composed: true,
127
168
  }));
169
+ if (this._useOpenInstall()) {
170
+ this._wakeupOpenInstall();
171
+ return;
172
+ }
128
173
  if (this._isHarmony || this._isSceneLink()) {
129
174
  this._showOpenInBrowserHint();
130
175
  return;
@@ -211,7 +256,12 @@ function createOpenDiandianElementClass() {
211
256
  this._initClipboardText();
212
257
  }
213
258
  }
214
- _handleAlinkClick() {
259
+ _handleAlinkClick(event) {
260
+ if (this._useOpenInstall()) {
261
+ event.preventDefault();
262
+ this._wakeupOpenInstall();
263
+ return;
264
+ }
215
265
  this._writeClickboard();
216
266
  if (this._alink_link != this._alink_fallback) {
217
267
  this._log("this._alink is clicked");
@@ -232,6 +282,7 @@ function createOpenDiandianElementClass() {
232
282
  disconnectedCallback() {
233
283
  this._log("disconnectedCallback")
234
284
  this._isConnected = false;
285
+ this._openInstallPending = false;
235
286
  if (this._openAppTag) {
236
287
  this._openAppTag.remove();
237
288
  this._openAppTag = null;
@@ -252,6 +303,40 @@ function createOpenDiandianElementClass() {
252
303
  this._fin5sVisHandler = null;
253
304
  }
254
305
  }
306
+ _useOpenInstall() {
307
+ return this.hasAttribute('openinstall') && !!(this._appInfo && this._appInfo.openinstall_app_key);
308
+ }
309
+ _initOpenInstall() {
310
+ if (!this._isConnected || this._isApp || !this._isMobile || !this._useOpenInstall()) return;
311
+ const key = this._appInfo.openinstall_app_key;
312
+ if (this._openInstallKey === key && this._openInstallPromise) return;
313
+ this._openInstallKey = key;
314
+ this._openInstall = null;
315
+ const promise = resolveOpenInstall(this._appInfo);
316
+ this._openInstallPromise = promise;
317
+ promise.then(instance => {
318
+ if (this._openInstallPromise !== promise) return;
319
+ this._openInstall = instance;
320
+ if (this._openInstallPending && this._isConnected && this._useOpenInstall()) {
321
+ this._openInstallPending = false;
322
+ instance.wakeupOrInstall();
323
+ }
324
+ }).catch(error => {
325
+ if (this._openInstallPromise !== promise) return;
326
+ this._openInstallPromise = null;
327
+ this._openInstallPending = false;
328
+ this._log('OpenInstall initialization failed', error);
329
+ this.dispatchEvent(new CustomEvent('openinstallerror', { detail: { error }, bubbles: true, composed: true }));
330
+ });
331
+ }
332
+ _wakeupOpenInstall() {
333
+ if (this._openInstall) {
334
+ this._openInstall.wakeupOrInstall();
335
+ } else {
336
+ this._openInstallPending = true;
337
+ this._initOpenInstall();
338
+ }
339
+ }
255
340
  _initClipboardText() {
256
341
  // 延迟20ms执行,如果20ms后卸载了,直接不执行了
257
342
  this._clipboardTextTimer = setTimeout(() => {
@@ -269,7 +354,13 @@ function createOpenDiandianElementClass() {
269
354
  this._clipboardTextPs = new Promise(async (resolve, reject) => {
270
355
  try {
271
356
  await resolveTwKit();
272
- const urlText = `https://${twKit.isTest ? 'y-test.tuwan.com' : 'y.tuwan.com'}/events/app_transfer_page?extinfo=` + encodeURIComponent(extinfo);
357
+ let urlText = `https://${twKit.isTest ? 'y-test.tuwan.com' : 'y.tuwan.com'}/events/app_transfer_page?extinfo=` + encodeURIComponent(extinfo);
358
+ try {
359
+ const info = JSON.parse(extinfo);
360
+ if (info && info.openType == 3 && info.isPop && info.url) {
361
+ urlText = info.url;
362
+ }
363
+ } catch (e) {}
273
364
  const shorUrl = await twKit.genShortUrl(urlText);
274
365
  this._clipboardText = `【${this._appInfo.app_name}】 ${shorUrl} `;
275
366
  this._log("initClipboardText success", this._clipboardText);
@@ -364,6 +455,12 @@ function createOpenDiandianElementClass() {
364
455
  this._debug = this.getAttribute('debug') !== null;
365
456
  this._appid = this.getAttribute('appid') || this._defaultAppid;
366
457
  this._appInfo = appInfoMap[this._appid];
458
+ if (this._openInstallKey && (!this._appInfo || this._openInstallKey !== this._appInfo.openinstall_app_key)) {
459
+ this._openInstallKey = '';
460
+ this._openInstall = null;
461
+ this._openInstallPromise = null;
462
+ this._openInstallPending = false;
463
+ }
367
464
  var fallbackTime = this.getAttribute('fallback-time');
368
465
  if (fallbackTime == null || fallbackTime === '' || isNaN(Number(fallbackTime))) {
369
466
  this._fallbackTime = 3000;
@@ -375,6 +472,10 @@ function createOpenDiandianElementClass() {
375
472
  console.error('open-diandian: appid not supported', this._appid);
376
473
  return;
377
474
  }
475
+ if (this._isConnected) {
476
+ if (!this._useOpenInstall()) this._openInstallPending = false;
477
+ this._initOpenInstall();
478
+ }
378
479
  this._log('open-diandian: appInfo', this._appInfo);
379
480
  if (this._isIOS) {
380
481
  this._alink_fallback = this._appInfo.fallback_link_ios;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@taole/dev-helper",
3
- "version": "0.1.22",
3
+ "version": "0.1.24",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "dev-helper": "./bin/dev-helper.js"