utils.js 3.01 KB
/**
 * Created by DEV005 on 2017/8/31.
 */
import{
    Dimensions,
    Platform,
    StatusBar
} from 'react-native';
import Toast from 'react-native-root-toast';
import {zoomH} from './getSize';

//是否 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/zoomH);
    }else if(Platform.OS === 'ios') {
        if(isIphoneX()){
            return (88/zoomH);
        }else {
            return (64/zoomH);
        }
    }
};
//设置头部上边距
export function getHeaderPadding(){
    if( Platform.OS === 'android'){
        return 0;
    }else if(Platform.OS === 'ios'){
        if(isIphoneX()){
            return (44/zoomH);
        }else {
            return (20/zoomH);
        }
    }
};
//设置底部填充高度
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;
        }
    }
};
//Toast
export const xnToast = (content) => {
    setTimeout(() =>{
        if(global.toast !== undefined){
            Toast.hide(toast);
        }
        global.toast = Toast.show(content.toString(),{
            duration:Toast.durations.LONG,
            position:Toast.positions.CENTER,
            shadow:true,
            animation:true,
            hideOnPress:true,
            delay:0
        });
    },500);
};
//剩余时间计算
export function spareTime(endTime){
    let leaveTime = '';
    let distanceTime = endTime - Number(new Date());
    let days = parseInt(distanceTime / (1000 * 60 * 60 * 24));
    let hours = parseInt((distanceTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    let minutes = parseInt((distanceTime % (1000 * 60 * 60)) / (1000 * 60));
    if(days !=0){
        leaveTime = leaveTime + days + '天';
    }
    if(hours !=0){
        leaveTime = leaveTime + hours + '小时';
    }
    if(minutes !=0){
        leaveTime = leaveTime + minutes + '分';
    }
    return leaveTime;
};
//录音时间装换
export function formatTime(seconds) {
    return[
        parseInt(seconds/60%60),
        parseInt(seconds%60)
    ].join(":").replace(/\b(\d)\b/g,"0$1");
};
//防止按钮多次触发
export const NoDoublePress = {
    lastPressTime: 1,
    onPress(callback){
        let curTime = new Date().getTime();
        if (curTime - this.lastPressTime > 1000) {
            this.lastPressTime = curTime;
            callback();
        }
    },
};