cross-magic
Version:
跨平台公共模块
96 lines (80 loc) • 2.01 kB
text/typescript
import UiConfig from './uiConfig'
import UiMgr, { PageParams } from './uiMgr'
import { RawLocation, RouteConfig, Location } from 'vue-router/types'
import getRunTime from '@/core/runTime'
import util from '@/core/utils/index'
import { WxEnv } from '@/core/runTime/wxMpRuntime'
export default class WxMpUiMgr extends UiMgr {
private _wxEnv!: WxEnv
constructor(config: UiConfig) {
super(config)
this._wxEnv = getRunTime().g
}
protected init() {}
pushPage(location: RawLocation, { onComplete, onAbort }: PageParams = {}) {
if (!this.routerConfig || !this._wxEnv) {
return
}
const path: string = this.findPath(location)
if (path) {
this._wxEnv.navigateTo({
url: path
})
}
}
popPage() {
if (!this._wxEnv) {
return
}
this._wxEnv.navigateBack()
}
/**
* 注意微信小程序的gotoPage只能往回反,意思n只能小于0
*/
gotoPage(n: number) {
if (!this._wxEnv || n < 0) {
return
}
this._wxEnv.navigateBack({
delta: n
})
}
redirectPage(location: RawLocation, { onComplete, onAbort }: PageParams = {}) {
if (!this._wxEnv) {
return
}
const path: string = this.findPath(location)
if (path) {
this._wxEnv.redirectTo({
url: path
})
}
}
goHomePage() {
if (!this._wxEnv) {
return
}
let pageNums: number = this._wxEnv.getCurrentPages()
this._wxEnv.navigateBack({
delta: pageNums
})
}
private findPath(location: RawLocation) {
if (util.isString(location)) {
return this.findPathByName(location as string)
}
return (location as Location).path || ''
}
private findPathByName(name: string) {
if (!name || !this.routerConfig) {
return ''
}
let routes: Array<RouteConfig> = this.routerConfig.routes || []
for (let i = 0; i < routes.length; ++i) {
if (routes[i].name === name) {
return routes[i].path
}
}
return ''
}
}