@newbeebox/newbeebox-client-web-sdk 1.0.2 → 1.0.4

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 CHANGED
@@ -19,7 +19,27 @@ npm install @newbeebox/newbeebox-client-web-sdk
19
19
  await client.Init("newbee_appid");
20
20
 
21
21
  // 2.功能调用
22
- // 示例: 打开应用订阅界面
23
- await client.ShowSubscriptionPage()
24
-
22
+ // 示例一: 打开应用订阅界面
23
+ await client.ShowSubscriptionPage();
24
+
25
+ // 示例二: 获取当前用户订阅信息
26
+ let user_subscribe_info = await client.GetSubscriptionInfo();
27
+ console.log(user_subscribe_info);
28
+ // 输出:
29
+ {
30
+ "app_id": "nb00002",
31
+ "user": {
32
+ "open_id": "nb0d968311...",
33
+ "nickname": "Kyuu",
34
+ "avatar": "https://cdn8.newbeebox.com/...."
35
+ },
36
+ "subscribe_mode": 1,
37
+ "end_time": 0,
38
+ "level": 0,
39
+ "level_label": "",
40
+ "is_yearly": false,
41
+ "timestamp": 1758613861250,
42
+ "nonce": 6363921989,
43
+ "sign": "485d82860578ff..."
44
+ }
25
45
  ```
package/index.js CHANGED
@@ -2,9 +2,11 @@
2
2
  const CLIENT_CHECK_TIMEOUT = 200; // 0.2秒
3
3
  // 任务超时时间
4
4
  const TASK_REQUEST_TIMEOUT = 1000; // 1秒
5
- // 客户端可能的列表
5
+ // 客户端可能使用的端口列表
6
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 {
7
+ // 服务端地址
8
+ const CLIENT_SERVICE_BASE_URL = "http://127.0.0.1";
9
+ export default class NewBeeClient {
8
10
  app_id = "";
9
11
 
10
12
  newbee_client_port = 0;
@@ -33,7 +35,7 @@ export class NewBeeClient {
33
35
  }, CLIENT_CHECK_TIMEOUT);
34
36
 
35
37
  try {
36
- let response = await fetch("http://127.0.0.1" + (port ? ":" + port : "") + "/ping", {
38
+ let response = await fetch( CLIENT_SERVICE_BASE_URL + (port ? ":" + port : "") + "/ping", {
37
39
  method: "GET",
38
40
  signal: signal
39
41
  });
@@ -56,6 +58,7 @@ export class NewBeeClient {
56
58
  console.log("客户端开启的端口号:", this.newbee_client_port);
57
59
  }
58
60
 
61
+ // 打开应用订阅界面
59
62
  async ShowSubscriptionPage() {
60
63
  if (!this.newbee_client_port) {
61
64
  throw new Error("SDK未初始化或者未检测到新手盒子客户端");
@@ -76,7 +79,7 @@ export class NewBeeClient {
76
79
 
77
80
  try {
78
81
  // 测试成功就请求客户端授权
79
- let response = await fetch("http://127.0.0.1:" + this.newbee_client_port + "/tool/open_subscription_dialog", {
82
+ let response = await fetch(CLIENT_SERVICE_BASE_URL + ":" + this.newbee_client_port + "/tool/open_subscription_dialog", {
80
83
  method: "POST",
81
84
  signal: signal,
82
85
  headers: {
@@ -102,4 +105,37 @@ export class NewBeeClient {
102
105
 
103
106
  return true;
104
107
  }
108
+
109
+ // 获取当前使用的用户的订阅信息
110
+ async GetUserSubscription() {
111
+ if (!this.newbee_client_port) {
112
+ throw new Error("SDK未初始化或者未检测到新手盒子客户端");
113
+ }
114
+
115
+ let user_subscribe_response = null;
116
+ try {
117
+ // 测试成功就请求客户端授权
118
+ let response = await fetch(CLIENT_SERVICE_BASE_URL + ":" + this.newbee_client_port + "/tool/subscription_status", {
119
+ method: "POST",
120
+ headers: {
121
+ "Content-Type": 'application/json',
122
+ },
123
+ body: JSON.stringify({
124
+ app_id: this.app_id
125
+ })
126
+ });
127
+
128
+ user_subscribe_response = await response.json();
129
+
130
+ console.log("用户订阅信息响应:", user_subscribe_response);
131
+ } catch (e) {
132
+ console.log("获取用户订阅错误:", e);
133
+ }
134
+
135
+ if (!user_subscribe_response || user_subscribe_response?.code !== 1 || !user_subscribe_response?.data) {
136
+ throw new Error("获取用户订阅信息发生错误:" + (user_subscribe_response?.message || "未知错误"));
137
+ }
138
+
139
+ return user_subscribe_response.data
140
+ }
105
141
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@newbeebox/newbeebox-client-web-sdk",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "NewBeeBox Client SDK for Web",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/types/index.d.ts CHANGED
@@ -1,5 +1,93 @@
1
+ /**
2
+ * NewBeeClient
3
+ * @class NewBeeClient
4
+ * @description 新手盒子客户端SDK(Web端)
5
+ * @author NewBeeTeam
6
+ * @date 2025年9月23日14:42:24
7
+ * @version 1.0.0
8
+ */
1
9
  export default class NewBeeClient {
10
+ /**
11
+ * 构造函数
12
+ */
2
13
  constructor();
3
- Init (app_id:string): Promise<void>;
4
- ShowSubscriptionPage (): Promise<boolean>;
14
+
15
+ /**
16
+ * 初始化
17
+ * @param app_id
18
+ * @constructor Init
19
+ * @return {Promise<void>}
20
+ */
21
+ Init(app_id: string): Promise<void>;
22
+
23
+ /**
24
+ * 打开订阅页面
25
+ * @constructor ShowSubscriptionPage
26
+ * @return {Promise<boolean>}
27
+ */
28
+ ShowSubscriptionPage(): Promise<boolean>;
29
+
30
+ /**
31
+ * 获取用户订阅信息
32
+ * @constructor GetUserSubscription
33
+ * @return {Promise<UserSubscribeInfo>}
34
+ */
35
+ GetUserSubscription(): Promise<UserSubscribeInfo>;
36
+ }
37
+
38
+ /**
39
+ * @enum SUBSCRIBE_MODE
40
+ * @description 订阅模式
41
+ * @property {number} 1 作者订阅
42
+ * @property {number} 2 应用订阅
43
+ */
44
+ export enum SUBSCRIBE_MODE {
45
+ /**
46
+ * 作者订阅
47
+ */
48
+ AUTHOR_SUBSCRIBE = 1,
49
+ /**
50
+ * 应用订阅
51
+ */
52
+ APP_SUBSCRIBE = 2
53
+ }
54
+
55
+ /**
56
+ * @interface UserInfo
57
+ * @description 用户信息
58
+ * @property {string} open_id - 用户平台ID
59
+ * @property {string} nickname - 用户昵称
60
+ * @property {string} avatar - 用户头像
61
+ */
62
+ export interface UserInfo {
63
+ open_id:string;
64
+ nickname:string;
65
+ avatar:string;
66
+ }
67
+
68
+ /**
69
+ * @interface UserSubscribeInfo
70
+ * @description 用户订阅信息
71
+ * @property {string} app_id - 应用ID
72
+ * @property {UserInfo} user - 用户信息
73
+ * @property {SUBSCRIBE_MODE} subscribe_mode - 订阅模式
74
+ * @property {number} end_time - 订阅到期时间
75
+ * @property {number} level - 订阅等级
76
+ * @property {string} level_label - 订阅等级标签
77
+ * @property {boolean} is_yearly - 是否是年订阅
78
+ * @property {number} timestamp - 时间戳
79
+ * @property {string} nonce - 随机数
80
+ * @property {string} [sign] - 签名
81
+ */
82
+ export interface UserSubscribeInfo {
83
+ app_id: string;
84
+ user: UserInfo;
85
+ subscribe_mode: SUBSCRIBE_MODE;
86
+ end_time: number;
87
+ level: number;
88
+ level_label: string;
89
+ is_yearly: boolean;
90
+ timestamp: number;
91
+ nonce: string;
92
+ sign?: string;
5
93
  }