@cpzxrobot/sdk 1.2.92 → 1.2.93

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.
@@ -25,7 +25,7 @@ class UserGateway extends Object {
25
25
  resolve(this._selectedFarm);
26
26
  break;
27
27
  case "miniapp_in_app":
28
- this.context._getSelectedFarmFromMiniApp().then((farm) => {
28
+ this.context.platform.getSelectedFarmFromMiniApp().then((farm) => {
29
29
  this._selectedFarm = farm;
30
30
  resolve(farm);
31
31
  });
@@ -39,6 +39,15 @@ class UserGateway extends Object {
39
39
  }
40
40
  });
41
41
  }
42
+ async selectUnit(unit) {
43
+ if ((typeof unit === "string") || (typeof unit === "number")) {
44
+ var axios = await this.context.ready;
45
+ this.context.platform.setSelectedUnit(unit);
46
+ }
47
+ else {
48
+ this.context.platform.setSelectedUnit(unit);
49
+ }
50
+ }
42
51
  //获得当前用户选择的单元
43
52
  getSelectedUnit() {
44
53
  return this.context.ready.then(() => {
@@ -48,7 +57,7 @@ class UserGateway extends Object {
48
57
  resolve(this._selectedUnit);
49
58
  break;
50
59
  case "miniapp_in_app":
51
- this.context._getSelectedUnitFromMiniApp().then((unit) => {
60
+ this.context.platform.getSelectedUnitFromMiniApp().then((unit) => {
52
61
  this._selectedUnit = unit;
53
62
  resolve(unit);
54
63
  });
@@ -64,37 +73,10 @@ class UserGateway extends Object {
64
73
  });
65
74
  }
66
75
  set selectedFarm(farm) {
67
- switch (this.context.mode) {
68
- case "dev":
69
- this._selectedFarm = farm;
70
- break;
71
- case "miniapp_in_app":
72
- console.log("miniapp_in_app mode, ignore");
73
- break;
74
- case "miniapp_in_web":
75
- this._selectedFarm = farm;
76
- break;
77
- default:
78
- console.log("dev mode, ignore");
79
- break;
80
- }
76
+ this.context.platform.setSelectedFarm(farm);
81
77
  }
82
- set selectedUnit(farm) {
83
- switch (this.context.mode) {
84
- case "dev":
85
- this._selectedUnit = farm;
86
- break;
87
- case "miniapp_in_app":
88
- console.log("miniapp_in_app mode, ignore");
89
- break;
90
- case "miniapp_in_web":
91
- this._selectedUnit = farm;
92
- console.log("miniapp_in_web mode, ignore");
93
- break;
94
- default:
95
- console.log("dev mode, ignore");
96
- break;
97
- }
78
+ set selectedUnit(unit) {
79
+ this.context.platform.setSelectedUnit(unit);
98
80
  }
99
81
  async add(user) {
100
82
  var axios = await this.context.ready;
@@ -0,0 +1,291 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WebPlatform = void 0;
4
+ class WebPlatform {
5
+ constructor(token, appCode, baseURL, selectedFarm, selectedUnit) {
6
+ this.token = "";
7
+ this.appCode = "";
8
+ this.baseURL = "";
9
+ this.token = token;
10
+ this.appCode = appCode;
11
+ this.baseURL = baseURL;
12
+ this._selectedFarm = selectedFarm;
13
+ this._selectedUnit = selectedUnit;
14
+ }
15
+ setSelectedFarm(farm) {
16
+ this._selectedFarm = farm;
17
+ }
18
+ setSelectedUnit(unit) {
19
+ this._selectedUnit = unit;
20
+ }
21
+ async fetchWithAuth(input, init) {
22
+ const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
23
+ headers.set('Authorization', this.token);
24
+ headers.set('Miniapp-Code', this.appCode);
25
+ const response = await fetch(this.baseURL + input.toString(), Object.assign(Object.assign({}, init), { headers }));
26
+ if (!response.ok) {
27
+ throw new Error(`HTTP error! status: ${response.status}`);
28
+ }
29
+ return response;
30
+ }
31
+ ;
32
+ async streamWithAuth(input, init, fn) {
33
+ const headers = new Headers(init === null || init === void 0 ? void 0 : init.headers);
34
+ headers.set('Authorization', this.token);
35
+ headers.set('Miniapp-Code', this.appCode);
36
+ const response = await fetch(this.baseURL + input.toString(), Object.assign(Object.assign({}, init), { headers }));
37
+ var reader = response.body.getReader();
38
+ const decoder = new TextDecoder("utf-8");
39
+ let done = false;
40
+ var buf = "";
41
+ while (!done) {
42
+ const { value, done } = await reader.read();
43
+ if (done) {
44
+ break;
45
+ }
46
+ const chunkValue = decoder.decode(value);
47
+ //split value to lines
48
+ // 将值拆分为行
49
+ buf = buf + chunkValue;
50
+ var i = buf.indexOf("\n");
51
+ while (i > -1) {
52
+ //find first \n
53
+ var line = buf.substring(0, i);
54
+ try {
55
+ if (line === "[DONE]") {
56
+ break;
57
+ }
58
+ else {
59
+ // remove prefix 'data: '
60
+ // 移除前缀 'data: '
61
+ var data = JSON.parse(line.split(": ")[1]);
62
+ //{"choices":[{"delta":{"content":" </"},"index":0}],"created":1741749068,"id":"chatcmpl-67ccb889154043f5874cbf3a64ec163e","model":"DeepSeek-R1","object":"chat.completion.chunk"}
63
+ fn === null || fn === void 0 ? void 0 : fn.call(data);
64
+ }
65
+ }
66
+ catch (e) {
67
+ console.error("解析失败", e);
68
+ }
69
+ buf = buf.substring(i + 1);
70
+ i = buf.indexOf("\n");
71
+ }
72
+ }
73
+ ;
74
+ return true;
75
+ }
76
+ ;
77
+ processQueryParams(url, config) {
78
+ if (config && config.params) {
79
+ const flattenParams = (params, prefix = '') => {
80
+ const result = {};
81
+ Object.keys(params).forEach(key => {
82
+ const value = params[key];
83
+ const fullKey = prefix ? `${prefix}[${key}]` : key;
84
+ if (value && typeof value === 'object' && !Array.isArray(value)) {
85
+ Object.assign(result, flattenParams(value, fullKey));
86
+ }
87
+ else if (value) {
88
+ result[fullKey] = value.toString();
89
+ }
90
+ });
91
+ return result;
92
+ };
93
+ const flattened = flattenParams(config.params);
94
+ url += "?" + new URLSearchParams(flattened).toString();
95
+ delete config.params;
96
+ }
97
+ return url;
98
+ }
99
+ getAxiosFromMiniApp() {
100
+ var instance = this;
101
+ return {
102
+ get: async (url, config) => {
103
+ url = instance.processQueryParams(url, config);
104
+ const response = await this.fetchWithAuth(url, {
105
+ method: 'GET',
106
+ headers: config === null || config === void 0 ? void 0 : config.headers
107
+ });
108
+ return { data: await response.json() };
109
+ },
110
+ post: async (url, data, config) => {
111
+ const response = await this.fetchWithAuth(url, {
112
+ method: 'POST',
113
+ headers: Object.assign({ 'Content-Type': 'application/json' }, config === null || config === void 0 ? void 0 : config.headers),
114
+ body: JSON.stringify(data)
115
+ });
116
+ return { data: await response.json() };
117
+ },
118
+ getAndSave: async function (url, config) {
119
+ var _a;
120
+ url = instance.processQueryParams(url, config);
121
+ const response = await instance.fetchWithAuth(url, {
122
+ method: 'GET',
123
+ headers: config === null || config === void 0 ? void 0 : config.headers
124
+ });
125
+ const blob = await response.blob();
126
+ // 文件名解析逻辑
127
+ const contentDisposition = response.headers.get('Content-Disposition');
128
+ let filename = (_a = config === null || config === void 0 ? void 0 : config.fileName) !== null && _a !== void 0 ? _a : ""; // 默认文件名
129
+ // 1. 优先使用响应头中的文件名
130
+ if (filename == "") {
131
+ if (contentDisposition) {
132
+ const utf8FilenameMatch = contentDisposition.match(/filename\*=UTF-8''(.+)/i);
133
+ if (utf8FilenameMatch) {
134
+ filename = decodeURIComponent(utf8FilenameMatch[1]);
135
+ }
136
+ else {
137
+ const filenameMatch = contentDisposition.match(/filename="?(.+?)"?(;|$)/i);
138
+ if (filenameMatch)
139
+ filename = filenameMatch[1];
140
+ }
141
+ }
142
+ else {
143
+ filename = "file";
144
+ }
145
+ }
146
+ await instance.saveBlob(blob, filename);
147
+ return { data: blob };
148
+ },
149
+ getAndPreview: async function (url, config) {
150
+ url = instance.processQueryParams(url, config);
151
+ return this.getAndSave(url, config);
152
+ },
153
+ upload: async (url, option) => {
154
+ return new Promise((resolve, reject) => {
155
+ const button = document.createElement("input");
156
+ button.type = "file";
157
+ button.style.display = "none";
158
+ button.multiple = (option === null || option === void 0 ? void 0 : option.multiple) || false;
159
+ document.body.appendChild(button);
160
+ button.click(); // 手动触发点击
161
+ button.onchange = async (e) => {
162
+ const files = e.target.files;
163
+ if (files && files.length > 0) {
164
+ const formData = new FormData();
165
+ if (option === null || option === void 0 ? void 0 : option.multiple) {
166
+ for (let i = 0; i < files.length; i++) {
167
+ formData.append("files[]", files[i]);
168
+ }
169
+ }
170
+ else {
171
+ formData.append((option === null || option === void 0 ? void 0 : option["fileField"]) || "file", files[0]);
172
+ }
173
+ for (let key in option === null || option === void 0 ? void 0 : option["data"]) {
174
+ formData.append(key, option["data"][key]);
175
+ }
176
+ const response = await instance.fetchWithAuth(url, {
177
+ method: 'POST',
178
+ body: formData
179
+ });
180
+ button.remove();
181
+ resolve({ data: await response.json() });
182
+ }
183
+ else {
184
+ button.remove();
185
+ resolve({ data: { Error: "没有选择文件" } });
186
+ }
187
+ };
188
+ });
189
+ },
190
+ getAsSse: (url, fn, config) => {
191
+ return new Promise((resolve, reject) => {
192
+ instance.streamWithAuth(url, {
193
+ method: 'GET',
194
+ }, fn);
195
+ });
196
+ }
197
+ };
198
+ }
199
+ ready() {
200
+ throw new Error("Method not implemented.");
201
+ }
202
+ getSelectedFarmFromMiniApp() {
203
+ throw new Error("Method not available in web platform");
204
+ }
205
+ getSelectedUnitFromMiniApp() {
206
+ throw new Error("Method not available in web platform");
207
+ }
208
+ jumpToMiniApp(url) {
209
+ window.location.href = url;
210
+ return Promise.resolve();
211
+ }
212
+ scanQrcode() {
213
+ throw new Error("Method not available in web platform");
214
+ }
215
+ setTitle(title) {
216
+ document.title = title;
217
+ }
218
+ async saveBase64(base64, filename) {
219
+ const byteCharacters = atob(base64);
220
+ const byteNumbers = new Array(byteCharacters.length);
221
+ for (let i = 0; i < byteCharacters.length; i++) {
222
+ byteNumbers[i] = byteCharacters.charCodeAt(i);
223
+ }
224
+ const byteArray = new Uint8Array(byteNumbers);
225
+ const blob = new Blob([byteArray], {
226
+ type: "application/octet-stream",
227
+ });
228
+ return this.saveBlob(blob, filename);
229
+ }
230
+ saveBlob(blob, filename) {
231
+ return new Promise((resolve, reject) => {
232
+ try {
233
+ const url = URL.createObjectURL(blob);
234
+ const a = document.createElement("a");
235
+ a.href = url;
236
+ a.download = filename;
237
+ document.body.appendChild(a);
238
+ a.click();
239
+ setTimeout(() => {
240
+ URL.revokeObjectURL(url);
241
+ document.body.removeChild(a);
242
+ resolve();
243
+ }, 0);
244
+ }
245
+ catch (error) {
246
+ reject(error);
247
+ }
248
+ });
249
+ }
250
+ vibrate(time) {
251
+ if (navigator.vibrate) {
252
+ navigator.vibrate(time || 200);
253
+ }
254
+ }
255
+ showInDeviceManager(deviceId, type) {
256
+ return Promise.resolve();
257
+ }
258
+ reloadGroup(key) {
259
+ // No implementation in web platform
260
+ }
261
+ getGeo() {
262
+ return new Promise((resolve, reject) => {
263
+ if (navigator.geolocation) {
264
+ navigator.geolocation.getCurrentPosition((position) => {
265
+ resolve({
266
+ lat: position.coords.latitude,
267
+ lng: position.coords.longitude
268
+ });
269
+ }, (error) => {
270
+ reject(error);
271
+ });
272
+ }
273
+ else {
274
+ reject(new Error("浏览器不支持获取位置信息"));
275
+ }
276
+ });
277
+ }
278
+ setResult(name, value) {
279
+ // No implementation in web platform
280
+ }
281
+ setResultAs(name, value, type) {
282
+ // No implementation in web platform
283
+ }
284
+ setError(message) {
285
+ console.error(message);
286
+ }
287
+ setProgress(precentage) {
288
+ // No implementation in web platform
289
+ }
290
+ }
291
+ exports.WebPlatform = WebPlatform;
@@ -0,0 +1,128 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WindwosMiniAppPlatform = void 0;
4
+ class WindwosMiniAppPlatform {
5
+ constructor() {
6
+ this.sseCallbacks = {};
7
+ this.token = "";
8
+ this.appCode = "";
9
+ this.baseURL = "";
10
+ window.addEventListener('message', (event) => {
11
+ if (event.data == 'capturePort') {
12
+ if (event.ports[0] != null) {
13
+ this.messagePort = event.ports[0];
14
+ this.messagePort.onmessage = (event) => {
15
+ if (event.data.id != undefined) {
16
+ const callback = this.sseCallbacks[event.data.id];
17
+ callback === null || callback === void 0 ? void 0 : callback(event.data.data);
18
+ }
19
+ };
20
+ }
21
+ }
22
+ }, false);
23
+ }
24
+ setSelectedFarm(farm) {
25
+ throw new Error("Method not implemented.");
26
+ }
27
+ setSelectedUnit(unit) {
28
+ var _a, _b;
29
+ // @ts-ignore
30
+ (_b = (_a = window.miniapp).selectUnit) === null || _b === void 0 ? void 0 : _b.call(_a, unit).then((res) => {
31
+ var _a;
32
+ // @ts-ignore
33
+ (_a = window._notifyUnitChanged) === null || _a === void 0 ? void 0 : _a.call(window, unit);
34
+ });
35
+ }
36
+ ready() {
37
+ return Promise.resolve(true);
38
+ }
39
+ getAxiosFromMiniApp() {
40
+ // @ts-ignore
41
+ return window.getAxiosFromMiniApp();
42
+ }
43
+ getSelectedFarmFromMiniApp() {
44
+ var _a;
45
+ // @ts-ignore
46
+ return (_a = window.getSelectedFarmFromMiniApp) === null || _a === void 0 ? void 0 : _a.call(window);
47
+ }
48
+ getSelectedUnitFromMiniApp() {
49
+ var _a;
50
+ // @ts-ignore
51
+ return (_a = window.getSelectedUnitFromMiniApp) === null || _a === void 0 ? void 0 : _a.call(window);
52
+ }
53
+ jumpToMiniApp(url) {
54
+ var _a;
55
+ // @ts-ignore
56
+ return (_a = window.miniapp) === null || _a === void 0 ? void 0 : _a.open(url);
57
+ }
58
+ scanQrcode() {
59
+ var _a;
60
+ // @ts-ignore
61
+ return (_a = window.miniapp) === null || _a === void 0 ? void 0 : _a.scanQrcode();
62
+ }
63
+ setTitle(title) {
64
+ var _a;
65
+ // @ts-ignore
66
+ (_a = window.miniapp) === null || _a === void 0 ? void 0 : _a.setTitle(title);
67
+ }
68
+ saveBase64(base64, filename) {
69
+ var _a;
70
+ // @ts-ignore
71
+ return (_a = window.miniapp) === null || _a === void 0 ? void 0 : _a.saveBase64(base64, filename);
72
+ }
73
+ saveBlob(blob, filename) {
74
+ const reader = new FileReader();
75
+ return new Promise((resolve, reject) => {
76
+ reader.readAsDataURL(blob);
77
+ reader.onloadend = async () => {
78
+ const base64 = reader.result;
79
+ await this.saveBase64(base64, filename);
80
+ resolve();
81
+ };
82
+ reader.onerror = (err) => {
83
+ reject(err);
84
+ };
85
+ });
86
+ }
87
+ vibrate(time) {
88
+ var _a;
89
+ // @ts-ignore
90
+ (_a = window.miniapp) === null || _a === void 0 ? void 0 : _a.vibrate(time);
91
+ }
92
+ showInDeviceManager(deviceId, type) {
93
+ var _a;
94
+ // @ts-ignore
95
+ return (_a = window.miniapp) === null || _a === void 0 ? void 0 : _a.showInDeviceManager(deviceId, type);
96
+ }
97
+ reloadGroup(key) {
98
+ var _a;
99
+ // @ts-ignore
100
+ (_a = window.miniapp) === null || _a === void 0 ? void 0 : _a.reloadGroup(key);
101
+ }
102
+ getGeo() {
103
+ var _a;
104
+ // @ts-ignore
105
+ return (_a = window.miniapp) === null || _a === void 0 ? void 0 : _a.getGeo();
106
+ }
107
+ setResult(name, value) {
108
+ var _a;
109
+ // @ts-ignore
110
+ (_a = window.miniapp) === null || _a === void 0 ? void 0 : _a.setResult(name, value);
111
+ }
112
+ setResultAs(name, value, type) {
113
+ var _a;
114
+ // @ts-ignore
115
+ (_a = window.miniapp) === null || _a === void 0 ? void 0 : _a.setResultAs(name, value, type);
116
+ }
117
+ setError(message) {
118
+ var _a;
119
+ // @ts-ignore
120
+ (_a = window.miniapp) === null || _a === void 0 ? void 0 : _a.setError(message);
121
+ }
122
+ setProgress(precentage) {
123
+ var _a;
124
+ // @ts-ignore
125
+ (_a = window.miniapp) === null || _a === void 0 ? void 0 : _a.setProgress(precentage);
126
+ }
127
+ }
128
+ exports.WindwosMiniAppPlatform = WindwosMiniAppPlatform;