From a5f81efe3d6409bbbcc7dca7835b5918c74cbbe7 Mon Sep 17 00:00:00 2001 From: hupeng Date: Fri, 21 Jun 2019 15:04:14 +0800 Subject: [PATCH] =?UTF-8?q?[=E9=80=9A=E4=BF=A1][=E4=BF=AE=E5=A4=8D]:?= =?UTF-8?q?=E8=AE=BF=E9=97=AE=E5=A2=9E=E5=8A=A0=E8=B6=85=E6=97=B6=E6=9C=BA?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.js | 44 +++++++++++++++++++++++++++++++------------- HttpUtils.js | 41 ++++++++++++++++++++++++++++++++++------- 2 files changed, 65 insertions(+), 20 deletions(-) diff --git a/App.js b/App.js index 75b2b31..e9145c5 100644 --- a/App.js +++ b/App.js @@ -89,6 +89,9 @@ var heartConfig = { pageChangeInterval:10, } +var requestFailedCount = 0; +var versionInfo = ''; + export default class MyPage extends Component { constructor(props) { super(props); @@ -122,8 +125,10 @@ codePushUpdate() { CodePush.getUpdateMetadata(CodePush.UpdateState.RUNNING) .then((metadata: LocalPackage) => { this.setState({ versionInfo: metadata ? metadata.appVersion +'b'+NativeModules.BuildConfig.versionCode+"-" + metadata.label : "Unknown"}); + versionInfo = this.state.versionInfo; }, (error: any) => { this.setState({ versionInfo: "Unknown"}); + versionInfo = this.state.versionInfo; }); } @@ -186,9 +191,10 @@ codePushStatusDidChange(syncStatus) { } hbRequest() { - return HttpUtils.post(NativeModules.BuildConfig.apiHost + 'api/wxDeviceScreenAd/heartbeat?deviceId=' - +encodeURIComponent(this.state.macAddress), - {version:this.state.versionInfo}) + return HttpUtils.post(NativeModules.BuildConfig.apiHost + + 'api/wxDeviceScreenAd/heartbeat?deviceId=' + + encodeURIComponent(this.state.macAddress), + {version:this.state.versionInfo}) .then(result => { if (result.code == 200) { let config = JSON.parse(result.data.config); @@ -223,7 +229,8 @@ codePushStatusDidChange(syncStatus) { }) .catch(error => { - this.setState({ errorMessage : error.message}); + requestFailedCount++ + this.setState({ versionInfo: versionInfo+`[${requestFailedCount}]`}); }) } @@ -232,9 +239,12 @@ codePushStatusDidChange(syncStatus) { this.setState({errorMessage : errMsg}); } - getAdDataList() { - return HttpUtils.get(NativeModules.BuildConfig.apiHost + 'api/wxDeviceScreenAd/list?deviceId='+encodeURIComponent(this.state.macAddress)+'&pageNum=1&pageSize=1000') - .then((result) => { + getAdDataList(first) { + HttpUtils.get(NativeModules.BuildConfig.apiHost + + 'api/wxDeviceScreenAd/list?deviceId=' + + encodeURIComponent(this.state.macAddress) + + '&pageNum=1&pageSize=1000') + .then(result => { if (result.code != 200) { this.showErrMsg(result.message+'\n'+this.state.macAddress); } else if (result.data.list) { @@ -249,11 +259,19 @@ codePushStatusDidChange(syncStatus) { } }) .catch(error => { - this.showErrMsg(error.message); + if (first) { + this.showErrMsg(error.message); + } + else { + requestFailedCount++ + this.setState({ versionInfo: versionInfo+`[${requestFailedCount}]`}); + } }) } getBaseInfo() { - return HttpUtils.get(NativeModules.BuildConfig.apiHost + 'api/wxDeviceScreenAd/info?deviceId='+encodeURIComponent(this.state.macAddress)) + HttpUtils.get(NativeModules.BuildConfig.apiHost + + 'api/wxDeviceScreenAd/info?deviceId=' + + encodeURIComponent(this.state.macAddress)) .then(result => { if (result.code != 200) { this.showErrMsg(result.message+'\n'+this.state.macAddress); @@ -296,7 +314,7 @@ async routine() { try { if (getAdDataCount<=0) { - await this.getAdDataList() + await this.getAdDataList(false) getAdDataCount = heartConfig.dataRefreshInterval } else if (hbRequestCount<=0) { await this.hbRequest() @@ -311,12 +329,12 @@ async routine() { } } else { await this.getBaseInfo() - await this.getAdDataList() + await this.getAdDataList(true) } pageChangeCount = heartConfig.pageChangeInterval } } catch(e){ - this.setState({ versionInfo: this.state.versionInfo+'[E]'}); + this.setState({ versionInfo: versionInfo+'[E]'}); } setTimeout(()=>{ @@ -326,7 +344,7 @@ async routine() { async startAd() { await this.getBaseInfo() - await this.getAdDataList() + await this.getAdDataList(true) await this.hbRequest() hbRequestCount = heartConfig.heartbeatInterval; diff --git a/HttpUtils.js b/HttpUtils.js index 5c8ac5b..be59d85 100644 --- a/HttpUtils.js +++ b/HttpUtils.js @@ -1,16 +1,30 @@ export default class HttpUtil { + /** * 利用 Promise 的 get 方式请求 * @param url * @returns {Promise} */ static get(url) { - return new Promise((resolve, reject) => { + request = new Promise((resolve, reject) => { fetch(url) .then(response => response.json()) .then(result => resolve(result)) - }) + }).catch(e => reject(e.message)); + + const timeoutRequest = new Promise((resolve, reject) => { + setTimeout(reject, 30000, 'Request timeout'); + }); + + return Promise + .race([request, timeoutRequest]) + .then(res => { + return res + }, m => { + throw new Error(m); + }); + } /** @@ -20,7 +34,7 @@ export default class HttpUtil { * @returns {Promise} */ static post(url, params) { - return new Promise((resolve, reject) => { + request = new Promise((resolve, reject) => { fetch(url, { method: 'POST', headers: { @@ -28,10 +42,23 @@ export default class HttpUtil { 'Content-Type': 'application/json' }, body: JSON.stringify(params) - }) - .then(response => response.json()) - .then(result => resolve(result)) - }) + }).then(response => response.json()) + .then(result => resolve(result)) + }).catch(e => reject(e.message)); + + const timeoutRequest = new Promise((resolve, reject) => { + setTimeout(reject, 30000, 'Request timeout'); + }); + + return Promise + .race([request, timeoutRequest]) + .then(res => { + return res + }, m => { + throw new Error(m); + }); + } + }