Commit c564c51b by 张立顺

路由bug修复

1 parent 76ae7ca4
const _routeHooks = Symbol('_routeHooks') const _routeHooks = Symbol('_routeHooks')
const _query = Symbol('_query') const _query = Symbol('_query')
const _routerList = Symbol('_routerList') const _routerList = Symbol('_routerList')
import { wxOnAppRouteFn, generateRoutList } from './wxOnAppRoute.js' import {
import { getNavigationBarTitleText } from '~/utils/cache/storageApi' generateRoutList
import { getUserInfo } from '~/utils/cache/userInfo' } from './wxOnAppRoute.js'
import {
getNavigationBarTitleText
} from '~/utils/cache/storageApi'
import {
getUserInfo
} from '~/utils/cache/userInfo'
class Router { class Router {
[_query] = {}; [_query] = {};
[_routeHooks] = []; [_routeHooks] = [];
[_routerList] = [] [_routerList] = []
constructor(router) { constructor(router) {
this[_routerList] = generateRoutList(router) this[_routerList] = generateRoutList(router)
let obj = { wx.onAppRoute &&
query: this[_query], wx.onAppRoute(e => {
routeHooks: this[_routeHooks], let thisPage = this.getPageInstance()
routerList: this[_routerList], if (thisPage) {
getPageInstance: this.getPageInstance, thisPage.options = this[_query]
getRootName: this.getRootName, this.setNavigationBarTitleText(thisPage)
setNavigationBarTitleText: this.setNavigationBarTitleText, let onRoute = thisPage.onRoute
getRouterList: this.getRouterList typeof onRoute === 'function' && onRoute(this[_query])
} }
wxOnAppRouteFn(obj) this[_routeHooks].forEach(fn => {
try {
typeof fn === 'function' && fn(e, this[_query], this[_routerList])
} catch (err) {
console.log(err)
}
})
this[_query] = {}
})
} }
/**设置标题栏 */ /**设置标题栏 */
setNavigationBarTitleText = thisPage => { setNavigationBarTitleText = thisPage => {
...@@ -27,8 +41,11 @@ class Router { ...@@ -27,8 +41,11 @@ class Router {
if (getUserInfo()) title = getUserInfo().merchantName if (getUserInfo()) title = getUserInfo().merchantName
const router = `${thisPage.__route__}.html` const router = `${thisPage.__route__}.html`
if (__wxConfig && __wxConfig.page && __wxConfig.page[router]) { if (__wxConfig && __wxConfig.page && __wxConfig.page[router]) {
let { navigationBarTitleText, navigationStyle } = __wxConfig.page[router].window let {
if (!title) title = '广汇车生活' navigationBarTitleText,
navigationStyle
} = __wxConfig.page[router].window
if (!title) title = '生意助手'
if (!navigationBarTitleText && !navigationStyle) if (!navigationBarTitleText && !navigationStyle)
wx.setNavigationBarTitle({ wx.setNavigationBarTitle({
title title
...@@ -39,12 +56,6 @@ class Router { ...@@ -39,12 +56,6 @@ class Router {
getRouterList = () => { getRouterList = () => {
return this[_routerList] return this[_routerList]
} }
/**获取当前分包名 */
getRootName = () => {
let router = `/${this.getPageInstance().__route__}`
let { rootName } = this.getRouterList().find(item => router === item.url)
return rootName
}
/**获取页面对象实例 */ /**获取页面对象实例 */
getPageInstance = (size = 1) => { getPageInstance = (size = 1) => {
const pages = getCurrentPages() const pages = getCurrentPages()
...@@ -67,15 +78,15 @@ class Router { ...@@ -67,15 +78,15 @@ class Router {
parent = _parent parent = _parent
} }
/**只传页面名 默认跳转当前包下的页面,相同名称默认第一个 */ /**只传页面名 默认跳转当前包下的页面,相同名称默认第一个 */
if (!_name && !last_name) { if (!_name && !last_name) name = _parent
name = _parent
parent = this.getRootName() || parent
}
const routerMapItem = this[_routerList].find(item => { const routerMapItem = this[_routerList].find(item => {
if (last_name) { if (route.includes('/')) return item.url === route
return item.parent === parent && item.name === last_name && item.subPages === name else {
} else { if (last_name) {
return item.parent === parent && item.name === name return item.parent === parent && item.name === last_name && item.subPages === name
} else {
return item.parent === parent && item.name === name
}
} }
}) })
if (!routerMapItem) { if (!routerMapItem) {
...@@ -89,11 +100,8 @@ class Router { ...@@ -89,11 +100,8 @@ class Router {
} }
/**跳转至H5 */ /**跳转至H5 */
jumpH5(url) { jumpH5(url) {
this.intercept().then(response => { wx.navigateTo({
if (response) return url: `/pages/webView/webView?url=${encodeURIComponent(url)}`
wx.navigateTo({
url: `/pages/webView/webView?url=${encodeURIComponent(url)}`
})
}) })
} }
/**页面跳转 /**页面跳转
...@@ -108,46 +116,39 @@ class Router { ...@@ -108,46 +116,39 @@ class Router {
* 保留当前页面,跳转到应用内的某个页面。但是不能跳到 tabbar 页面。使用 $router.back() 可以返回到原页面。小程序中页面栈最多十层 * 保留当前页面,跳转到应用内的某个页面。但是不能跳到 tabbar 页面。使用 $router.back() 可以返回到原页面。小程序中页面栈最多十层
*/ */
go(route, events) { go(route, events) {
let { routerMapItem, query } = this.checkRouter(route, events) let {
routerMapItem,
query
} = this.checkRouter(route, events)
if (routerMapItem.isTab) { if (routerMapItem.isTab) {
wx.switchTab({ wx.switchTab({
url: routerMapItem.url url: routerMapItem.url
}) })
} else { } else {
if (events && events.notIntercept) { wx.navigateTo({
wx.navigateTo({ url: `${routerMapItem.url}${query}`,
url: `${routerMapItem.url}${query}`, success: res => events && res.eventChannel.emit('getData', events)
success: res => events && res.eventChannel.emit('getData', events) })
})
} else {
this.intercept().then(response => {
if (response) return
wx.navigateTo({
url: `${routerMapItem.url}${query}`,
success: res => events && res.eventChannel.emit('getData', events)
})
})
}
} }
} }
/**关闭所有页面,打开到应用内的某个页面 */ /**关闭所有页面,打开到应用内的某个页面 */
reLaunch(route, events) { reLaunch(route, events) {
let { routerMapItem, query } = this.checkRouter(route, events) let {
this.intercept().then(response => { routerMapItem,
if (response) return query
wx.reLaunch({ } = this.checkRouter(route, events)
url: `${routerMapItem.url}${query}` wx.reLaunch({
}) url: `${routerMapItem.url}${query}`
}) })
} }
/**关闭当前页面,跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面。 */ /**关闭当前页面,跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面。 */
redirectTo(route, events) { redirectTo(route, events) {
let { routerMapItem, query } = this.checkRouter(route, events) let {
this.intercept().then(response => { routerMapItem,
if (response) return query
wx.redirectTo({ } = this.checkRouter(route, events)
url: `${routerMapItem.url}${query}` wx.redirectTo({
}) url: `${routerMapItem.url}${query}`
}) })
} }
/**prevPageSize = 1 /**prevPageSize = 1
...@@ -164,7 +165,7 @@ class Router { ...@@ -164,7 +165,7 @@ class Router {
} }
goHome() { goHome() {
let [index] = this[_routerList] let [index] = this[_routerList]
wx.switchTab({ wx.reLaunch({
url: index.url url: index.url
}) })
} }
...@@ -180,18 +181,6 @@ class Router { ...@@ -180,18 +181,6 @@ class Router {
if (i > -1) list.splice(i, 1) if (i > -1) list.splice(i, 1)
} }
} }
async intercept() {
const authStatus = wx.$authStatus
if (authStatus) {
const thisPage = this.getPageInstance()
thisPage.setData({
showLogin: true
})
return await true
} else {
return await false
}
}
} }
export default Router export default Router
\ No newline at end of file \ No newline at end of file
...@@ -10,6 +10,9 @@ const routerBefore = next => { ...@@ -10,6 +10,9 @@ const routerBefore = next => {
let page = wx.$router.getPageInstance() let page = wx.$router.getPageInstance()
page && (page.data.IMG_URL = IMG_URL) page && (page.data.IMG_URL = IMG_URL)
// page && page.setData({ IMG_URL })
// console.log()
// console.log(next)
next() next()
} }
/**处理后置钩子逻辑 */ /**处理后置钩子逻辑 */
......
import util from '../utils/util.js' // import util from '../utils/util.js'
import { getUserInfo } from '~/utils/cache/userInfo' // import {
import { IMG_URL } from '~/utils/constantData' // getUserInfo
// } from '~/utils/cache/userInfo'
/** /**
* wx.onAppRoute钩子方法, * wx.onAppRoute钩子方法,
...@@ -13,74 +14,6 @@ import { IMG_URL } from '~/utils/constantData' ...@@ -13,74 +14,6 @@ import { IMG_URL } from '~/utils/constantData'
* @param {*} obj.setNavigationBarTitleText * @param {*} obj.setNavigationBarTitleText
* @param {*} obj.getRouterList * @param {*} obj.getRouterList
*/ */
export const wxOnAppRouteFn = obj => {
wx.onAppRoute &&
wx.onAppRoute(e => {
let thisPage = obj.getPageInstance()
const rootName = obj.getRootName()
if (rootName) {
const selectedColor = rootName == 'packageEquity' ? '#f7dcbc' : '#FC5B32'
wx.setTabBarStyle({
selectedColor,
color: '#979899',
backgroundColor: '#FFFFFF',
borderStyle: 'white'
})
}
if (thisPage) {
obj.setNavigationBarTitleText(thisPage)
let onRoute = thisPage.onRoute
thisPage.$query = obj.query
typeof onRoute === 'function' && onRoute(obj.query)
thisPage.rootName = rootName
obj.getRouterList().find(item => {
if (`/${thisPage.__route__}` === item.url) {
const authStatus = wx.$authStatus
// if (item.isTab) thisPage.setData({
// showLogin: authStatus
// })
}
})
if (!wx.$version)
util.getImgVersion().then(({ version }) => {
wx.$version = version || '1.0.0'
thisPage.setData({
img_version: wx.$version
})
})
else
thisPage.setData({
img_version: wx.$version
})
// console.log(app)
thisPage.setData({
IMGURL: IMG_URL
})
let userInfo = getUserInfo()
if (userInfo && userInfo.phone) {
if (wx.$authStatus) {
thisPage.setData({
userInfo: null
})
} else {
thisPage.setData({
userInfo
})
if (thisPage.init) thisPage.init(userInfo)
}
}
}
obj.routeHooks.forEach(fn => {
try {
typeof fn === 'function' && fn(e, obj.query, obj.routerList)
} catch (err) {
console.log(err)
}
})
obj.query = {}
})
}
/** /**
* 生成特定格式路由 * 生成特定格式路由
* *
...@@ -107,11 +40,6 @@ export const generateRoutList = routerList => { ...@@ -107,11 +40,6 @@ export const generateRoutList = routerList => {
obj.subPages = item.url.split(`/${item.parent}/pages/`)[1].split('/')[0] obj.subPages = item.url.split(`/${item.parent}/pages/`)[1].split('/')[0]
} }
} }
if (obj.isTab) {
__wxConfig.tabBar.list.map(e => {
if (obj.url === `/${e.pagePath.replace('.html', '')}`) obj.rootName = e.rootName
})
}
} }
return obj return obj
})) }))
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!