|
-
- import React, { Component } from 'react';
- import {
- Platform,
- StyleSheet,
- Text,
- View,
- Image,
- ImageBackground,
- PixelRatio,
- NativeModules
- } from 'react-native';
-
-
- //引用插件
- 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 * as Animatable from 'react-native-animatable';
-
- 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:'设备编号获取错误',
- ERR_DATA_GET:'广告数据获取错误'
-
- }
-
- export default class MyPage extends Component {
-
- constructor(props) {
- super(props);
- this.state = {
- baseInfo: baseInfoDefault,
- adDataList:[],
- contentItem0:null,
- contentItem1:null,
- contentItem2:null,
-
- contentDisplayStatus:false,
-
- syncMessage: "初始化..." ,
- errorMessage: '加载中...',
-
- progress: {
- receivedBytes:0,
- totalBytes:0
- },
- macAddress: '',
- versionInfo:'Unknown'
- };
- }
-
- // 监听更新状态
- codePushStatusDidChange(syncStatus) {
- switch(syncStatus) {
- case CodePush.SyncStatus.CHECKING_FOR_UPDATE:
- this.setState({ syncMessage: "Checking for update." });
- break;
- case CodePush.SyncStatus.DOWNLOADING_PACKAGE:
- this.setState({ syncMessage: "Downloading package." });
- break;
- case CodePush.SyncStatus.AWAITING_USER_ACTION:
- this.setState({ syncMessage: "Awaiting user action." });
- break;
- case CodePush.SyncStatus.INSTALLING_UPDATE:
- this.setState({ syncMessage: "Installing update." });
- break;
- case CodePush.SyncStatus.UP_TO_DATE:
- this.setState({ syncMessage: "App up to date.", progress: false });
- break;
- case CodePush.SyncStatus.UPDATE_IGNORED:
- this.setState({ syncMessage: "Update cancelled by user.", progress: false });
- break;
- case CodePush.SyncStatus.UPDATE_INSTALLED:
- this.setState({ syncMessage: "Update installed and will be applied on restart.", progress: false });
- break;
- case CodePush.SyncStatus.UNKNOWN_ERROR:
- this.setState({ syncMessage: "An unknown error occurred.", progress: false });
- break;
- }
- }
-
-
- codePushDownloadDidProgress(progress) {
- this.setState({ progress });
- }
-
- updateContent() {
- if (this.state.adDataList.length <=0)
- return false;
-
- if (curAdIndex >= this.state.adDataList.length) {
- curAdIndex = 0
- }
-
- this.setState({contentItem0:this.state.adDataList[curAdIndex]});
-
- if (curAdIndex + 1 < this.state.adDataList.length)
- this.setState({contentItem1:this.state.adDataList[curAdIndex+1]});
- else
- this.setState({contentItem1:null});
-
- if (curAdIndex + 2 < this.state.adDataList.length)
- this.setState({contentItem2:this.state.adDataList[curAdIndex+2]});
- else
- this.setState({contentItem2:null});
-
- curAdIndex+=3
- return true;
- }
-
- hbRequest() {
- HttpUtils.post(NativeModules.BuildConfig.apiHost + 'api/wxDeviceScreenAd/heartbeat?deviceId='
- +encodeURIComponent(this.state.macAddress),
- {version:this.state.versionInfo})
- .then(result => {
- })
- .catch(error => {
- })
- }
-
- getAdDataList() {
- HttpUtils.get(NativeModules.BuildConfig.apiHost + 'api/wxDeviceScreenAd/list?deviceId='+encodeURIComponent(this.state.macAddress)+'&pageNum=1&pageSize=1000')
- .then((result) => {
- if (result.code != 200) {
- this.setState({contentDisplayStatus: false})
- this.setState({errorMessage : result.message+'\n'+this.state.macAddress});
- } else if (result.data.list) {
- this.setState({adDataList : result.data.list});
- if (this.updateContent()) {
- this.setState({contentDisplayStatus: true})
- }
- } else {
- this.setState({contentDisplayStatus: false})
- this.setState({errorMessage : errorMsg.ERR_DATA_GET});
- }
- })
- .catch(error => {
- this.setState({contentDisplayStatus: false})
- this.setState({errorMessage : error.message});
- })
-
- }
-
- getBaseInfo() {
-
- HttpUtils.get(NativeModules.BuildConfig.apiHost + 'api/wxDeviceScreenAd/info?deviceId='+encodeURIComponent(this.state.macAddress))
- .then(result => {
- if (result.code != 200) {
- this.setState({contentDisplayStatus: false})
- this.setState({errorMessage : result.message+'\n'+this.state.macAddress});
- } else if (result.data) {
- this.setState({baseInfo:result.data});
- } else {
- this.setState({contentDisplayStatus: false})
- this.setState({errorMessage : errorMsg.ERR_DATA_GET});
- }
- })
- .catch(error => {
- this.setState({contentDisplayStatus: false})
- this.setState({errorMessage : error.message});
- })
-
- }
-
- componentDidMount() {
- CodePush.allowRestart();
- CodePush.sync(
- { installMode: CodePush.InstallMode.IMMEDIATE},
- this.codePushStatusDidChange.bind(this),
- this.codePushDownloadDidProgress.bind(this)
- );
-
- CodePush.getUpdateMetadata(CodePush.UpdateState.RUNNING)
- .then((metadata: LocalPackage) => {
- this.setState({ versionInfo: metadata ? metadata.appVersion + "-" + metadata.label : "Unknown"});
- }, (error: any) => {
- this.setState({ versionInfo: "Unknown"});
- });
-
- DeviceInfo.getMACAddress().then((mac)=>{
- if (mac=='') {
- if (NativeModules.BuildConfig.buildType == 'debug') {
- this.setState({macAddress : this.testMac})
- this.startAd()
- } else {
- this.setState({ contentDisplayStatus: false})
- this.setState({ errorMessage : errorMsg.ERR_MAC_GET});
- }
- } else {
- this.setState({macAddress : mac})
- this.startAd()
- }
- this.setState({ contentDisplayStatus: true})
- }).catch(e => {
- this.setState({ contentDisplayStatus: false})
- this.setState({ errorMessage : errorMsg.ERR_MAC_GET});
- })
- }
-
- startAd() {
- this.getBaseInfo()
- this.getAdDataList()
- setInterval(
- ()=>{
- if (this.state.contentDisplayStatus) {
- this.setState({ contentDisplayStatus: false})
- if (this.updateContent()) {
- this.setState({ contentDisplayStatus: true})
- }
- } else {
- this.getBaseInfo()
- this.getAdDataList()
- }
- },
- 1000*10, //10秒更新一下页面
- );
- setInterval(
- ()=>{
- this.getAdDataList()
- },
- 1000*60*60, //1小时一次
- );
- setInterval(
- ()=>{
- this.hbRequest()
- },
- 1000*60*5, //5分钟一次
- );
-
- }
-
- getContentViewItemUplayer(contentItem) {
- if (contentItem.type == 0) {
-
- if (contentItem.subType == 2)
- return (<Image source={require('./image/groupbuy.png')} resizeMode='center' style={styles.contentItemUpLayer} /> )
- if (contentItem.subType == 6)
- return (<Image source={require('./image/discount.png')} resizeMode='center' style={styles.contentItemUpLayer} /> )
- if (contentItem.subType == 7)
- return (<Image source={require('./image/groupbuy.png')} resizeMode='center' style={styles.contentItemUpLayer} /> )
- }
- }
-
- getContentViewItem(contentItem, annomation) {
- if (!contentItem)
- return (
- <View style={styles.contentItem}>
- </View>
- )
-
- actionText = '扫码领取';
-
- if (contentItem.target.type == 8)
- actionText = '扫码砍价';
- else if (contentItem.target.type == 9)
- actionText = '扫码拼团';
-
- merchantName = "";
- if (contentItem.target.merchantVoList.length > 1)
- merchantName = '多商户通用';
- else
- merchantName = contentItem.target.merchantVoList[0].merchantName;
-
- return (
- <Animatable.View animation={annomation} style={styles.contentItem}>
-
- <ImageBackground source={require('./image/item-background.png')} resizeMode='center' style={styles.contentItemDownLayer} >
-
- <View style={styles.contentItemTop}>
- <Image source={{uri: contentItem.target.coverImg}} resizeMode='cover' style={styles.contentItemTopCoverImg} />
- <View style={styles.contentItemTopDetail}>
- <Text style={styles.contentItemTopDetailTitle} numberOfLines={1} ellipsizeMode="tail">{contentItem.target.title}</Text>
- <Text style={styles.contentItemTopDetailRemark} numberOfLines={1} ellipsizeMode="tail">{contentItem.target.remark}</Text>
- <View style={styles.contentItemTopDetailPrice}>
- <View style={styles.contentItemTopDetailCurPrice}>
- <Text style={styles.contentItemTopDetailCurPriceStr}>{contentItem.target.priceStr}</Text>
- <Text style={styles.contentItemTopDetailCurPriceStrUnit}>元</Text>
- </View>
- <View style={styles.contentItemTopDetailSalePrice}>
- <Text style={styles.contentItemTopDetailSalePriceStr} >{contentItem.target.salePriceStr}元</Text>
- </View>
- </View>
- </View>
- <Image source={{uri:'data:image/png;base64,'+contentItem.qrcode}} resizeMode='contain' style={styles.contentItemTopQrcode} />
- </View>
-
- <View style={styles.contentItemBottom}>
- <Text style={styles.contentItemBottomMerchantName}>{merchantName}</Text>
- <View style={styles.contentItemBottomPlacehold}></View>
- <Text style={styles.contentItemBottomAction}>{actionText}</Text>
- </View>
-
- </ImageBackground>
-
- {this.getContentViewItemUplayer(contentItem)}
-
- </Animatable.View>
- )
- }
-
- getContentView() {
- if (this.state.contentDisplayStatus) {
- return (<View style={styles.content}>
- {this.getContentViewItem(this.state.contentItem0,"slideInDown")}
- {this.getContentViewItem(this.state.contentItem1,"slideInLeft")}
- {this.getContentViewItem(this.state.contentItem2,"slideInRight")}
- </View>)
- }else{
- return (
- <View style={styles.content}>
- <Text style={styles.updatePage}>{this.state.errorMessage}</Text>
- </View>)
- }
- }
-
- render() {
-
- if (this.state.progress) {
- if (this.state.progress.totalBytes > 0)
- return (
- <Text style={styles.updatePage}>{this.state.syncMessage}{'\n'}
- {this.state.progress.receivedBytes} of {this.state.progress.totalBytes} bytes received{'\n'}
- </Text>
- );
- return (
- <Text style={styles.updatePage}>{this.state.syncMessage}
- </Text>
- );
- }
-
-
- return (
- <ImageBackground style={styles.background} resizeMode='center' source={require('./image/background.png')}>
-
- <View style={styles.title}>
- <View style={styles.titleImagePlace}>
- <Image source={require('./image/title.png')} resizeMode='center' style={styles.titleImage} />
- </View>
-
- <ImageBackground source={require('./image/title-qrcode.png') } resizeMode='center' style={styles.titleQrcodeBonder} >
- <View style={styles.titleQrcodePlacehold}></View>
- <Image source={{uri: this.state.baseInfo.imgQrcodeWeapp}} resizeMode='center' style={styles.titleQrcode} />
- <View style={styles.titleQrcodePlacehold}></View>
- </ImageBackground>
- </View>
-
- {this.getContentView()}
-
- <View style={styles.bottom}>
- <View style={styles.bottomLogoBackground}>
- <Image source={{ uri: this.state.baseInfo.imgUrlH, cache: 'force-cache'}} resizeMode='center' style={styles.bottomLogo} />
- </View>
- <View style={styles.bottomQrcode1Background}>
- <Image source={{ uri: this.state.baseInfo.imgQrcodeWeapp, cache: 'force-cache'}} resizeMode='center' style={styles.bottomQrcode1} />
- <Text style={styles.bottomQrcode1Str}>小程序</Text>
- </View>
- <View style={styles.bottomQrcode2Background}>
- <Image source={{ uri: this.state.baseInfo.imgQrcodeWemp, cache: 'force-cache'}} resizeMode='center' style={styles.bottomQrcode2} />
- <Text style={styles.bottomQrcode1Str}>公众号</Text>
- </View>
- <Text style={styles.bottomPlacehold}>{this.state.versionInfo}+{PixelRatio.get()}}</Text>
- </View>
-
- </ImageBackground>
- );
- }
- }
-
- const styles = StyleSheet.create({
- updatePage:{
- flex: 2,
- width:'100%',
- height:'100%',
- backgroundColor:'transparent',
- textAlign :'center',
- textAlignVertical:'center',
- fontSize: 24*PixelRatio.get(),
- color: 'darkblue'
- },
-
- //layer background
- background:{
- flex: 1,
- width:'100%',
- height:'100%'
- },
-
- //layer top
- title:{
- flex: 6,
- flexDirection: 'row',
- alignItems: 'center',
- paddingLeft:16*PixelRatio.get(),
- paddingRight:16*PixelRatio.get(),
- backgroundColor:'transparent'
- },
-
- content:{
- flex: 18,
- flexDirection: 'column',
- alignItems: 'center',
- backgroundColor:'transparent'
- },
-
- bottom:{
- flex: 2,
- flexDirection: 'row',
- alignItems: 'center',
- paddingLeft:16*PixelRatio.get(),
- paddingRight:16*PixelRatio.get(),
- backgroundColor:'transparent'
- },
-
- //layer title
- titleImagePlace: {
- flex: 2,
- paddingTop: '20%',
- paddingLeft: '15%',
- alignItems: 'center',
- backgroundColor:'transparent'
- },
- titleImage: {
- width:'100%',
- height:'100%',
- flex: 1,
- backgroundColor:'transparent'
- },
-
- titleQrcodeBonder: {
- width:'100%',
- height:'100%',
- alignItems: 'center',
- flexDirection: 'column',
- flex: 1,
- backgroundColor:'transparent'
- },
-
- titleQrcodePlacehold: {
- flex: 5,
- backgroundColor:'transparent'
- },
-
- titleQrcode: {
- flex: 6,
- width:'100%',
- height:'100%',
- backgroundColor:'transparent'
- },
-
- //layer bottom
- bottomLogoBackground: {
- flex: 3,
- borderRadius:6*PixelRatio.get(),
- marginTop: 10*PixelRatio.get(),
- marginBottom: 10*PixelRatio.get(),
- backgroundColor:'white'
- },
- bottomLogo: {
- width:'100%',
- height:'100%',
- borderRadius:10*PixelRatio.get(),
- backgroundColor:'transparent'
- },
- bottomQrcode1Background : {
- flex: 1,
- width:'100%',
- height:'100%',
- marginLeft: 8*PixelRatio.get(),
- paddingTop: 10*PixelRatio.get(),
- paddingBottom: 4*PixelRatio.get(),
- flexDirection: 'column',
- backgroundColor:'transparent'
- },
-
- bottomQrcode2Background : {
- flex: 1,
- width:'100%',
- height:'100%',
- marginLeft: 8*PixelRatio.get(),
- paddingTop: 10*PixelRatio.get(),
- paddingBottom: 4*PixelRatio.get(),
- flexDirection: 'column',
- backgroundColor:'transparent'
- },
-
- bottomQrcode1: {
- flex: 2,
- width:'100%',
- height:'100%',
- backgroundColor:'transparent'
- },
-
- bottomQrcode2: {
- flex: 2,
- width:'100%',
- height:'100%',
- backgroundColor:'transparent'
- },
-
- bottomQrcode1Str: {
- flex: 1,
- textAlign :'center',
- textAlignVertical:'center',
- fontSize: 6*PixelRatio.get(),
- color : 'black',
- backgroundColor:'transparent'
- },
-
- bottomQrcode2Str: {
- flex: 1,
- textAlign :'center',
- textAlignVertical:'center',
- fontSize: 6*PixelRatio.get(),
- color : 'black',
- backgroundColor:'transparent'
- },
-
- bottomPlacehold: {
- flex: 8,
- width:'100%',
- height:'100%',
- backgroundColor:'transparent',
- textAlign :'right',
- textAlignVertical:'bottom',
- fontSize: 10*PixelRatio.get(),
- color : 'gray'
- },
-
- //layer content item
- contentItem:{
- flex: 1,
- width:'100%',
- height:'100%',
- flexDirection: 'column',
- backgroundColor:'transparent'
- },
-
- contentItemDownLayer:{
- position:'absolute',
- width:'100%',
- height:'100%',
- flexDirection: 'column',
- alignItems: 'center',
- backgroundColor:'transparent'
- },
-
- contentItemUpLayer:{
- width:80*PixelRatio.get(),
- height:58*PixelRatio.get(),
- position:'absolute',
- backgroundColor:'transparent'
- },
-
- contentItemTop:{
- flex: 3,
- width:'100%',
- height:'100%',
- paddingLeft: 25*PixelRatio.get(),
- paddingRight: 20*PixelRatio.get(),
- paddingTop: 15*PixelRatio.get(),
- flexDirection: 'row',
- alignItems: 'center',
- backgroundColor:'transparent'
- },
-
- contentItemBottom:{
- flex: 1,
- width:'100%',
- height:'100%',
- paddingLeft: 25*PixelRatio.get(),
- paddingRight: 20*PixelRatio.get(),
- paddingBottom: 15*PixelRatio.get(),
- flexDirection: 'row',
- alignItems: 'center',
- backgroundColor:'transparent'
- },
-
- //layer content item top
- contentItemTopCoverImg:{
- flex: 5,
- width:'100%',
- height:'100%',
- borderRadius:10*PixelRatio.get(),
- backgroundColor:'transparent'
- },
-
- contentItemTopDetail:{
- flex: 6,
- width:'100%',
- height:'100%',
- paddingLeft: 10*PixelRatio.get(),
- paddingRight: 30*PixelRatio.get(),
- alignItems: 'center',
- backgroundColor:'transparent'
- },
-
- contentItemTopQrcode:{
- flex: 5,
- width:'100%',
- height:'100%',
- backgroundColor:'transparent'
- },
-
- //layer content item bottom
- contentItemBottomMerchantName:{
- flex: 5,
- width:'100%',
- height:'100%',
- alignItems: 'center',
- backgroundColor:'transparent',
- textAlign :'center',
- textAlignVertical:'center',
- fontSize: 10*PixelRatio.get(),
- fontWeight : 'bold'
- },
-
- contentItemBottomPlacehold:{
- flex: 6,
- paddingLeft: 10*PixelRatio.get(),
- paddingRight: 30*PixelRatio.get(),
- width:'100%',
- height:'100%',
- backgroundColor:'transparent',
- },
-
- contentItemBottomAction:{
- flex: 5,
- width:'100%',
- height:'100%',
- backgroundColor:'transparent',
- fontWeight : 'bold',
- textAlign :'center',
- textAlignVertical:'center',
- },
-
- //layer content item top detail
- contentItemTopDetailTitle:{
- flex: 2,
- width:'100%',
- height:'100%',
- backgroundColor:'transparent',
- textAlign :'left',
- textAlignVertical:'center',
- fontSize: 20*PixelRatio.get(),
- color: 'darkgray'
- },
-
- contentItemTopDetailRemark:{
- flex: 1,
- width:'100%',
- height:'100%',
- backgroundColor:'transparent',
- textAlign :'left',
- textAlignVertical:'center',
- fontSize: 10*PixelRatio.get(),
- color : 'gray'
- },
-
- contentItemTopDetailPrice:{
- flex: 2,
- width:'100%',
- height:'100%',
- backgroundColor:'transparent',
- flexDirection: 'row',
- },
-
- contentItemTopDetailCurPrice:{
- flex: 3,
- width:'100%',
- height:'100%',
- flexDirection: 'row',
- backgroundColor:'transparent',
- },
-
- contentItemTopDetailCurPriceStr:{
- backgroundColor:'transparent',
- textAlign :'left',
- textAlignVertical:'bottom',
- fontSize: 32*PixelRatio.get(),
- color : 'red'
- },
-
- contentItemTopDetailCurPriceStrUnit:{
- backgroundColor:'transparent',
- textAlign :'left',
- textAlignVertical:'bottom',
- fontSize: 10*PixelRatio.get(),
- color : 'red'
- },
-
- contentItemTopDetailSalePrice:{
- flex: 1,
- width:'100%',
- height:'100%',
- backgroundColor:'transparent',
- },
-
- contentItemTopDetailSalePriceStr:{
- width:'100%',
- height:'100%',
- backgroundColor:'transparent',
- textAlign :'left',
- textAlignVertical:'bottom',
- fontSize: 10*PixelRatio.get(),
- textDecorationLine: 'line-through',
- color : 'gray'
- },
-
- });
|