Commit a1d2a20d6085232b87b23e6e2603457e12323a09

Authored by chenxt
1 parent 92c28c1d

解决扫脸泄露问题

src/components/FaceDetect/index.js
@@ -13,6 +13,7 @@ let token = "92d3bf5f3edcb55c731acb11b7f28202"; // 测试的删除token @@ -13,6 +13,7 @@ let token = "92d3bf5f3edcb55c731acb11b7f28202"; // 测试的删除token
13 class FaceDetect extends Component { 13 class FaceDetect extends Component {
14 constructor(props) { 14 constructor(props) {
15 super(props); 15 super(props);
  16 + this._isMounted = false;
16 this.video = null; 17 this.video = null;
17 this.checkParams = []; 18 this.checkParams = [];
18 this.group_id = "xlyprint"; 19 this.group_id = "xlyprint";
@@ -40,6 +41,7 @@ class FaceDetect extends Component { @@ -40,6 +41,7 @@ class FaceDetect extends Component {
40 } 41 }
41 42
42 componentDidMount() { 43 componentDidMount() {
  44 + this._isMounted = true;
43 const { loginAfterInit } = this.props; 45 const { loginAfterInit } = this.props;
44 if (loginAfterInit) { 46 if (loginAfterInit) {
45 setTimeout(() => { 47 setTimeout(() => {
@@ -47,21 +49,26 @@ class FaceDetect extends Component { @@ -47,21 +49,26 @@ class FaceDetect extends Component {
47 }, 300); 49 }, 300);
48 } 50 }
49 } 51 }
50 - 52 + componentWillUnmount() {
  53 + this._isMounted = false;
  54 + this.stopMedia();
  55 + }
51 changeStatusContent = content => { 56 changeStatusContent = content => {
52 - this.setState({  
53 - statusContent: content  
54 - }); 57 + if (this._isMounted) {
  58 + this.setState({ statusContent: content });
  59 + }
55 }; 60 };
  61 +
56 changeCurrentPercent = percent => { 62 changeCurrentPercent = percent => {
57 - this.setState({  
58 - currentPercent: percent  
59 - }); 63 + if (this._isMounted) {
  64 + this.setState({ currentPercent: percent });
  65 + }
60 }; 66 };
  67 +
61 changePercentStatus = status => { 68 changePercentStatus = status => {
62 - this.setState({  
63 - percentStatus: status  
64 - }); 69 + if (this._isMounted) {
  70 + this.setState({ percentStatus: status });
  71 + }
65 }; 72 };
66 73
67 getFace = () => { 74 getFace = () => {
@@ -73,6 +80,10 @@ class FaceDetect extends Component { @@ -73,6 +80,10 @@ class FaceDetect extends Component {
73 }; 80 };
74 //登录认证 81 //登录认证
75 getMedia = () => { 82 getMedia = () => {
  83 + if (!this._isMounted) {
  84 + clearInterval(timer);
  85 + return;
  86 + }
76 this.setState({ thumbImg: "" }); 87 this.setState({ thumbImg: "" });
77 this.checkParams = []; 88 this.checkParams = [];
78 const _this = this; 89 const _this = this;
@@ -179,7 +190,7 @@ class FaceDetect extends Component { @@ -179,7 +190,7 @@ class FaceDetect extends Component {
179 } 190 }
180 }, 200); 191 }, 200);
181 }) 192 })
182 - .catch(function(err) { 193 + .catch(function (err) {
183 console.log(err.name + ": " + err.message); 194 console.log(err.name + ": " + err.message);
184 reject(); 195 reject();
185 }); 196 });
@@ -188,6 +199,10 @@ class FaceDetect extends Component { @@ -188,6 +199,10 @@ class FaceDetect extends Component {
188 199
189 //人脸采集 200 //人脸采集
190 getMediaAddFace = () => { 201 getMediaAddFace = () => {
  202 + if (!this._isMounted) {
  203 + clearInterval(timer);
  204 + return;
  205 + }
191 this.setState({ thumbImg: "" }); 206 this.setState({ thumbImg: "" });
192 this.checkParams = []; 207 this.checkParams = [];
193 const _this = this; 208 const _this = this;
@@ -288,7 +303,7 @@ class FaceDetect extends Component { @@ -288,7 +303,7 @@ class FaceDetect extends Component {
288 } 303 }
289 }, 200); 304 }, 200);
290 }) 305 })
291 - .catch(function(err) { 306 + .catch(function (err) {
292 console.log(err.name + ": " + err.message); 307 console.log(err.name + ": " + err.message);
293 reject(); 308 reject();
294 }); 309 });
@@ -386,7 +401,7 @@ class FaceDetect extends Component { @@ -386,7 +401,7 @@ class FaceDetect extends Component {
386 const stream = this.video.srcObject; 401 const stream = this.video.srcObject;
387 if (stream) { 402 if (stream) {
388 const tracks = stream.getTracks(); 403 const tracks = stream.getTracks();
389 - tracks.forEach(function(track) { 404 + tracks.forEach(function (track) {
390 track.stop(); 405 track.stop();
391 }); 406 });
392 this.video.srcObject = null; 407 this.video.srcObject = null;