utils.ts
3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
}