import MP from '../mp' ; import { API , core , Event , Integration , IPerformance , Resource , utils } from '../shared' ; import { SDK_NAME , SDK_VERSION } from '../version' ; const { uuid4 , logger } = utils ; const { getCurrentHub , addGlobalEventProcessor } = core ; let uuid ; function singleUUID ( ) { if ( uuid ) return uuid ; return ( uuid = uuid4 ( ) ) ; } function parseData < T > ( data = [ ] ) { return data . map ( ( item ) => ( { eventId : uuid4 ( ) , ... item , timestamp : Date . now ( ) , } ) ) ; } export class Common implements Integration { inheritDoc public static id : string = 'Common' ; inheritDoc public name : string = Common . id ; inheritDoc public setupOnce = ( ) : void => { const client = getCurrentHub ( ) . getClient ( ) || { getOptions ( ) { return { projectId : '' , } ; } , } ; const { projectId } = client . getOptions ( ) ; addGlobalEventProcessor ( ( event ) => { try { event . projectId = projectId ; event . sdk = { name : SDK_NAME , version : SDK_VERSION , } ; let mpInstance = MP . instance ( ) ; const systemInfo = mpInstance . systemInfo || '' ; const networkInfo = mpInstance . networkInfo || '' ; let sceneInfo = mpInstance . sceneInfo || { } ; const userInfo = mpInstance . userInfo || '' ; var pageParams = mpInstance . pageParams || { } ; if ( mpInstance . currentPage === pageParams . url ) { sceneInfo = Object . assign ( sceneInfo , { pageParams : pageParams } ) ; } event . request = { url : mpInstance . currentPage , headers : { appName : mpInstance . appName , appVersion : systemInfo . version , appSDKVersion : systemInfo . SDKVersion , imei : singleUUID ( ) , uuid : singleUUID ( ) , pid : singleUUID ( ) , os : systemInfo . system . split ( ' ' ) [ 0 ] , osv : systemInfo . system . split ( ' ' ) [ 1 ] , device : systemInfo . model , brand : systemInfo . brand , benchmarkLevel : systemInfo . benchmarkLevel , host : systemInfo . host , apn : networkInfo !== '' ? networkInfo . networkType : '' , scene : sceneInfo !== '' ? sceneInfo . scene : '' , systemInfo : encodeURIComponent ( JSON . stringify ( systemInfo ) ) , userInfo : encodeURIComponent ( JSON . stringify ( userInfo ) ) , networkInfo : encodeURIComponent ( JSON . stringify ( networkInfo ) ) , sceneInfo : encodeURIComponent ( JSON . stringify ( sceneInfo ) ) , } , } ; event . apis && ( event . apis = parseData < API > ( event . apis ) ) ; event . resources && ( event . resources = parseData < Resource > ( event . resources ) ) ; event . performances && ( event . performances = parseData < IPerformance > ( event . performances ) ) ; } catch ( e ) { logger . warn ( 'get commom error ' ) ; } return event ; } ) ; } ; }