@cpzxrobot/sdk 1.3.90 → 1.3.92

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/device_gateway.ts CHANGED
@@ -216,7 +216,7 @@ export class DeviceGateway extends Object {
216
216
  // 参数验证
217
217
  if (!factoryId) {
218
218
  let farm = await this.context.user.getSelectedFarm();
219
- factoryId = farm?.id;
219
+ factoryId = farm?.id || 0;
220
220
  }
221
221
 
222
222
  return axios.get(`/api/v2/device/stat/alarms/rule`, {
@@ -238,8 +238,8 @@ export class DeviceGateway extends Object {
238
238
  const axios = await this.context.ready;
239
239
 
240
240
  // 参数验证
241
- if (!ruleId || ruleId <= 0) {
242
- throw new Error('规则ID不能为空且必须大于0');
241
+ if (!ruleId) {
242
+ throw new Error('规则ID不能为空');
243
243
  }
244
244
 
245
245
  return axios.get(`/api/v2/device/stat/alarms/rule/${ruleId}`).then((res) => {
@@ -261,7 +261,7 @@ export class DeviceGateway extends Object {
261
261
  // 参数验证
262
262
  if (!request.factoryId) {
263
263
  let farm = await this.context.user.getSelectedFarm();
264
- request.factoryId = farm?.id;
264
+ request.factoryId = farm?.id || 0;
265
265
  }
266
266
  if (!request.ruleName) {
267
267
  throw new Error('规则名称不能为空');
@@ -159,7 +159,7 @@ class DeviceGateway extends Object {
159
159
  // 参数验证
160
160
  if (!factoryId) {
161
161
  let farm = yield this.context.user.getSelectedFarm();
162
- factoryId = farm === null || farm === void 0 ? void 0 : farm.id;
162
+ factoryId = (farm === null || farm === void 0 ? void 0 : farm.id) || 0;
163
163
  }
164
164
  return axios.get(`/api/v2/device/stat/alarms/rule`, {
165
165
  params: { factoryId }
@@ -178,8 +178,8 @@ class DeviceGateway extends Object {
178
178
  get: (ruleId) => __awaiter(this, void 0, void 0, function* () {
179
179
  const axios = yield this.context.ready;
180
180
  // 参数验证
181
- if (!ruleId || ruleId <= 0) {
182
- throw new Error('规则ID不能为空且必须大于0');
181
+ if (!ruleId) {
182
+ throw new Error('规则ID不能为空');
183
183
  }
184
184
  return axios.get(`/api/v2/device/stat/alarms/rule/${ruleId}`).then((res) => {
185
185
  if (res.data.code !== 200) {
@@ -198,7 +198,7 @@ class DeviceGateway extends Object {
198
198
  // 参数验证
199
199
  if (!request.factoryId) {
200
200
  let farm = yield this.context.user.getSelectedFarm();
201
- request.factoryId = farm === null || farm === void 0 ? void 0 : farm.id;
201
+ request.factoryId = (farm === null || farm === void 0 ? void 0 : farm.id) || 0;
202
202
  }
203
203
  if (!request.ruleName) {
204
204
  throw new Error('规则名称不能为空');
@@ -120,12 +120,28 @@ class FactoryGateway extends Object {
120
120
  //获得工厂的基础信息,城市,省份,建造信息等
121
121
  get info() {
122
122
  return {
123
- get: (factory) => {
124
- return this.context.axios.get(`/api/v2/factory/${factory.id}/info`);
125
- },
126
- post: (info) => {
127
- return this.context.axios.post(`/api/v2/factory/${info.id}/info`, info);
128
- },
123
+ get: (factory) => __awaiter(this, void 0, void 0, function* () {
124
+ var axios = yield this.context.ready;
125
+ const response = yield axios.get(`/api/v2/factory/${factory.id}/info`);
126
+ const body = response.data;
127
+ if (body.code === 200) {
128
+ return body.data;
129
+ }
130
+ else {
131
+ throw new Error(body.message);
132
+ }
133
+ }),
134
+ post: (info) => __awaiter(this, void 0, void 0, function* () {
135
+ var axios = yield this.context.ready;
136
+ const response = yield axios.post(`/api/v2/factory/${info.id}/info`, info);
137
+ const body = response.data;
138
+ if (body.code === 200) {
139
+ return body.data;
140
+ }
141
+ else {
142
+ throw new Error(body.message);
143
+ }
144
+ }),
129
145
  };
130
146
  }
131
147
  //获得工厂的所有车间信息
package/dist/index.js CHANGED
@@ -133,7 +133,7 @@ class Cpzxrobot {
133
133
  this.mode = "miniapp_in_web";
134
134
  baseURL = "https://www.cpzxrobot.com";
135
135
  }
136
- this.platform = new web_platform_1.WebPlatform(this.appCode, baseURL, initSelectedFactory, initSelectedUnit);
136
+ this.platform = new web_platform_1.WebPlatform(this, this.appCode, baseURL, initSelectedFactory, initSelectedUnit);
137
137
  }
138
138
  this.platformReady = this.platform.ready();
139
139
  this.axios = this.platform.getAxiosFromMiniApp();
@@ -20,6 +20,9 @@ class MobilePlatform {
20
20
  // @ts-ignore
21
21
  this.platform = window.flutter_inappwebview;
22
22
  }
23
+ openFactorySelector() {
24
+ throw new Error("Method not implemented.");
25
+ }
23
26
  addMessage(message) {
24
27
  if (message.id != undefined) {
25
28
  const callback = this.sseCallbacks[message.id];
@@ -11,14 +11,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.WebPlatform = void 0;
13
13
  class WebPlatform {
14
- constructor(appCode, baseURL, selectedFarm, selectedUnit) {
14
+ constructor(context, appCode, baseURL, selectedFarm, selectedUnit) {
15
15
  this.token = "";
16
16
  this.appCode = "";
17
17
  this.baseURL = "";
18
+ this.context = context;
18
19
  this.appCode = appCode;
19
20
  this.baseURL = baseURL !== null && baseURL !== void 0 ? baseURL : "";
20
- this._selectedFarm = selectedFarm;
21
- this._selectedUnit = selectedUnit;
21
+ this._defaultSelectedFarm = selectedFarm;
22
+ this._defaultSelectedUnit = selectedUnit;
23
+ }
24
+ openFactorySelector() {
25
+ //call open function of factory-selector
26
+ var factorySelector = document.getElementById("cpzxrobot-factory-selector");
27
+ //@ts-ignore
28
+ factorySelector.open();
22
29
  }
23
30
  addMessage(_message) {
24
31
  throw new Error("Method not implemented.");
@@ -31,9 +38,13 @@ class WebPlatform {
31
38
  }
32
39
  setSelectedFarm(farm) {
33
40
  this._selectedFarm = farm;
41
+ //保存到本地缓存
42
+ localStorage.setItem("selectedFarm", JSON.stringify(farm));
34
43
  }
35
44
  setSelectedUnit(unit) {
36
45
  this._selectedUnit = unit;
46
+ //保存到本地缓存
47
+ localStorage.setItem("selectedUnit", JSON.stringify(unit));
37
48
  }
38
49
  fetchWithAuth(input, init) {
39
50
  return __awaiter(this, void 0, void 0, function* () {
@@ -337,9 +348,35 @@ class WebPlatform {
337
348
  return Promise.resolve(true);
338
349
  }
339
350
  getSelectedFarmFromMiniApp() {
340
- return Promise.resolve(this._selectedFarm);
351
+ // 从本地缓存中获取选中的工厂
352
+ const farm = localStorage.getItem("selectedFarm");
353
+ if (farm) {
354
+ this._selectedFarm = JSON.parse(farm);
355
+ return Promise.resolve(this._selectedFarm);
356
+ }
357
+ else {
358
+ return new Promise((resolve, reject) => {
359
+ try {
360
+ this.context.factory.info.get(this._defaultSelectedFarm).then(() => {
361
+ // 成功获取默认选中的工厂后,将其赋值给 _selectedFarm
362
+ this._selectedFarm = this._defaultSelectedFarm;
363
+ resolve(this._selectedFarm);
364
+ });
365
+ }
366
+ catch (error) {
367
+ //表示用户没有相关工厂权限
368
+ console.error("Failed to get default selected farm:", error);
369
+ reject(error);
370
+ }
371
+ });
372
+ }
341
373
  }
342
374
  getSelectedUnitFromMiniApp() {
375
+ // 从本地缓存中获取选中的单位
376
+ const unit = localStorage.getItem("selectedUnit");
377
+ if (unit) {
378
+ this._selectedUnit = JSON.parse(unit);
379
+ }
343
380
  return Promise.resolve(this._selectedUnit);
344
381
  }
345
382
  jumpToMiniApp(url) {
@@ -18,6 +18,9 @@ class WindwosMiniAppPlatform {
18
18
  this.appCode = "";
19
19
  this.baseURL = "";
20
20
  }
21
+ openFactorySelector() {
22
+ throw new Error("Method not implemented.");
23
+ }
21
24
  addMessage(message) {
22
25
  if (message.id != undefined) {
23
26
  const callback = this.sseCallbacks[message.id];
@@ -105,12 +105,26 @@ export class FactoryGateway extends Object {
105
105
  //获得工厂的基础信息,城市,省份,建造信息等
106
106
  get info() {
107
107
  return {
108
- get: (factory: Factory) => {
109
- return this.context.axios.get(`/api/v2/factory/${factory.id}/info`);
110
- },
111
- post: (info: any) => {
112
- return this.context.axios.post(`/api/v2/factory/${info.id}/info`, info);
108
+ get: async (factory: Factory) => {
109
+ var axios = await this.context.ready;
110
+ const response = await axios.get(`/api/v2/factory/${factory.id}/info`);
111
+ const body = response.data;
112
+ if (body.code === 200) {
113
+ return body.data;
114
+ }else{
115
+ throw new Error(body.message);
116
+ }
113
117
  },
118
+ post: async (info: any) => {
119
+ var axios = await this.context.ready;
120
+ const response = await axios.post(`/api/v2/factory/${info.id}/info`, info);
121
+ const body = response.data;
122
+ if (body.code === 200) {
123
+ return body.data;
124
+ }else{
125
+ throw new Error(body.message);
126
+ }
127
+ },
114
128
  };
115
129
  }
116
130
 
package/index.ts CHANGED
@@ -140,7 +140,7 @@ export class Cpzxrobot {
140
140
  this.mode = "miniapp_in_web";
141
141
  baseURL = "https://www.cpzxrobot.com"
142
142
  }
143
- this.platform = new WebPlatform(this.appCode, baseURL, initSelectedFactory, initSelectedUnit);
143
+ this.platform = new WebPlatform(this, this.appCode, baseURL, initSelectedFactory, initSelectedUnit);
144
144
  }
145
145
 
146
146
  this.platformReady = this.platform.ready();
@@ -16,6 +16,11 @@ export class MobilePlatform implements PlatformInterface {
16
16
  this.platform = window.flutter_inappwebview;
17
17
 
18
18
  }
19
+
20
+ openFactorySelector(): void {
21
+ throw new Error("Method not implemented.");
22
+ }
23
+
19
24
  addMessage(message: any): void {
20
25
  if (message.id != undefined) {
21
26
  const callback = this.sseCallbacks[message.id];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cpzxrobot/sdk",
3
- "version": "1.3.90",
3
+ "version": "1.3.92",
4
4
  "description": "提供给上海正芯数智APP第三方H5应用使用的SDK",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -31,4 +31,6 @@ export abstract class PlatformInterface {
31
31
  abstract ready(): Promise<boolean>;
32
32
  abstract setSelectedUnit(unit: Unit | String | Number): void;
33
33
  abstract setSelectedFarm(farm: Factory | String | Number): void;
34
+ //change factory
35
+ abstract openFactorySelector():void;
34
36
  }
package/web_platform.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { PlatformInterface } from "./platform_interface";
2
- import { Factory, MyAxiosInstance, Unit } from "./types";
2
+ import { Cpzxrobot, Factory, MyAxiosInstance, Unit } from "./types";
3
3
 
4
4
  export class WebPlatform implements PlatformInterface {
5
5
  token: string = "";
@@ -7,12 +7,22 @@ export class WebPlatform implements PlatformInterface {
7
7
  baseURL: string = "";
8
8
  _selectedFarm: Factory | undefined;
9
9
  _selectedUnit: Unit | undefined;
10
+ _defaultSelectedFarm: Factory | undefined;
11
+ _defaultSelectedUnit: Unit | undefined;
12
+ context: Cpzxrobot;
10
13
 
11
- constructor(appCode: string, baseURL: string, selectedFarm: Factory | undefined, selectedUnit: Unit | undefined) {
14
+ constructor(context: Cpzxrobot, appCode: string, baseURL: string, selectedFarm: Factory | undefined, selectedUnit: Unit | undefined) {
15
+ this.context = context;
12
16
  this.appCode = appCode;
13
17
  this.baseURL = baseURL ?? "";
14
- this._selectedFarm = selectedFarm;
15
- this._selectedUnit = selectedUnit;
18
+ this._defaultSelectedFarm = selectedFarm;
19
+ this._defaultSelectedUnit = selectedUnit;
20
+ }
21
+ openFactorySelector() {
22
+ //call open function of factory-selector
23
+ var factorySelector = document.getElementById("cpzxrobot-factory-selector");
24
+ //@ts-ignore
25
+ factorySelector.open();
16
26
  }
17
27
  addMessage(_message: any): void {
18
28
  throw new Error("Method not implemented.");
@@ -25,9 +35,13 @@ export class WebPlatform implements PlatformInterface {
25
35
  }
26
36
  setSelectedFarm(farm: Factory): void {
27
37
  this._selectedFarm = farm;
38
+ //保存到本地缓存
39
+ localStorage.setItem("selectedFarm", JSON.stringify(farm));
28
40
  }
29
41
  setSelectedUnit(unit: Unit): void {
30
42
  this._selectedUnit = unit;
43
+ //保存到本地缓存
44
+ localStorage.setItem("selectedUnit", JSON.stringify(unit));
31
45
  }
32
46
 
33
47
  async fetchWithAuth(input: RequestInfo, init?: RequestInit) {
@@ -340,10 +354,34 @@ export class WebPlatform implements PlatformInterface {
340
354
  return Promise.resolve(true);
341
355
  }
342
356
  getSelectedFarmFromMiniApp(): Promise<any> {
343
- return Promise.resolve(this._selectedFarm);
357
+ // 从本地缓存中获取选中的工厂
358
+ const farm = localStorage.getItem("selectedFarm");
359
+ if (farm) {
360
+ this._selectedFarm = JSON.parse(farm);
361
+ return Promise.resolve(this._selectedFarm);
362
+ }else{
363
+ return new Promise<any>((resolve, reject) => {
364
+ try {
365
+ this.context.factory.info.get(this._defaultSelectedFarm).then(() => {
366
+ // 成功获取默认选中的工厂后,将其赋值给 _selectedFarm
367
+ this._selectedFarm = this._defaultSelectedFarm;
368
+ resolve(this._selectedFarm);
369
+ });
370
+ } catch (error) {
371
+ //表示用户没有相关工厂权限
372
+ console.error("Failed to get default selected farm:", error);
373
+ reject(error);
374
+ }
375
+ });
376
+ }
344
377
  }
345
378
 
346
379
  getSelectedUnitFromMiniApp(): Promise<any> {
380
+ // 从本地缓存中获取选中的单位
381
+ const unit = localStorage.getItem("selectedUnit");
382
+ if (unit) {
383
+ this._selectedUnit = JSON.parse(unit);
384
+ }
347
385
  return Promise.resolve(this._selectedUnit);
348
386
  }
349
387
 
@@ -10,6 +10,9 @@ export class WindwosMiniAppPlatform implements PlatformInterface {
10
10
 
11
11
  constructor() {
12
12
 
13
+ }
14
+ openFactorySelector(): void {
15
+ throw new Error("Method not implemented.");
13
16
  }
14
17
  addMessage(message: any): void {
15
18
  if (message.id != undefined) {