| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- // 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 (
- <Text
- {...rest}
- verticalAnchor="middle"
- textAnchor="middle"
- y={y + (y - cy) / 8}
- x={x + (x - cx) / 5}
- fill="var(--btn-text)"
- >
- {payload.value}
- </Text>
- );
- }
- /**
- * 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)=>{
- 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)=>{
- cash=Number(cash);
- cash=cash/global.CASH_BASE;
- 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;
- }
|