-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathScreenUtil.js
369 lines (336 loc) · 10 KB
/
ScreenUtil.js
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/**
* 屏幕工具类
* ui设计基准,iphone 6
* width:750px
* height:1334px
*/
import { PixelRatio, Dimensions, Platform, StatusBar } from 'react-native'
export let screenW = Dimensions.get('window').width
export let screenH = Dimensions.get('window').height
export let pixelRatio = PixelRatio.get()
//像素密度
export const DEFAULT_DENSITY = 1
//px转换成dp
//以iphone6为基准,如果以其他尺寸为基准的话,请修改下面的750和1334为对应尺寸即可.
const w2 = 750 / DEFAULT_DENSITY
//px转换成dp
const h2 = 1334 / DEFAULT_DENSITY
// iPhoneX
export const X_WIDTH = 375
export const X_HEIGHT = 812
//iPhoneX底部高度
export const IPHONEX_BOTTOM_HEIGHT = 54;
export const STATUS_HEIGHT = Platform.OS === 'ios' ? 20 : (Platform.Version > 19 ? StatusBar.currentHeight : 0);
// 边缘圆角
export const radiusNum = scaleSize(5);
/**
* 设置字体的size(单位px)
* @param size 传入设计稿上的px
* @returns {Number} 返回实际sp
*/
export function setSpText(size) {
isIphoneX()
let scaleWidth = screenW / w2
let scaleHeight = screenH / h2
let scale = Math.min(scaleWidth, scaleHeight)
size = Math.round(size * 2 * scale + 0.5)
return size / DEFAULT_DENSITY
}
export const getUrlParam = (name) => {
const that = this
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
var r = window.location.search.substr(1).match(reg)
if (r != null) {
return r[2]
}
}
export const getParams = (params, name) => {
const that = this
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
var r = params.substr(1).match(reg)
if (r != null) {
return r[2]
}
}
/**
* 屏幕适配,缩放size
* @param size
* @returns {Number}
*/
export function scaleSize(size) {
isIphoneX()
let scaleWidth = screenW / w2
let scaleHeight = screenH / h2
let scale = Math.min(scaleWidth, scaleHeight)
size = Math.round(size * 2 * scale + 0.5)
return size / DEFAULT_DENSITY
}
/**
* 判断android API是否小于19(4.4以下),如果是则不能使用沉浸状态栏
*
*/
export function isLT19() {
return Platform.OS === 'android' && Platform.Version < 19
}
//时间处理
Date.prototype.format = function (format) {
let date = {
'M+': this.getMonth() + 1,
'd+': this.getDate(),
'h+': this.getHours(),
'm+': this.getMinutes(),
's+': this.getSeconds(),
'q+': Math.floor((this.getMonth() + 3) / 3),
'S+': this.getMilliseconds()
}
if (/(y+)/i.test(format)) {
format = format.replace(
RegExp.$1,
(this.getFullYear() + '').substr(4 - RegExp.$1.length)
)
}
for (let k in date) {
if (new RegExp('(' + k + ')').test(format)) {
format = format.replace(
RegExp.$1,
RegExp.$1.length === 1
? date[k]
: ('00' + date[k]).substr(('' + date[k]).length)
)
}
}
return format
}
export function TimesAgo(timestamp) {
var mistiming = Math.round(new Date() / 1000) - timestamp;
var arrr = ['年', '个月', '星期', '天', '小时', '分钟', '秒'];
var arrn = [31536000, 2592000, 604800, 86400, 3600, 60, 1];
for (var i = 6; i >= 0; i--) {
var inm = Math.floor(mistiming / arrn[i]);
if (inm != 0) {
return inm + arrr[i] + '前';
}
}
}
//获取时间差 current:1497235409744 当前时间 start:1497235419744 开始时间
export function getRemainingime(current, start) {
let time = start - current
return time / 1000 //["0", "0", "2", "7", "33", "30"]0年0月2日 7时33分30秒
}
//1497235419
export function getRemainingimeDistance(distance) {
let time = distance * 1000
if (time < 0) {
return ['0', '0', '0', '0', '0', '0']
}
let year = Math.floor(time / (365 * 30 * 24 * 3600 * 1000)) //年
let month = Math.floor(time / (30 * 24 * 3600 * 1000)) //月
let days = Math.floor(time / (24 * 3600 * 1000)) //日
let temp1 = time % (24 * 3600 * 1000)
let hours = Math.floor(temp1 / (3600 * 1000)) //时
let temp2 = temp1 % (3600 * 1000)
let minutes = Math.floor(temp2 / (60 * 1000)) //分
let temp3 = temp2 % (60 * 1000)
let seconds = Math.round(temp3 / 1000) //秒
let strs = [
year,
toNormal(month),
toNormal(days),
toNormal(hours),
toNormal(minutes),
toNormal(seconds)
]
return strs //["0", "0", "2", "7", "33", "30"]0年0月2日 7时33分30秒
}
export function toNormal(time) {
return time >= 10 ? time : '0' + time
}
//转换成日期
export function toDate(timestamp, format1 = 'yyyy-MM-dd hh:mm:ss') {
try {
let date = new Date()
date.setTime(timestamp)
return date.format(format1) //2014-07-10 10:21:12
} catch (erro) {
return ''
}
}
//1970/1/1至今的秒数
export function toTimestamp(date) {
let timestamp = Date.parse(date)
return timestamp / 1000 // 1497233827569/1000
}
//CST时间=>转换成日期yyyy-MM-dd hh:mm:ss
export function getTaskTime(strDate) {
if (null == strDate || '' == strDate) {
return ''
}
let dateStr = strDate.trim().split(' ')
let strGMT =
dateStr[0] +
' ' +
dateStr[1] +
' ' +
dateStr[2] +
' ' +
dateStr[5] +
' ' +
dateStr[3] +
' GMT+0800'
let date = new Date(Date.parse(strGMT))
let y = date.getFullYear()
let m = date.getMonth() + 1
m = m < 10 ? '0' + m : m
let d = date.getDate()
d = d < 10 ? '0' + d : d
let h = date.getHours()
let minute = date.getMinutes()
minute = minute < 10 ? '0' + minute : minute
let second = date.getSeconds()
second = second < 10 ? '0' + second : second
return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second
}
//1497235419
export function getRemainingimeDistance2(distance) {
let time = distance
let days = Math.floor(time / (24 * 3600 * 1000))
let temp1 = time % (24 * 3600 * 1000)
let hours = Math.floor(temp1 / (3600 * 1000))
let temp2 = temp1 % (3600 * 1000)
let minutes = Math.floor(temp2 / (60 * 1000))
if (time <= 60 * 1000) {
minutes = 1
}
let temp3 = temp2 % (60 * 1000)
let seconds = Math.round(temp3 / 1000)
return [hours, minutes] //["0", "0", "2", "7", "33", "30"]0年0月2日 7时33分30秒
}
/**
* 判断是否为iphoneX
* @returns {boolean}
*/
export function isIphoneX() {
const dimen = Dimensions.get('window');
return (
Platform.OS === 'ios' &&
((dimen.height === 812 || dimen.width === 812) || (dimen.height === 896 || dimen.width === 896))
)
}
export function isIphoneNum(mun) {
const dimen = Dimensions.get('window');
var topMum = mun;
if (Platform.OS === 'ios' && !Platform.isPad && !Platform.isTVOS
&& ((dimen.height === 812 || dimen.width === 812) || (dimen.height === 896 || dimen.width === 896))) {
topMum = mun + 20;
}
return scaleSize(topMum)
}
/**
* 根据是否是iPhoneX返回不同的样式
* @param iphoneXStyle
* @param iosStyle
* @param androidStyle
* @returns {*}
*/
export function ifIphoneX(iphoneXStyle, iosStyle = {}, androidStyle) {
if (isIphoneX()) {
return iphoneXStyle
} else if (Platform.OS === 'ios') {
return iosStyle
} else {
if (androidStyle) return androidStyle
return iosStyle
}
}
export function guid() {
function S4() {
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
return (S4() + S4() + S4() + S4() + S4() + S4() + S4() + S4());
}
export function Accuracy(num, len) {
const wordCount = num + ''
var countNum = num;
if (wordCount.length > 4 && wordCount.length < 9) {
countNum = parseInt(wordCount.substring(0, wordCount.length - 4) + '.' + wordCount.substring(wordCount.length - 4, wordCount.length)).toFixed(len || 0)
} else if (wordCount.length > 8) {
countNum = parseInt(wordCount.substring(0, wordCount.length - 8) + '.' + wordCount.substring(wordCount.length - 8, wordCount.length - 7)).toFixed(len || 0)
}
return countNum;
}
export function AccuracyNUm(num) {
const wordCount = num + ''
var countNum = '';
if (wordCount.length > 4 && wordCount.length < 9) {
countNum = '万'
} else if (wordCount.length > 8) {
countNum = '亿'
}
return countNum;
}
export function insertionSort(array) {
for (var i = 1; i < array.length; i++) {
var key = array[i];
var j = i - 1;
while (j >= 0 && array[j] > key) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = key;
}
return array;
}
export function insertionSortJSON(array) {
for (var i = 1; i < array.length; i++) {
var key = array[i];
var j = i - 1;
while (j >= 0 && array[j].seqNum > key.seqNum) {
array[j + 1] = array[j];
j--;
}
array[j + 1] = key;
}
return array;
}
export default class ScreenUtil {
static screenW = screenW
static screenH = screenH
static pixelRatio = pixelRatio
static DEFAULT_DENSITY = DEFAULT_DENSITY
static setSpText(size) {
return setSpText(size)
}
static scaleSize(size) {
return scaleSize(size)
}
static getRemainingimeDistance(distance) {
return getRemainingimeDistance(distance)
}
static toDate(timestamp, format1 = 'yyyy-MM-dd hh:mm:ss') {
return toDate(timestamp, format1)
}
static toTimestamp(date) {
return toTimestamp(date)
}
static getTaskTime(strDate) {
return getTaskTime(strDate)
}
static guid() {
return guid()
}
static Accuracy(num) {
return Accuracy(num)
}
static AccuracyNUm(num) {
return AccuracyNUm(num)
}
static getRemainingimeDistance2(distance) {
return getRemainingimeDistance2(distance)
}
static insertionSort(array) {
return insertionSort(array)
}
static insertionSortJSON(array) {
return insertionSortJSON(array)
}
}