index.js 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import * as RNGSModule from "./module";
  2. export { BackgroundBlurEffectIOS, Lang } from "./module";
  3. export var Errors;
  4. (function (Errors) {
  5. // 参数解析错误
  6. Errors[Errors["PARAMETER_PARSE_FAILED"] = -1] = "PARAMETER_PARSE_FAILED";
  7. // 安卓 activity 已经销毁
  8. Errors[Errors["ANDROID_ACTIVITY_DESTROYED"] = -2] = "ANDROID_ACTIVITY_DESTROYED";
  9. // 重复运行
  10. Errors[Errors["DUPLICATE_START"] = -3] = "DUPLICATE_START";
  11. })(Errors || (Errors = {}));
  12. export var Events;
  13. (function (Events) {
  14. // 验证结果
  15. Events[Events["RESULT"] = 1] = "RESULT";
  16. // 验证窗口关闭
  17. Events[Events["CLOSED"] = 2] = "CLOSED";
  18. // 验证失败
  19. Events[Events["FAILED"] = 3] = "FAILED";
  20. // 发生错误
  21. Events[Events["ERROR"] = 0] = "ERROR";
  22. })(Events || (Events = {}));
  23. var InternalStatus;
  24. (function (InternalStatus) {
  25. InternalStatus[InternalStatus["None"] = 0] = "None";
  26. // 认证中
  27. InternalStatus[InternalStatus["Running"] = 1] = "Running";
  28. // 停止认证中
  29. InternalStatus[InternalStatus["Stoping"] = 0] = "Stoping";
  30. })(InternalStatus || (InternalStatus = {}));
  31. let internalStatus = InternalStatus.None;
  32. let eventListener = null;
  33. const DEFAULT_OPTION = {
  34. api1Result: "",
  35. debug: false,
  36. loadTimeout: 10000,
  37. reqTimeout: 10000,
  38. lang: RNGSModule.Lang.System,
  39. enableBackgroundCancel: false,
  40. backgroundColorIOS: 0,
  41. backgroundBlurEffectIOS: RNGSModule.BackgroundBlurEffectIOS.None,
  42. };
  43. // 进行行为认证
  44. export function start(option) {
  45. return new Promise((resolve, reject) => {
  46. if (internalStatus & InternalStatus.Running) {
  47. return reject(new GeetestError(Errors.DUPLICATE_START, "Duplicate start"));
  48. }
  49. internalStatus |= InternalStatus.Running;
  50. eventListener = RNGSModule.addListener(([code, ...data]) => {
  51. switch (code) {
  52. case Events.RESULT:
  53. resolve(JSON.parse(data[0]));
  54. stop();
  55. break;
  56. case Events.FAILED:
  57. case Events.CLOSED:
  58. stop();
  59. break;
  60. case Events.ERROR:
  61. reject(new GeetestError(data[0], data[1]));
  62. stop();
  63. break;
  64. }
  65. if (typeof option.onEvent === "function") {
  66. option.onEvent(code, data);
  67. }
  68. });
  69. RNGSModule.start(RNGSModule.parseOption(option, DEFAULT_OPTION));
  70. });
  71. }
  72. function stop() {
  73. if (internalStatus & InternalStatus.Stoping) {
  74. return;
  75. }
  76. internalStatus |= InternalStatus.Stoping;
  77. RNGSModule.stop(() => {
  78. internalStatus = InternalStatus.None;
  79. if (eventListener && typeof eventListener.remove === "function") {
  80. eventListener.remove();
  81. eventListener = null;
  82. }
  83. });
  84. }
  85. export class GeetestError extends Error {
  86. constructor(code, message) {
  87. super(message);
  88. this.code = code;
  89. this.message = message;
  90. // @ts-ignore
  91. if (Error.captureStackTrace) {
  92. // @ts-ignore
  93. Error.captureStackTrace(this, GeetestError);
  94. }
  95. this.name = "GeetestError";
  96. }
  97. }