utils.js 3.55 KB
/**
 * Created by DEV005 on 2017/8/31.
 */
import Toast from 'react-native-root-toast';
import {
    Alert,
    Dimensions,
    Platform,
    StatusBar
} from 'react-native';
import {zoomH} from './getSize';
/**
 * 冒一个时间比较短的Toast
 * @param content
 */
export const xnToast = (content) => {
    if (global.toast !== undefined) {
        Toast.hide(toast);
    };
    global.toast = Toast.show(content.toString(), {
        duration: Toast.durations.SHORT,
        position: Toast.positions.CENTER,
        shadow: true,
        animation: true,
        hideOnPress: true,
        delay: 0
    });
};


export const xnAlert = (content,okBack,cancel) => {

    Alert.alert(
        '',
        content,
        [
            {text: '取消', onPress: () =>cancel&&cancel()},
            {text: '确定', onPress: () =>okBack&&okBack()},
        ]
    )
};


//是否 isIphoneX
export function isIphoneX() {
    let dimen = Dimensions.get('window');
    return (
        Platform.OS === 'ios' &&
        !Platform.isPad &&
        !Platform.isTVOS &&
        (dimen.height === 812 || dimen.width === 812)
    );
}

//获取头部高度
export function getHeaderHeight() {
    if( Platform.OS === 'android'){
        return 48;

    }else if(Platform.OS === 'ios') {

        if(isIphoneX()){
            return 88;
        }else {
            return 64;
        }
    }
}
//获取头部填充高度
export function getHeaderPadding() {
    if( Platform.OS === 'android'){
        return 0;
    }else if(Platform.OS === 'ios') {
        if(isIphoneX()){
            return 44;
        }else {
            return 20;
        }
    }
}

//获取头部填充底部填充高度
export function getFooterBottom() {
    if( Platform.OS === 'android'){
        return 0;
    }else if(Platform.OS === 'ios') {
        if(isIphoneX()){
            return 34;
        }else {
            return 0;
        }
    }
}

/*获取头部整体高度*/
export function getTopHeight(){
    if( Platform.OS === 'android'){
        return (48/zoomH) + StatusBar.currentHeight;
    }else if(Platform.OS === 'ios') {
        if(isIphoneX()){
            return (48/zoomH) + 44;
        }else {
            return (48/zoomH) + 20;
        }
    }
};

export function xnBorderWidth() {
    if(isIphoneX()){
        return 1;
    }else {
        return 0.5;
    }
}

//时间显示
export function timeShow(time) {
    let timeStr = time;

    if (timeStr.length == 10){
        timeStr = timeStr +'000';
    }

    if (new Date(Number(timeStr)).toDateString() === new Date().toDateString()) {
        //今天
        console.log(Date(timeStr));
        return "今天";
    } else if (isYestday(timeStr)){
        //之前
        console.log(Date(timeStr));
        return "昨天";
    }




}

function isYestday(theDate){
    let date = (new Date());    //当前时间
    let today = new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime(); //今天凌晨
    let yestday = new Date(today - 24*3600*1000).getTime();
    return theDate.getTime() < today && yestday <= theDate.getTime();
}

const xnUtils={
    xnToast:xnToast,
    xnAlert:xnAlert,
    isIphoneX:isIphoneX,
    getHeaderHeight:getHeaderHeight,
    getHeaderPadding:getHeaderPadding,
    getFooterBottom:getFooterBottom,
    xnBorderWidth:xnBorderWidth,
}
export default xnUtils;

//防止按钮多次触发
export const NoDoublePress = {
    lastPressTime: 1,
    onPress(callback){
        let curTime = new Date().getTime();
        if (curTime - this.lastPressTime > 1000) {
            this.lastPressTime = curTime;
            callback();
        }
    },
};