在各种项目的开发,有许多公共的方法是通用的,放入utils中,直接引用十分的方便!
1、(app)版本号对比
const getSum = (total, num) => total + num;
export const compareVersion = (s1, s2) => { // s1当前版本,s2需对比的版本
let version = true;
const a = s1.split('.').reduce(getSum);
const b = s2.split('.').reduce(getSum);
if (a > b) {
version = true;
} else {
version = false;
}
return version;
};
2、获取当前平台(android、iOS)
export function getPlatform() {
let platform = false;
const system = window.navigator.appVersion.toLocaleLowerCase();
if (system.indexOf('iphone; cpu iphone') > 0) {
platform = true;
}
return platform; // ios返回true
}
3、获取链接指定参数
export function getUrlParameter(parameterName, str) {
const reg = new RegExp(`(^|&|\\?)${parameterName}=([^&]*)(&|$)`, 'i');
let arr;
let arr1;
if (str) {
arr = str.match(reg);
arr1 = str.match(reg);
} else {
arr = window.location.search.substr(1).match(reg);
arr1 = window.location.hash.substr(2).match(reg);
}
if (arr) {
return arr[2];
} else if (arr1) {
return arr1[2];
}
return null;
}
4、时间转换(解决iphone兼容问题)
export const formatDate = (time) => {
const Time = new Date(time.replace(/-/g, '/'));
const date = new Date(Time);
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
// const hour = date.getHours();
// const minute = date.getMinutes();
const addO = str => (str <= 9 ? `0${str}` : str);
// const second = date.getSeconds();
return `${year}-${addO(month)}-${addO(day)}`;
};
5、日期区间的间隔天数
export function restTime(nowDate,date) {
const setTime = new Date(date.replace(/-/g, '/'));
const nowTime = new Date(nowDate.replace(/-/g, '/'));
const restSec = setTime.getTime() - nowTime.getTime();
const day = parseInt(Math.ceil((restSec / (60 * 60 * 24 * 1000))), 10);
return day;
}
6、转换单位(万、亿)
export const getNumText = (num, isPre = false) => {
const number = Math.abs(num);
let text = number;
if (number >= 100000000) {
text = `${(number / 100000000).toFixed(1)}亿`;
} else if (number < 100000000 && number >= 10000) {
text = `${(number / 10000).toFixed(1)}万`;
} else {
text = number;
}
if (num < 0 && isPre) text = `-${text}`;
return text;
};
7、千位数加逗号
export const formatNum(num) {
var n = []
num = num.toString()
num = num.split('')
num.map((item, index) => {
if((index+1)%3 == 0) {
n.push(',')
}
n.push(item)
})
return n.join('')
}
8、日期格式化
export const formatTime = date => {
var date = new Date(date)
const year = date.getFullYear()
const month = ("0" + (date.getMonth() + 1)).slice(-2)
const day = ("0" + date.getDate()).slice(-2)
const hour = ("0" + date.getHours()).slice(-2)
const minute = ("0" + date.getMinutes()).slice(-2)
const second = ("0" + date.getSeconds()).slice(-2)
return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
9、阿拉伯数字转汉语
export const sectionToChinese = (section) => {
var chnNumChar = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
var chnUnitChar = ["", "十", "百", "千", "万", "亿", "万亿", "亿亿"];
var strIns = '',
chnStr = '';
var unitPos = 0;
var zero = true;
while (section > 0) {
var v = section % 10;
if (v === 0) {
if (!zero) {
zero = true;
chnStr = chnNumChar[v] + chnStr;
}
} else {
zero = false;
strIns = chnNumChar[v];
strIns += chnUnitChar[unitPos];
chnStr = strIns + chnStr;
}
unitPos++;
section = Math.floor(section / 10);
}
return chnStr;
}
未完待续...❤