diff --git a/App.js b/App.js index ebf02d0..d9c9f2e 100644 --- a/App.js +++ b/App.js @@ -17,31 +17,9 @@ import Package from './package.json' import HttpUtils from './HttpUtils.js'; import CodePush from "react-native-code-push"; import DeviceInfo from "react-native-device-info"; -import RNFetchBlob from 'react-native-fetch-blob'; 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:'设备编号获取错误', @@ -77,6 +55,8 @@ var animationThemes = [ } ] +var timer = null; + var hbRequestCount var getAdDataCount var pageChangeCount @@ -92,11 +72,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, @@ -108,30 +97,32 @@ export default class MyPage extends Component { receivedBytes:0, totalBytes:0 }, + updating:true, macAddress: '', versionInfo:'未知' }; } //更新函数 -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; - }); +async codePushUpdate() { + + 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(); + } @@ -151,22 +142,23 @@ codePushStatusDidChange(syncStatus) { this.setState({ syncMessage: "安装更新" }); break; case CodePush.SyncStatus.UP_TO_DATE: - this.setState({ syncMessage: "已更新到最新程序", progress: false }); + this.setState({ syncMessage: "已更新到最新程序", updating: false }); break; case CodePush.SyncStatus.UPDATE_IGNORED: - this.setState({ syncMessage: "更新被取消", progress: false }); + this.setState({ syncMessage: "更新被取消", updating: false }); break; case CodePush.SyncStatus.UPDATE_INSTALLED: - this.setState({ syncMessage: "更新已安装,重启应用后生效", progress: false }); + this.setState({ syncMessage: "更新已安装,重启应用后生效", updating: false }); break; case CodePush.SyncStatus.UNKNOWN_ERROR: - this.setState({ syncMessage: "未知错误", progress: false }); + this.setState({ syncMessage: "未知错误", updating: false }); break; } } codePushDownloadDidProgress(progress) { this.setState({ progress }); } + // 列表滚动 updateContent() { if (adDataList.length <=0) @@ -250,7 +242,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}) @@ -271,7 +262,7 @@ codePushStatusDidChange(syncStatus) { } }) } - getBaseInfo() { + getBaseInfo() { HttpUtils.get(NativeModules.BuildConfig.apiHost + 'api/wxDeviceScreenAd/info?deviceId=' + encodeURIComponent(this.state.macAddress)) @@ -310,7 +301,13 @@ componentDidMount() { }) } -routine() { +componentWillUnmount(){ + if (timer) + clearTimeout(timer) +} + + +async routine() { hbRequestCount -=1; getAdDataCount -=1; pageChangeCount -=1; @@ -325,14 +322,16 @@ routine() { } 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}) } } else { - this.getBaseInfo() - this.getAdDataList(true) + await this.getBaseInfo() + await this.getAdDataList(true) + getAdDataCount = heartConfig.dataRefreshInterval + hbRequestCount = heartConfig.heartbeatInterval } pageChangeCount = heartConfig.pageChangeInterval } @@ -340,23 +339,16 @@ routine() { this.setState({ versionInfo: versionInfo+'[E]'}); } - setTimeout(()=>{ + timer = setTimeout(()=>{ this.routine(); },1000); } -async startAd() { - this.getBaseInfo() - this.getAdDataList(true) - this.hbRequest() - - hbRequestCount = heartConfig.heartbeatInterval; - getAdDataCount =heartConfig.dataRefreshInterval; - pageChangeCount =heartConfig.pageChangeInterval; - - setTimeout(()=>{ - this.routine(); - },1000); +startAd() { + hbRequestCount = 0; + getAdDataCount = 0; + pageChangeCount = 0; + this.routine(); } getContentViewItemUplayer(contentItem) { @@ -410,7 +402,7 @@ getContentViewItem(contentItem, annomation) { {/* 列表 */} - + {/* 券的title */} {contentItem.target.title} @@ -427,7 +419,7 @@ getContentViewItem(contentItem, annomation) { {/* */} - + {/* 优惠券底部商户名称 */} @@ -469,46 +461,72 @@ getUpdateView() { ); } -render() { - - if (this.state.progress) { - return this.getUpdateView(); - } - +getAdView(){ + if (this.state.baseInfo) { + return ( + + {/* 头部title二维码开始 */} + + + + + + + + + + + {/* 券列表展示列表开始 */} + {this.getContentView()} + {/* 底部商场信息展示开始 */} + + + + + + + 小程序 + + + + 公众号 + + {this.state.versionInfo} + + + );} + else { return ( {/* 头部title二维码开始 */} - - - - - - - - {/* 券列表展示列表开始 */} {this.getContentView()} {/* 底部商场信息展示开始 */} - - - 小程序 - - 公众号 {this.state.versionInfo} ); } - } + +} + +render() { + + if (this.state.updating) { + return this.getUpdateView(); + } + return this.getAdView(); + } +} const styles = StyleSheet.create({ updatePage:{ diff --git a/android/app/build.gradle b/android/app/build.gradle index c68f647..afcacdd 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -181,7 +181,6 @@ android { } dependencies { - implementation project(':react-native-fetch-blob') implementation project(':react-native-camera') implementation project(':react-native-device-info') implementation project(':react-native-code-push') diff --git a/android/app/src/main/java/com/screenad/MainApplication.java b/android/app/src/main/java/com/screenad/MainApplication.java index 6ab9b0c..2664dc1 100644 --- a/android/app/src/main/java/com/screenad/MainApplication.java +++ b/android/app/src/main/java/com/screenad/MainApplication.java @@ -3,7 +3,6 @@ package com.screenad; import android.app.Application; import com.facebook.react.ReactApplication; -import com.RNFetchBlob.RNFetchBlobPackage; import org.reactnative.camera.RNCameraPackage; import com.facebook.react.bridge.ReactApplicationContext; @@ -38,7 +37,6 @@ public class MainApplication extends Application implements ReactApplication { protected List getPackages() { return Arrays.asList( new MainReactPackage(), - new RNFetchBlobPackage(), new RNCameraPackage(), new RNBuildConfigPackage(), new RNDeviceInfo(), diff --git a/android/settings.gradle b/android/settings.gradle index c7cbd7f..399f0dc 100644 --- a/android/settings.gradle +++ b/android/settings.gradle @@ -1,6 +1,4 @@ rootProject.name = 'screenAd' -include ':react-native-fetch-blob' -project(':react-native-fetch-blob').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-fetch-blob/android') include ':react-native-camera' project(':react-native-camera').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-camera/android') include ':react-native-device-info' diff --git a/package.json b/package.json index 39871b6..f98efa9 100644 --- a/package.json +++ b/package.json @@ -13,8 +13,6 @@ "react-native-camera": "^1.12.0", "react-native-code-push": "^5.6.0", "react-native-device-info": "^2.1.2", - "react-native-fetch-blob": "^0.10.8", - "react-native-img-cache": "^1.6.0", "react-native-swiper": "^1.5.14", "rn-fetch-blob": "^0.10.15" },