utils.ts 3.39 KB
import { Dimensions } from 'react-native'
import { UI_WIDTH } from '../config'
import DeviceInfo from 'react-native-device-info'
import { CommonActions } from '@react-navigation/native'
import CheckUtil from './check'
import { APP } from '../config'
import { FETCH } from './fetch'

/** 窗口属性 */
export const WINDOWS = Dimensions.get('window')

/** UI图缩放比例 */
const UI_SACEL = WINDOWS.width / UI_WIDTH

/**
 * 设置px单位
 * @param {*} num
 */
export const setUnit = (num): number => {
    return num * UI_SACEL
}

/** 获取版本号 */
export const getVersionCode = async (): Promise<number> => Number(await DeviceInfo.getBuildNumber())

/** 获取版本名称 */
export const getVersion = async (): Promise<string> => DeviceInfo.getVersion()

/** 获取屏幕高度 */
export const getHeight = (): number => WINDOWS.height


/**
 * 校验手机号和验证码
 * @param {string} phone
 * @returns {Object}
 */
export const checkPhoneAndCode = (phone, code): { status: number; msg: string } => {
    if (!phone.trim().length)
        return { status: 0, msg: '手机号不能为空' }

    if (!CheckUtil.isMobile(phone))
        return { status: 0, msg: '手机号格式不正确' }

    if (!code.trim().length)
        return { status: 0, msg: '验证码不能为空' }

    return { status: 1, msg: '' }
}

/**
 * 校验两次输入的邮箱
 * @param {*} email
 * @param {*} confirmEmail
 * @returns {Object}
 */
export const checkEmails = (email, confirmEmail): { status: number; msg: string } => {
    if (!email.trim().length)
        return { status: 0, msg: '邮箱不能为空' }

    if (!CheckUtil.isEmail(email))
        return { status: 0, msg: '邮箱格式不正确' }

    if (email !== confirmEmail)
        return { status: 0, msg: '两次输入邮箱不一致' }

    return { status: 1, msg: '' }
}

/**
 * 判断token是否过期
 * @param {*} saveTime token存储时间
 */
export const isTokenExpired = (saveTime): boolean => {
    const now = new Date().getTime()
    return now - saveTime > APP.tokenExpires * 24 * 60 * 60 * 1000
}

/**
 * 设置网络请求头
 * @param {*} jwt
 * @param {*} sessionId
 */
export const setHeader = (jwt, sessionId): void => {
    FETCH.setHeader('Authorization', jwt)
    FETCH.setHeader('Sessionid', sessionId)
}


/**
 * 重置路由栈
 * @param {*} navigation
 */
export const resetRoutes = (navigation): void => {
    navigation.dispatch(
        CommonActions.reset({
            index: 1,
            routes: [
                { name: 'Index' },
                { name: 'Login' }
            ]
        })
    )
}

/**
 * 日期格式化
 * @param {*} fmt
 * @param {*} time 毫秒时间戳
 */
export function dateFormat(fmt, time): string {
    let ret
    const newDate: Date = new Date()  // 实例化一个Date对象
    newDate.setTime(time)
    const opt = {
      "Y+": newDate.getFullYear().toString(),        // 年
      "M+": (newDate.getMonth() + 1).toString(),     // 月
      "D+": newDate.getDate().toString(),            // 日
      "h+": newDate.getHours().toString(),           // 时
      "m+": newDate.getMinutes().toString(),         // 分
      "s+": newDate.getSeconds().toString()          // 秒
    }
    for (const k in opt) {
      ret = new RegExp("(" + k + ")").exec(fmt)
      if (ret) {
        fmt = fmt.replace(ret[1], (ret[1].length === 1) ?
          (opt[k]) :
          (opt[k].padStart(ret[1].length, "0")))
      }
    }
    return fmt
  }