Skip to content

Commit

Permalink
feat(miniProgramBus): 新增 routeChange
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Aug 29, 2020
1 parent 6474892 commit 5981d30
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 13 deletions.
13 changes: 9 additions & 4 deletions src/mp/getCurrentPagePath.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
/// <reference types="miniprogram-api-typings" />
import { AnyObject } from '../types'
import { ensureInMiniProgram } from './ensureInMiniProgram'
import { last } from 'lodash'

/**
* 获取当前页面的路径(不含查询参数),始终以 `/` 开头。
*
* @param pageInstance 页面实例,默认当前页面
* @returns 返回当前页面的路径
*/
export function getCurrentPagePath(): string {
export function getCurrentPagePath(
pageInstance?: WechatMiniprogram.Page.Instance<AnyObject, AnyObject>,
): string {
return ensureInMiniProgram(() => {
const pages = getCurrentPages()
const currentPage = pages[pages.length - 1]
pageInstance = pageInstance || last(getCurrentPages())!
const path = `/${
currentPage.route || /* 字节跳动 */ currentPage.__route__
pageInstance.route || /* 字节跳动 */ pageInstance.__route__
}`.replace(/\/{2,}/g, '/')
return path
})
Expand Down
12 changes: 7 additions & 5 deletions src/mp/getCurrentPageQuery.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
/// <reference types="miniprogram-api-typings" />
import { AnyObject } from '../types'
import { ensureInMiniProgram } from './ensureInMiniProgram'
import { mapValues } from '../utils'
import { last, mapValues } from '../utils'

/**
* 获取当前页面的查询参数,已经对每个值执行了 decodeURIComponent。
*
* @param pageInstance 页面实例,默认当前页面
* @returns 返回当前页面的查询参数
*/
export function getCurrentPageQuery<
T extends Record<string, string | undefined>
>(): T {
>(pageInstance?: WechatMiniprogram.Page.Instance<AnyObject, AnyObject>): T {
return ensureInMiniProgram(() => {
const pages = getCurrentPages()
const currentPage = pages[pages.length - 1]
pageInstance = pageInstance || last(getCurrentPages())!
const query = mapValues(
currentPage.options || {},
pageInstance.options || {},
value => value && decodeURIComponent(value),
)
return (query as any) as T
Expand Down
11 changes: 8 additions & 3 deletions src/mp/getCurrentPageUrl.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <reference types="miniprogram-api-typings" />
import { AnyObject } from '../types'
import { createUrlQueryString } from '../utils'
import { ensureInMiniProgram } from './ensureInMiniProgram'
import { getCurrentPagePath } from './getCurrentPagePath'
Expand All @@ -6,12 +8,15 @@ import { getCurrentPageQuery } from './getCurrentPageQuery'
/**
* 获取当前页面的地址(包含查询参数)。
*
* @param pageInstance 页面实例,默认当前页面
* @returns 返回当前页面的地址
*/
export function getCurrentPageUrl(): string {
export function getCurrentPageUrl(
pageInstance?: WechatMiniprogram.Page.Instance<AnyObject, AnyObject>,
): string {
return ensureInMiniProgram(() => {
const path = getCurrentPagePath()
const query = getCurrentPageQuery()
const path = getCurrentPagePath(pageInstance)
const query = getCurrentPageQuery(pageInstance)
const queryString = createUrlQueryString(query)
const url = `${path}${queryString ? `?${queryString}` : ''}`
return url
Expand Down
17 changes: 16 additions & 1 deletion src/mp/miniProgramBus.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
/// <reference types="miniprogram-api-typings" />
import { Defined } from '../types'
import { AnyObject, Defined } from '../types'
import { EventBus, EventBusBeforeOn, EventBusListener } from '../utils'
import { patchMiniProgram } from './patchMiniProgram'

export interface MiniProgramBusRouteChangePageInfo {
url: string
path: string
query: AnyObject
}

export type MiniProgramBusRouteChangeAction = 'replace' | 'pop' | 'push'

export interface MiniProgramBusRouteChangePayload {
from: MiniProgramBusRouteChangePageInfo | undefined
to: MiniProgramBusRouteChangePageInfo
action: MiniProgramBusRouteChangeAction
}

export interface MiniProgramBusListeners {
appLaunch: Defined<WechatMiniprogram.App.Options<{}>['onLaunch']>
appShow: Defined<WechatMiniprogram.App.Options<{}>['onShow']>
Expand Down Expand Up @@ -66,6 +80,7 @@ export interface MiniProgramBusListeners {
currentPageTabItemTap: Defined<
WechatMiniprogram.Page.Options<{}, {}>['onTabItemTap']
>
routeChange: (payload: MiniProgramBusRouteChangePayload) => any
}

/** @private */
Expand Down
60 changes: 60 additions & 0 deletions src/mp/patchMiniProgram.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import { AnyObject } from '../types'
import { currentPageListeners, miniProgramBus } from './miniProgramBus'
import { ensureInMiniProgram } from './ensureInMiniProgram'
import { getCurrentPagePath } from './getCurrentPagePath'
import { getCurrentPageQuery } from './getCurrentPageQuery'
import { getCurrentPageUrl } from './getCurrentPageUrl'
import { last, MiniProgramApi } from '../utils'

let prevPagesInfo:
| {
count: number
lastPageUrl: string
lastPagePath: string
lastPageQuery: AnyObject
}
| undefined

function patchAppOptions(
mp: MiniProgramApi,
appOptions: WechatMiniprogram.App.Options<{}>,
Expand Down Expand Up @@ -69,6 +82,39 @@ function patchPageOptions(

const onShow = pageOptions.onShow
pageOptions.onShow = function () {
const currentPagesInfo = {
count: getCurrentPages().length,
lastPageUrl: getCurrentPageUrl(),
lastPagePath: getCurrentPagePath(),
lastPageQuery: getCurrentPageQuery(),
}
if (
!(
prevPagesInfo &&
currentPagesInfo.count === prevPagesInfo.count &&
currentPagesInfo.lastPageUrl === prevPagesInfo.lastPageUrl
)
) {
miniProgramBus.emit('routeChange', {
from: prevPagesInfo && {
url: prevPagesInfo.lastPageUrl,
path: prevPagesInfo.lastPagePath,
query: prevPagesInfo.lastPageQuery,
},
to: {
url: currentPagesInfo.lastPageUrl,
path: currentPagesInfo.lastPagePath,
query: currentPagesInfo.lastPageQuery,
},
action:
!prevPagesInfo || currentPagesInfo.count > prevPagesInfo.count
? 'push'
: currentPagesInfo.count < prevPagesInfo.count
? 'pop'
: 'replace',
})
}

patchMiniProgram.__CURRENT_PAGE_ID__ = (this as any).__PAGE_ID__
miniProgramBus.emit({
name: 'currentPageShow',
Expand All @@ -92,6 +138,13 @@ function patchPageOptions(

const onHide = pageOptions.onHide
pageOptions.onHide = function () {
prevPagesInfo = {
count: getCurrentPages().length,
lastPageUrl: getCurrentPageUrl(),
lastPagePath: getCurrentPagePath(),
lastPageQuery: getCurrentPageQuery(),
}

miniProgramBus.emit({
name: 'currentPageHide',
context: this,
Expand All @@ -103,6 +156,13 @@ function patchPageOptions(

const onUnload = pageOptions.onUnload
pageOptions.onUnload = function () {
prevPagesInfo = {
count: getCurrentPages().length,
lastPageUrl: getCurrentPageUrl(),
lastPagePath: getCurrentPagePath(),
lastPageQuery: getCurrentPageQuery(),
}

miniProgramBus.emit({
name: 'currentPageUnload',
context: this,
Expand Down

0 comments on commit 5981d30

Please sign in to comment.