@cpzxrobot/sdk 1.2.36 → 1.2.38

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.
Files changed (3) hide show
  1. package/dist/index.js +34 -18
  2. package/index.ts +37 -22
  3. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -131,14 +131,21 @@ class Cpzxrobot {
131
131
  }
132
132
  }
133
133
  _saveBlobAsBase64(blob, filename) {
134
- const reader = new FileReader();
135
134
  var that = this;
136
- reader.onload = function () {
137
- const base64 = reader.result;
138
- // @ts-ignore
139
- that.saveBase64(base64, filename);
140
- };
141
- reader.readAsDataURL(blob);
135
+ return new Promise((resolve, reject) => {
136
+ const reader = new FileReader();
137
+ reader.readAsDataURL(blob);
138
+ reader.onloadend = () => {
139
+ // @ts-ignoreconst reader = new FileReader();
140
+ const base64 = reader.result;
141
+ // @ts-ignore
142
+ await that.saveBase64(base64, filename);
143
+ resolve();
144
+ };
145
+ reader.onerror = (err) => {
146
+ reject(err);
147
+ };
148
+ });
142
149
  }
143
150
  //获取当前浏览器的域名
144
151
  getDomain() {
@@ -208,9 +215,7 @@ class Cpzxrobot {
208
215
  this.setTitle = function (title) {
209
216
  platform.callHandler("app.setTitle", title);
210
217
  };
211
- this.saveBlob = function (blob, filename) {
212
- return platform.callHandler("app.saveBlob", blob, filename);
213
- };
218
+ this.saveBlob = this._saveBlobAsBase64;
214
219
  this.saveBase64 = function (base64, filename) {
215
220
  return platform.callHandler("app.saveBase64", base64, filename);
216
221
  };
@@ -296,16 +301,27 @@ class Cpzxrobot {
296
301
  const blob = new Blob([byteArray], {
297
302
  type: "application/octet-stream",
298
303
  });
299
- this.saveBlob(blob, filename);
304
+ return this.saveBlob(blob, filename);
300
305
  };
301
306
  this.saveBlob = function (blob, filename) {
302
- const url = URL.createObjectURL(blob);
303
- const a = document.createElement("a");
304
- a.href = url;
305
- a.download = filename;
306
- a.click();
307
- URL.revokeObjectURL(url);
308
- document.body.removeChild(a);
307
+ return new Promise((resolve, reject) => {
308
+ try {
309
+ const url = URL.createObjectURL(blob);
310
+ const a = document.createElement("a");
311
+ a.href = url;
312
+ a.download = filename;
313
+ document.body.appendChild(a);
314
+ a.click();
315
+ setTimeout(() => {
316
+ URL.revokeObjectURL(url);
317
+ document.body.removeChild(a);
318
+ resolve();
319
+ }, 0);
320
+ }
321
+ catch (error) {
322
+ reject(error);
323
+ }
324
+ });
309
325
  };
310
326
  this.getGeo = async function () {
311
327
  return new Promise((resolve, reject) => {
package/index.ts CHANGED
@@ -46,8 +46,8 @@ export class Cpzxrobot {
46
46
  _getSelectedUnitFromMiniApp!: () => any;
47
47
  _jumpToMiniApp!: (url: string) => any;
48
48
  setTitle!: (title: string) => void;
49
- saveBase64!: (base64: string, filename: string) => void;
50
- saveBlob!: (blob: Blob, filename: string) => void;
49
+ saveBase64!: (base64: string, filename: string) => Promise<void>;
50
+ saveBlob!: (blob: Blob, filename: string) => Promise<void>;
51
51
  scanQrcode!: () => Promise<string>;
52
52
  vibrate!: (time?: number) => void;
53
53
  reloadGroup!: (key: string) => void;
@@ -170,15 +170,22 @@ export class Cpzxrobot {
170
170
  }
171
171
  }
172
172
 
173
- _saveBlobAsBase64(blob: Blob, filename: string) {
174
- const reader = new FileReader();
173
+ _saveBlobAsBase64(blob: Blob, filename: string): Promise<void> {
175
174
  var that = this;
176
- reader.onload = function () {
177
- const base64 = reader.result as string;
178
- // @ts-ignore
179
- that.saveBase64(base64, filename);
180
- };
181
- reader.readAsDataURL(blob);
175
+ return new Promise<void>((resolve, reject) => {
176
+ const reader = new FileReader();
177
+ reader.readAsDataURL(blob);
178
+ reader.onloadend = () => {
179
+ // @ts-ignoreconst reader = new FileReader();
180
+ const base64 = reader.result as string;
181
+ // @ts-ignore
182
+ await that.saveBase64(base64, filename);
183
+ resolve();
184
+ };
185
+ reader.onerror = (err) => {
186
+ reject(err);
187
+ };
188
+ });
182
189
  }
183
190
 
184
191
  //获取当前浏览器的域名
@@ -252,9 +259,7 @@ export class Cpzxrobot {
252
259
  this.setTitle = function (title: string) {
253
260
  platform.callHandler("app.setTitle", title);
254
261
  };
255
- this.saveBlob = function (blob: Blob, filename: string) {
256
- return platform.callHandler("app.saveBlob", blob, filename);
257
- };
262
+ this.saveBlob = this._saveBlobAsBase64;
258
263
  this.saveBase64 = function (base64: string, filename: string) {
259
264
  return platform.callHandler("app.saveBase64", base64, filename);
260
265
  };
@@ -343,16 +348,26 @@ export class Cpzxrobot {
343
348
  const blob = new Blob([byteArray], {
344
349
  type: "application/octet-stream",
345
350
  });
346
- this.saveBlob(blob, filename);
351
+ return this.saveBlob(blob, filename);
347
352
  };
348
- this.saveBlob = function (blob: Blob, filename: string) {
349
- const url = URL.createObjectURL(blob);
350
- const a = document.createElement("a");
351
- a.href = url;
352
- a.download = filename;
353
- a.click();
354
- URL.revokeObjectURL(url);
355
- document.body.removeChild(a);
353
+ this.saveBlob = function (blob: Blob, filename: string): Promise<void> {
354
+ return new Promise((resolve, reject) => {
355
+ try {
356
+ const url = URL.createObjectURL(blob);
357
+ const a = document.createElement("a");
358
+ a.href = url;
359
+ a.download = filename;
360
+ document.body.appendChild(a);
361
+ a.click();
362
+ setTimeout(() => {
363
+ URL.revokeObjectURL(url);
364
+ document.body.removeChild(a);
365
+ resolve();
366
+ }, 0);
367
+ } catch (error) {
368
+ reject(error);
369
+ }
370
+ });
356
371
  };
357
372
  this.getGeo = async function () {
358
373
  return new Promise((resolve, reject) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cpzxrobot/sdk",
3
- "version": "1.2.36",
3
+ "version": "1.2.38",
4
4
  "description": "提供给上海正芯数智APP第三方H5应用使用的SDK",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {