Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
saas_plus
/
store-assistant
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit c564c51b
authored
Oct 12, 2022
by
张立顺
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
路由bug修复
1 parent
76ae7ca4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
71 additions
and
151 deletions
router/router.js
router/routerBefore.js
router/wxOnAppRoute.js
router/router.js
View file @
c564c51
const
_routeHooks
=
Symbol
(
'_routeHooks'
)
const
_query
=
Symbol
(
'_query'
)
const
_routerList
=
Symbol
(
'_routerList'
)
import
{
wxOnAppRouteFn
,
generateRoutList
}
from
'./wxOnAppRoute.js'
import
{
getNavigationBarTitleText
}
from
'~/utils/cache/storageApi'
import
{
getUserInfo
}
from
'~/utils/cache/userInfo'
import
{
generateRoutList
}
from
'./wxOnAppRoute.js'
import
{
getNavigationBarTitleText
}
from
'~/utils/cache/storageApi'
import
{
getUserInfo
}
from
'~/utils/cache/userInfo'
class
Router
{
[
_query
]
=
{};
[
_routeHooks
]
=
[];
[
_routerList
]
=
[]
constructor
(
router
)
{
this
[
_routerList
]
=
generateRoutList
(
router
)
let
obj
=
{
query
:
this
[
_query
],
routeHooks
:
this
[
_routeHooks
],
routerList
:
this
[
_routerList
],
getPageInstance
:
this
.
getPageInstance
,
getRootName
:
this
.
getRootName
,
setNavigationBarTitleText
:
this
.
setNavigationBarTitleText
,
getRouterList
:
this
.
getRouterList
}
wxOnAppRouteFn
(
obj
)
wx
.
onAppRoute
&&
wx
.
onAppRoute
(
e
=>
{
let
thisPage
=
this
.
getPageInstance
()
if
(
thisPage
)
{
thisPage
.
options
=
this
[
_query
]
this
.
setNavigationBarTitleText
(
thisPage
)
let
onRoute
=
thisPage
.
onRoute
typeof
onRoute
===
'function'
&&
onRoute
(
this
[
_query
])
}
this
[
_routeHooks
].
forEach
(
fn
=>
{
try
{
typeof
fn
===
'function'
&&
fn
(
e
,
this
[
_query
],
this
[
_routerList
])
}
catch
(
err
)
{
console
.
log
(
err
)
}
})
this
[
_query
]
=
{}
})
}
/**设置标题栏 */
setNavigationBarTitleText
=
thisPage
=>
{
...
...
@@ -27,8 +41,11 @@ class Router {
if
(
getUserInfo
())
title
=
getUserInfo
().
merchantName
const
router
=
`
${
thisPage
.
__route__
}
.html`
if
(
__wxConfig
&&
__wxConfig
.
page
&&
__wxConfig
.
page
[
router
])
{
let
{
navigationBarTitleText
,
navigationStyle
}
=
__wxConfig
.
page
[
router
].
window
if
(
!
title
)
title
=
'广汇车生活'
let
{
navigationBarTitleText
,
navigationStyle
}
=
__wxConfig
.
page
[
router
].
window
if
(
!
title
)
title
=
'生意助手'
if
(
!
navigationBarTitleText
&&
!
navigationStyle
)
wx
.
setNavigationBarTitle
({
title
...
...
@@ -39,12 +56,6 @@ class Router {
getRouterList
=
()
=>
{
return
this
[
_routerList
]
}
/**获取当前分包名 */
getRootName
=
()
=>
{
let
router
=
`/
${
this
.
getPageInstance
().
__route__
}
`
let
{
rootName
}
=
this
.
getRouterList
().
find
(
item
=>
router
===
item
.
url
)
return
rootName
}
/**获取页面对象实例 */
getPageInstance
=
(
size
=
1
)
=>
{
const
pages
=
getCurrentPages
()
...
...
@@ -67,15 +78,15 @@ class Router {
parent
=
_parent
}
/**只传页面名 默认跳转当前包下的页面,相同名称默认第一个 */
if
(
!
_name
&&
!
last_name
)
{
name
=
_parent
parent
=
this
.
getRootName
()
||
parent
}
if
(
!
_name
&&
!
last_name
)
name
=
_parent
const
routerMapItem
=
this
[
_routerList
].
find
(
item
=>
{
if
(
last_name
)
{
return
item
.
parent
===
parent
&&
item
.
name
===
last_name
&&
item
.
subPages
===
name
}
else
{
return
item
.
parent
===
parent
&&
item
.
name
===
name
if
(
route
.
includes
(
'/'
))
return
item
.
url
===
route
else
{
if
(
last_name
)
{
return
item
.
parent
===
parent
&&
item
.
name
===
last_name
&&
item
.
subPages
===
name
}
else
{
return
item
.
parent
===
parent
&&
item
.
name
===
name
}
}
})
if
(
!
routerMapItem
)
{
...
...
@@ -89,11 +100,8 @@ class Router {
}
/**跳转至H5 */
jumpH5
(
url
)
{
this
.
intercept
().
then
(
response
=>
{
if
(
response
)
return
wx
.
navigateTo
({
url
:
`/pages/webView/webView?url=
${
encodeURIComponent
(
url
)}
`
})
wx
.
navigateTo
({
url
:
`/pages/webView/webView?url=
${
encodeURIComponent
(
url
)}
`
})
}
/**页面跳转
...
...
@@ -108,46 +116,39 @@ class Router {
* 保留当前页面,跳转到应用内的某个页面。但是不能跳到 tabbar 页面。使用 $router.back() 可以返回到原页面。小程序中页面栈最多十层
*/
go
(
route
,
events
)
{
let
{
routerMapItem
,
query
}
=
this
.
checkRouter
(
route
,
events
)
let
{
routerMapItem
,
query
}
=
this
.
checkRouter
(
route
,
events
)
if
(
routerMapItem
.
isTab
)
{
wx
.
switchTab
({
url
:
routerMapItem
.
url
})
}
else
{
if
(
events
&&
events
.
notIntercept
)
{
wx
.
navigateTo
({
url
:
`
${
routerMapItem
.
url
}${
query
}
`
,
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
)
})
})
}
wx
.
navigateTo
({
url
:
`
${
routerMapItem
.
url
}${
query
}
`
,
success
:
res
=>
events
&&
res
.
eventChannel
.
emit
(
'getData'
,
events
)
})
}
}
/**关闭所有页面,打开到应用内的某个页面 */
reLaunch
(
route
,
events
)
{
let
{
routerMapItem
,
query
}
=
this
.
checkRouter
(
route
,
events
)
this
.
intercept
().
then
(
response
=>
{
if
(
response
)
return
wx
.
reLaunch
({
url
:
`
${
routerMapItem
.
url
}${
query
}
`
})
let
{
routerMapItem
,
query
}
=
this
.
checkRouter
(
route
,
events
)
wx
.
reLaunch
({
url
:
`
${
routerMapItem
.
url
}${
query
}
`
})
}
/**关闭当前页面,跳转到应用内的某个页面。但是不允许跳转到 tabbar 页面。 */
redirectTo
(
route
,
events
)
{
let
{
routerMapItem
,
query
}
=
this
.
checkRouter
(
route
,
events
)
this
.
intercept
().
then
(
response
=>
{
if
(
response
)
return
wx
.
redirectTo
({
url
:
`
${
routerMapItem
.
url
}${
query
}
`
})
let
{
routerMapItem
,
query
}
=
this
.
checkRouter
(
route
,
events
)
wx
.
redirectTo
({
url
:
`
${
routerMapItem
.
url
}${
query
}
`
})
}
/**prevPageSize = 1
...
...
@@ -164,7 +165,7 @@ class Router {
}
goHome
()
{
let
[
index
]
=
this
[
_routerList
]
wx
.
switchTab
({
wx
.
reLaunch
({
url
:
index
.
url
})
}
...
...
@@ -180,18 +181,6 @@ class Router {
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
router/routerBefore.js
View file @
c564c51
...
...
@@ -10,6 +10,9 @@ const routerBefore = next => {
let
page
=
wx
.
$router
.
getPageInstance
()
page
&&
(
page
.
data
.
IMG_URL
=
IMG_URL
)
// page && page.setData({ IMG_URL })
// console.log()
// console.log(next)
next
()
}
/**处理后置钩子逻辑 */
...
...
router/wxOnAppRoute.js
View file @
c564c51
import
util
from
'../utils/util.js'
import
{
getUserInfo
}
from
'~/utils/cache/userInfo'
import
{
IMG_URL
}
from
'~/utils/constantData'
// import util from '../utils/util.js'
// import {
// getUserInfo
// } from '~/utils/cache/userInfo'
/**
* wx.onAppRoute钩子方法,
...
...
@@ -13,74 +14,6 @@ import { IMG_URL } from '~/utils/constantData'
* @param {*} obj.setNavigationBarTitleText
* @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 => {
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
}))
...
...
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment