// components import {Text} from 'recharts'; // utils import dayjs from 'dayjs'; // constants import CLUBS from '@constants/clubs'; // represent large numbers in a shortened format export const numFormatter = (num, decimals = 2) => { num=Number(num); if (num > 999 && num < 1000000) { return (num / 1000).toFixed(decimals) + 'K'; } else if (num > 1000000) { return (num / 1000000).toFixed(decimals) + 'M'; } else if (num <= 999) { return num.toFixed(decimals); } } // random integer generator export const getRandomInt = (min, max) => { return Math.floor(Math.random() * (max - min + 1)) + min; } // add zero to numbers less than 10 export function addZero(num) { return num < 10 ? '0' + num : num; } // grid y-axis or x-axis points generator for recharts /** * * @param id - container id * @param gutter - grid gutter * @param axis - 'x' or 'y' * @returns {*[]} - array of grid points */ export const generateGridPoints = (id, gutter = 20, axis = 'y') => { const gridWidth = document.getElementById(id).offsetWidth; const gridHeight = document.getElementById(id).offsetHeight; let points = []; for (let i = 0; i < (axis === 'y' ? gridWidth : gridHeight); i += gutter) { points.push(i); } return points; } // prevent default behavior for all forms and links with href="#" export const preventDefault = () => { document.querySelectorAll('form').forEach(form => { form.addEventListener('submit', e => e.preventDefault()); }); document.querySelectorAll('a[href="#"]').forEach(a => { a.addEventListener('click', e => e.preventDefault()); }); } // get club info by id /** @param id - club id * * @param id * @returns {{country: string, color: string, city: string, name: string, logo: *, id: string, shortName: string} | {country: string, color: string, city: string, name: string, logo: *, id: string, shortName: string} | {country: string, color: string, city: string, name: string, logo: *, id: string, shortName: string} | {country: string, color: string, city: string, name: string, logo: *, id: string, shortName: string} | {country: string, color: string, city: string, name: string, logo: *, id: string, shortName: string}} */ export const getClubInfo = (id) => CLUBS.find((club) => club.id === id); // get array of month days (date and weekday) export const getMonthDays = (month = dayjs().month(), year = dayjs().year()) => { const days = []; const daysInMonth = dayjs(`${year}-${month + 1}`).daysInMonth(); for (let i = 1; i <= daysInMonth; i++) { days.push({ date: dayjs(`${year}-${month + 1}-${i}`).format('DD'), weekday: dayjs(`${year}-${month + 1}-${i}`).format('dd') }); } return days; } /** * render polar angle axis * @param payload * @param x * @param y * @param cx * @param cy * @param rest * @returns {JSX.Element} */ export const renderPolarAngleAxis = ({payload, x, y, cx, cy, ...rest}) => { return ( {payload.value} ); } /** * modify card number * @param cardNumber * @returns {string} */ export const modifyCardNumber = (cardNumber) => { const lastDigits = cardNumber.substring(8); const maskedDigits = "**** "; // Combine the modified parts to form the final card number const modifiedCardNumber = maskedDigits + lastDigits.slice(-4); return modifiedCardNumber; } export const localStorageGet=(key,defaultValue="")=>{ let value=localStorage.getItem(key); if (value==null)value=defaultValue; return value; } export const localStorageSet=(key,value)=>{ localStorage.setItem(key,value); } export const getHttpMsg=(data)=>{ if (data==null)return "" let result=data.result; if (result==null)result=data.msg; let msg=""; if (result!=null){ let key=result[0]; let def=result[1]; if (global.i18n.exists(key))msg=global.i18n.t(key); else msg=def; } return msg; } export const sleep=async (time)=>{ return new Promise((resolve, reject) => { setTimeout(() => { resolve(true); }, time); }); } export const stringFormat=(str,args)=>{ if (str==null)return str; if (args==null)args=[]; return str.replace(/{(\d+)}/g, function(match, number) { return typeof args[number] != 'undefined' ? args[number] : match ; }); } export const formatDate=(timestamp,format=null)=>{ let date=new Date(timestamp); let year = `${date.getFullYear()}`; let month = `${date.getMonth() + 1}`; if (month.length === 1) { month = `0${month}`; } let day = `${date.getDate()}`; if (day.length === 1) { day = `0${day}`; } let hours = `${date.getHours()}`; if (hours.length === 1) { hours = `0${hours}`; } let minutes = `${date.getMinutes()}`; if (minutes.length === 1) { minutes = `0${minutes}`; } let seconds = `${date.getSeconds()}`; if (seconds.length === 1) { seconds = `0${seconds}`; } return (format || "yyyy-MM-dd hh:mm:ss") .replace(/yyyy/g, year) .replace(/MM/g, month) .replace(/dd/g, day) .replace(/hh/g, hours) .replace(/mm/g, minutes) .replace(/ss/g, seconds); } export const formatTime=(time,isms=false)=>{ if (time==null)return ""; let seconds=time; //毫秒 if(isms)seconds=Math.floor(time/1000); let hours = Math.floor(seconds / 3600); let minutes = Math.floor((seconds % 3600) / 60); let remainingSeconds = seconds % 60; let formattedTime = `${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}:${String(remainingSeconds).padStart(2, '0')}`; return formattedTime; } export const openFullScreen=()=>{ var elem = document.documentElement; // 获取整个文档的根元素 if (elem.requestFullscreen) { elem.requestFullscreen(); } else if (elem.mozRequestFullScreen) { elem.mozRequestFullScreen(); } else if (elem.webkitRequestFullscreen) { elem.webkitRequestFullscreen(); } else if (elem.msRequestFullscreen) { elem.msRequestFullscreen(); } } export const validateBRPhone=(t)=>{ if (t && 11 === t.length) { var e = parseInt(t.substring(0, 2), 10) , i = parseInt(t.charAt(2), 10); return e >= 11 && e <= 99 && 9 == i } return false; } export const validateCountryPhone=(country,phone)=>{ return validateBRPhone(phone); } export const formatCash=(cash,fixed=2,isIntIgnore=false)=>{ cash=Number(cash); cash=cash/global.CASH_BASE; if (isIntIgnore){ let temp=parseInt(cash); if (temp===cash){ fixed=0; } } return cash.toFixed(fixed); } export const getUrlParam=(name)=>{ if (window.location==null||window.location.href==null)return null; let url = window.location.href; return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(url) || [, ""])[1].replace(/\+/g, '%20')) || null; } //简单加密 export const encrypt=(str, pwd)=>{ if(str == '') { return ''; } str = encodeURIComponent(str); if(!pwd || pwd == '') { pwd = 'xbd4jl'; } pwd = encodeURIComponent(pwd); if(pwd == '' || pwd.length <= 0) { return ''; } var prand= ''; for(var i = 0, len = pwd.length; i < len; i += 1) { prand += pwd.charCodeAt(i).toString(); } var sPos = Math.floor(prand.length / 5); var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) + prand.charAt(sPos * 4) + prand.charAt(sPos * 5)); var incr = Math.ceil(pwd.length / 2); var modu = Math.pow(2, 31) - 1; if(mult < 2) { return ''; } var salt = Math.round(Math.random() * 1000000000) % 100000000; prand += salt; while(prand.length > 10) { let left=prand; let num=0; while(left.length>0){ let temp=parseInt(left.substring(0, 10)); num=num+temp; left=left.substring(10,left.length); } prand=num.toString(); } prand = (mult * prand + incr) % modu; var encChr= ''; var encStr = ''; for(var i = 0, len = str.length; i < len; i += 1) { encChr = parseInt(str.charCodeAt(i) ^ Math.floor((prand / modu) * 255)); if(encChr < 16) { encStr += '0' + encChr.toString(16); }else{ encStr += encChr.toString(16); } prand = (mult * prand + incr) % modu; } salt = salt.toString(16); while(salt.length < 8) { salt = "0" + salt; } encStr += salt; return encStr; } //简单解密 export const decrypt=(str, pwd)=>{ if(str == ''){ return ''; } if(!pwd || pwd == ''){ pwd = 'xbd4jl'; } pwd = encodeURIComponent(pwd); if(str == undefined || str.length < 8) { return ''; } if(pwd == undefined || pwd.length <= 0) { return ''; } var prand = ''; for(var i = 0, len = pwd.length; i < len; i += 1) { prand += pwd.charCodeAt(i).toString(); } var sPos = Math.floor(prand.length / 5); // console.log(prand.length+"sPos____"+sPos+"prand"+prand); var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) + prand.charAt(sPos * 4) + prand.charAt(sPos * 5)); var incr = Math.round(pwd.length / 2); var modu = Math.pow(2, 31) - 1; var salt = parseInt(str.substring(str.length - 8, str.length), 16); str = str.substring(0, str.length - 8); // console.log("prand"+prand); prand += salt; // console.log("prand"+prand); while(prand.length > 10) { let left=prand; let num=0; while(left.length>0){ let temp=parseInt(left.substring(0, 10)); num=num+temp; left=left.substring(10,left.length); } prand=num.toString(); } // console.log("prand"+prand); prand = (mult * prand + incr) % modu; var encChr= ''; var encStr= ''; for(var i = 0, len = str.length; i < len; i += 2) { encChr = parseInt(parseInt(str.substring(i, i + 2), 16) ^ Math.floor((prand / modu) * 255)); encStr += String.fromCharCode(encChr); prand = (mult * prand + incr) % modu; } return decodeURIComponent(encStr); }