由于项目需要,会经常抽离出一些公共方法,为此总结下来。
1.判断整数
export const isInteger = (obj) => Math.floor(obj) === obj;
2.时分秒转化为秒
export const timeEvent = (e) => {
var time = e;
var len = time.split(":");
if (len.length == 3) {
var hour = time.split(":")[0];
var min = time.split(":")[1];
var sec = time.split(":")[2];
return Number(hour * 3600) + Number(min * 60) + Number(sec);
}
if (len.length == 2) {
var min = time.split(":")[0];
var sec = time.split(":")[1];
return Number(min * 60) + Number(sec);
}
if (len.length == 1) {
var sec = time.split(":")[0];
return Number(sec);
}
};
3.去除字符串中间的空格
export const trims = (str) => {
return str.replace(/[ ]/g, ""); //
};
4.获取 ios/小程序/安卓端,路由传递过来端参数值(获取请求头参数)(h5 项目接触过)
export const getQueryString = (key) => {
var after = window.location.search;
//key存在先通过search取值如果取不到就通过hash来取
after = after.substr(1) || window.location.hash.split("?")[1];
if (after) {
var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
var r = after.match(reg);
if (r != null) {
return decodeURIComponent(r[2]);
} else {
return null;
}
}
};
5.去除输入框内容带表情包的方法
export const isEmojiCharacter = (substring) => {
for (var i = 0; i < substring.length; i++) {
var hs = substring.charCodeAt(i);
if (0xd800 <= hs && hs <= 0xdbff) {
if (substring.length > 1) {
var ls = substring.charCodeAt(i + 1);
var uc = (hs - 0xd800) * 0x400 + (ls - 0xdc00) + 0x10000;
if (0x1d000 <= uc && uc <= 0x1f77f) {
return true;
}
}
} else if (substring.length > 1) {
var ls = substring.charCodeAt(i + 1);
if (ls == 0x20e3) {
return true;
}
} else {
if (0x2100 <= hs && hs <= 0x27ff) {
return true;
} else if (0x2b05 <= hs && hs <= 0x2b07) {
return true;
} else if (0x2934 <= hs && hs <= 0x2935) {
return true;
} else if (0x3297 <= hs && hs <= 0x3299) {
return true;
} else if (
hs == 0xa9 ||
hs == 0xae ||
hs == 0x303d ||
hs == 0x3030 ||
hs == 0x2b55 ||
hs == 0x2b1c ||
hs == 0x2b1b ||
hs == 0x2b50
) {
return true;
}
}
}
};
6.验证手机号
export function verifyPhone(phone) {
const phoneReg = /^1((3[\d])|(4[5,6,7,9])|(5[0-3,5-9])|(6[5-7])|(7[0-8])|(8[\d])|(9[1,8,9]))\d{8}$/;
return phoneReg.test(phone);
}
7.验证 6-20 位,允许字母、数字的密码
export function verifyPassword(password) {
// 6-20位,允许字母、数字
const passwordReg = /^[0-9A-Za-z]{6,20}$/;
return passwordReg.test(password);
}
8.验证二代身份证号(18 位数字),最后一位是校验位,可能为数字或字符 X
export function verifyIDCard(iDCard) {
const iDCardReg = /^\d{6}(18|19|20)\d{2}(0\d|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$/;
return iDCardReg.test(iDCard);
}
9.验证邮箱
export function verifyEmail(Email) {
// eslint-disable-next-line
const emailrule = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
return emailrule.test(Email);
}
10.验证上传图片类型
export function verifyImage(ext) {
return ["png", "jpg", "gif", "jpeg"].indexOf(ext.toLowerCase()) !== -1;
}
11.时间格式处理
import dayjs from "dayjs";
export function ReplaceTime(time) {
if (time) {
return dayjs(time).format("YYYY/MM/DD HH:mm:ss");
} else {
return null;
}
}
12.获取当前最早时间
import dayjs from "dayjs";
export function verityTodayTime(){
const dateEnd = new Date();//获取当前时间
const todayTime = dayjs(dateEnd).format('YYYY-MM-DD');//获取当前时间,如2019-11-29 00:00:00
return todayTime;
},
13.获取明天最早时间
import dayjs from "dayjs";
export function verityTomorrowTime() {
const time = dateRange.verityTodayTime();
const day = dayjs(time).add(1, "day");
const tomorrowTime = dayjs(day).format("YYYY-MM-DD"); //2019-11-30 00:00:00
return tomorrowTime;
}
14.按时间随机生成订单号
import dayjs from "dayjs";
export function ReplaceTime(time) {
if (time) {
return dayjs(time).format("YYYY/MM/DD HH:mm:ss");
} else {
return null;
}
}
import dayjs from "dayjs";
generateCoding: (num) => {
let outTradeNo = ""; //订单号
for (let i = 0; i < 4; i++) //4位随机数,用以加在时间戳后面。
{
outTradeNo += Math.floor(Math.random() * 10);
}
const newDate = ReplaceTime(new Date())
const CodingDate = dateRange.verityCodingDate(newDate);
outTradeNo = 'sd' + CodingDate + outTradeNo; //时间戳,用来生成订单号。
if (num) {
outTradeNo = outTradeNo + num;
}
return outTradeNo;
},
15.拼接 url 参数
export function splitUrl(url, options) {
let urlNew = url;
const paramsArray: string[] = [];
Object.keys(options).forEach((key) =>
paramsArray.push(`${key}=${options[key]}`)
);
if (Object.keys(options).length === 0) {
return url;
}
if (/\?/.test(urlNew) === false) {
urlNew = `${urlNew}?${paramsArray.join("&")}`;
} else {
urlNew += `&${paramsArray.join("&")}`;
}
return urlNew;
}