项目采用Vue做为开发框架,用户浏览页面时有两种状况:前端
在无需用户登陆的页面中,可能包含须要用户信息的操做,此时就须要用户登陆以后方能进行后续操做。所以,须要对受权登陆策略进行区分。后端
通常而言,咱们为微信开发的H5页面,进入页面的时候就进行鉴权,要求用户登陆以后才能继续浏览。但因为产品需求,这个项目咱们须要对不一样页面的鉴权策略进行划分,按照通常与特殊进行设计:promise
// routerRule.js
export default function routerRule (router, whiteList = []) {
// other codes...
router.beforeEach( (to, from, next ) => {
// 由于受权登陆涉及异步操做,所以使用promise,成功的回调中调用next函数
new Promise((resolve, rejects) => {
if ( whiteListRouter.indexOf(to.path) !== -1 ) {
resolve()
return
}
// 常规页面受权登陆过程
if (hasToken()) {
// codes,获取用户信息而且跳转所需跳转的页面
} else {
// 判断用户是否已经进行微信受权
if (hasAuthed()) {
// 进行过微信受权以后,重定向回来的url中包含了微信的受权信息,能够将url上截取的参数发送到服务器,换取用户的token,随后进入上述有token时候的步骤
getWechatUserInfo().then(res => {
resolve()
})
} else {
// 用户还没有进行微信受权,则调用微信受权的方法,进行受权登陆。
getWechatAuth()
}
}
}).then( res => {
next()
})
})
router.afterEach(( to, from ) => {
wxShare({ title: to.meta.title, desc: to.meta.shareDesc, link: to.meta.shareLink, logo: to.meta.shareLogo})
})
}
复制代码
本项目是在用户初次进行微信绑定时,就将用户的微信信息与本站的用户信息进行的绑定,所以在获取用户微信受权信息后,就能够获取到用户的token,从而获取用户在本站的其余用户信息。服务器
根据上面的逻辑,进入白名单以后,整个函数已经被return掉,不会进入下面的鉴权过程。可是若是是在此种页面上进行须要权限的操做,那么就须要触发受权登陆流程,而且在受权以后,要一并获取用户信息。微信
// checkLogin.js
export function checkLogin({ redirectUrl, wxAuthLoading, wxAuthLoaded, callback } = {}) {
if (getToken()) {
// ...
callback && callback()
} else {
// 提示用户正在受权中
wxAuthLoading && wxAuthLoading()
getWechatAuth( redirectUrl || window.location.href ).then( res => {
// 受权完毕,提示用户受权成功
wxAuthLoaded && wxAuthLoaded()
})
}
}
复制代码
同时,咱们须要对路由白名单添加一些操做微信开发
// routerRule.js
export default function routerRule (router, whiteList = []) {
// other codes...
router.beforeEach( (to, from, next ) => {
// 由于受权登陆涉及异步操做,所以使用promise,成功的回调中调用next函数
new Promise((resolve, rejects) => {
if ( whiteListRouter.indexOf(to.path) !== -1 ) {
// 若是已经进行微信受权可是没有token值的,就调用如下函数获取token值
if ( !hasToken() && hasAuthed() ) {
getWechatUserInfo().then(res => {
resolve()
})
}
resolve()
return
}
// 常规页面受权登陆过程
if (hasToken()) {
// codes,获取用户信息而且跳转所需跳转的页面
} else {
// 判断用户是否已经进行微信受权
if (hasAuthed()) {
// 进行过微信受权以后,重定向回来的url中包含了微信的受权信息,能够将url上截取的参数发送到服务器,换取用户的token,随后进入上述有token时候的步骤
getWechatUserInfo().then(res => {
resolve()
})
} else {
// 用户还没有进行微信受权,则调用微信受权的方法,进行受权登陆。
getWechatAuth()
}
}
}).then( res => {
next()
})
})
// other codes...
}
复制代码
这是本人开发过程当中想到的不成熟的方案,若是有更好的方法,请不吝告知,谢谢!框架