@cpzxrobot/sdk 1.2.57 → 1.2.58

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 +37 -7
  2. package/index.ts +42 -7
  3. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -393,19 +393,39 @@ class Cpzxrobot {
393
393
  openMiniApp(url) {
394
394
  this._jumpToMiniApp(url);
395
395
  }
396
- setAuth(auth, baseURL) {
396
+ setAuth(auth, args) {
397
+ var logger = (msg, level) => {
398
+ switch (level) {
399
+ case "debug":
400
+ if (args.verbose) {
401
+ console.debug(msg);
402
+ }
403
+ break;
404
+ case "info":
405
+ console.info(msg);
406
+ break;
407
+ case "warn":
408
+ console.warn(msg);
409
+ break;
410
+ case "error":
411
+ console.error(msg);
412
+ break;
413
+ }
414
+ };
397
415
  if (this.mode === "miniapp_in_app") {
398
416
  this.platformReady.then(() => {
399
- this.initAxios(baseURL);
400
- console.log("shzx is ready");
417
+ this.initAxios(args.baseURL);
418
+ logger("shzx is ready", "info");
401
419
  this.resolveReady(this.axios);
402
420
  });
403
421
  return;
404
422
  }
405
423
  else if (this.mode === "miniapp_in_web") {
424
+ logger("sdk under miniapp in web mode", "debug");
406
425
  //if localstorage has token, use it
407
426
  const token = localStorage.getItem("token");
408
427
  if (token) {
428
+ logger(`found token(${token}) in localstorage`, "debug");
409
429
  this.token = token;
410
430
  this.initAxios("https://www.cpzxrobot.com");
411
431
  this.axios.get("/api/v1/user/auth").then((res) => {
@@ -415,7 +435,8 @@ class Cpzxrobot {
415
435
  }
416
436
  this.resolveReady(this.axios);
417
437
  });
418
- if (!Cpzxrobot.factorySelectorLoaded) {
438
+ if (!Cpzxrobot.factorySelectorLoaded && !args.disableFactorySelector) {
439
+ logger("loading factory-selector module", "debug");
419
440
  Cpzxrobot.factorySelectorLoaded = true;
420
441
  //@ts-ignore
421
442
  Promise.resolve().then(() => __importStar(require('https://webc.cpzxrobot.com/webc/factory-selector.js'))).then(() => {
@@ -453,12 +474,14 @@ class Cpzxrobot {
453
474
  return;
454
475
  }
455
476
  else {
477
+ logger("no token found in localstorage", "debug");
456
478
  //if url has access_token and app_code, use it
457
479
  const url = new URL(window.location.href);
458
480
  const access_token = url.searchParams.get("access_token");
459
481
  const app_code = url.searchParams.get("app_code");
460
482
  if (access_token && app_code) {
461
- this.initAxios(baseURL);
483
+ logger(`found access_token(${access_token}) and app_code(${app_code}) in url`, "debug");
484
+ this.initAxios(args.baseURL);
462
485
  this.axios
463
486
  .post("/api/v1/user/auth", {
464
487
  appCode: app_code,
@@ -487,7 +510,8 @@ class Cpzxrobot {
487
510
  }
488
511
  }
489
512
  else {
490
- this.initAxios(baseURL);
513
+ logger(`sdk under ${this.mode} mode`, "debug");
514
+ this.initAxios(args.baseURL);
491
515
  this.auth = auth;
492
516
  if (this.auth.startsWith("Bearer ")) {
493
517
  this.token = auth;
@@ -538,14 +562,20 @@ function default_1(args = {
538
562
  id: 0,
539
563
  name: "",
540
564
  },
565
+ verbose: false,
541
566
  }) {
567
+ var _a;
542
568
  // @ts-ignore
543
569
  var instance = window.single_cpzxrobot_instance;
544
570
  if (!instance) {
545
571
  instance = new Cpzxrobot(args.appCode);
546
572
  // @ts-ignore
547
573
  window.single_cpzxrobot_instance = instance;
548
- instance.setAuth(args.devAuth, args.baseURL);
574
+ instance.setAuth(args.devAuth, {
575
+ baseURL: args.baseURL,
576
+ verbose: args.verbose,
577
+ disableFactorySelector: (_a = args.disableFactorySelector) !== null && _a !== void 0 ? _a : false,
578
+ });
549
579
  if (args.selectedFarm) {
550
580
  instance.user.selectedFarm = args.selectedFarm;
551
581
  }
package/index.ts CHANGED
@@ -423,18 +423,42 @@ export class Cpzxrobot {
423
423
  this._jumpToMiniApp(url);
424
424
  }
425
425
 
426
- setAuth(auth: string, baseURL: string) {
426
+ setAuth(auth: string, args: {
427
+ baseURL: string;
428
+ verbose: boolean;
429
+ disableFactorySelector?: boolean;
430
+ }) {
431
+ var logger = (msg: string, level: "debug" | "info" | "warn" | "error") => {
432
+ switch (level) {
433
+ case "debug":
434
+ if (args.verbose) {
435
+ console.debug(msg);
436
+ }
437
+ break;
438
+ case "info":
439
+ console.info(msg);
440
+ break;
441
+ case "warn":
442
+ console.warn(msg);
443
+ break;
444
+ case "error":
445
+ console.error(msg);
446
+ break;
447
+ }
448
+ };
427
449
  if (this.mode === "miniapp_in_app") {
428
450
  this.platformReady.then(() => {
429
- this.initAxios(baseURL);
430
- console.log("shzx is ready");
451
+ this.initAxios(args.baseURL);
452
+ logger("shzx is ready", "info");
431
453
  this.resolveReady(this.axios);
432
454
  });
433
455
  return;
434
456
  } else if (this.mode === "miniapp_in_web") {
457
+ logger("sdk under miniapp in web mode", "debug");
435
458
  //if localstorage has token, use it
436
459
  const token = localStorage.getItem("token");
437
460
  if (token) {
461
+ logger(`found token(${token}) in localstorage`, "debug");
438
462
  this.token = token;
439
463
  this.initAxios("https://www.cpzxrobot.com");
440
464
  this.axios.get("/api/v1/user/auth").then((res) => {
@@ -444,7 +468,8 @@ export class Cpzxrobot {
444
468
  }
445
469
  this.resolveReady(this.axios);
446
470
  });
447
- if (!Cpzxrobot.factorySelectorLoaded) {
471
+ if (!Cpzxrobot.factorySelectorLoaded && !args.disableFactorySelector) {
472
+ logger("loading factory-selector module", "debug");
448
473
  Cpzxrobot.factorySelectorLoaded = true;
449
474
  //@ts-ignore
450
475
  import('https://webc.cpzxrobot.com/webc/factory-selector.js')
@@ -480,12 +505,14 @@ export class Cpzxrobot {
480
505
  }
481
506
  return;
482
507
  } else {
508
+ logger("no token found in localstorage", "debug");
483
509
  //if url has access_token and app_code, use it
484
510
  const url = new URL(window.location.href);
485
511
  const access_token = url.searchParams.get("access_token");
486
512
  const app_code = url.searchParams.get("app_code");
487
513
  if (access_token && app_code) {
488
- this.initAxios(baseURL);
514
+ logger(`found access_token(${access_token}) and app_code(${app_code}) in url`, "debug");
515
+ this.initAxios(args.baseURL);
489
516
  this.axios
490
517
  .post("/api/v1/user/auth", {
491
518
  appCode: app_code,
@@ -512,7 +539,8 @@ export class Cpzxrobot {
512
539
 
513
540
  }
514
541
  } else {
515
- this.initAxios(baseURL);
542
+ logger(`sdk under ${this.mode} mode`, "debug");
543
+ this.initAxios(args.baseURL);
516
544
  this.auth = auth;
517
545
  if (this.auth.startsWith("Bearer ")) {
518
546
  this.token = auth;
@@ -555,6 +583,8 @@ export default function (
555
583
  appCode: string;
556
584
  selectedFarm: Factory | null | undefined;
557
585
  selectedUnit: Unit | null | undefined;
586
+ verbose?: boolean;
587
+ disableFactorySelector?: boolean;
558
588
  } = {
559
589
  devAuth: "",
560
590
  appCode: "",
@@ -569,6 +599,7 @@ export default function (
569
599
  id: 0,
570
600
  name: "",
571
601
  },
602
+ verbose: false,
572
603
  }
573
604
  ): Cpzxrobot {
574
605
  // @ts-ignore
@@ -577,7 +608,11 @@ export default function (
577
608
  instance = new Cpzxrobot(args.appCode);
578
609
  // @ts-ignore
579
610
  window.single_cpzxrobot_instance = instance;
580
- instance.setAuth(args.devAuth, args.baseURL);
611
+ instance.setAuth(args.devAuth, {
612
+ baseURL: args.baseURL,
613
+ verbose: args.verbose,
614
+ disableFactorySelector: args.disableFactorySelector ?? false,
615
+ });
581
616
  if (args.selectedFarm) {
582
617
  instance.user.selectedFarm = args.selectedFarm;
583
618
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cpzxrobot/sdk",
3
- "version": "1.2.57",
3
+ "version": "1.2.58",
4
4
  "description": "提供给上海正芯数智APP第三方H5应用使用的SDK",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {