index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* eslint-disable */
  2. import React from 'react';
  3. import ReactDOM from 'react-dom';
  4. import PropTypes from 'prop-types';
  5. class Geetest extends React.Component {
  6. constructor(props) {
  7. // console.log('constructor', props);
  8. super(props);
  9. this.state = {
  10. ins: null,
  11. script: null,
  12. };
  13. this._init = this._init.bind(this);
  14. this._ready = this._ready.bind(this);
  15. this._load = this._load.bind(this);
  16. this._destroy = this._destroy.bind(this);
  17. }
  18. // componentWillMount() {
  19. // const that = this;
  20. // console.log('componentWillMount', that.props, that.state);
  21. // }
  22. componentDidMount() {
  23. const that = this;
  24. // console.log('componentDidMount', that.props, that.state);
  25. that._init();
  26. }
  27. // componentWillReceiveProps(nextProps) {
  28. // const that = this;
  29. // console.log('componentWillReceiveProps', that.props, nextProps);
  30. // }
  31. shouldComponentUpdate(nextProps, nextState) {
  32. const that = this;
  33. // console.log('shouldComponentUpdate', that.props, nextProps, that.state, nextState);
  34. // console.log(that.props.challenge, nextProps.challenge, nextProps.challenge !== that.props.challenge)
  35. return nextProps.challenge !== that.props.challenge;
  36. }
  37. // componentWillUpdate(nextProps, nextState) {
  38. // const that = this;
  39. // console.log('componentWillUpdate', that.props, nextProps, that.state, nextState);
  40. // }
  41. componentDidUpdate(prevProps, prevState) {
  42. const that = this;
  43. // console.log('componentDidUpdate', prevProps, that.props, prevState, that.state);
  44. that._init();
  45. }
  46. componentWillUnmount() {
  47. const that = this;
  48. // console.log('componentWillUnmount', that.props, that.state);
  49. that._destroy();
  50. }
  51. _init() {
  52. const that = this;
  53. console.log('_init');
  54. if (window.initGeetest) {
  55. return that._ready();
  56. }
  57. const ds = document.createElement('script');
  58. ds.type = 'text/javascript';
  59. ds.async = true;
  60. ds.charset = 'utf-8';
  61. if (ds.readyState) {
  62. ds.onreadystatechange = function() {
  63. if (ds.readyState === 'loaded' || ds.readyState === 'complete') {
  64. ds.onreadystatechange = null;
  65. that._ready();
  66. }
  67. };
  68. } else {
  69. ds.onload = function() {
  70. ds.onload = null;
  71. that._ready();
  72. };
  73. }
  74. ds.src = `${document.location.protocol}//static.geetest.com/static/tools/gt.js?_t=${new Date().getTime()}`;
  75. const s = document.getElementsByTagName('script')[0];
  76. s.parentNode.insertBefore(ds, s);
  77. that.setState({
  78. script: ds,
  79. });
  80. }
  81. _ready() {
  82. const that = this;
  83. console.log('_ready');
  84. const { gt, challenge, https, product, lang, sandbox, width, success, new_captcha } = that.props;
  85. const { ins } = that.state;
  86. if (!window.initGeetest) {
  87. return;
  88. }
  89. if (!gt || !challenge) {
  90. return;
  91. }
  92. // if (ins) {
  93. // return that._reset(ins);
  94. // }
  95. return window.initGeetest(
  96. {
  97. gt,
  98. challenge,
  99. https,
  100. product,
  101. lang,
  102. sandbox,
  103. width,
  104. offline: !success,
  105. new_captcha,
  106. },
  107. function(geetest) {
  108. that._load(geetest);
  109. that.setState({
  110. ins: geetest,
  111. });
  112. },
  113. );
  114. }
  115. _load(ins) {
  116. const that = this;
  117. console.log('_load');
  118. const { onReady, onRefresh, onSuccess, onClose, onError } = that.props;
  119. ins.appendTo(ReactDOM.findDOMNode(that));
  120. ins.onReady(onReady);
  121. // ins.onRefresh(onRefresh);
  122. ins.onSuccess(() => onSuccess(ins.getValidate()));
  123. ins.onClose(onClose);
  124. ins.onError(onError);
  125. }
  126. _destroy() {
  127. const that = this;
  128. // that.state.script.parentNode.removeChild(that.state.script);
  129. that.setState({
  130. ins: null,
  131. script: null,
  132. });
  133. }
  134. render() {
  135. const that = this;
  136. // console.log('render');
  137. const { challenge } = that.props;
  138. return <div className="i-geetest" key={challenge} />;
  139. }
  140. }
  141. Geetest.propTypes = {
  142. gt: PropTypes.string.isRequired,
  143. challenge: PropTypes.string.isRequired,
  144. success: PropTypes.number.isRequired,
  145. new_captcha: PropTypes.number.new_captcha,
  146. https: PropTypes.bool,
  147. product: PropTypes.string,
  148. lang: PropTypes.string,
  149. sandbox: PropTypes.bool,
  150. width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  151. onReady: PropTypes.func,
  152. // onRefresh: PropTypes.func,
  153. onSuccess: PropTypes.func,
  154. onClose: PropTypes.func,
  155. onError: PropTypes.func,
  156. };
  157. Geetest.defaultProps = {
  158. https: true,
  159. product: 'float',
  160. lang: 'zh-cn',
  161. sandbox: false,
  162. onReady: function() {},
  163. // onRefresh: function () { },
  164. onSuccess: function() {},
  165. onClose: function() {},
  166. onError: function() {},
  167. };
  168. export default Geetest;