examTeamApp/tools/tools.js

179 lines
4.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import $store from '@/store'
export default {
msg,
gethms,
str2hex,
hex2str,
ab2hex,
inArray,
getMonth,
getDate,
GetDateStr,
PrefixZero,
validatePhoneEmail,
}
// 2进制位数不足补0
function PrefixZero(num, n) {
return (Array(n).join(0) + num).slice(-n);
}
function inArray(arr, key, val) {
if (!arr || !arr.length || typeof arr != 'object' || !Array.isArray(arr)) {
return -1
}
for (let i = 0; i < arr.length; i++) {
if (!key) {
if (arr[i] == val) {
return i
}
} else if (arr[i][key] === val) {
return i
}
}
return -1;
}
function validatePhoneEmail(input) {
const phoneRegex = /^(\+?\d{1,4})?[-\s.]?\(?(\d{3})\)?[-\s.]?(\d{3})[-\s.]?(\d{4})$/;
const emailRegex = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
const isPhone = phoneRegex.test(input);
const isEmail = emailRegex.test(input);
return {
isPhone,
isEmail
};
}
function msg(str) {
uni.showToast({
title: str,
icon: 'none'
})
}
function ab2hex(buffer, split) {
var hexArr = Array.prototype.map.call(
new Uint8Array(buffer),
function(bit) {
return ('00' + bit.toString(16)).slice(-2)
}
)
return hexArr.join(split);
}
function hex2str(arr) {
let decoder = new TextDecoder('utf8')
let uint8 = new Uint8Array(arr)
let res = decoder.decode(uint8)
return res
}
function str2hex(str) {
let encoder = new TextEncoder('utf8')
return encoder.encode(str)
}
// 跳绳分秒时间选择
function gethms(type) {
var mindata = []
var secondData = []
let timeList = []
for (var i = 1; i <= 59; i++) {
i = i > 9 ? i : '0' + i
mindata.push(i + '分');
}
for (var i = 0; i <= 59; i++) {
i = i > 9 ? i : '0' + i
secondData.push(i + '秒');
}
timeList[0] = mindata
timeList[1] = secondData
return timeList
}
function getMonth(dates, months) {
var d = new Date(dates.substring(0, 10));
let year = d.getFullYear();
var month = d.getMonth() + 1;
if (Math.abs(months) > 12) {
months = months % 12;
};
if (months != 0) {
if (month + months > 12) {
year++;
month = (month + months) % 12;
} else if (month + months < 1) {
year--;
month = 12 + month + months;
} else {
month = month + months;
};
};
month = month < 10 ? "0" + month : month;
var date = d.getDate();
if (month == "01" || month == "03" || month == "05" || month == "07" || month == "08" || month == "10" ||
month == "12") {
return year + "/" + month + "/01" + "~" + year + "/" + month + "/31";
} else if (month == "02") {
if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) {
return year + '/' + month + "/01" + "~" + year + "/" + month + "/29";
} else {
return year + '/' + month + "/01" + "~" + year + "/" + month + "/28";
};
} else {
return year + '/' + month + "/01" + "~" + year + "/" + month + "/30";
};
};
function getDate(type) {
const date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
month = month > 9 ? month : '0' + month;
day = day > 9 ? day : '0' + day;
if (type === 'tow') {
year = year - 2;
return `${year}/${month}/${day}`;
}
if (type === 'start') {
year = year;
return `${year}/${month}/${day}`;
}
if (type === 'end') {
year = year + 60;
return `${year}/${month}/${day}`;
}
if (type === 'year') {
year = year;
return `${year}`;
}
if (type === 'month') {
year = year;
return `${year}/${month}`;
}
if (type == "m") {
if (month == "01" || month == "03" || month == "05" || month == "07" || month == "08" || month == "10" ||
month == "12") {
return year + "/" + month + "/01" + "~" + year + "/" + month + "/31";
} else if (month == "02") {
if ((year % 4 == 0 && year % 100 != 0) || (year % 100 == 0 && year % 400 == 0)) {
return year + "/" + month + "/01" + "~" + year + "/" + month + "/29";
} else {
return year + "/" + month + "/01" + "~" + year + "/" + month + "/28";
};
} else {
return year + "/" + month + "/01" + "~" + year + "/" + month + "/30";
};
}
}
//获取AddDayCount天后的日期
function GetDateStr(AddDayCount) {
var dd = new Date();
dd.setDate(dd.getDate() + AddDayCount);
var y = dd.getFullYear();
var m = (dd.getMonth() + 1) < 10 ? "0" + (dd.getMonth() + 1) : (dd.getMonth() + 1); //获取当前月份的日期不足10补0
var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate(); //获取当前几号不足10补0
return y + "/" + m + "/" + d;
}