assistsx-js 0.1.24 → 0.1.26

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,60 @@
1
+ /**
2
+ * 相册操作响应接口定义
3
+ */
4
+ export interface GalleryResponse {
5
+ success: boolean;
6
+ uri?: string;
7
+ id?: number;
8
+ type?: string;
9
+ message?: string;
10
+ }
11
+ /**
12
+ * 删除相册项响应接口定义
13
+ */
14
+ export interface GalleryDeleteResponse {
15
+ success: boolean;
16
+ deletedRows: number;
17
+ message?: string;
18
+ }
19
+ export declare class Gallery {
20
+ /**
21
+ * 执行异步调用
22
+ * @param method 方法名
23
+ * @param args 参数对象
24
+ * @param timeout 超时时间(秒),默认30秒
25
+ * @returns Promise<调用响应>
26
+ */
27
+ private asyncCall;
28
+ /**
29
+ * 添加图片到系统相册
30
+ * @param filePath 图片文件路径(必需)
31
+ * @param displayName 显示名称(可选,默认使用文件名)
32
+ * @param timeout 超时时间(秒),默认30秒
33
+ * @returns Promise<相册操作响应>
34
+ */
35
+ addImageToGallery(filePath: string, displayName?: string, timeout?: number): Promise<GalleryResponse>;
36
+ /**
37
+ * 添加视频到系统相册
38
+ * @param filePath 视频文件路径(必需)
39
+ * @param displayName 显示名称(可选,默认使用文件名)
40
+ * @param timeout 超时时间(秒),默认30秒
41
+ * @returns Promise<相册操作响应>
42
+ */
43
+ addVideoToGallery(filePath: string, displayName?: string, timeout?: number): Promise<GalleryResponse>;
44
+ /**
45
+ * 从系统相册删除
46
+ * @param uri 媒体文件的URI(必需,格式如:content://media/external/images/media/123)
47
+ * @param timeout 超时时间(秒),默认30秒
48
+ * @returns Promise<删除响应>
49
+ */
50
+ deleteFromGalleryByUri(uri: string, timeout?: number): Promise<GalleryDeleteResponse>;
51
+ /**
52
+ * 从系统相册删除(通过ID和类型)
53
+ * @param id 媒体文件的ID(必需)
54
+ * @param type 媒体类型,"image" 或 "video"(必需)
55
+ * @param timeout 超时时间(秒),默认30秒
56
+ * @returns Promise<删除响应>
57
+ */
58
+ deleteFromGalleryById(id: number, type: "image" | "video", timeout?: number): Promise<GalleryDeleteResponse>;
59
+ }
60
+ export declare const gallery: Gallery;
@@ -0,0 +1,142 @@
1
+ /**
2
+ * 系统相册相关功能
3
+ * 提供添加图片/视频到相册和从相册删除的功能
4
+ */
5
+ import { CallResponse } from "../CallResponse";
6
+ import { decodeBase64UTF8, generateUUID } from "../Utils";
7
+ // 回调函数存储对象
8
+ const callbacks = new Map();
9
+ // 初始化全局回调函数
10
+ if (typeof window !== "undefined" && !window.assistsxGalleryCallback) {
11
+ window.assistsxGalleryCallback = (data) => {
12
+ let callbackId;
13
+ try {
14
+ const json = decodeBase64UTF8(data);
15
+ const response = JSON.parse(json);
16
+ callbackId = response.callbackId;
17
+ if (callbackId) {
18
+ const callback = callbacks.get(callbackId);
19
+ if (callback) {
20
+ callback(json);
21
+ }
22
+ }
23
+ }
24
+ catch (e) {
25
+ console.error("Gallery callback error:", e);
26
+ }
27
+ finally {
28
+ if (callbackId) {
29
+ callbacks.delete(callbackId);
30
+ }
31
+ }
32
+ };
33
+ }
34
+ export class Gallery {
35
+ /**
36
+ * 执行异步调用
37
+ * @param method 方法名
38
+ * @param args 参数对象
39
+ * @param timeout 超时时间(秒),默认30秒
40
+ * @returns Promise<调用响应>
41
+ */
42
+ async asyncCall(method, args, timeout = 30) {
43
+ const uuid = generateUUID();
44
+ const params = {
45
+ method,
46
+ arguments: args ? args : undefined,
47
+ callbackId: uuid,
48
+ };
49
+ const promise = new Promise((resolve) => {
50
+ callbacks.set(uuid, (data) => {
51
+ resolve(data);
52
+ });
53
+ setTimeout(() => {
54
+ callbacks.delete(uuid);
55
+ resolve(JSON.stringify(new CallResponse(0, null, uuid)));
56
+ }, timeout * 1000);
57
+ });
58
+ const result = window.assistsxGallery.call(JSON.stringify(params));
59
+ const promiseResult = await promise;
60
+ if (typeof promiseResult === "string") {
61
+ const responseData = JSON.parse(promiseResult);
62
+ return new CallResponse(responseData.code, responseData.data, responseData.callbackId);
63
+ }
64
+ throw new Error("Call failed");
65
+ }
66
+ /**
67
+ * 添加图片到系统相册
68
+ * @param filePath 图片文件路径(必需)
69
+ * @param displayName 显示名称(可选,默认使用文件名)
70
+ * @param timeout 超时时间(秒),默认30秒
71
+ * @returns Promise<相册操作响应>
72
+ */
73
+ async addImageToGallery(filePath, displayName, timeout) {
74
+ var _a;
75
+ if (!filePath) {
76
+ throw new Error("filePath参数不能为空");
77
+ }
78
+ const response = await this.asyncCall("addImageToGallery", { filePath, displayName }, timeout);
79
+ if (!response.isSuccess()) {
80
+ throw new Error(((_a = response.data) === null || _a === void 0 ? void 0 : _a.message) || "添加图片到相册失败");
81
+ }
82
+ return response.data;
83
+ }
84
+ /**
85
+ * 添加视频到系统相册
86
+ * @param filePath 视频文件路径(必需)
87
+ * @param displayName 显示名称(可选,默认使用文件名)
88
+ * @param timeout 超时时间(秒),默认30秒
89
+ * @returns Promise<相册操作响应>
90
+ */
91
+ async addVideoToGallery(filePath, displayName, timeout) {
92
+ var _a;
93
+ if (!filePath) {
94
+ throw new Error("filePath参数不能为空");
95
+ }
96
+ const response = await this.asyncCall("addVideoToGallery", { filePath, displayName }, timeout);
97
+ if (!response.isSuccess()) {
98
+ throw new Error(((_a = response.data) === null || _a === void 0 ? void 0 : _a.message) || "添加视频到相册失败");
99
+ }
100
+ return response.data;
101
+ }
102
+ /**
103
+ * 从系统相册删除
104
+ * @param uri 媒体文件的URI(必需,格式如:content://media/external/images/media/123)
105
+ * @param timeout 超时时间(秒),默认30秒
106
+ * @returns Promise<删除响应>
107
+ */
108
+ async deleteFromGalleryByUri(uri, timeout) {
109
+ var _a;
110
+ if (!uri) {
111
+ throw new Error("uri参数不能为空");
112
+ }
113
+ const response = await this.asyncCall("deleteFromGallery", { uri }, timeout);
114
+ if (!response.isSuccess()) {
115
+ throw new Error(((_a = response.data) === null || _a === void 0 ? void 0 : _a.message) || "从相册删除失败");
116
+ }
117
+ return response.data;
118
+ }
119
+ /**
120
+ * 从系统相册删除(通过ID和类型)
121
+ * @param id 媒体文件的ID(必需)
122
+ * @param type 媒体类型,"image" 或 "video"(必需)
123
+ * @param timeout 超时时间(秒),默认30秒
124
+ * @returns Promise<删除响应>
125
+ */
126
+ async deleteFromGalleryById(id, type, timeout) {
127
+ var _a;
128
+ if (id === undefined || id === null) {
129
+ throw new Error("id参数不能为空");
130
+ }
131
+ if (!type || (type !== "image" && type !== "video")) {
132
+ throw new Error("type参数必须为 'image' 或 'video'");
133
+ }
134
+ const response = await this.asyncCall("deleteFromGallery", { id, type }, timeout);
135
+ if (!response.isSuccess()) {
136
+ throw new Error(((_a = response.data) === null || _a === void 0 ? void 0 : _a.message) || "从相册删除失败");
137
+ }
138
+ return response.data;
139
+ }
140
+ }
141
+ // 导出常量实例
142
+ export const gallery = new Gallery();
package/dist/index.d.ts CHANGED
@@ -21,3 +21,4 @@ export * from "./filesystem/fileio/file-io";
21
21
  export * from "./filesystem/fileutils/file-utils";
22
22
  export * from "./ime/ime";
23
23
  export * from "./imageutils/image-utils";
24
+ export * from "./gallery/gallery";
package/dist/index.js CHANGED
@@ -21,3 +21,4 @@ export * from "./filesystem/fileio/file-io";
21
21
  export * from "./filesystem/fileutils/file-utils";
22
22
  export * from "./ime/ime";
23
23
  export * from "./imageutils/image-utils";
24
+ export * from "./gallery/gallery";
@@ -23,6 +23,10 @@ export interface HttpDownloadResponse {
23
23
  statusMessage: string;
24
24
  savePath: string;
25
25
  fileSize: number;
26
+ saveToGallerySuccess?: boolean;
27
+ galleryUri?: string;
28
+ galleryId?: number;
29
+ galleryType?: string;
26
30
  headers: Record<string, string>;
27
31
  }
28
32
  /**
@@ -94,10 +98,12 @@ export declare class Http {
94
98
  * @param url 下载 URL
95
99
  * @param savePath 保存路径
96
100
  * @param headers 请求头
101
+ * @param saveToGallery 是否保存到系统相册(仅支持图片和视频文件),默认 false
102
+ * @param displayName 保存到相册时的显示名称(可选,默认使用文件名)
97
103
  * @param timeout 超时时间(秒),默认30秒
98
104
  * @returns Promise<下载响应>
99
105
  */
100
- httpDownload(url: string, savePath: string, headers?: Record<string, string>, timeout?: number): Promise<HttpDownloadResponse>;
106
+ httpDownload(url: string, savePath: string, headers?: Record<string, string>, saveToGallery?: boolean, displayName?: string, timeout?: number): Promise<HttpDownloadResponse>;
101
107
  /**
102
108
  * 配置 OkHttpClient
103
109
  * @param config 配置选项
@@ -144,12 +144,14 @@ export class Http {
144
144
  * @param url 下载 URL
145
145
  * @param savePath 保存路径
146
146
  * @param headers 请求头
147
+ * @param saveToGallery 是否保存到系统相册(仅支持图片和视频文件),默认 false
148
+ * @param displayName 保存到相册时的显示名称(可选,默认使用文件名)
147
149
  * @param timeout 超时时间(秒),默认30秒
148
150
  * @returns Promise<下载响应>
149
151
  */
150
- async httpDownload(url, savePath, headers, timeout) {
152
+ async httpDownload(url, savePath, headers, saveToGallery, displayName, timeout) {
151
153
  var _a;
152
- const response = await this.asyncCall("httpDownload", { url, savePath, headers }, timeout);
154
+ const response = await this.asyncCall("httpDownload", { url, savePath, headers, saveToGallery, displayName }, timeout);
153
155
  if (!response.isSuccess()) {
154
156
  throw new Error(((_a = response.data) === null || _a === void 0 ? void 0 : _a.message) || "HTTP download failed");
155
157
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "assistsx-js",
3
- "version": "0.1.24",
3
+ "version": "0.1.26",
4
4
  "description": "assistsx-js自动化开发SDK",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -0,0 +1,207 @@
1
+ /**
2
+ * 系统相册相关功能
3
+ * 提供添加图片/视频到相册和从相册删除的功能
4
+ */
5
+ import { CallResponse } from "../CallResponse";
6
+ import { decodeBase64UTF8, generateUUID } from "../Utils";
7
+
8
+ /**
9
+ * 相册操作响应接口定义
10
+ */
11
+ export interface GalleryResponse {
12
+ success: boolean;
13
+ uri?: string;
14
+ id?: number;
15
+ type?: string;
16
+ message?: string;
17
+ }
18
+
19
+ /**
20
+ * 删除相册项响应接口定义
21
+ */
22
+ export interface GalleryDeleteResponse {
23
+ success: boolean;
24
+ deletedRows: number;
25
+ message?: string;
26
+ }
27
+
28
+ // 回调函数存储对象
29
+ const callbacks: Map<string, (data: string) => void> = new Map();
30
+
31
+ // 初始化全局回调函数
32
+ if (typeof window !== "undefined" && !window.assistsxGalleryCallback) {
33
+ window.assistsxGalleryCallback = (data: string) => {
34
+ let callbackId: string | undefined;
35
+ try {
36
+ const json = decodeBase64UTF8(data);
37
+ const response = JSON.parse(json);
38
+ callbackId = response.callbackId;
39
+ if (callbackId) {
40
+ const callback = callbacks.get(callbackId);
41
+ if (callback) {
42
+ callback(json);
43
+ }
44
+ }
45
+ } catch (e) {
46
+ console.error("Gallery callback error:", e);
47
+ } finally {
48
+ if (callbackId) {
49
+ callbacks.delete(callbackId);
50
+ }
51
+ }
52
+ };
53
+ }
54
+
55
+ export class Gallery {
56
+ /**
57
+ * 执行异步调用
58
+ * @param method 方法名
59
+ * @param args 参数对象
60
+ * @param timeout 超时时间(秒),默认30秒
61
+ * @returns Promise<调用响应>
62
+ */
63
+ private async asyncCall(
64
+ method: string,
65
+ args?: any,
66
+ timeout: number = 30
67
+ ): Promise<CallResponse> {
68
+ const uuid = generateUUID();
69
+ const params = {
70
+ method,
71
+ arguments: args ? args : undefined,
72
+ callbackId: uuid,
73
+ };
74
+ const promise = new Promise<string>((resolve) => {
75
+ callbacks.set(uuid, (data: string) => {
76
+ resolve(data);
77
+ });
78
+ setTimeout(() => {
79
+ callbacks.delete(uuid);
80
+ resolve(JSON.stringify(new CallResponse(0, null, uuid)));
81
+ }, timeout * 1000);
82
+ });
83
+ const result = window.assistsxGallery.call(JSON.stringify(params));
84
+ const promiseResult = await promise;
85
+ if (typeof promiseResult === "string") {
86
+ const responseData = JSON.parse(promiseResult);
87
+ return new CallResponse(
88
+ responseData.code,
89
+ responseData.data,
90
+ responseData.callbackId
91
+ );
92
+ }
93
+ throw new Error("Call failed");
94
+ }
95
+
96
+ /**
97
+ * 添加图片到系统相册
98
+ * @param filePath 图片文件路径(必需)
99
+ * @param displayName 显示名称(可选,默认使用文件名)
100
+ * @param timeout 超时时间(秒),默认30秒
101
+ * @returns Promise<相册操作响应>
102
+ */
103
+ async addImageToGallery(
104
+ filePath: string,
105
+ displayName?: string,
106
+ timeout?: number
107
+ ): Promise<GalleryResponse> {
108
+ if (!filePath) {
109
+ throw new Error("filePath参数不能为空");
110
+ }
111
+
112
+ const response = await this.asyncCall(
113
+ "addImageToGallery",
114
+ { filePath, displayName },
115
+ timeout
116
+ );
117
+ if (!response.isSuccess()) {
118
+ throw new Error(response.data?.message || "添加图片到相册失败");
119
+ }
120
+ return response.data as GalleryResponse;
121
+ }
122
+
123
+ /**
124
+ * 添加视频到系统相册
125
+ * @param filePath 视频文件路径(必需)
126
+ * @param displayName 显示名称(可选,默认使用文件名)
127
+ * @param timeout 超时时间(秒),默认30秒
128
+ * @returns Promise<相册操作响应>
129
+ */
130
+ async addVideoToGallery(
131
+ filePath: string,
132
+ displayName?: string,
133
+ timeout?: number
134
+ ): Promise<GalleryResponse> {
135
+ if (!filePath) {
136
+ throw new Error("filePath参数不能为空");
137
+ }
138
+
139
+ const response = await this.asyncCall(
140
+ "addVideoToGallery",
141
+ { filePath, displayName },
142
+ timeout
143
+ );
144
+ if (!response.isSuccess()) {
145
+ throw new Error(response.data?.message || "添加视频到相册失败");
146
+ }
147
+ return response.data as GalleryResponse;
148
+ }
149
+
150
+ /**
151
+ * 从系统相册删除
152
+ * @param uri 媒体文件的URI(必需,格式如:content://media/external/images/media/123)
153
+ * @param timeout 超时时间(秒),默认30秒
154
+ * @returns Promise<删除响应>
155
+ */
156
+ async deleteFromGalleryByUri(
157
+ uri: string,
158
+ timeout?: number
159
+ ): Promise<GalleryDeleteResponse> {
160
+ if (!uri) {
161
+ throw new Error("uri参数不能为空");
162
+ }
163
+
164
+ const response = await this.asyncCall(
165
+ "deleteFromGallery",
166
+ { uri },
167
+ timeout
168
+ );
169
+ if (!response.isSuccess()) {
170
+ throw new Error(response.data?.message || "从相册删除失败");
171
+ }
172
+ return response.data as GalleryDeleteResponse;
173
+ }
174
+
175
+ /**
176
+ * 从系统相册删除(通过ID和类型)
177
+ * @param id 媒体文件的ID(必需)
178
+ * @param type 媒体类型,"image" 或 "video"(必需)
179
+ * @param timeout 超时时间(秒),默认30秒
180
+ * @returns Promise<删除响应>
181
+ */
182
+ async deleteFromGalleryById(
183
+ id: number,
184
+ type: "image" | "video",
185
+ timeout?: number
186
+ ): Promise<GalleryDeleteResponse> {
187
+ if (id === undefined || id === null) {
188
+ throw new Error("id参数不能为空");
189
+ }
190
+ if (!type || (type !== "image" && type !== "video")) {
191
+ throw new Error("type参数必须为 'image' 或 'video'");
192
+ }
193
+
194
+ const response = await this.asyncCall(
195
+ "deleteFromGallery",
196
+ { id, type },
197
+ timeout
198
+ );
199
+ if (!response.isSuccess()) {
200
+ throw new Error(response.data?.message || "从相册删除失败");
201
+ }
202
+ return response.data as GalleryDeleteResponse;
203
+ }
204
+ }
205
+
206
+ // 导出常量实例
207
+ export const gallery = new Gallery();
package/src/global.d.ts CHANGED
@@ -35,6 +35,10 @@ declare global {
35
35
  call(method: string): string | null;
36
36
  };
37
37
  assistsxImageUtilsCallback: (data: string) => void;
38
+ assistsxGallery: {
39
+ call(method: string): string | null;
40
+ };
41
+ assistsxGalleryCallback: (data: string) => void;
38
42
  }
39
43
  }
40
44
 
package/src/index.ts CHANGED
@@ -20,4 +20,5 @@ export * from "./filesystem/path";
20
20
  export * from "./filesystem/fileio/file-io";
21
21
  export * from "./filesystem/fileutils/file-utils";
22
22
  export * from "./ime/ime";
23
- export * from "./imageutils/image-utils";
23
+ export * from "./imageutils/image-utils";
24
+ export * from "./gallery/gallery";
@@ -32,6 +32,10 @@ export interface HttpDownloadResponse {
32
32
  statusMessage: string;
33
33
  savePath: string;
34
34
  fileSize: number;
35
+ saveToGallerySuccess?: boolean;
36
+ galleryUri?: string;
37
+ galleryId?: number;
38
+ galleryType?: string;
35
39
  headers: Record<string, string>;
36
40
  }
37
41
 
@@ -223,6 +227,8 @@ export class Http {
223
227
  * @param url 下载 URL
224
228
  * @param savePath 保存路径
225
229
  * @param headers 请求头
230
+ * @param saveToGallery 是否保存到系统相册(仅支持图片和视频文件),默认 false
231
+ * @param displayName 保存到相册时的显示名称(可选,默认使用文件名)
226
232
  * @param timeout 超时时间(秒),默认30秒
227
233
  * @returns Promise<下载响应>
228
234
  */
@@ -230,11 +236,13 @@ export class Http {
230
236
  url: string,
231
237
  savePath: string,
232
238
  headers?: Record<string, string>,
239
+ saveToGallery?: boolean,
240
+ displayName?: string,
233
241
  timeout?: number
234
242
  ): Promise<HttpDownloadResponse> {
235
243
  const response = await this.asyncCall(
236
244
  "httpDownload",
237
- { url, savePath, headers },
245
+ { url, savePath, headers, saveToGallery, displayName },
238
246
  timeout
239
247
  );
240
248
  if (!response.isSuccess()) {