123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. reject(new Error('FAILED'));
  58. case Events.CLOSED:
  59. reject(new Error('CLOSED'));
  60. stop();
  61. break;
  62. case Events.ERROR:
  63. reject(new GeetestError(data[0], data[1]));
  64. stop();
  65. break;
  66. }
  67. if (typeof option.onEvent === "function") {
  68. option.onEvent(code, data);
  69. }
  70. });
  71. RNGSModule.start(RNGSModule.parseOption(option, DEFAULT_OPTION));
  72. });
  73. }
  74. function stop() {
  75. if (internalStatus & InternalStatus.Stoping) {
  76. return;
  77. }
  78. internalStatus |= InternalStatus.Stoping;
  79. RNGSModule.stop(() => {
  80. internalStatus = InternalStatus.None;
  81. if (eventListener && typeof eventListener.remove === "function") {
  82. eventListener.remove();
  83. eventListener = null;
  84. }
  85. });
  86. }
  87. export class GeetestError extends Error {
  88. constructor(code, message) {
  89. super(message);
  90. this.code = code;
  91. this.message = message;
  92. // @ts-ignore
  93. if (Error.captureStackTrace) {
  94. // @ts-ignore
  95. Error.captureStackTrace(this, GeetestError);
  96. }
  97. this.name = "GeetestError";
  98. }
  99. }