@newbeebox/newbeebox-client-web-sdk 1.0.0

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/README.md ADDED
File without changes
package/index.js ADDED
@@ -0,0 +1,105 @@
1
+ // 设置超时时间
2
+ const CLIENT_CHECK_TIMEOUT = 200; // 0.2秒
3
+ // 任务超时时间
4
+ const TASK_REQUEST_TIMEOUT = 1000; // 1秒
5
+ // 客户端可能的列表
6
+ const LOCAL_PORT_LIST = [35560, 35561, 35562, 35563, 35564, 35565, 35566, 35567, 35568, 35569, 35570, 35571, 35572, 35573, 35574, 35575, 35576, 35577, 35578, 35579];
7
+ export class NewBeeClient {
8
+ app_id = "";
9
+
10
+ newbee_client_port = 0;
11
+
12
+ constructor() {}
13
+
14
+ async Init(app_id) {
15
+
16
+ if (!app_id || typeof app_id !== "string") {
17
+ throw new Error("APPID格式不正确 请前往后台获取");
18
+ }
19
+
20
+ this.app_id = app_id;
21
+
22
+ for (const port of LOCAL_PORT_LIST) {
23
+ // 创建 AbortController 实例
24
+ const controller = new AbortController();
25
+
26
+ const signal = controller.signal;
27
+
28
+ let check_response;
29
+
30
+ const timeoutId = setTimeout(() => {
31
+ controller.abort(); // 超时后取消请求
32
+ console.log("端口:", port, '请求超时');
33
+ }, CLIENT_CHECK_TIMEOUT);
34
+
35
+ try {
36
+ let response = await fetch("http://127.0.0.1" + (port ? ":" + port : "") + "/ping", {
37
+ method: "GET",
38
+ signal: signal
39
+ });
40
+
41
+ check_response = await response.json();
42
+
43
+ clearTimeout(timeoutId);
44
+ } catch (e) {
45
+ console.error(port, "获取信息失败:", e);
46
+ }
47
+
48
+ console.log("端口检查:", check_response);
49
+
50
+ if (check_response && check_response.code === 1) {
51
+ this.newbee_client_port = port;
52
+ break;
53
+ }
54
+ }
55
+
56
+ console.log("客户端开启的端口号:", this.newbee_client_port);
57
+ }
58
+
59
+ async ShowSubscriptionPage() {
60
+ if (!this.newbee_client_port) {
61
+ throw new Error("SDK未初始化或者未检测到新手盒子客户端");
62
+ }
63
+
64
+
65
+ // 创建 AbortController 实例
66
+ const controller = new AbortController();
67
+
68
+ const signal = controller.signal;
69
+
70
+ const timeoutId = setTimeout(() => {
71
+ controller.abort("请求超时"); // 超时后取消请求
72
+ console.log('打开订单页面超时');
73
+ }, TASK_REQUEST_TIMEOUT);
74
+
75
+ let show_page_response;
76
+
77
+ try {
78
+ // 测试成功就请求客户端授权
79
+ let response = await fetch("http://127.0.0.1:" + this.newbee_client_port + "/tool/open_subscription_dialog", {
80
+ method: "POST",
81
+ signal: signal,
82
+ headers: {
83
+ "Content-Type": 'application/json',
84
+ },
85
+ body: JSON.stringify({
86
+ app_id: this.app_id
87
+ })
88
+ });
89
+
90
+ clearTimeout(timeoutId);
91
+
92
+ show_page_response = await response.json();
93
+
94
+ console.log("开启页面结果:", show_page_response);
95
+ } catch (e) {
96
+ console.log("打开订单页面超时", e);
97
+ }
98
+
99
+ if (!show_page_response || show_page_response?.code !== 1) {
100
+ throw new Error("打开订单页面发生错误:" + (show_page_response?.message || "未知错误"));
101
+ }
102
+
103
+ return true;
104
+ }
105
+ }
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@newbeebox/newbeebox-client-web-sdk",
3
+ "version": "1.0.0",
4
+ "description": "NewBeeBox Client SDK for Web",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "types": "./types/index.d.ts",
8
+ "files": [
9
+ "*.js",
10
+ "types/*.d.ts"
11
+ ],
12
+ "scripts": {},
13
+ "keywords": ["newbeebox","wow","sdk","client"],
14
+ "author": "NewBeeBoxTeam",
15
+ "license": "ISC",
16
+ "publishConfig": {
17
+ "registry": "https://registry.npmjs.org/",
18
+ "access": "public"
19
+ }
20
+ }
@@ -0,0 +1,5 @@
1
+ export default class NewBeeClient {
2
+ constructor();
3
+ Init (app_id:string): Promise<void>;
4
+ ShowSubscriptionPage (): Promise<boolean>;
5
+ }