@cpzxrobot/sdk 1.3.90 → 1.3.91

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();
@@ -11,14 +11,15 @@ 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;
22
23
  }
23
24
  addMessage(_message) {
24
25
  throw new Error("Method not implemented.");
@@ -31,9 +32,13 @@ class WebPlatform {
31
32
  }
32
33
  setSelectedFarm(farm) {
33
34
  this._selectedFarm = farm;
35
+ //保存到本地缓存
36
+ localStorage.setItem("selectedFarm", JSON.stringify(farm));
34
37
  }
35
38
  setSelectedUnit(unit) {
36
39
  this._selectedUnit = unit;
40
+ //保存到本地缓存
41
+ localStorage.setItem("selectedUnit", JSON.stringify(unit));
37
42
  }
38
43
  fetchWithAuth(input, init) {
39
44
  return __awaiter(this, void 0, void 0, function* () {
@@ -337,9 +342,35 @@ class WebPlatform {
337
342
  return Promise.resolve(true);
338
343
  }
339
344
  getSelectedFarmFromMiniApp() {
340
- return Promise.resolve(this._selectedFarm);
345
+ // 从本地缓存中获取选中的工厂
346
+ const farm = localStorage.getItem("selectedFarm");
347
+ if (farm) {
348
+ this._selectedFarm = JSON.parse(farm);
349
+ return Promise.resolve(this._selectedFarm);
350
+ }
351
+ else {
352
+ return new Promise((resolve, reject) => {
353
+ try {
354
+ this.context.factory.info.get(this._defaultSelectedFarm).then(() => {
355
+ // 成功获取默认选中的工厂后,将其赋值给 _selectedFarm
356
+ this._selectedFarm = this._defaultSelectedFarm;
357
+ resolve(this._selectedFarm);
358
+ });
359
+ }
360
+ catch (error) {
361
+ //表示用户没有相关工厂权限
362
+ console.error("Failed to get default selected farm:", error);
363
+ reject(error);
364
+ }
365
+ });
366
+ }
341
367
  }
342
368
  getSelectedUnitFromMiniApp() {
369
+ // 从本地缓存中获取选中的单位
370
+ const unit = localStorage.getItem("selectedUnit");
371
+ if (unit) {
372
+ this._selectedUnit = JSON.parse(unit);
373
+ }
343
374
  return Promise.resolve(this._selectedUnit);
344
375
  }
345
376
  jumpToMiniApp(url) {
@@ -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();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cpzxrobot/sdk",
3
- "version": "1.3.90",
3
+ "version": "1.3.91",
4
4
  "description": "提供给上海正芯数智APP第三方H5应用使用的SDK",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
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,16 @@ 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;
16
20
  }
17
21
  addMessage(_message: any): void {
18
22
  throw new Error("Method not implemented.");
@@ -25,9 +29,13 @@ export class WebPlatform implements PlatformInterface {
25
29
  }
26
30
  setSelectedFarm(farm: Factory): void {
27
31
  this._selectedFarm = farm;
32
+ //保存到本地缓存
33
+ localStorage.setItem("selectedFarm", JSON.stringify(farm));
28
34
  }
29
35
  setSelectedUnit(unit: Unit): void {
30
36
  this._selectedUnit = unit;
37
+ //保存到本地缓存
38
+ localStorage.setItem("selectedUnit", JSON.stringify(unit));
31
39
  }
32
40
 
33
41
  async fetchWithAuth(input: RequestInfo, init?: RequestInit) {
@@ -340,10 +348,34 @@ export class WebPlatform implements PlatformInterface {
340
348
  return Promise.resolve(true);
341
349
  }
342
350
  getSelectedFarmFromMiniApp(): Promise<any> {
343
- 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
+ }else{
357
+ return new Promise<any>((resolve, reject) => {
358
+ try {
359
+ this.context.factory.info.get(this._defaultSelectedFarm).then(() => {
360
+ // 成功获取默认选中的工厂后,将其赋值给 _selectedFarm
361
+ this._selectedFarm = this._defaultSelectedFarm;
362
+ resolve(this._selectedFarm);
363
+ });
364
+ } catch (error) {
365
+ //表示用户没有相关工厂权限
366
+ console.error("Failed to get default selected farm:", error);
367
+ reject(error);
368
+ }
369
+ });
370
+ }
344
371
  }
345
372
 
346
373
  getSelectedUnitFromMiniApp(): Promise<any> {
374
+ // 从本地缓存中获取选中的单位
375
+ const unit = localStorage.getItem("selectedUnit");
376
+ if (unit) {
377
+ this._selectedUnit = JSON.parse(unit);
378
+ }
347
379
  return Promise.resolve(this._selectedUnit);
348
380
  }
349
381