@cpzxrobot/sdk 1.0.5 → 1.0.6

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/index.ts +46 -5
  2. package/package.json +1 -1
  3. package/types.d.ts +2 -0
package/index.ts CHANGED
@@ -32,6 +32,8 @@ export class Cpzxrobot {
32
32
  _getSelectedUnitFromMiniApp!: () => any;
33
33
  _jumpToMiniApp!: (url: string) => any;
34
34
  setTitle: Function = () => {};
35
+ saveBase64: Function = (base64: string, filename: string) => {};
36
+ saveBlob: Function = (blob: Blob, filename: string) => {};
35
37
  assistant: AssistantGateway;
36
38
  energy: EnergyGateway;
37
39
  camera: CameraGateway;
@@ -66,7 +68,7 @@ export class Cpzxrobot {
66
68
  return this.mode === "miniapp_in_app";
67
69
  }
68
70
 
69
- initAxios(baseURL:string) {
71
+ initAxios(baseURL: string) {
70
72
  if (this.mode !== "miniapp_in_app") {
71
73
  const instance = axios.create({
72
74
  baseURL,
@@ -88,6 +90,16 @@ export class Cpzxrobot {
88
90
  }
89
91
  }
90
92
 
93
+ _saveBlobAsBase64(blob: Blob, filename: string) {
94
+ const reader = new FileReader();
95
+ reader.onload = function () {
96
+ const base64 = reader.result as string;
97
+ // @ts-ignore
98
+ window.miniapp.saveBase64(base64, filename);
99
+ };
100
+ reader.readAsDataURL(blob);
101
+ }
102
+
91
103
  //获取当前浏览器的域名
92
104
  getDomain() {
93
105
  const domain = window.location.hostname;
@@ -105,6 +117,9 @@ export class Cpzxrobot {
105
117
  this._jumpToMiniApp = window.miniapp.open;
106
118
  // @ts-ignore
107
119
  this.setTitle = window.miniapp.setTitle;
120
+ // @ts-ignore
121
+ this.saveBase64 = window.miniapp.saveBase64;
122
+ this.saveBlob = this._saveBlobAsBase64;
108
123
  } else if (
109
124
  domain == "appassets.androidplatform.net" ||
110
125
  this.isIosMiniApp(window.location)
@@ -126,7 +141,11 @@ export class Cpzxrobot {
126
141
  };
127
142
  this.setTitle = function (title: string) {
128
143
  return platform.callHandler("app.setTitle", title);
129
- }
144
+ };
145
+ this.saveBlob = this._saveBlobAsBase64;
146
+ this.saveBase64 = function (base64: string, filename: string) {
147
+ return platform.callHandler("app.saveBase64", base64, filename);
148
+ };
130
149
  this.axios = {
131
150
  get: function (url, data) {
132
151
  console.log(url, data);
@@ -153,7 +172,29 @@ export class Cpzxrobot {
153
172
  this.mode = "dev";
154
173
  this.setTitle = function (title: string) {
155
174
  document.title = title;
156
- }
175
+ };
176
+ this.saveBase64 = function (base64: string, filename: string) {
177
+ //decode base64 to blob
178
+ const byteCharacters = atob(base64);
179
+ const byteNumbers = new Array(byteCharacters.length);
180
+ for (let i = 0; i < byteCharacters.length; i++) {
181
+ byteNumbers[i] = byteCharacters.charCodeAt(i);
182
+ }
183
+ const byteArray = new Uint8Array(byteNumbers);
184
+ const blob = new Blob([byteArray], {
185
+ type: "application/octet-stream",
186
+ });
187
+ this.saveBlob(blob, filename);
188
+ };
189
+ this.saveBlob = function (blob: Blob, filename: string) {
190
+ const url = URL.createObjectURL(blob);
191
+ const a = document.createElement("a");
192
+ a.href = url;
193
+ a.download = filename;
194
+ a.click();
195
+ URL.revokeObjectURL(url);
196
+ document.body.removeChild(a);
197
+ };
157
198
  console.log(
158
199
  "欢迎使用上海正诚物联网IOT系统,您当前是开发环境,请在平台获取开发用jwt key,并传入devAuth设置"
159
200
  );
@@ -182,7 +223,7 @@ export class Cpzxrobot {
182
223
  this._jumpToMiniApp(url);
183
224
  }
184
225
 
185
- setAuth(auth: string,baseURL:string) {
226
+ setAuth(auth: string, baseURL: string) {
186
227
  if (this.mode === "miniapp_in_app") {
187
228
  this.platformReady.then(() => {
188
229
  this.initAxios(baseURL);
@@ -248,7 +289,7 @@ export default function (
248
289
  ): Cpzxrobot {
249
290
  if (!_instance) {
250
291
  _instance = new Cpzxrobot(args.appCode);
251
- _instance.setAuth(args.devAuth,args.baseURL);
292
+ _instance.setAuth(args.devAuth, args.baseURL);
252
293
  if (args.selectedFarm) {
253
294
  _instance.user.selectedFarm = args.selectedFarm;
254
295
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cpzxrobot/sdk",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "description": "提供给上海正芯数智APP第三方H5应用使用的SDK",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/types.d.ts CHANGED
@@ -132,6 +132,8 @@ export class Cpzxrobot {
132
132
  axios: MyAxiosInstance;
133
133
  _getSelectedFarmFromMiniApp: () => Promise<Factory>;
134
134
  _getSelectedUnitFromMiniApp: () => Promise<Unit>;
135
+ saveBase64: Function = (base64: string, filename: string) => {};
136
+ saveBlob: Function = (blob: Blob, filename: string) => {};
135
137
  }
136
138
 
137
139
  declare global {