RNLGeetestSensebotModule.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package com.rnlib.geetestsensebot;
  2. import android.app.Activity;
  3. import com.facebook.react.bridge.Arguments;
  4. import com.facebook.react.bridge.Callback;
  5. import com.facebook.react.bridge.LifecycleEventListener;
  6. import com.facebook.react.bridge.ReactApplicationContext;
  7. import com.facebook.react.bridge.ReactContextBaseJavaModule;
  8. import com.facebook.react.bridge.ReactMethod;
  9. import com.facebook.react.bridge.ReadableMap;
  10. import com.facebook.react.bridge.UiThreadUtil;
  11. import com.facebook.react.bridge.WritableArray;
  12. import com.facebook.react.bridge.WritableMap;
  13. import com.facebook.react.modules.core.DeviceEventManagerModule;
  14. import com.geetest.sdk.GT3ConfigBean;
  15. import com.geetest.sdk.GT3ErrorBean;
  16. import com.geetest.sdk.GT3GeetestUtils;
  17. import com.geetest.sdk.GT3Listener;
  18. import org.json.JSONObject;
  19. import javax.annotation.Nonnull;
  20. import javax.annotation.Nullable;
  21. public class RNLGeetestSensebotModule extends ReactContextBaseJavaModule implements LifecycleEventListener {
  22. private static final String ModuleName = "RNLGeetestSensebot";
  23. private static final String EventName = "RNLGeetestSensebotEvent";
  24. private final ReactApplicationContext mReactContext;
  25. private GT3GeetestUtils mGT3GeetestUtils;
  26. private GT3ConfigBean mGT3ConfigBean;
  27. RNLGeetestSensebotModule(ReactApplicationContext reactContext) {
  28. super(reactContext);
  29. mReactContext = reactContext;
  30. mReactContext.addLifecycleEventListener(this);
  31. }
  32. @Nonnull
  33. @Override
  34. public String getName() {
  35. return ModuleName;
  36. }
  37. @ReactMethod
  38. public void start(final ReadableMap option) {
  39. GT3ConfigBean gt3ConfigBean = getSharedGT3ConfigBean();
  40. try {
  41. // debug
  42. boolean debug = option.getBoolean("debug");
  43. gt3ConfigBean.setDebug(debug);
  44. // view load timeout
  45. int timeout = option.getInt("loadTimeout");
  46. gt3ConfigBean.setTimeout(timeout);
  47. // request timeout
  48. int webviewTimeout = option.getInt("reqTimeout");
  49. gt3ConfigBean.setWebviewTimeout(webviewTimeout);
  50. // lang
  51. String lang = option.getString("lang");
  52. if (lang != null && !lang.equals("system")) {
  53. gt3ConfigBean.setLang(lang);
  54. }
  55. // enable background cancel
  56. boolean canceledOnTouchOutside = option.getBoolean("enableBackgroundCancel");
  57. gt3ConfigBean.setCanceledOnTouchOutside(canceledOnTouchOutside);
  58. // api1 json result
  59. gt3ConfigBean.setApi1Json(new JSONObject(
  60. option.getString("api1Result")));
  61. } catch (Exception e) {
  62. sendEvent(Event.Error.getCode(),
  63. Error.ParameterParseFailed.getCode(), e.getMessage());
  64. return;
  65. }
  66. Activity activity = mReactContext.getCurrentActivity();
  67. if (activity == null) {
  68. sendEvent(Event.Error.getCode(),
  69. Error.AndroidActivityDestroyed.getCode(), "Activity has been destroyed.");
  70. return;
  71. }
  72. mGT3GeetestUtils = new GT3GeetestUtils(activity);
  73. mGT3GeetestUtils.init(gt3ConfigBean);
  74. UiThreadUtil.runOnUiThread(new Runnable() {
  75. @Override
  76. public void run() {
  77. mGT3GeetestUtils.startCustomFlow();
  78. mGT3GeetestUtils.getGeetest();
  79. }
  80. });
  81. }
  82. @ReactMethod
  83. public void stop(final Callback callback) {
  84. UiThreadUtil.runOnUiThread(new Runnable() {
  85. @Override
  86. public void run() {
  87. mGT3GeetestUtils.destory();
  88. callback.invoke();
  89. }
  90. });
  91. mReactContext.removeLifecycleEventListener(this);
  92. }
  93. private GT3ConfigBean getSharedGT3ConfigBean() {
  94. if (mGT3ConfigBean == null) {
  95. mGT3ConfigBean = new GT3ConfigBean();
  96. mGT3ConfigBean.setPattern(1); // 1 -> bind 自定义按钮
  97. mGT3ConfigBean.setListener(new GT3Listener() {
  98. @Override
  99. public void onDialogResult(String s) {
  100. super.onDialogResult(s);
  101. sendEvent(Event.Result.getCode(), s);
  102. }
  103. @Override
  104. public void onClosed(int i) {
  105. sendEvent(Event.Closed.getCode());
  106. }
  107. @Override
  108. public void onFailed(GT3ErrorBean gt3ErrorBean) {
  109. WritableMap result = Arguments.createMap();
  110. result.putString("errorCode", gt3ErrorBean.errorCode);
  111. result.putString("errorDesc", gt3ErrorBean.errorDesc);
  112. result.putDouble("duration", gt3ErrorBean.duration);
  113. result.putString("challenge", gt3ErrorBean.challenge);
  114. result.putString("type", gt3ErrorBean.type);
  115. result.putString("sdkVersion", gt3ErrorBean.sdkVersion);
  116. JSONObject json = new JSONObject(result.toHashMap());
  117. sendEvent(Event.Failed.getCode(), json.toString());
  118. }
  119. @Override
  120. public void onButtonClick() {
  121. }
  122. @Override
  123. public void onSuccess(String s) {
  124. }
  125. @Override
  126. public void onStatistics(String s) {
  127. }
  128. });
  129. }
  130. return mGT3ConfigBean;
  131. }
  132. @Override
  133. public void onHostResume() {
  134. }
  135. @Override
  136. public void onHostPause() {
  137. }
  138. @Override
  139. public void onHostDestroy() {
  140. if (mGT3GeetestUtils != null) {
  141. UiThreadUtil.runOnUiThread(new Runnable() {
  142. @Override
  143. public void run() {
  144. if (mGT3GeetestUtils != null) {
  145. mGT3GeetestUtils.destory();
  146. }
  147. }
  148. });
  149. }
  150. }
  151. private void sendEvent(int code, @Nullable Object... data) {
  152. WritableArray event = Arguments.createArray();
  153. event.pushInt(code);
  154. if (data != null) {
  155. for (Object i : data) {
  156. if (i == null) {
  157. event.pushNull();
  158. } else if (i instanceof Boolean) {
  159. event.pushBoolean((boolean) i);
  160. } else if (i instanceof Double) {
  161. event.pushDouble((double) i);
  162. } else if (i instanceof Integer) {
  163. event.pushInt((int) i);
  164. } else if (i instanceof String) {
  165. event.pushString((String) i);
  166. } else if (i instanceof WritableArray) {
  167. event.pushArray((WritableArray) i);
  168. } else if (i instanceof WritableMap) {
  169. event.pushMap((WritableMap) i);
  170. }
  171. }
  172. }
  173. mReactContext
  174. .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
  175. .emit(EventName, event);
  176. }
  177. private enum Event {
  178. Result(1),
  179. Closed(2),
  180. Failed(3),
  181. Error(0);
  182. private final int code;
  183. Event(int code) {
  184. this.code = code;
  185. }
  186. public int getCode() {
  187. return this.code;
  188. }
  189. }
  190. private enum Error {
  191. ParameterParseFailed(-1),
  192. AndroidActivityDestroyed(-2);
  193. private final int code;
  194. Error(int code) {
  195. this.code = code;
  196. }
  197. public int getCode() {
  198. return this.code;
  199. }
  200. }
  201. }