ms-vite-plugin 1.1.2 → 1.1.3
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/build.js +6 -0
- package/dist/cli.js +91 -1
- package/dist/mcp/device-config.d.ts +55 -0
- package/dist/mcp/device-config.js +183 -0
- package/dist/mcp/docs-service.d.ts +65 -0
- package/dist/mcp/docs-service.js +168 -0
- package/dist/mcp/project.d.ts +16 -0
- package/dist/mcp/project.js +74 -0
- package/dist/mcp/tools.d.ts +18 -0
- package/dist/mcp/tools.js +825 -0
- package/dist/mcp/types.d.ts +32 -0
- package/dist/mcp/types.js +11 -0
- package/dist/mcp-server.d.ts +2 -0
- package/dist/mcp-server.js +86 -0
- package/dist/project.d.ts +89 -0
- package/dist/project.js +306 -0
- package/dist/version.d.ts +12 -0
- package/dist/version.js +63 -0
- package/docs/api/action.md +922 -0
- package/docs/api/appleocr.md +229 -0
- package/docs/api/config.md +122 -0
- package/docs/api/cryptoUtils.md +232 -0
- package/docs/api/device.md +374 -0
- package/docs/api/file.md +516 -0
- package/docs/api/global.md +617 -0
- package/docs/api/hid.md +1032 -0
- package/docs/api/hotUpdate.md +166 -0
- package/docs/api/http.md +548 -0
- package/docs/api/image.md +907 -0
- package/docs/api/ime.md +290 -0
- package/docs/api/logger.md +324 -0
- package/docs/api/media.md +248 -0
- package/docs/api/mysql.md +441 -0
- package/docs/api/netCard.md +200 -0
- package/docs/api/node.md +353 -0
- package/docs/api/paddleocr.md +246 -0
- package/docs/api/pip.md +242 -0
- package/docs/api/system.md +572 -0
- package/docs/api/thread.md +269 -0
- package/docs/api/tomatoocr.md +425 -0
- package/docs/api/tts.md +334 -0
- package/docs/api/ui.md +947 -0
- package/docs/api/utils.md +265 -0
- package/docs/api/yolo.md +310 -0
- package/docs/apicn/action.md +919 -0
- package/docs/apicn/appleocr.md +233 -0
- package/docs/apicn/config.md +120 -0
- package/docs/apicn/device.md +385 -0
- package/docs/apicn/file.md +511 -0
- package/docs/apicn/global.md +613 -0
- package/docs/apicn/hid.md +1033 -0
- package/docs/apicn/hotUpdate.md +170 -0
- package/docs/apicn/http.md +672 -0
- package/docs/apicn/image.md +924 -0
- package/docs/apicn/ime.md +290 -0
- package/docs/apicn/logger.md +332 -0
- package/docs/apicn/media.md +252 -0
- package/docs/apicn/mysql.md +445 -0
- package/docs/apicn/netCard.md +200 -0
- package/docs/apicn/node.md +362 -0
- package/docs/apicn/paddleocr.md +255 -0
- package/docs/apicn/pip.md +242 -0
- package/docs/apicn/system.md +575 -0
- package/docs/apicn/thread.md +269 -0
- package/docs/apicn/tts.md +338 -0
- package/docs/apicn/ui.md +933 -0
- package/docs/apicn/utils.md +265 -0
- package/docs/apicn/yolo.md +314 -0
- package/docs/apipython/action.md +901 -0
- package/docs/apipython/appleocr.md +226 -0
- package/docs/apipython/config.md +126 -0
- package/docs/apipython/cryptoUtils.md +246 -0
- package/docs/apipython/device.md +365 -0
- package/docs/apipython/file.md +476 -0
- package/docs/apipython/g.md +154 -0
- package/docs/apipython/hid.md +1059 -0
- package/docs/apipython/hotUpdate.md +154 -0
- package/docs/apipython/image.md +938 -0
- package/docs/apipython/ime.md +306 -0
- package/docs/apipython/logger.md +330 -0
- package/docs/apipython/media.md +221 -0
- package/docs/apipython/mysql.md +432 -0
- package/docs/apipython/netCard.md +219 -0
- package/docs/apipython/node.md +331 -0
- package/docs/apipython/overview.md +66 -0
- package/docs/apipython/paddleocr.md +211 -0
- package/docs/apipython/pip.md +231 -0
- package/docs/apipython/system.md +458 -0
- package/docs/apipython/tomatoocr.md +444 -0
- package/docs/apipython/tts.md +331 -0
- package/docs/apipython/ui.md +949 -0
- package/docs/apipython/utils.md +284 -0
- package/docs/apipython/yolo.md +281 -0
- package/package.json +8 -4
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# 热更新模块 (HotUpdate)
|
|
2
|
+
|
|
3
|
+
热更新模块提供了完整的应用热更新功能,支持检查更新、下载更新包、MD5 校验、对话框展示等核心功能。
|
|
4
|
+
|
|
5
|
+
## 自动检查更新
|
|
6
|
+
|
|
7
|
+
以下 api 功能给脚本启动时调用,检查是否有更新,有更新则下载更新包,下载完成后重启应用。
|
|
8
|
+
|
|
9
|
+
即使脚本没有调用,程序仍会在启动时自动检查更新。
|
|
10
|
+
|
|
11
|
+
## 类型定义
|
|
12
|
+
|
|
13
|
+
### HotUpdateResult
|
|
14
|
+
|
|
15
|
+
热更新检查结果接口。
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
interface HotUpdateResult {
|
|
19
|
+
needUpdate: boolean; // 是否需要更新
|
|
20
|
+
error?: string; // 错误信息(当检查失败时)
|
|
21
|
+
data?: HotUpdateResponse; // 服务器返回的更新数据(当 needUpdate 为 true 时)
|
|
22
|
+
}
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**参数说明**
|
|
26
|
+
|
|
27
|
+
| 参数名 | 类型 | 是否必填 | 默认值 | 描述 |
|
|
28
|
+
| ------------ | ----------------- | -------- | ------ | ------------------------------------------------ |
|
|
29
|
+
| `needUpdate` | boolean | 是 | - | 是否需要更新 |
|
|
30
|
+
| `error` | string | 否 | - | 错误信息(当检查失败时) |
|
|
31
|
+
| `data` | HotUpdateResponse | 否 | - | 服务器返回的更新数据(当 needUpdate 为 true 时) |
|
|
32
|
+
|
|
33
|
+
### HotUpdateResponse
|
|
34
|
+
|
|
35
|
+
服务器响应数据结构。
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
interface HotUpdateResponse {
|
|
39
|
+
download_url: string; // 新包的下载地址
|
|
40
|
+
version: number; // 新包的版本号
|
|
41
|
+
download_timeout: number; // 下载超时时间(秒)
|
|
42
|
+
dialog: boolean; // 是否用对话框展示
|
|
43
|
+
msg: string; // 对话框消息
|
|
44
|
+
force: boolean; // 是否强制更新
|
|
45
|
+
md5?: string; // 更新包MD5值(可选)
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
**参数说明**
|
|
50
|
+
|
|
51
|
+
| 参数名 | 类型 | 是否必填 | 默认值 | 描述 |
|
|
52
|
+
| ------------------ | ------- | -------- | ------ | --------------------- |
|
|
53
|
+
| `download_url` | string | 是 | - | 新包的下载地址 |
|
|
54
|
+
| `version` | number | 是 | - | 新包的版本号 |
|
|
55
|
+
| `download_timeout` | number | 是 | - | 下载超时时间(秒) |
|
|
56
|
+
| `dialog` | boolean | 是 | - | 是否用对话框展示 |
|
|
57
|
+
| `msg` | string | 是 | - | 对话框消息 |
|
|
58
|
+
| `force` | boolean | 是 | - | 是否强制更新 |
|
|
59
|
+
| `md5` | string | 否 | - | 更新包 MD5 值(可选) |
|
|
60
|
+
|
|
61
|
+
## API 参考
|
|
62
|
+
|
|
63
|
+
- 只有你需要代码检查更新,才需要调用 `checkUpdate` 方法
|
|
64
|
+
|
|
65
|
+
### 更新管理
|
|
66
|
+
|
|
67
|
+
#### checkUpdate - 检查更新
|
|
68
|
+
|
|
69
|
+
```typescript
|
|
70
|
+
function checkUpdate(): HotUpdateResult;
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
**返回值:**
|
|
74
|
+
|
|
75
|
+
| 类型 | 描述 |
|
|
76
|
+
| --------------- | ------------ |
|
|
77
|
+
| HotUpdateResult | 检查结果对象 |
|
|
78
|
+
|
|
79
|
+
**示例:**
|
|
80
|
+
|
|
81
|
+
```javascript
|
|
82
|
+
// 检查更新
|
|
83
|
+
const result = hotUpdate.checkUpdate();
|
|
84
|
+
|
|
85
|
+
if (result.needUpdate) {
|
|
86
|
+
logi("发现新版本:", result.data.version);
|
|
87
|
+
logi("更新消息:", result.data.msg);
|
|
88
|
+
const installResult = hotUpdate.downloadAndInstall();
|
|
89
|
+
if (installResult.updated) {
|
|
90
|
+
logi("更新安装成功");
|
|
91
|
+
} else {
|
|
92
|
+
loge("更新安装失败:", installResult.error);
|
|
93
|
+
}
|
|
94
|
+
} else if (result.error) {
|
|
95
|
+
loge("检查更新失败:", result.error);
|
|
96
|
+
} else {
|
|
97
|
+
logi("已是最新版本");
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
#### downloadAndInstall - 下载并安装更新
|
|
102
|
+
|
|
103
|
+
下载并安装更新,安装完成后自动重启脚本。
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
function downloadAndInstall(): {
|
|
107
|
+
updated: boolean;
|
|
108
|
+
error?: string;
|
|
109
|
+
};
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
**参数:**
|
|
113
|
+
|
|
114
|
+
- 无需参数,自动使用上次 `checkUpdate` 的结果
|
|
115
|
+
|
|
116
|
+
**返回值:**
|
|
117
|
+
|
|
118
|
+
| 参数名 | 类型 | 描述 |
|
|
119
|
+
| --------- | ------- | -------------------- |
|
|
120
|
+
| `updated` | boolean | 是否安装成功 |
|
|
121
|
+
| `error` | string | 安装失败时的错误信息 |
|
|
122
|
+
|
|
123
|
+
**示例:**
|
|
124
|
+
|
|
125
|
+
```javascript
|
|
126
|
+
// 先检查更新,再手动下载安装
|
|
127
|
+
const result = hotUpdate.checkUpdate();
|
|
128
|
+
|
|
129
|
+
if (result.needUpdate) {
|
|
130
|
+
const installResult = hotUpdate.downloadAndInstall();
|
|
131
|
+
if (installResult.updated) {
|
|
132
|
+
logi("更新安装成功,自动重启应用");
|
|
133
|
+
} else {
|
|
134
|
+
loge("更新失败: " + installResult.error);
|
|
135
|
+
}
|
|
136
|
+
} else {
|
|
137
|
+
logi("已是最新版本");
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### 版本信息
|
|
142
|
+
|
|
143
|
+
#### getCurrentVersion - 获取当前版本号
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
function getCurrentVersion(): number;
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
**返回值:**
|
|
150
|
+
|
|
151
|
+
| 参数名 | 类型 | 描述 |
|
|
152
|
+
| --------- | ------ | ---------------------------------- |
|
|
153
|
+
| `version` | number | 当前应用版本号(仅支持整数版本号) |
|
|
154
|
+
|
|
155
|
+
**版本获取逻辑:**
|
|
156
|
+
|
|
157
|
+
- 从 `package.json` 文件读取 `appVersion` 字段
|
|
158
|
+
- 支持数字类型的 `appVersion`
|
|
159
|
+
- 如果获取不到则返回 0
|
|
160
|
+
|
|
161
|
+
**示例:**
|
|
162
|
+
|
|
163
|
+
```javascript
|
|
164
|
+
const version = hotUpdate.getCurrentVersion();
|
|
165
|
+
logi("当前版本:", version);
|
|
166
|
+
```
|
package/docs/api/http.md
ADDED
|
@@ -0,0 +1,548 @@
|
|
|
1
|
+
# HTTP 模块 (HTTP)
|
|
2
|
+
|
|
3
|
+
HTTP 模块提供了完整的网络请求功能,包括 HTTP 请求、文件下载、WebSocket 连接等。
|
|
4
|
+
|
|
5
|
+
## 通用 HTTP 请求
|
|
6
|
+
|
|
7
|
+
### request - 发送通用 HTTP 请求,支持所有 HTTP 方法和完整的配置选项。
|
|
8
|
+
|
|
9
|
+
```typescript
|
|
10
|
+
function request(params: {
|
|
11
|
+
url: string;
|
|
12
|
+
timeout?: number;
|
|
13
|
+
method?: string;
|
|
14
|
+
proxy?: { host: string; port: number };
|
|
15
|
+
followRedirects?: boolean;
|
|
16
|
+
requestBody?: string;
|
|
17
|
+
userAgent?: string;
|
|
18
|
+
ignoreContentType?: boolean;
|
|
19
|
+
ignoreHttpErrors?: boolean;
|
|
20
|
+
maxBodySize?: number;
|
|
21
|
+
referrer?: string;
|
|
22
|
+
header?: Record<string, string>;
|
|
23
|
+
cookie?: Record<string, string>;
|
|
24
|
+
data?: Record<string, any>;
|
|
25
|
+
file?: Record<string, string>;
|
|
26
|
+
responseCharset?: string;
|
|
27
|
+
}): {
|
|
28
|
+
header: Record<string, string>;
|
|
29
|
+
cookie: Record<string, string>;
|
|
30
|
+
statusCode: number;
|
|
31
|
+
statusMessage: string;
|
|
32
|
+
charset: string;
|
|
33
|
+
contentType: string;
|
|
34
|
+
body: string;
|
|
35
|
+
};
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
**参数:**
|
|
39
|
+
|
|
40
|
+
| 参数名 | 类型 | 是否必填 | 默认值 | 描述 |
|
|
41
|
+
| ------------------- | ------- | -------- | -------- | --------------------------- |
|
|
42
|
+
| `url` | string | 是 | - | 请求地址 |
|
|
43
|
+
| `timeout` | number | 否 | 10000 | 请求超时时间(毫秒) |
|
|
44
|
+
| `method` | string | 否 | GET | 请求方法(GET、POST 等) |
|
|
45
|
+
| `proxy` | object | 否 | - | 代理服务器配置 |
|
|
46
|
+
| `followRedirects` | boolean | 否 | true | 是否跟随重定向 |
|
|
47
|
+
| `requestBody` | string | 否 | - | 请求体(POST 等方法使用) |
|
|
48
|
+
| `userAgent` | string | 否 | - | 用户代理字符串 |
|
|
49
|
+
| `ignoreContentType` | boolean | 否 | false | 是否忽略内容类型 |
|
|
50
|
+
| `ignoreHttpErrors` | boolean | 否 | false | 是否忽略 HTTP 错误 |
|
|
51
|
+
| `maxBodySize` | number | 否 | 10485760 | 最大响应体大小(默认 10MB) |
|
|
52
|
+
| `referrer` | string | 否 | - | 引用来源 |
|
|
53
|
+
| `header` | object | 否 | - | 请求头 |
|
|
54
|
+
| `cookie` | object | 否 | - | Cookie |
|
|
55
|
+
| `data` | object | 否 | - | 请求参数 |
|
|
56
|
+
| `file` | object | 否 | - | 请求文件 |
|
|
57
|
+
| `responseCharset` | string | 否 | - | 响应字符集 |
|
|
58
|
+
|
|
59
|
+
**返回值:**
|
|
60
|
+
|
|
61
|
+
| 字段名 | 类型 | 描述 |
|
|
62
|
+
| --------------- | ------ | ------------- |
|
|
63
|
+
| `header` | object | 响应头 |
|
|
64
|
+
| `cookie` | object | 响应 Cookie |
|
|
65
|
+
| `statusCode` | number | HTTP 状态码 |
|
|
66
|
+
| `statusMessage` | string | HTTP 状态消息 |
|
|
67
|
+
| `charset` | string | 响应字符集 |
|
|
68
|
+
| `contentType` | string | 响应内容类型 |
|
|
69
|
+
| `body` | string | 响应体内容 |
|
|
70
|
+
|
|
71
|
+
**示例:**
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
// GET 请求
|
|
75
|
+
const response = http.request({
|
|
76
|
+
url: "https://api.example.com/users",
|
|
77
|
+
method: "GET",
|
|
78
|
+
timeout: 5000,
|
|
79
|
+
header: {
|
|
80
|
+
Authorization: "Bearer token123",
|
|
81
|
+
Accept: "application/json",
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
logi(`状态码: ${response.statusCode}`);
|
|
86
|
+
logi(`响应体: ${response.body}`);
|
|
87
|
+
|
|
88
|
+
// POST 请求发送 JSON 数据
|
|
89
|
+
const postResponse = http.request({
|
|
90
|
+
url: "https://api.example.com/users",
|
|
91
|
+
method: "POST",
|
|
92
|
+
header: {
|
|
93
|
+
"Content-Type": "application/json",
|
|
94
|
+
Authorization: "Bearer token123",
|
|
95
|
+
},
|
|
96
|
+
requestBody: JSON.stringify({
|
|
97
|
+
name: "张三",
|
|
98
|
+
email: "zhangsan@example.com",
|
|
99
|
+
}),
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// 使用代理
|
|
103
|
+
const proxyResponse = http.request({
|
|
104
|
+
url: "https://api.example.com/data",
|
|
105
|
+
method: "GET",
|
|
106
|
+
proxy: {
|
|
107
|
+
host: "proxy.example.com",
|
|
108
|
+
port: 8080,
|
|
109
|
+
},
|
|
110
|
+
});
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## 简化 HTTP 方法
|
|
114
|
+
|
|
115
|
+
### httpGet - 发送 GET 请求的简化方法。
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
function httpGet(
|
|
119
|
+
url: string,
|
|
120
|
+
params?: Record<string, string>,
|
|
121
|
+
timeout?: number,
|
|
122
|
+
headers?: Record<string, string>,
|
|
123
|
+
): string | null;
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
**参数:**
|
|
127
|
+
|
|
128
|
+
| 参数名 | 类型 | 是否必填 | 默认值 | 描述 |
|
|
129
|
+
| --------- | ---------------------- | -------- | ------ | ---------------- |
|
|
130
|
+
| `url` | string | 是 | - | 请求地址 |
|
|
131
|
+
| `params` | Record<string, string> | 否 | - | URL 查询参数 |
|
|
132
|
+
| `timeout` | number | 否 | 10000 | 超时时间(毫秒) |
|
|
133
|
+
| `headers` | Record<string, string> | 否 | - | 请求头 |
|
|
134
|
+
|
|
135
|
+
**返回值:**
|
|
136
|
+
|
|
137
|
+
| 类型 | 描述 |
|
|
138
|
+
| -------- | ---------- |
|
|
139
|
+
| `string` | 响应体内容 |
|
|
140
|
+
|
|
141
|
+
**示例:**
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
// 简单 GET 请求
|
|
145
|
+
const html = http.httpGet("https://www.example.com");
|
|
146
|
+
logi(html);
|
|
147
|
+
|
|
148
|
+
// 带参数的 GET 请求
|
|
149
|
+
const result = http.httpGet(
|
|
150
|
+
"https://api.example.com/search",
|
|
151
|
+
{ q: "快点JS", limit: "10" },
|
|
152
|
+
5000,
|
|
153
|
+
{ "User-Agent": "快点JS/1.0" },
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
// 解析 JSON 响应
|
|
157
|
+
try {
|
|
158
|
+
const data = JSON.parse(result);
|
|
159
|
+
logi(`搜索结果: ${JSON.stringify(data)}`);
|
|
160
|
+
} catch (error) {
|
|
161
|
+
loge(`JSON 解析失败: ${error}`);
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### httpPost - 发送 POST 请求的简化方法。
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
function httpPost(
|
|
169
|
+
url: string,
|
|
170
|
+
params?: Record<string, string>,
|
|
171
|
+
files?: Record<string, string>,
|
|
172
|
+
timeout?: number,
|
|
173
|
+
headers?: Record<string, string>,
|
|
174
|
+
): string | null;
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
**参数:**
|
|
178
|
+
|
|
179
|
+
| 参数名 | 类型 | 是否必填 | 默认值 | 描述 |
|
|
180
|
+
| --------- | ---------------------- | -------- | ------ | ---------------- |
|
|
181
|
+
| `url` | string | 是 | - | 请求地址 |
|
|
182
|
+
| `params` | Record<string, string> | 否 | - | 表单参数 |
|
|
183
|
+
| `files` | Record<string, string> | 否 | - | 文件参数 |
|
|
184
|
+
| `timeout` | number | 否 | 10000 | 超时时间(毫秒) |
|
|
185
|
+
| `headers` | Record<string, string> | 否 | - | 请求头 |
|
|
186
|
+
|
|
187
|
+
**返回值:**
|
|
188
|
+
|
|
189
|
+
| 类型 | 描述 |
|
|
190
|
+
| -------- | ---------- |
|
|
191
|
+
| `string` | 响应体内容 |
|
|
192
|
+
|
|
193
|
+
**示例:**
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
// 发送表单数据
|
|
197
|
+
const response = http.httpPost("https://api.example.com/submit", {
|
|
198
|
+
username: "admin",
|
|
199
|
+
password: "123456",
|
|
200
|
+
action: "login",
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// 上传文件
|
|
204
|
+
const uploadResponse = http.httpPost(
|
|
205
|
+
"https://api.example.com/upload",
|
|
206
|
+
{ description: "测试文件" },
|
|
207
|
+
{ file: "/path/to/file.txt" },
|
|
208
|
+
10000,
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
logi(`上传结果: ${JSON.stringify(uploadResponse)}`);
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### postJSON - 发送 JSON 数据的 POST 请求。
|
|
215
|
+
|
|
216
|
+
```typescript
|
|
217
|
+
function postJSON(
|
|
218
|
+
url: string,
|
|
219
|
+
json?: Record<string, any>,
|
|
220
|
+
timeout?: number,
|
|
221
|
+
headers?: Record<string, string>,
|
|
222
|
+
): string | null;
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
**参数:**
|
|
226
|
+
|
|
227
|
+
| 参数名 | 类型 | 是否必填 | 默认值 | 描述 |
|
|
228
|
+
| --------- | ---------------------- | -------- | ------ | ---------------- |
|
|
229
|
+
| `url` | string | 是 | - | 请求地址 |
|
|
230
|
+
| `json` | Record<string, string> | 否 | - | JSON 数据 |
|
|
231
|
+
| `timeout` | number | 否 | 10000 | 超时时间(毫秒) |
|
|
232
|
+
| `headers` | Record<string, string> | 否 | - | 请求头 |
|
|
233
|
+
|
|
234
|
+
**返回值:**
|
|
235
|
+
|
|
236
|
+
| 类型 | 描述 |
|
|
237
|
+
| ------ | ---------- |
|
|
238
|
+
| string | 响应体内容 |
|
|
239
|
+
|
|
240
|
+
**示例:**
|
|
241
|
+
|
|
242
|
+
```typescript
|
|
243
|
+
// 发送 JSON 数据
|
|
244
|
+
const response = http.postJSON(
|
|
245
|
+
"https://api.example.com/api/users",
|
|
246
|
+
{
|
|
247
|
+
name: "快点JS",
|
|
248
|
+
version: "1.0.0",
|
|
249
|
+
platform: "iOS",
|
|
250
|
+
},
|
|
251
|
+
5000,
|
|
252
|
+
{ Authorization: "Bearer token123" },
|
|
253
|
+
);
|
|
254
|
+
|
|
255
|
+
// 解析响应
|
|
256
|
+
try {
|
|
257
|
+
const result = JSON.parse(response);
|
|
258
|
+
if (result.success) {
|
|
259
|
+
logi(`创建成功: ${JSON.stringify(result.data)}`);
|
|
260
|
+
} else {
|
|
261
|
+
loge(`创建失败: ${result.message}`);
|
|
262
|
+
}
|
|
263
|
+
} catch (error) {
|
|
264
|
+
loge(`响应解析失败: ${error}`);
|
|
265
|
+
}
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
## 文件下载
|
|
269
|
+
|
|
270
|
+
### downloadFile - 下载文件到本地,支持断点续传。
|
|
271
|
+
|
|
272
|
+
```typescript
|
|
273
|
+
function downloadFile(
|
|
274
|
+
url: string,
|
|
275
|
+
file: string,
|
|
276
|
+
timeout?: number,
|
|
277
|
+
headers?: Record<string, string>,
|
|
278
|
+
): boolean;
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
**参数:**
|
|
282
|
+
|
|
283
|
+
| 参数名 | 类型 | 是否必填 | 默认值 | 描述 |
|
|
284
|
+
| --------- | ---------------------- | -------- | ------ | -------------------- |
|
|
285
|
+
| `url` | string | 是 | - | 文件下载地址 |
|
|
286
|
+
| `file` | string | 是 | - | 本地保存路径 |
|
|
287
|
+
| `timeout` | number | 否 | 10000 | 下载超时时间(毫秒) |
|
|
288
|
+
| `headers` | Record<string, string> | 否 | - | 请求头 |
|
|
289
|
+
|
|
290
|
+
**返回值:**
|
|
291
|
+
|
|
292
|
+
| 类型 | 描述 |
|
|
293
|
+
| --------- | ------------ |
|
|
294
|
+
| `boolean` | 下载是否成功 |
|
|
295
|
+
|
|
296
|
+
**示例:**
|
|
297
|
+
|
|
298
|
+
```typescript
|
|
299
|
+
const documentsDir = file.getInternalDir("documents");
|
|
300
|
+
// 下载文件
|
|
301
|
+
const success = http.downloadFile(
|
|
302
|
+
"https://example.com/files/document.pdf",
|
|
303
|
+
`${documentsDir}/document.pdf`,
|
|
304
|
+
30000, // 30秒超时
|
|
305
|
+
{ "User-Agent": "快点JS Downloader" },
|
|
306
|
+
);
|
|
307
|
+
|
|
308
|
+
if (success) {
|
|
309
|
+
logi("文件下载成功");
|
|
310
|
+
} else {
|
|
311
|
+
logi("文件下载失败");
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// 下载大文件
|
|
315
|
+
function downloadLargeFile(url, filename) {
|
|
316
|
+
logi(`开始下载: ${filename}`);
|
|
317
|
+
|
|
318
|
+
const documentsDir = file.getInternalDir("documents");
|
|
319
|
+
|
|
320
|
+
const success = http.downloadFile(
|
|
321
|
+
url,
|
|
322
|
+
`${documentsDir}/${filename}`,
|
|
323
|
+
60000, // 1分钟超时
|
|
324
|
+
);
|
|
325
|
+
|
|
326
|
+
if (success) {
|
|
327
|
+
logi(`下载完成: ${filename}`);
|
|
328
|
+
return true;
|
|
329
|
+
} else {
|
|
330
|
+
loge(`下载失败: ${filename}`);
|
|
331
|
+
return false;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// 批量下载
|
|
336
|
+
const files = [
|
|
337
|
+
{ url: "https://example.com/file1.zip", name: "file1.zip" },
|
|
338
|
+
{ url: "https://example.com/file2.zip", name: "file2.zip" },
|
|
339
|
+
];
|
|
340
|
+
|
|
341
|
+
files.forEach((file) => {
|
|
342
|
+
downloadLargeFile(file.url, file.name);
|
|
343
|
+
});
|
|
344
|
+
```
|
|
345
|
+
|
|
346
|
+
## WebSocket 连接
|
|
347
|
+
|
|
348
|
+
### newWebsocket - 创建 WebSocket 连接。
|
|
349
|
+
|
|
350
|
+
```typescript
|
|
351
|
+
function newWebsocket(url: string, header?: Record<string, string>): WebSocket;
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
**参数:**
|
|
355
|
+
|
|
356
|
+
| 参数名 | 类型 | 是否必填 | 默认值 | 描述 |
|
|
357
|
+
| -------- | ---------------------- | -------- | ------ | -------------------- |
|
|
358
|
+
| `url` | string | 是 | - | WebSocket 服务器地址 |
|
|
359
|
+
| `header` | Record<string, string> | 否 | - | 连接头 |
|
|
360
|
+
|
|
361
|
+
**返回值:**
|
|
362
|
+
|
|
363
|
+
| 类型 | 描述 |
|
|
364
|
+
| ----------- | ------------------ |
|
|
365
|
+
| `WebSocket` | WebSocket 连接对象 |
|
|
366
|
+
|
|
367
|
+
### WebSocket 接口方法
|
|
368
|
+
|
|
369
|
+
#### 连接管理
|
|
370
|
+
|
|
371
|
+
```typescript
|
|
372
|
+
// 连接到服务器
|
|
373
|
+
connect(): void
|
|
374
|
+
|
|
375
|
+
// 关闭连接
|
|
376
|
+
close(): void
|
|
377
|
+
|
|
378
|
+
// 重置连接
|
|
379
|
+
reset(): boolean
|
|
380
|
+
|
|
381
|
+
// 检查连接状态
|
|
382
|
+
isClosed(): boolean
|
|
383
|
+
isConnected(): boolean
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
#### 消息发送
|
|
387
|
+
|
|
388
|
+
```typescript
|
|
389
|
+
// 发送文本消息
|
|
390
|
+
sendText(data: string): void
|
|
391
|
+
|
|
392
|
+
// 发送二进制消息
|
|
393
|
+
sendBinary(data: ArrayBuffer): void
|
|
394
|
+
```
|
|
395
|
+
|
|
396
|
+
#### 事件回调
|
|
397
|
+
|
|
398
|
+
```typescript
|
|
399
|
+
// 设置连接打开回调
|
|
400
|
+
onOpen(callback: () => void): void
|
|
401
|
+
|
|
402
|
+
// 设置连接关闭回调
|
|
403
|
+
onClose(callback: () => void): void
|
|
404
|
+
|
|
405
|
+
// 设置文本消息回调
|
|
406
|
+
onText(callback: (data: string) => void): void
|
|
407
|
+
|
|
408
|
+
// 设置二进制消息回调
|
|
409
|
+
onBinary(callback: (data: ArrayBuffer) => void): void
|
|
410
|
+
|
|
411
|
+
// 设置错误回调
|
|
412
|
+
onError(callback: (error: string) => void): void
|
|
413
|
+
```
|
|
414
|
+
|
|
415
|
+
#### 配置选项
|
|
416
|
+
|
|
417
|
+
```typescript
|
|
418
|
+
// 设置连接超时
|
|
419
|
+
setConnectionTimeout(timeout: number): void
|
|
420
|
+
|
|
421
|
+
// 设置写入超时
|
|
422
|
+
setWriteTimeout(timeout: number): void
|
|
423
|
+
|
|
424
|
+
// 设置自动重连
|
|
425
|
+
setAutoReconnect(autoReconnect: boolean): void
|
|
426
|
+
|
|
427
|
+
// 心跳管理
|
|
428
|
+
startHeartbeatInterval(interval: number, callback: () => string): void
|
|
429
|
+
stopHeartbeatInterval(): void
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
**WebSocket 完整示例:**
|
|
433
|
+
|
|
434
|
+
```typescript
|
|
435
|
+
/**
|
|
436
|
+
* 创建 WebSocket 连接示例
|
|
437
|
+
*/
|
|
438
|
+
function createWebSocketConnection() {
|
|
439
|
+
const ws = http.newWebsocket("wss://echo.websocket.org", {
|
|
440
|
+
"User-Agent": "快点JS/1.0",
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
// 设置连接超时
|
|
444
|
+
ws.setConnectionTimeout(10); // 10秒
|
|
445
|
+
ws.setWriteTimeout(5); // 5秒
|
|
446
|
+
ws.setAutoReconnect(true); // 自动重连
|
|
447
|
+
|
|
448
|
+
// 设置事件回调
|
|
449
|
+
ws.onOpen(() => {
|
|
450
|
+
logi("WebSocket 连接已打开");
|
|
451
|
+
|
|
452
|
+
// 连接成功后发送消息
|
|
453
|
+
ws.sendText(
|
|
454
|
+
JSON.stringify({
|
|
455
|
+
type: "greeting",
|
|
456
|
+
message: "Hello from 快点JS!",
|
|
457
|
+
timestamp: Date.now(),
|
|
458
|
+
}),
|
|
459
|
+
);
|
|
460
|
+
});
|
|
461
|
+
|
|
462
|
+
ws.onText((data) => {
|
|
463
|
+
logi(`收到文本消息: ${data}`);
|
|
464
|
+
|
|
465
|
+
try {
|
|
466
|
+
const message = JSON.parse(data);
|
|
467
|
+
handleMessage(message);
|
|
468
|
+
} catch (error) {
|
|
469
|
+
loge(`消息解析失败: ${error}`);
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
ws.onBinary((data) => {
|
|
474
|
+
logi(`收到二进制消息,大小: ${data.byteLength}`);
|
|
475
|
+
// 处理二进制数据
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
ws.onClose(() => {
|
|
479
|
+
logi("WebSocket 连接已关闭");
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
ws.onError((error) => {
|
|
483
|
+
loge(`WebSocket 错误: ${error}`);
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
// 设置心跳
|
|
487
|
+
ws.startHeartbeatInterval(30, () => {
|
|
488
|
+
return JSON.stringify({ type: "ping", timestamp: Date.now() });
|
|
489
|
+
});
|
|
490
|
+
|
|
491
|
+
// 连接到服务器
|
|
492
|
+
ws.connect();
|
|
493
|
+
|
|
494
|
+
return ws;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
/**
|
|
498
|
+
* 处理接收到的消息
|
|
499
|
+
*/
|
|
500
|
+
function handleMessage(message: any) {
|
|
501
|
+
switch (message.type) {
|
|
502
|
+
case "pong":
|
|
503
|
+
logi("收到心跳响应");
|
|
504
|
+
break;
|
|
505
|
+
|
|
506
|
+
case "notification":
|
|
507
|
+
logi(`收到通知: ${message.content}`);
|
|
508
|
+
break;
|
|
509
|
+
|
|
510
|
+
case "command":
|
|
511
|
+
logi(`收到命令: ${message.command}`);
|
|
512
|
+
executeCommand(message.command, message.params);
|
|
513
|
+
break;
|
|
514
|
+
|
|
515
|
+
default:
|
|
516
|
+
logi(`未知消息类型: ${message.type}`);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* 使用 WebSocket
|
|
522
|
+
*/
|
|
523
|
+
const websocket = createWebSocketConnection();
|
|
524
|
+
|
|
525
|
+
// 发送状态消息
|
|
526
|
+
function sendStatusMessage() {
|
|
527
|
+
if (websocket.isConnected()) {
|
|
528
|
+
websocket.sendText(
|
|
529
|
+
JSON.stringify({
|
|
530
|
+
type: "status",
|
|
531
|
+
battery: device.getBatteryInfo(),
|
|
532
|
+
timestamp: Date.now(),
|
|
533
|
+
}),
|
|
534
|
+
);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
// 调用发送状态消息
|
|
539
|
+
sendStatusMessage();
|
|
540
|
+
|
|
541
|
+
// 程序结束时关闭连接
|
|
542
|
+
function cleanup() {
|
|
543
|
+
if (websocket && websocket.isConnected()) {
|
|
544
|
+
websocket.stopHeartbeatInterval();
|
|
545
|
+
websocket.close();
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
```
|