From a1d2a20d6085232b87b23e6e2603457e12323a09 Mon Sep 17 00:00:00 2001 From: chenxt <10125295+chen-xintao97@user.noreply.gitee.com> Date: Fri, 30 Jan 2026 11:31:29 +0800 Subject: [PATCH] 解决扫脸泄露问题 --- src/components/FaceDetect/index.js | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/src/components/FaceDetect/index.js b/src/components/FaceDetect/index.js index f91223b..fc571dc 100644 --- a/src/components/FaceDetect/index.js +++ b/src/components/FaceDetect/index.js @@ -13,6 +13,7 @@ let token = "92d3bf5f3edcb55c731acb11b7f28202"; // 测试的删除token class FaceDetect extends Component { constructor(props) { super(props); + this._isMounted = false; this.video = null; this.checkParams = []; this.group_id = "xlyprint"; @@ -40,6 +41,7 @@ class FaceDetect extends Component { } componentDidMount() { + this._isMounted = true; const { loginAfterInit } = this.props; if (loginAfterInit) { setTimeout(() => { @@ -47,21 +49,26 @@ class FaceDetect extends Component { }, 300); } } - + componentWillUnmount() { + this._isMounted = false; + this.stopMedia(); + } changeStatusContent = content => { - this.setState({ - statusContent: content - }); + if (this._isMounted) { + this.setState({ statusContent: content }); + } }; + changeCurrentPercent = percent => { - this.setState({ - currentPercent: percent - }); + if (this._isMounted) { + this.setState({ currentPercent: percent }); + } }; + changePercentStatus = status => { - this.setState({ - percentStatus: status - }); + if (this._isMounted) { + this.setState({ percentStatus: status }); + } }; getFace = () => { @@ -73,6 +80,10 @@ class FaceDetect extends Component { }; //登录认证 getMedia = () => { + if (!this._isMounted) { + clearInterval(timer); + return; + } this.setState({ thumbImg: "" }); this.checkParams = []; const _this = this; @@ -179,7 +190,7 @@ class FaceDetect extends Component { } }, 200); }) - .catch(function(err) { + .catch(function (err) { console.log(err.name + ": " + err.message); reject(); }); @@ -188,6 +199,10 @@ class FaceDetect extends Component { //人脸采集 getMediaAddFace = () => { + if (!this._isMounted) { + clearInterval(timer); + return; + } this.setState({ thumbImg: "" }); this.checkParams = []; const _this = this; @@ -288,7 +303,7 @@ class FaceDetect extends Component { } }, 200); }) - .catch(function(err) { + .catch(function (err) { console.log(err.name + ": " + err.message); reject(); }); @@ -386,7 +401,7 @@ class FaceDetect extends Component { const stream = this.video.srcObject; if (stream) { const tracks = stream.getTracks(); - tracks.forEach(function(track) { + tracks.forEach(function (track) { track.stop(); }); this.video.srcObject = null; -- libgit2 0.22.2