utils.js 4.69 KB
/**
 * 一些公共方法设定
 */
import {
    Dimensions,
    Platform
} from 'react-native';
import Toast from 'react-native-root-toast';
import moment from 'moment';

//Toast
export function 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
    });
};
//是否IphoneX
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 timeShow(time){
    let timeStr = time;
    if (timeStr.length == 10){
        timeStr = timeStr +'000';
    }
    if (new Date(Number(timeStr)).toDateString() === new Date().toDateString()) {
        //今天
        return "今天";
    } else if (isYestday(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();
};
//数量处理
export function numChange(num){
    if(num > 100000){
        number = '10w+';
    }else if(num >= 10000){
        number = parseFloat(num/10000).toFixed(1)+'w';
    }else if(num >= 1000){
        number = parseFloat(num/1000).toFixed(1)+'k';
    }else{
        number = num;
    }
    return number;
};
//时间戳处理(xxx前)
export function timeChange(timespan){
    var dateTime = new Date(timespan);

    var year = dateTime.getFullYear();
    var month = dateTime.getMonth() + 1;
    var day = dateTime.getDate();
    var hour = dateTime.getHours();
    var minute = dateTime.getMinutes();
    var second = dateTime.getSeconds();
    var now = new Date();
    var now_new = Number(now);  //typescript转换写法

    var milliseconds = 0;
    var timeSpanStr;

    milliseconds = now_new - timespan;

    if (milliseconds <= 1000 * 60 * 1) {
        timeSpanStr = '刚刚';
    }
    else if (1000 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60) {
        timeSpanStr = Math.round((milliseconds / (1000 * 60))) + '分钟前';
    }
    else if (1000 * 60 * 60 * 1 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24) {
        timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60)) + '小时前';
    }
    else if (1000 * 60 * 60 * 24 < milliseconds && milliseconds <= 1000 * 60 * 60 * 24 * 15) {
        timeSpanStr = Math.round(milliseconds / (1000 * 60 * 60 * 24)) + '天前';
    }
    else if (milliseconds > 1000 * 60 * 60 * 24 * 15 && year == now.getFullYear()) {
        if(month < 10){
            month = '0' + month;
        }
        if(day < 10){
            day = '0' + day;
        }
        timeSpanStr = month + '-' + day;
    } else {
        if(month < 10){
            month = '0' + month;
        }
        if(day < 10){
            day = '0' + day;
        }
        timeSpanStr = year + '-' + month + '-' + day;
    }
    return timeSpanStr;
};
//时间戳处理(x月x日)
export function dateChange(time){
    let year = new Date(time).getFullYear();
    if(year == new Date().getFullYear()){
        return moment(time).format('MM-DD')
    }else{
        return moment(time).format('YYYY-MM-DD')
    }
};
//钟表
export function clock(){
    let now = new Date();
    let hours = now.getHours();
    let minutes = now.getMinutes();
    let seconds = now.getSeconds();
    if(hours < 10){
        hours = '0' + hours;
    }
    if(minutes < 10){
        minutes = '0' + minutes;
    }
    if(seconds < 10){
        seconds = '0' + seconds;
    }
    return (hours + ':' + minutes + ':' + seconds);
}