Explorar el Código

[网络][修改]:优化网络获取数据的方式

tags/jenkins-front-end-screenad-188-v104^0
hupeng hace 5 años
padre
commit
8f360c9371
Se han modificado 1 ficheros con 69 adiciones y 56 borrados
  1. +69
    -56
      App.js

+ 69
- 56
App.js Ver fichero

@@ -22,26 +22,6 @@ import * as Animatable from 'react-native-animatable';
import {CachedImage,ImageCache} from "react-native-img-cache";
var testMac = 'FF:FF:FF:FF:FF:FF'

var contentItemDefault = {
type:0,
subType:0,
qrcode:[],
target:{
coverImg:'',
title:'',
subTitle:'',
priceStr:'',
salePriceStr:'',
remark:'',
}
}

var baseInfoDefault = {
imgUrlH:'',
imgQrcodeWeapp:'',
imgQrcodeWemp:''
}

var curAdIndex = 0;
var errorMsg = {
ERR_MAC_GET:'设备编号获取错误',
@@ -94,11 +74,20 @@ var heartConfig = {
var requestFailedCount = 0;
var versionInfo = '';


let codePushOptions = {
//设置检查更新的频率
//ON_APP_RESUME APP恢复到前台的时候
//ON_APP_START APP开启的时候
//MANUAL 手动检查
checkFrequency : CodePush.CheckFrequency.ON_APP_START
};

export default class MyPage extends Component {
constructor(props) {
super(props);
this.state = {
baseInfo: baseInfoDefault,
baseInfo: null,
contentItem0:null,
contentItem1:null,
contentItem2:null,
@@ -118,23 +107,24 @@ export default class MyPage extends Component {

//更新函数
async codePushUpdate() {
CodePush.allowRestart();
CodePush.sync(
{ mandatoryInstallMode: CodePush.InstallMode.IMMEDIATE,
installMode: CodePush.InstallMode.IMMEDIATE},
this.codePushStatusDidChange.bind(this),
this.codePushDownloadDidProgress.bind(this)
);

CodePush.getUpdateMetadata(CodePush.UpdateState.RUNNING)
.then((metadata: LocalPackage) => {
this.setState({ versionInfo: metadata ? 'b'+NativeModules.BuildConfig.versionCode+"-" + metadata.label : 'b'+ NativeModules.BuildConfig.versionCode});
versionInfo = this.state.versionInfo;
}, (error: any) => {
this.setState({ versionInfo: "Unknown"});
versionInfo = this.state.versionInfo;
});
await CodePush.getUpdateMetadata(CodePush.UpdateState.RUNNING)
.then((metadata: LocalPackage) => {
this.setState({ versionInfo: metadata ? 'b'+NativeModules.BuildConfig.versionCode+"-" + metadata.label : 'b'+ NativeModules.BuildConfig.versionCode});
versionInfo = this.state.versionInfo;
}, (error: any) => {
this.setState({ versionInfo: "Unknown"});
versionInfo = this.state.versionInfo;
});
await CodePush.disallowRestart();
await CodePush.sync(
{ mandatoryInstallMode: CodePush.InstallMode.IMMEDIATE,
installMode: CodePush.InstallMode.IMMEDIATE},
this.codePushStatusDidChange.bind(this),
this.codePushDownloadDidProgress.bind(this)
);
await CodePush.allowRestart();
}


@@ -196,7 +186,7 @@ codePushStatusDidChange(syncStatus) {
return true;
}
async hbRequest() {
hbRequest() {
return HttpUtils.post(NativeModules.BuildConfig.apiHost
+ 'api/wxDeviceScreenAd/heartbeat?deviceId='
+ encodeURIComponent(this.state.macAddress),
@@ -245,7 +235,7 @@ codePushStatusDidChange(syncStatus) {
this.setState({errorMessage : errMsg});
}

async getAdDataList(first) {
getAdDataList(first) {
HttpUtils.get(NativeModules.BuildConfig.apiHost
+ 'api/wxDeviceScreenAd/list?deviceId='
+ encodeURIComponent(this.state.macAddress)
@@ -254,7 +244,6 @@ codePushStatusDidChange(syncStatus) {
if (result.code != 200) {
this.showErrMsg(result.message+'\n'+this.state.macAddress);
} else if (result.data.list) {
ImageCache.get().clear();
adDataList = result.data.list;
if (this.updateContent()) {
this.setState({contentDisplayStatus: true})
@@ -275,7 +264,7 @@ codePushStatusDidChange(syncStatus) {
}
})
}
async getBaseInfo() {
getBaseInfo() {
HttpUtils.get(NativeModules.BuildConfig.apiHost
+ 'api/wxDeviceScreenAd/info?deviceId='
+ encodeURIComponent(this.state.macAddress))
@@ -295,7 +284,7 @@ codePushStatusDidChange(syncStatus) {
}

componentDidMount() {
await this.codePushUpdate();
this.codePushUpdate();

DeviceInfo.getMACAddress().then((mac)=>{
if (mac=='' || mac==null ) {
@@ -314,28 +303,28 @@ componentDidMount() {
})
}

componentDidUnmount(){
componentWillUnmount(){
if (timer)
clearTimeout(timer)
}


routine() {
async routine() {
hbRequestCount -=1;
getAdDataCount -=1;
pageChangeCount -=1;

try {
if (getAdDataCount<=0) {
await this.getAdDataList(false)
this.getAdDataList(false)
getAdDataCount = heartConfig.dataRefreshInterval
} else if (hbRequestCount<=0) {
await this.hbRequest()
this.hbRequest()
hbRequestCount = heartConfig.heartbeatInterval
}

if (pageChangeCount <= 0) { //页面切换间隔
if (this.state.contentDisplayStatus) {
if (this.state.contentDisplayStatus && this.state.baseInfo) {
this.setState({ contentDisplayStatus: false})
if (this.updateContent()) {
this.setState({ contentDisplayStatus: true})
@@ -343,6 +332,8 @@ routine() {
} else {
await this.getBaseInfo()
await this.getAdDataList(true)
getAdDataCount = heartConfig.dataRefreshInterval
hbRequestCount = heartConfig.heartbeatInterval
}
pageChangeCount = heartConfig.pageChangeInterval
}
@@ -356,9 +347,9 @@ routine() {
}

startAd() {
hbRequestCount = heartConfig.heartbeatInterval;
getAdDataCount = heartConfig.dataRefreshInterval;
pageChangeCount = heartConfig.pageChangeInterval;
hbRequestCount = 0;
getAdDataCount = 0;
pageChangeCount = 0;
this.routine();
}

@@ -473,6 +464,7 @@ getUpdateView() {
}

getAdView(){
if (this.state.baseInfo) {
return (
<ImageBackground style={styles.background} resizeMode='center' source={require('./image/background.png')}>
{/* 头部title二维码开始 */}
@@ -482,7 +474,7 @@ getAdView(){
</View>
<ImageBackground source={require('./image/title-qrcode.png') } resizeMode='center' style={styles.titleQrcodeBonder} >
<View style={styles.titleQrcodePlacehold}></View>
<CachedImage source={{uri: this.state.baseInfo.imgQrcodeWeapp}} resizeMode='center' style={styles.titleQrcode} />
<Image source={{uri: this.state.baseInfo.imgQrcodeWeapp}} resizeMode='center' style={styles.titleQrcode} />
<View style={styles.titleQrcodePlacehold}></View>
</ImageBackground>
</View>
@@ -491,20 +483,41 @@ getAdView(){
{/* 底部商场信息展示开始 */}
<View style={styles.bottom}>
<View style={styles.bottomLogoBackground}>
<CachedImage source={{ uri: this.state.baseInfo.imgUrlH}} resizeMode='contain' style={styles.bottomLogo} />
<Image source={{ uri: this.state.baseInfo.imgUrlH}} resizeMode='contain' style={styles.bottomLogo} />
</View>
<View style={styles.bottomQrcode1Background}>
<CachedImage source={{ uri: this.state.baseInfo.imgQrcodeWeapp}} resizeMode='center' style={styles.bottomQrcode1} />
<Image source={{ uri: this.state.baseInfo.imgQrcodeWeapp}} resizeMode='center' style={styles.bottomQrcode1} />
<Text style={styles.bottomQrcode1Str}>小程序</Text>
</View>
<View style={styles.bottomQrcode2Background}>
<CachedImage source={{ uri: this.state.baseInfo.imgQrcodeWemp}} resizeMode='center' style={styles.bottomQrcode2} />
<Image source={{ uri: this.state.baseInfo.imgQrcodeWemp}} resizeMode='center' style={styles.bottomQrcode2} />
<Text style={styles.bottomQrcode1Str}>公众号</Text>
</View>
<Text style={styles.bottomPlacehold}>{this.state.versionInfo}</Text>
</View>
</ImageBackground>
);
);}
else {
return (
<ImageBackground style={styles.background} resizeMode='center' source={require('./image/background.png')}>
{/* 头部title二维码开始 */}
<View style={styles.title}>
</View>
{/* 券列表展示列表开始 */}
{this.getContentView()}
{/* 底部商场信息展示开始 */}
<View style={styles.bottom}>
<View style={styles.bottomLogoBackground}>
</View>
<View style={styles.bottomQrcode1Background}>
</View>
<View style={styles.bottomQrcode2Background}>
</View>
<Text style={styles.bottomPlacehold}>{this.state.versionInfo}</Text>
</View>
</ImageBackground>
);
}

}

@@ -513,7 +526,7 @@ render() {
if (this.state.updating) {
return this.getUpdateView();
}
return getAdView();
return this.getAdView();
}
}



Cargando…
Cancelar
Guardar