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