import { SrcConfig } from '@/models/user' ; import moment from 'moment' ; import { SITE_BASIC_INFO } from './constant' ; moment . lang ( 'zh-cn' ) ; interface Ref { current ? : { getValue ? : Function ; } ; } export function delay ( time ) { return new Promise ( ( ok ) => { setTimeout ( ( ) => { ok ( undefined ) ; } , time * 1000 ) ; } ) ; } export function getParam ( search , key ) { try { if ( ! search || ! key ) return null ; const url = search . slice ( 1 ) ; const chunks = url . split ( '&' ) ; for ( const item of chunks ) { const [ k , value ] = item . split ( '=' ) ; if ( k === key ) return value || undefined ; } return '' ; } catch ( error ) { return '' ; } } export function setCache ( key , data ) { try { window . localStorage . setItem ( key , data instanceof Object ? JSON . stringify ( data ) : data ) ; } catch ( error ) { } } export function getCache ( key ) { const str = window . localStorage . getItem ( key ) ; if ( str ) { try { return JSON . parse ( str ) ; } catch ( error ) { return str ; } } return null ; } export function hasChange ( data1 , data2 ) { return JSON . stringify ( data1 ) !== JSON . stringify ( data2 ) ; } export function relativeTime ( time ) { if ( isNaN ( time ) ) { return moment ( time ) . startOf ( 'minute' ) . fromNow ( ) ; } return moment ( new Date ( Number ( time ) ) ) . startOf ( 'minute' ) . fromNow ( ) ; } export function resetObj ( obj ) { Object . keys ( obj ) . forEach ( ( key ) => ( obj [ key ] = '' ) ) ; return obj ; } export function qqSign ( ) { try { window . QC . Login . showPopup ( { appId : '101946967' , redirectURI : 'https://sites.link/qq-login' , } ) ; } catch ( error ) { } } export function getValueByRef ( ref ) { if ( ref . current ) { return ref . current . getValue ( ) ; } return null ; } export function stringify ( obj ) { if ( ! obj ) return '' ; let result = '' ; Object . keys ( obj ) . forEach ( ( key , index ) => { const prefix = index === 0 ? '' : '&' ; result += `${ prefix }${ key }=${ obj [ key ] }` ; } ) ; return result ; } function setTime ( str ) { if ( `${ str }` . length === 1 ) return `0${ str }` ; return str ; } export function formatTime ( time ) { const date = new Date ( time ) ; const year = date . getFullYear ( ) ; const month = `${ date . getMonth ( ) + 1 }` ; const day = date . getDate ( ) + '' ; const hour = date . getHours ( ) + '' ; const minu = date . getMinutes ( ) + '' ; return `${ year }-${ setTime ( month ) }-${ setTime ( day ) } ${ setTime ( hour ) }:${ setTime ( minu ) }` ; } export function getURL2Base64 ( url ) { return new Promise ( ( resolve , reject ) => { const xhr = new XMLHttpRequest ( ) ; xhr . open ( 'get' , url , true ) ; xhr . responseType = 'blob' ; xhr . onload = function ( ) { if ( this . status === 200 ) { const blob = this . response ; const fileReader = new FileReader ( ) ; fileReader . onloadend = function ( e ) { const result = e ?. target ?. result ; resolve ( result ) ; } ; fileReader . readAsDataURL ( blob ) ; } } ; xhr . onerror = function ( ) { reject ( ) ; } ; xhr . send ( ) ; } ) ; } const { TITLE , DESC } = SITE_BASIC_INFO ; export function updTitleDesc ( title = '' , desc ) { document . title = `${ title } ${ TITLE }` ; const { location : { pathname } , } = window ; const newDesc = pathname === '/' ? DESC : desc ; const meta = document . querySelector ( 'meta[name="Description"]' ) ; meta ?. setAttribute ( 'content' , newDesc ) ; } export function push2Baidu ( ) { const bp = document . createElement ( 'script' ) ; const curProtocol = window . location . protocol . split ( ':' ) [ 0 ] ; if ( curProtocol === 'https' ) { bp . src = 'https://zz.bdstatic.com/linksubmit/push.js' ; } else { bp . src = 'http://push.zhanzhang.baidu.com/push.js' ; } const s = document . getElementsByTagName ( 'script' ) [ 0 ] ; s ?. parentNode ?. insertBefore ( bp , s ) ; } export function sliceNumber ( num ) { if ( num . toString ( ) . length > 1 ) return num ; return `0${ num }` ; } description param { string } src returns { width export function loadImgSize ( src ) { const img = new Image ( ) ; img . src = src ; return new Promise ( ( resolve , reject ) => { img . onload = ( e ) => { const { path } = e ; const [ { naturalHeight , naturalWidth } ] = path ; resolve ( { width : naturalWidth , height : naturalWidth , ratio : naturalWidth / naturalHeight , } ) ; } ; img . onerror = reject ; } ) ; } export function simpleThrole < T extends ( ... arg : any [ ] ) => any > ( fn , delay = 100 ) { let preTime = Date . now ( ) ; return ( ... arg ) => { if ( Date . now ( ) - preTime >= delay ) { fn . call ( this , ... arg ) ; preTime = Date . now ( ) ; } } ; } export function randomNum ( length ) { return Math . floor ( Math . random ( ) * length ) ; } param configs export function getSrcConfig ( configs ) { const { pathname } = location ; const config = configs . find ( ( item ) => item . pathname === pathname ) || { } ; return config ; } export function simpleLoadImg ( src ) { return new Promise ( ( resolve , reject ) => { const img = new Image ( ) ; img . src = src ; img . onload = ( ) => { resolve ( true ) ; } ; img . onerror = ( ) => { reject ( false ) ; } ; } ) ; }