import { getLogger } from "./log" ; import wxp from "./wxp" ; import { objectCopy , addUrlParams , getRandomId } from "./util" ; const logger = getLogger ( "navigate" ) ; function matchOriginPath ( originPageUrl ) { let currentPages = getCurrentPages ( ) ; const currentPage = currentPages [ currentPages . length - 1 ] . route ; const isMatch = ! originPageUrl || ( originPageUrl && currentPage . indexOf ( originPageUrl ) > - 1 ) ; if ( ! isMatch ) { logger . ERROR ( "matchOriginPath do not match" , `currentPage: ${ currentPage }, originPageUrl: ${ originPageUrl }` ) ; } return isMatch ; } let globalPageParams = undefined ; let globalPageParamsId = undefined ; interface IOptions { urlParams ? : object ; pageParams ? : object ; } function mangeUrl ( url , options = { } ) { const { urlParams , pageParams } = options ; if ( urlParams ) { url = addUrlParams ( url , urlParams ) ; } if ( pageParams ) { globalPageParams = objectCopy ( pageParams ) ; globalPageParamsId = getRandomId ( ) ; url = addUrlParams ( url , { randomid : globalPageParamsId } ) ; } else { globalPageParams = undefined ; globalPageParamsId = undefined ; } return url ; } param { object } url param { object } options param { object } originPageUrl export async function navigateTo ( url , options = { } , originPageUrl ? ) { url = mangeUrl ( url , options ) ; if ( ! matchOriginPath ( originPageUrl ! ) ) { logger . RUN ( "navigateTo" , "originPageUrl != currentPage, return" ) ; return Promise . resolve ( ) ; } console . log ( url ) ; return await wxp . navigateTo ( { url } ) ; } param { object } url param { object } options param { object } originPageUrl export async function redirectTo ( url , options = { } , originPageUrl ? ) { url = mangeUrl ( url , options ) ; if ( ! matchOriginPath ( originPageUrl ! ) ) { logger . RUN ( "redirectTo" , "originPageUrl != currentPage, return" ) ; return Promise . resolve ( ) ; } return await wxp . redirectTo ( { url } ) ; } param { number } delta param { object } originPageUrl export async function navigateBack ( delta = 1 , originPageUrl ? ) { if ( ! matchOriginPath ( originPageUrl ! ) ) { logger . RUN ( "navigateBack" , "originPageUrl != currentPage, return" ) ; return Promise . resolve ( ) ; } return await wxp . navigateBack ( { delta } ) ; } param { object } url param { object } options param { object } originPageUrl export async function reLaunch ( url , options = { } , originPageUrl ? ) { url = mangeUrl ( url , options ) ; if ( ! matchOriginPath ( originPageUrl ! ) ) { logger . RUN ( "reLaunch" , "originPageUrl != currentPage, return" ) ; return Promise . resolve ( ) ; } return await wxp . reLaunch ( { url } ) ; } /**
 * 获取页面传参
 */ export function getPageParams ( randomId ) { if ( globalPageParamsId === randomId ) { return globalPageParams || { } ; } return { } ; } /**
 * 清除页面传参
 */ export function clearPageParams ( ) { globalPageParams = undefined ; }