@cpzxrobot/sdk 1.0.3 → 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.
Files changed (3) hide show
  1. package/dist/index.js +221 -184
  2. package/package.json +1 -1
  3. package/readme.md +12 -1
package/dist/index.js CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
2
+ var __importDefault =
3
+ (this && this.__importDefault) ||
4
+ function (mod) {
5
+ return mod && mod.__esModule ? mod : { default: mod };
6
+ };
5
7
  Object.defineProperty(exports, "__esModule", { value: true });
6
8
  exports.default = default_1;
7
9
  const device_gateway_1 = require("./device_gateway");
@@ -14,203 +16,238 @@ const energy_gateway_1 = require("./energy_gateway");
14
16
  const camera_gateway_1 = require("./camera_gateway");
15
17
  const pigfarm_gateway_1 = require("./pigfarm_gateway");
16
18
  class shzx {
17
- constructor(appCode) {
18
- this.pigfarm = new pigfarm_gateway_1.PigfarmGateway(this);
19
- this.hash = "shzx";
20
- this.mode = "dev";
21
- this.auth = "";
22
- this.token = "";
23
- this.setTitle = () => { };
24
- //获取当前浏览器的域名
25
- this.ready = new Promise((resolve, reject) => {
26
- this.resolveReady = resolve;
27
- this.rejectReady = reject;
28
- });
29
- this.getDomain();
30
- this.appCode = appCode;
31
- this.user = new user_gateway_1.UserGateway(this);
32
- this.factory = new factory_gateway_1.FactoryGateway(this);
33
- this.device = new device_gateway_1.DeviceGateway(this);
34
- this.transport = new transport_gateway_1.TransportGateway(this);
35
- this.assistant = new assistant_gateway_1.AssistantGateway(this);
36
- this.energy = new energy_gateway_1.EnergyGateway(this);
37
- this.camera = new camera_gateway_1.CameraGateway(this);
38
- }
39
- exit() {
40
- if (this.mode === "miniapp_in_app") {
41
- // @ts-ignore
42
- window.exitMiniapp();
43
- }
44
- else {
45
- window.close();
46
- }
47
- }
48
- inApp() {
49
- return this.mode === "miniapp_in_app";
50
- }
51
- initAxios(baseURL) {
52
- if (this.mode !== "miniapp_in_app") {
53
- const instance = axios_1.default.create({
54
- baseURL,
55
- });
56
- instance.interceptors.request.use((config) => {
57
- // 在发送请求之前在头部添加token
58
- config.headers.Authorization = this.token;
59
- // 解析和验证Token
60
- return config;
61
- }, function (error) {
62
- // 对请求错误做些什么
63
- return Promise.reject(error);
64
- });
65
- this.axios = instance;
66
- }
67
- }
19
+ constructor(appCode) {
20
+ this.pigfarm = new pigfarm_gateway_1.PigfarmGateway(this);
21
+ this.hash = "shzx";
22
+ this.mode = "dev";
23
+ this.auth = "";
24
+ this.token = "";
25
+ this.setTitle = () => {};
68
26
  //获取当前浏览器的域名
69
- getDomain() {
70
- const domain = window.location.hostname;
71
- console.log("load domain ", domain);
72
- if (domain == "miniapp") {
73
- this.mode = "miniapp_in_app";
74
- // @ts-ignore
75
- this.axios = getAxiosFromMiniApp();
76
- this.platformReady = Promise.resolve(true);
77
- // @ts-ignore
78
- this._getSelectedFarmFromMiniApp = window.getSelectedFarmFromMiniApp;
79
- // @ts-ignore
80
- this._getSelectedUnitFromMiniApp = window.getSelectedUnitFromMiniApp;
81
- // @ts-ignore
82
- this._jumpToMiniApp = window.miniapp.open;
83
- // @ts-ignore
84
- this.setTitle = window.miniapp.setTitle;
85
- }
86
- else if (domain == "appassets.androidplatform.net" ||
87
- this.isIosMiniApp(window.location)) {
88
- this.mode = "miniapp_in_app";
89
- const platform =
90
- // @ts-ignore
91
- window.flutter_inappwebview;
92
- this._getSelectedFarmFromMiniApp = function () {
93
- return platform.callHandler("getSelectedFarmFromMiniApp");
94
- };
95
- this._getSelectedUnitFromMiniApp = function () {
96
- return platform.callHandler("getSelectedUnitFromMiniApp");
97
- };
98
- this._jumpToMiniApp = function (url) {
99
- return platform.callHandler("app.openMiniapp", url);
100
- };
101
- this.setTitle = function (title) {
102
- return platform.callHandler("app.setTitle", title);
103
- };
104
- this.axios = {
105
- get: function (url, data) {
106
- console.log(url, data);
107
- return platform.callHandler("axios_get", url, data);
108
- },
109
- post: function (url, data) {
110
- console.log(url, data);
111
- return platform.callHandler("axios_post", url, data);
112
- },
113
- };
114
- this.platformReady = new Promise((resolve, reject) => {
115
- window.addEventListener("flutterInAppWebViewPlatformReady", function () {
116
- console.log("flutter android/ios platform is ready");
117
- resolve(true);
118
- });
119
- setTimeout(() => {
120
- reject("timeout");
121
- }, 1500);
122
- });
123
- }
124
- else if (this.isLocalDomain(domain)) {
125
- this.mode = "dev";
126
- this.setTitle = function (title) {
127
- document.title = title;
128
- };
129
- console.log("欢迎使用上海正诚物联网IOT系统,您当前是开发环境,请在平台获取开发用jwt key,并传入devAuth设置");
130
- }
131
- else {
132
- this.mode = "miniapp_in_web";
27
+ this.ready = new Promise((resolve, reject) => {
28
+ this.resolveReady = resolve;
29
+ this.rejectReady = reject;
30
+ });
31
+ this.getDomain();
32
+ this.appCode = appCode;
33
+ this.user = new user_gateway_1.UserGateway(this);
34
+ this.factory = new factory_gateway_1.FactoryGateway(this);
35
+ this.device = new device_gateway_1.DeviceGateway(this);
36
+ this.transport = new transport_gateway_1.TransportGateway(this);
37
+ this.assistant = new assistant_gateway_1.AssistantGateway(this);
38
+ this.energy = new energy_gateway_1.EnergyGateway(this);
39
+ this.camera = new camera_gateway_1.CameraGateway(this);
40
+ }
41
+ exit() {
42
+ if (this.mode === "miniapp_in_app") {
43
+ // @ts-ignore
44
+ window.exitMiniapp();
45
+ } else {
46
+ window.close();
47
+ }
48
+ }
49
+ inApp() {
50
+ return this.mode === "miniapp_in_app";
51
+ }
52
+ initAxios(baseURL) {
53
+ if (this.mode !== "miniapp_in_app") {
54
+ const instance = axios_1.default.create({
55
+ baseURL,
56
+ });
57
+ instance.interceptors.request.use(
58
+ (config) => {
59
+ // 在发送请求之前在头部添加token
60
+ config.headers.Authorization = this.token;
61
+ // 解析和验证Token
62
+ return config;
63
+ },
64
+ function (error) {
65
+ // 对请求错误做些什么
66
+ return Promise.reject(error);
133
67
  }
68
+ );
69
+ this.axios = instance;
134
70
  }
135
- isIosMiniApp(location) {
136
- //hostname is 'localhost' and has search params starting with 'mode=ios'
137
- return (location.hostname === "localhost" &&
138
- location.search.startsWith("?mode=ios"));
71
+ }
72
+ //获取当前浏览器的域名
73
+ getDomain() {
74
+ const domain = window.location.hostname;
75
+ console.log("load domain ", domain);
76
+ if (domain == "miniapp") {
77
+ this.mode = "miniapp_in_app";
78
+ // @ts-ignore
79
+ this.axios = getAxiosFromMiniApp();
80
+ this.platformReady = Promise.resolve(true);
81
+ // @ts-ignore
82
+ this._getSelectedFarmFromMiniApp = window.getSelectedFarmFromMiniApp;
83
+ // @ts-ignore
84
+ this._getSelectedUnitFromMiniApp = window.getSelectedUnitFromMiniApp;
85
+ // @ts-ignore
86
+ this._jumpToMiniApp = window.miniapp.open;
87
+ // @ts-ignore
88
+ this.setTitle = window.miniapp.setTitle;
89
+ // @ts-ignore
90
+ this.saveBase64 = window.miniapp.saveBase64;
91
+ } else if (
92
+ domain == "appassets.androidplatform.net" ||
93
+ this.isIosMiniApp(window.location)
94
+ ) {
95
+ this.mode = "miniapp_in_app";
96
+ const platform =
97
+ // @ts-ignore
98
+ window.flutter_inappwebview;
99
+ this._getSelectedFarmFromMiniApp = function () {
100
+ return platform.callHandler("getSelectedFarmFromMiniApp");
101
+ };
102
+ this._getSelectedUnitFromMiniApp = function () {
103
+ return platform.callHandler("getSelectedUnitFromMiniApp");
104
+ };
105
+ this._jumpToMiniApp = function (url) {
106
+ return platform.callHandler("app.openMiniapp", url);
107
+ };
108
+ this.setTitle = function (title) {
109
+ return platform.callHandler("app.setTitle", title);
110
+ };
111
+ this.saveBase64 = function (base64, filename) {
112
+ return platform.callHandler("app.saveBase64", base64, filename);
113
+ };
114
+ this.axios = {
115
+ get: function (url, data) {
116
+ console.log(url, data);
117
+ return platform.callHandler("axios_get", url, data);
118
+ },
119
+ post: function (url, data) {
120
+ console.log(url, data);
121
+ return platform.callHandler("axios_post", url, data);
122
+ },
123
+ };
124
+ this.platformReady = new Promise((resolve, reject) => {
125
+ window.addEventListener(
126
+ "flutterInAppWebViewPlatformReady",
127
+ function () {
128
+ console.log("flutter android/ios platform is ready");
129
+ resolve(true);
130
+ }
131
+ );
132
+ setTimeout(() => {
133
+ reject("timeout");
134
+ }, 1500);
135
+ });
136
+ } else if (this.isLocalDomain(domain)) {
137
+ this.mode = "dev";
138
+ this.setTitle = function (title) {
139
+ document.title = title;
140
+ };
141
+ console.log(
142
+ "欢迎使用上海正诚物联网IOT系统,您当前是开发环境,请在平台获取开发用jwt key,并传入devAuth设置"
143
+ );
144
+ } else {
145
+ this.mode = "miniapp_in_web";
139
146
  }
140
- on(event, cb) {
141
- switch (event) {
142
- case "unitChanged":
143
- // @ts-ignore
144
- window._notifyUnitChanged = cb;
145
- break;
146
- }
147
+ }
148
+ isIosMiniApp(location) {
149
+ //hostname is 'localhost' and has search params starting with 'mode=ios'
150
+ return (
151
+ location.hostname === "localhost" &&
152
+ location.search.startsWith("?mode=ios")
153
+ );
154
+ }
155
+ on(event, cb) {
156
+ switch (event) {
157
+ case "unitChanged":
158
+ // @ts-ignore
159
+ window._notifyUnitChanged = cb;
160
+ break;
147
161
  }
148
- //打开其他小程序
149
- openMiniApp(url) {
150
- this._jumpToMiniApp(url);
162
+ }
163
+ //打开其他小程序
164
+ openMiniApp(url) {
165
+ this._jumpToMiniApp(url);
166
+ }
167
+ saveBlob(blob, filename) {
168
+ if (this.mode === "miniapp_in_app") {
169
+ //blob to base64
170
+ const reader = new FileReader();
171
+ reader.readAsDataURL(blob);
172
+ const that = this;
173
+ reader.onload = function () {
174
+ that.saveBase64(reader.result, filename);
175
+ };
176
+ } else {
177
+ // For browsers: Create a link and trigger a download
178
+ const a = document.createElement("a");
179
+ const url = URL.createObjectURL(blob);
180
+ a.href = url;
181
+ a.download = filename; // Set the file name for the download
182
+ document.body.appendChild(a); // Append the link to the document
183
+ a.click(); // Trigger the download
184
+ URL.revokeObjectURL(url); // Clean up by revoking the Blob URL
185
+ a.remove(); // Remove the link from the document
151
186
  }
152
- setAuth(auth, baseURL) {
153
- if (this.mode === "miniapp_in_app") {
154
- this.platformReady.then(() => {
155
- this.initAxios(baseURL);
156
- console.log("shzx is ready");
157
- this.resolveReady(this.axios);
158
- });
159
- return;
160
- }
187
+ }
188
+ setAuth(auth, baseURL) {
189
+ if (this.mode === "miniapp_in_app") {
190
+ this.platformReady.then(() => {
161
191
  this.initAxios(baseURL);
162
- this.auth = auth;
163
- this.axios
164
- .post("/api/v1/user/auth", {
165
- appCode: this.appCode,
166
- auth: auth,
167
- })
168
- .then((res) => {
169
- if (res.data.Error) {
170
- throw res.data.Error;
171
- }
172
- else {
173
- this.token = "Bearer " + res.data.token;
174
- }
175
- this.resolveReady(this.axios);
176
- })
177
- .catch((err) => {
178
- console.log("shzx ready caller reject", err);
179
- this.rejectReady(err);
180
- });
181
- }
182
- isLocalDomain(domain) {
183
- // 列出所有的本地开发域名
184
- const localDomains = ["localhost", "127.0.0.1", "0.0.0.0"];
185
- // 检查当前域名是否在本地开发域名列表中
186
- return localDomains.includes(domain);
192
+ console.log("shzx is ready");
193
+ this.resolveReady(this.axios);
194
+ });
195
+ return;
187
196
  }
197
+ this.initAxios(baseURL);
198
+ this.auth = auth;
199
+ this.axios
200
+ .post("/api/v1/user/auth", {
201
+ appCode: this.appCode,
202
+ auth: auth,
203
+ })
204
+ .then((res) => {
205
+ if (res.data.Error) {
206
+ throw res.data.Error;
207
+ } else {
208
+ this.token = "Bearer " + res.data.token;
209
+ }
210
+ this.resolveReady(this.axios);
211
+ })
212
+ .catch((err) => {
213
+ console.log("shzx ready caller reject", err);
214
+ this.rejectReady(err);
215
+ });
216
+ }
217
+ isLocalDomain(domain) {
218
+ // 列出所有的本地开发域名
219
+ const localDomains = ["localhost", "127.0.0.1", "0.0.0.0"];
220
+ // 检查当前域名是否在本地开发域名列表中
221
+ return localDomains.includes(domain);
222
+ }
188
223
  }
189
224
  let _instance;
190
- function default_1(args = {
225
+ function default_1(
226
+ args = {
191
227
  devAuth: "",
192
228
  appCode: "",
193
229
  baseURL: "https://www.cpzxrobot.com/",
194
230
  selectedFarm: {
195
- id: 0,
196
- code: "",
197
- name: "",
198
- company_code: "",
231
+ id: 0,
232
+ code: "",
233
+ name: "",
234
+ company_code: "",
199
235
  },
200
236
  selectedUnit: {
201
- id: 0,
202
- name: "",
237
+ id: 0,
238
+ name: "",
203
239
  },
204
- }) {
205
- if (!_instance) {
206
- _instance = new shzx(args.appCode);
207
- _instance.setAuth(args.devAuth, args.baseURL);
208
- if (args.selectedFarm) {
209
- _instance.user.selectedFarm = args.selectedFarm;
210
- }
211
- if (args.selectedUnit) {
212
- _instance.user.selectedUnit = args.selectedUnit;
213
- }
240
+ }
241
+ ) {
242
+ if (!_instance) {
243
+ _instance = new shzx(args.appCode);
244
+ _instance.setAuth(args.devAuth, args.baseURL);
245
+ if (args.selectedFarm) {
246
+ _instance.user.selectedFarm = args.selectedFarm;
247
+ }
248
+ if (args.selectedUnit) {
249
+ _instance.user.selectedUnit = args.selectedUnit;
214
250
  }
215
- return _instance;
251
+ }
252
+ return _instance;
216
253
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cpzxrobot/sdk",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "description": "提供给上海正芯数智APP第三方H5应用使用的SDK",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/readme.md CHANGED
@@ -85,7 +85,7 @@ factory_id: 工厂id
85
85
 
86
86
  ### 摄像头相关
87
87
 
88
- cpzxrobot().camera.search(args)
88
+ `cpzxrobot().camera.search(args)`
89
89
 
90
90
  搜索摄像头
91
91
 
@@ -96,3 +96,14 @@ cpzxrobot().camera.search(args)
96
96
  如果入参为:{unit_id:xxx}则按单元搜索
97
97
 
98
98
  如果入参为:{factory_id:xxx}则按工厂搜索
99
+
100
+ ### app功能相关
101
+
102
+ `cpzxrobot().saveBlob(blob,filname)`
103
+
104
+ 保存blob为文件
105
+
106
+
107
+ `cpzxrobot().saveBase64(base64,filename)`
108
+
109
+ 保存Base64内容为文件