@cpzxrobot/sdk 1.2.58 → 1.2.59
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/index.js +63 -50
- package/index.ts +71 -57
- package/package.json +1 -4
- package/types.d.ts +6 -0
package/dist/index.js
CHANGED
|
@@ -22,9 +22,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
26
|
exports.Cpzxrobot = void 0;
|
|
30
27
|
exports.default = default_1;
|
|
@@ -32,7 +29,6 @@ const device_gateway_1 = require("./device_gateway");
|
|
|
32
29
|
const user_gateway_1 = require("./user_gateway");
|
|
33
30
|
const factory_gateway_1 = require("./factory_gateway");
|
|
34
31
|
const transport_gateway_1 = require("./transport_gateway");
|
|
35
|
-
const axios_1 = __importDefault(require("axios"));
|
|
36
32
|
const assistant_gateway_1 = require("./assistant_gateway");
|
|
37
33
|
const energy_gateway_1 = require("./energy_gateway");
|
|
38
34
|
const camera_gateway_1 = require("./camera_gateway");
|
|
@@ -99,55 +95,72 @@ class Cpzxrobot {
|
|
|
99
95
|
}
|
|
100
96
|
initAxios(baseURL) {
|
|
101
97
|
if (this.mode !== "miniapp_in_app") {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
return
|
|
110
|
-
}, function (error) {
|
|
111
|
-
// 对请求错误做些什么
|
|
112
|
-
return Promise.reject(error);
|
|
113
|
-
});
|
|
114
|
-
// @ts-ignore
|
|
115
|
-
instance.getAndSave = (url, data, fileName) => {
|
|
116
|
-
var args = Object.assign({ responseType: "blob" }, data);
|
|
117
|
-
return instance.get(url, args).then((res) => {
|
|
118
|
-
this.saveBlob(res.data, fileName || "file");
|
|
119
|
-
});
|
|
98
|
+
const fetchWithAuth = async (input, init) => {
|
|
99
|
+
const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
|
|
100
|
+
headers.set('Authorization', this.token);
|
|
101
|
+
const response = await fetch(baseURL + input.toString(), Object.assign(Object.assign({}, init), { headers }));
|
|
102
|
+
if (!response.ok) {
|
|
103
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
104
|
+
}
|
|
105
|
+
return response;
|
|
120
106
|
};
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
107
|
+
const instance = {
|
|
108
|
+
get: async (url, config) => {
|
|
109
|
+
const response = await fetchWithAuth(url, {
|
|
110
|
+
method: 'GET',
|
|
111
|
+
headers: config === null || config === void 0 ? void 0 : config.headers
|
|
112
|
+
});
|
|
113
|
+
return { data: await response.json() };
|
|
114
|
+
},
|
|
115
|
+
post: async (url, data, config) => {
|
|
116
|
+
const response = await fetchWithAuth(url, {
|
|
117
|
+
method: 'POST',
|
|
118
|
+
headers: Object.assign({ 'Content-Type': 'application/json' }, config === null || config === void 0 ? void 0 : config.headers),
|
|
119
|
+
body: JSON.stringify(data)
|
|
120
|
+
});
|
|
121
|
+
return { data: await response.json() };
|
|
122
|
+
},
|
|
123
|
+
getAndSave: async (url, config, fileName) => {
|
|
124
|
+
const response = await fetchWithAuth(url, {
|
|
125
|
+
method: 'GET',
|
|
126
|
+
headers: config === null || config === void 0 ? void 0 : config.headers
|
|
127
|
+
});
|
|
128
|
+
const blob = await response.blob();
|
|
129
|
+
await this.saveBlob(blob, fileName || "file");
|
|
130
|
+
return { data: blob };
|
|
131
|
+
},
|
|
132
|
+
getAndPreview: async (url, config, fileName) => {
|
|
133
|
+
return instance.getAndSave(url, config, fileName);
|
|
134
|
+
},
|
|
135
|
+
upload: async (url, option) => {
|
|
136
|
+
return new Promise((resolve, reject) => {
|
|
137
|
+
const button = document.createElement("input");
|
|
138
|
+
button.type = "file";
|
|
139
|
+
button.style.display = "none";
|
|
140
|
+
document.body.appendChild(button);
|
|
141
|
+
button.onclick = async (e) => {
|
|
142
|
+
var _a;
|
|
143
|
+
const file = (_a = e.target.files) === null || _a === void 0 ? void 0 : _a[0];
|
|
144
|
+
if (file) {
|
|
145
|
+
const formData = new FormData();
|
|
146
|
+
formData.append((option === null || option === void 0 ? void 0 : option["fileField"]) || "file", file);
|
|
147
|
+
for (let key in option === null || option === void 0 ? void 0 : option["data"]) {
|
|
148
|
+
formData.append(key, option["data"][key]);
|
|
149
|
+
}
|
|
150
|
+
const response = await fetchWithAuth(url, {
|
|
151
|
+
method: 'POST',
|
|
152
|
+
body: formData
|
|
153
|
+
});
|
|
154
|
+
button.remove();
|
|
155
|
+
resolve({ data: await response.json() });
|
|
139
156
|
}
|
|
140
|
-
|
|
157
|
+
else {
|
|
141
158
|
button.remove();
|
|
142
|
-
resolve(
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
resolve({ data: { Error: "没有选择文件" } });
|
|
148
|
-
}
|
|
149
|
-
};
|
|
150
|
-
});
|
|
159
|
+
resolve({ data: { Error: "没有选择文件" } });
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
});
|
|
163
|
+
}
|
|
151
164
|
};
|
|
152
165
|
// @ts-ignore
|
|
153
166
|
this.axios = instance;
|
package/index.ts
CHANGED
|
@@ -6,7 +6,6 @@ import axios from "axios";
|
|
|
6
6
|
import { AssistantGateway } from "./assistant_gateway";
|
|
7
7
|
import { EnergyGateway } from "./energy_gateway";
|
|
8
8
|
import { CameraGateway } from "./camera_gateway";
|
|
9
|
-
import { type ElectricMeterRate } from "./energy_types/electric_meter_gateway";
|
|
10
9
|
import { PigfarmGateway } from "./pigfarm_gateway";
|
|
11
10
|
import { Device, Factory, MyAxiosInstance, Unit } from "./types";
|
|
12
11
|
import { UnitGateway } from "./unit_gateway";
|
|
@@ -110,62 +109,79 @@ export class Cpzxrobot {
|
|
|
110
109
|
|
|
111
110
|
initAxios(baseURL: string) {
|
|
112
111
|
if (this.mode !== "miniapp_in_app") {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
config.headers.Authorization = this.token;
|
|
120
|
-
// 解析和验证Token
|
|
121
|
-
return config;
|
|
122
|
-
},
|
|
123
|
-
function (error) {
|
|
124
|
-
// 对请求错误做些什么
|
|
125
|
-
return Promise.reject(error);
|
|
126
|
-
}
|
|
127
|
-
);
|
|
128
|
-
// @ts-ignore
|
|
129
|
-
instance.getAndSave = (url: string, data?: any, fileName?: string) => {
|
|
130
|
-
var args = {
|
|
131
|
-
responseType: "blob",
|
|
132
|
-
...data,
|
|
133
|
-
};
|
|
134
|
-
return instance.get(url, args).then((res) => {
|
|
135
|
-
this.saveBlob(res.data, fileName || "file");
|
|
112
|
+
const fetchWithAuth = async (input: RequestInfo, init?: RequestInit) => {
|
|
113
|
+
const headers = new Headers(init?.headers);
|
|
114
|
+
headers.set('Authorization', this.token);
|
|
115
|
+
const response = await fetch(baseURL + input.toString(), {
|
|
116
|
+
...init,
|
|
117
|
+
headers
|
|
136
118
|
});
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
119
|
+
|
|
120
|
+
if (!response.ok) {
|
|
121
|
+
throw new Error(`HTTP error! status: ${response.status}`);
|
|
122
|
+
}
|
|
123
|
+
return response;
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const instance = {
|
|
127
|
+
get: async (url: string, config?: any) => {
|
|
128
|
+
const response = await fetchWithAuth(url, {
|
|
129
|
+
method: 'GET',
|
|
130
|
+
headers: config?.headers
|
|
131
|
+
});
|
|
132
|
+
return { data: await response.json() };
|
|
133
|
+
},
|
|
134
|
+
post: async (url: string, data?: any, config?: any) => {
|
|
135
|
+
const response = await fetchWithAuth(url, {
|
|
136
|
+
method: 'POST',
|
|
137
|
+
headers: {
|
|
138
|
+
'Content-Type': 'application/json',
|
|
139
|
+
...config?.headers
|
|
140
|
+
},
|
|
141
|
+
body: JSON.stringify(data)
|
|
142
|
+
});
|
|
143
|
+
return { data: await response.json() };
|
|
144
|
+
},
|
|
145
|
+
getAndSave: async (url: string, config?: any, fileName?: string) => {
|
|
146
|
+
const response = await fetchWithAuth(url, {
|
|
147
|
+
method: 'GET',
|
|
148
|
+
headers: config?.headers
|
|
149
|
+
});
|
|
150
|
+
const blob = await response.blob();
|
|
151
|
+
await this.saveBlob(blob, fileName || "file");
|
|
152
|
+
return { data: blob };
|
|
153
|
+
},
|
|
154
|
+
getAndPreview: async (url: string, config?: any, fileName?: string) => {
|
|
155
|
+
return instance.getAndSave(url, config, fileName);
|
|
156
|
+
},
|
|
157
|
+
upload: async (url: string, option?: any) => {
|
|
158
|
+
return new Promise<any>((resolve, reject) => {
|
|
159
|
+
const button = document.createElement("input");
|
|
160
|
+
button.type = "file";
|
|
161
|
+
button.style.display = "none";
|
|
162
|
+
document.body.appendChild(button);
|
|
163
|
+
button.onclick = async (e: Event) => {
|
|
164
|
+
const file = (e.target as HTMLInputElement).files?.[0];
|
|
165
|
+
if (file) {
|
|
166
|
+
const formData = new FormData();
|
|
167
|
+
formData.append(option?.["fileField"] || "file", file);
|
|
168
|
+
for (let key in option?.["data"]) {
|
|
169
|
+
formData.append(key, option!["data"][key]);
|
|
170
|
+
}
|
|
171
|
+
const response = await fetchWithAuth(url, {
|
|
172
|
+
method: 'POST',
|
|
173
|
+
body: formData
|
|
174
|
+
});
|
|
159
175
|
button.remove();
|
|
160
|
-
resolve(
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
}
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
}
|
|
176
|
+
resolve({ data: await response.json() });
|
|
177
|
+
} else {
|
|
178
|
+
button.remove();
|
|
179
|
+
resolve({ data: { Error: "没有选择文件" } });
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
};
|
|
169
185
|
// @ts-ignore
|
|
170
186
|
this.axios = instance;
|
|
171
187
|
}
|
|
@@ -622,5 +638,3 @@ export default function (
|
|
|
622
638
|
}
|
|
623
639
|
return instance;
|
|
624
640
|
}
|
|
625
|
-
|
|
626
|
-
export { type ElectricMeterRate };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cpzxrobot/sdk",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.59",
|
|
4
4
|
"description": "提供给上海正芯数智APP第三方H5应用使用的SDK",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -12,9 +12,6 @@
|
|
|
12
12
|
"devDependencies": {
|
|
13
13
|
"typescript": "^5.5.3"
|
|
14
14
|
},
|
|
15
|
-
"dependencies": {
|
|
16
|
-
"axios": "^1.7.2"
|
|
17
|
-
},
|
|
18
15
|
"types": "types.d.ts",
|
|
19
16
|
"typings": "types.d.ts"
|
|
20
17
|
}
|
package/types.d.ts
CHANGED
|
@@ -18,6 +18,7 @@ import { RobotGateway } from "@cpzxrobot/sdk/robot_gateway";
|
|
|
18
18
|
import { QuotationGateway } from "./quotation_gateway";
|
|
19
19
|
import { AiGateway } from "./ai_gateway";
|
|
20
20
|
import { EnergyGateway } from "./energy_gateway";
|
|
21
|
+
import { type ElectricMeterRate } from "./energy_types/electric_meter_gateway";
|
|
21
22
|
|
|
22
23
|
type Device = {
|
|
23
24
|
id: number;
|
|
@@ -372,6 +373,8 @@ declare module "@cpzxrobot/sdk" {
|
|
|
372
373
|
appCode: string;
|
|
373
374
|
selectedFarm: Factory | null | undefined;
|
|
374
375
|
selectedUnit: Unit | null | undefined;
|
|
376
|
+
verbose?: boolean;
|
|
377
|
+
disableFactorySelector?: boolean;
|
|
375
378
|
} = {
|
|
376
379
|
devAuth: "",
|
|
377
380
|
appCode: "",
|
|
@@ -386,6 +389,8 @@ declare module "@cpzxrobot/sdk" {
|
|
|
386
389
|
id: 0,
|
|
387
390
|
name: "",
|
|
388
391
|
},
|
|
392
|
+
verbose: false,
|
|
393
|
+
disableFactorySelector: false,
|
|
389
394
|
}
|
|
390
395
|
): Cpzxrobot;
|
|
391
396
|
export {
|
|
@@ -410,5 +415,6 @@ declare module "@cpzxrobot/sdk" {
|
|
|
410
415
|
DeviceV2,
|
|
411
416
|
FieldDatas,
|
|
412
417
|
SensorDatas,
|
|
418
|
+
ElectricMeterRate
|
|
413
419
|
};
|
|
414
420
|
}
|