import { getLogger } from "./log" ; const logger = getLogger ( "util" ) ; param { * } asyncFunc export function singleInstancePromise ( asyncFunc ) { let promiseInstance = undefined ; return function ( this , ... args ) { if ( promiseInstance ) { return promiseInstance ; } promiseInstance = asyncFunc . bind ( this ) ( ... args ) . then ( resp => { promiseInstance = undefined ; return Promise . resolve ( resp ) ; } ) . catch ( e => { promiseInstance = undefined ; return Promise . reject ( e ) ; } ) ; return promiseInstance ; } ; } export function getLaunchOptions ( ) { try { const options = wx . getLaunchOptionsSync ( ) ; return options ; } catch ( error ) { logger . ERROR ( "getLaunchOptions error" , error ) ; return { } as any ; } } export function getRandomId ( ) { return ( Date . now ( ) . toString ( 32 ) + Math . random ( ) . toString ( 32 ) . substring ( 2 ) ) ; } param { object } obj export function objectCopy ( obj ) { if ( typeof obj == "undefined" ) { return obj ; } return JSON . parse ( JSON . stringify ( obj ) ) ; } param { object } obj export function addUrlParams ( url = "" , params ) { url += url . indexOf ( "?" ) >= 0 ? "&" : "?" ; url += Object . keys ( params ) . map ( key => { return `${ key }=${ params [ key ] }` ; } ) . join ( "&" ) ; return url ; } param { object } obj export function encodeJSON ( obj ) { try { return encodeURIComponent ( JSON . stringify ( obj ) ) ; } catch ( error ) { logger . ERROR ( "encodeJSON error" , error ) ; return "" ; } } param { string } str export function decodeJSON ( str ) { try { return JSON . parse ( decodeURIComponent ( str ) ) ; } catch ( error ) { logger . ERROR ( "decodeJSON error" , error ) ; return "" ; } }