export function randomBoolean() {
return Math.random() >= 0.5
}
export function isWeek(date) {
return date.getDay()%6 !== 0
}
export function reverse(str) {
return str.split('').reverse().join('')
}
export function isEven(num) {
return num % 2 === 0
}
export function timeFromDate(date) {
return date.toTimeString().slice(0, 8)
}
export function toFixed(n, fixed) {
return ~~(Math.pow(10, fixed) * n) / Math.pow(10, fixed)
}
export function touchSupported() {
return ('ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch)
}
export function isAppleDevice() {
return /Mac|iPod|iPhone|iPad/.test(navigator.platform)
}
export function goToTop() {
window.scrollTo(0, 0)
}
export function average(...args) {
return args.reduce((a, b) => a + b) / args.length
}
export function celsiusToFahrenheit(celsius) {
return celsius * 9/5 + 32
}
export function fahrenheitToCelsius(fahrenheit) {
return (fahrenheit - 32) * 5/9
}
export function moneyFormatter(money){
if(!money){
return ''
}else{
let val = parseFloat(money) || 0
let newVal = parseFloat(val/100)
let forMateVal = newVal.toFixed(2)
return forMateVal
}
}
export function getDate(){
let date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
if (month < 10) {
month = "0" + month;
}
if (day < 10) {
day = "0" + day;
}
let nowDate = year + "-" + month + "-" + day;
return nowDate
}
export function getMonthDayFirst() {
let nowdays = new Date();
let year = nowdays.getFullYear();
let month = nowdays.getMonth();
let day = nowdays.getDate()
if (day != 1) {
return getCurrentMonthFirst()
}
if (month == 0) {
month = 12;
year = year - 1;
}
if (month < 10) {
month = '0' + month;
}
let myDate = new Date(year, month, 0);
let startDate = year + '-' + month + '-01';
return startDate
}
export function exportDateHand(blobVal, fileName = '数据导出') {
let blob = blobVal;
let reader = new FileReader();
reader.readAsDataURL(blob);
reader.onload = e => {
let a = document.createElement("a");
a.download = fileName;
a.href = e.target.result;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
};
}
export function createColorCode() {
return '#' + Math.floor(Math.random() * 16777215).toString(16);
}
export function getDeviceOSType() {
let _return = "pc";
let ua = navigator.userAgent.toLowerCase();
if (/iphone|ipad|ipod/.test(ua)) {
_return = "ios"
}
else if (/android/.test(ua)) {
_return = "android"
}
return _return;
}
export function stopEventPropagation() {
if (event.stopPropagation) {
event.stopPropagation();
}
else if (window.event) {
window.event.cancelBubble = true;
}
}
export function clone(obj) {
let newObj;
switch (typeof obj) {
case 'undefined':
break;
case 'string':
newObj = obj + '';
break;
case 'number':
newObj = obj - 0;
break;
case 'boolean':
newObj = obj;
break;
case 'object':
if (obj === null) {
newObj = null;
}
else {
newObj = JSON.parse(JSON.stringify(obj));
}
break;
default:
newObj = obj;
break;
}
return newObj;
}
export function strToUtf8(text) {
var out, i, len, c;
out = "";
len = text.length;
for (i = 0; i < len; i++) {
c = text.charCodeAt(i);
if ((c >= 0x0001) && (c <= 0x007F)) {
out += text.charAt(i);
}
else if (c > 0x07FF) {
out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
}
else {
out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
}
}
return out;
}
export function toTrim(x) {
return x.replace(/^\s+|\s+$/gm,'');
}
export function setCookie(name, value, iDay) {
let oDate = new Date();
oDate.setDate(oDate.getDate() + iDay);
document.cookie = name + '=' + value + ';expires=' + oDate;
}
export function getCookie(name) {
let arr = document.cookie.split('; ');
for (var i = 0; i < arr.length; i++) {
var arr2 = arr[i].split('=');
if (arr2[0] == name) {
return arr2[1];
}
}
return '';
}
export function removeCookie(name) {
setCookie(name, 1, -1);
}
export function removeRepeatArray(arr) {
return Array.from(new Set(arr))
}
export function upsetArr(arr) {
return arr.sort(function() { return Math.random() - 0.5 })
}
export function compareArr(arr,type) {
if (type === 1) {
return Math.max.apply(null, arr);
} else {
return Math.min.apply(null, arr);
}
}
export function getEndTime(endTime) {
let startDate = new Date();
let endDate = new Date(endTime);
let t = endDate.getTime() - startDate.getTime();
let d = 0,
h = 0,
m = 0,
s = 0;
if (t >= 0) {
d = Math.floor(t / 1000 / 3600 / 24);
h = Math.floor(t / 1000 / 60 / 60 % 24);
m = Math.floor(t / 1000 / 60 % 60);
s = Math.floor(t / 1000 % 60);
}
return "剩余时间" + d + "天 " + h + "小时 " + m + " 分钟" + s + " 秒";
}
export function getUUID() {
return 'xxxxxxxxxxxx4xyx'.replace(/[xy]/g, c => {
return (c === 'x' ? (Math.random() * 16 | 0) : ('r&0x3' | '0x8')).toString(16)
})
}
function Encrypt(word, rand) {
var key = CryptoJS.enc.Utf8.parse(rand);
var srcs = CryptoJS.enc.Utf8.parse(word);
var encrypted = CryptoJS.AES.encrypt(srcs, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString();
}
function Decrypt(word, rand) {
var key = CryptoJS.enc.Utf8.parse(rand);
var decrypt = CryptoJS.AES.decrypt(word, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}
export function getEncryptData(params, rand, type = 'Object') {
var paramStr = ''
if (type == 'Object') {
paramStr = JSON.stringify(params)
} else {
paramStr = params
}
var data = Encrypt(paramStr, rand);
var encrypt = new JSEncrypt();
var publickKey = store.state.user.publicKey
encrypt.setPublicKey(publickKey);
var encrypted = encrypt.encrypt(rand);
var json = {
"requestData": data,
"encrypted": encrypted
};
return json
}
export function getDecryptData(word, rand, type = 'Object') {
var decData = ''
try {
decData = Decrypt(word, rand)
} catch (e) { }
if (decData && type == 'Object') {
return JSON.parse(decData)
} else {
return decData
}
}
export function getSimpDecryptData(word, type = 'Object') {
var rand = store.state.user.simpDecryptKey
var decData = ''
try {
decData = Decrypt(word, rand)
} catch (e) { }
if (decData && type == 'Object') {
return JSON.parse(decData)
} else {
return decData
}
}