Преглед изворни кода

Merge branch 'release' into develop

tags/jenkins-front-end-screenad-139-v89^0
hupeng пре 6 година
родитељ
комит
e2ec37d0e1
2 измењених фајлова са 65 додато и 20 уклоњено
  1. +31
    -13
      App.js
  2. +34
    -7
      HttpUtils.js

+ 31
- 13
App.js Прегледај датотеку

@@ -89,6 +89,9 @@ var heartConfig = {
pageChangeInterval:10, pageChangeInterval:10,
} }


var requestFailedCount = 0;
var versionInfo = '';

export default class MyPage extends Component { export default class MyPage extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
@@ -122,8 +125,10 @@ codePushUpdate() {
CodePush.getUpdateMetadata(CodePush.UpdateState.RUNNING) CodePush.getUpdateMetadata(CodePush.UpdateState.RUNNING)
.then((metadata: LocalPackage) => { .then((metadata: LocalPackage) => {
this.setState({ versionInfo: metadata ? metadata.appVersion +'b'+NativeModules.BuildConfig.versionCode+"-" + metadata.label : "Unknown"}); this.setState({ versionInfo: metadata ? metadata.appVersion +'b'+NativeModules.BuildConfig.versionCode+"-" + metadata.label : "Unknown"});
versionInfo = this.state.versionInfo;
}, (error: any) => { }, (error: any) => {
this.setState({ versionInfo: "Unknown"}); this.setState({ versionInfo: "Unknown"});
versionInfo = this.state.versionInfo;
}); });
} }


@@ -186,9 +191,10 @@ codePushStatusDidChange(syncStatus) {
} }
hbRequest() { 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 => { .then(result => {
if (result.code == 200) { if (result.code == 200) {
let config = JSON.parse(result.data.config); let config = JSON.parse(result.data.config);
@@ -223,7 +229,8 @@ codePushStatusDidChange(syncStatus) {
}) })
.catch(error => { .catch(error => {
this.setState({ errorMessage : error.message});
requestFailedCount++
this.setState({ versionInfo: versionInfo+`[${requestFailedCount}]`});
}) })
} }


@@ -232,9 +239,12 @@ codePushStatusDidChange(syncStatus) {
this.setState({errorMessage : errMsg}); 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) { if (result.code != 200) {
this.showErrMsg(result.message+'\n'+this.state.macAddress); this.showErrMsg(result.message+'\n'+this.state.macAddress);
} else if (result.data.list) { } else if (result.data.list) {
@@ -249,11 +259,19 @@ codePushStatusDidChange(syncStatus) {
} }
}) })
.catch(error => { .catch(error => {
this.showErrMsg(error.message);
if (first) {
this.showErrMsg(error.message);
}
else {
requestFailedCount++
this.setState({ versionInfo: versionInfo+`[${requestFailedCount}]`});
}
}) })
} }
getBaseInfo() { 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 => { .then(result => {
if (result.code != 200) { if (result.code != 200) {
this.showErrMsg(result.message+'\n'+this.state.macAddress); this.showErrMsg(result.message+'\n'+this.state.macAddress);
@@ -296,7 +314,7 @@ async routine() {


try { try {
if (getAdDataCount<=0) { if (getAdDataCount<=0) {
await this.getAdDataList()
await this.getAdDataList(false)
getAdDataCount = heartConfig.dataRefreshInterval getAdDataCount = heartConfig.dataRefreshInterval
} else if (hbRequestCount<=0) { } else if (hbRequestCount<=0) {
await this.hbRequest() await this.hbRequest()
@@ -311,12 +329,12 @@ async routine() {
} }
} else { } else {
await this.getBaseInfo() await this.getBaseInfo()
await this.getAdDataList()
await this.getAdDataList(true)
} }
pageChangeCount = heartConfig.pageChangeInterval pageChangeCount = heartConfig.pageChangeInterval
} }
} catch(e){ } catch(e){
this.setState({ versionInfo: this.state.versionInfo+'[E]'});
this.setState({ versionInfo: versionInfo+'[E]'});
} }


setTimeout(()=>{ setTimeout(()=>{
@@ -326,7 +344,7 @@ async routine() {


async startAd() { async startAd() {
await this.getBaseInfo() await this.getBaseInfo()
await this.getAdDataList()
await this.getAdDataList(true)
await this.hbRequest() await this.hbRequest()
hbRequestCount = heartConfig.heartbeatInterval; hbRequestCount = heartConfig.heartbeatInterval;


+ 34
- 7
HttpUtils.js Прегледај датотеку

@@ -1,16 +1,30 @@
export default class HttpUtil { export default class HttpUtil {
/** /**
* 利用 Promise 的 get 方式请求 * 利用 Promise 的 get 方式请求
* @param url * @param url
* @returns {Promise} * @returns {Promise}
*/ */
static get(url) { static get(url) {
return new Promise((resolve, reject) => {
request = new Promise((resolve, reject) => {
fetch(url) fetch(url)
.then(response => response.json()) .then(response => response.json())
.then(result => resolve(result)) .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} * @returns {Promise}
*/ */
static post(url, params) { static post(url, params) {
return new Promise((resolve, reject) => {
request = new Promise((resolve, reject) => {
fetch(url, { fetch(url, {
method: 'POST', method: 'POST',
headers: { headers: {
@@ -28,10 +42,23 @@ export default class HttpUtil {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
body: JSON.stringify(params) 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);
});
} }
} }

Loading…
Откажи
Сачувај