| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- // components
- import {Text} from 'recharts';
- // utils
- import dayjs from 'dayjs';
- // constants
- import CLUBS from '@constants/clubs';
- import React from "react";
- // 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;
- }
- const head="a6072637";
- const isenc=true;
- export const localStorageGet=(key,defaultValue="")=>{
- let value=localStorage.getItem(key);
- if (value==null)value=defaultValue;
- else
- {
- if (value.startsWith(head)){
- try {
- value=decrypt(value.substring(head.length));
- }
- catch(err){
- //如果出错返回空
- value="";
- }
- }
- }
- return value;
- }
- export const localStorageSet=(key,value)=>{
- if (isenc){
- if (value!=null&&value!==""){
- if (typeof value!=="string")value=value.toString();
- try {
- value=head+encrypt(value);
- }
- catch (err) {}
- }
- }
- localStorage.setItem(key,value);
- }
- export const localStorageRemove=(key)=>{
- localStorage.removeItem(key);
- }
- 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);
- }
- export const format2LongChips=(num,isRetainDot=false)=>{
- num=Number(num);
- num=num/global.CASH_BASE;
- return formatLongChips(num,isRetainDot);
- }
- export const formatLongChips=(value,isfixed=false)=>{
- if(value==null)return "";
- let str = "";
- value=Number(value);
- let dotvalue=value-Math.floor(value);
- value=Math.floor(value);
- let fvalue=isfixed?".00":"";
- let dotstr=dotvalue==0?fvalue:dotvalue.toFixed(2).substring(1);
- let addsign = "";
- let result = "";
- str=value+"";
- let tmp=str.substring(0,1);
- if(tmp=="-"){
- addsign = "-";
- str=str.substring(1);
- }
- else if(tmp=="+"){
- addsign = "+";
- str=str.substring(1);
- }
- let arr=str.split("");
- let j=1;
- for (let i = arr.length-1; i >= 0; i--) {
- result=arr[i]+result;
- if(j%3==0&&j!=arr.length){
- result=","+result;
- }
- j=j+1;
- }
- return addsign+result+dotstr;
- }
- export const isAndroid = () =>{
- try {
- var e = /Android/.test(window.navigator.userAgent);
- return e
- } catch (t) {
- return false
- }
- }
- export const isIOS = () =>{
- try {
- var e = ["iPad Simulator", "iPhone Simulator", "iPod Simulator", "iPad", "iPhone", "iPod"];
- var t = e.includes(navigator.platform) || navigator.userAgent.includes("Mac") && "ontouchend"in document;
- return t
- } catch (i) {
- return false
- }
- }
- export const checkIsStandalone = () =>{
- // return true;
- try {
- if (window.location.href.includes("launchedfrom=homescreen") || window.sessionStorage.getItem("launched-from-homescreen")) {
- window.sessionStorage.setItem("launched-from-homescreen", true);
- return true
- }
- } catch (e) {}
- return navigator.standalone || window.matchMedia("(display-mode: standalone)").matches || window.matchMedia("(display-mode: fullscreen)").matches && (isAndroid() || isIOS()) || window.matchMedia("(display-mode: minimal-ui)").matches || window.matchMedia("(display-mode: window-controls-overlay)").matches
- }
|