Commit 997efc74 authored by Damon's avatar Damon

feat: 用户授权拦截

parent 56507c13
......@@ -3,8 +3,6 @@ export default {
onLaunch: function () {
// 版本更新
this.updateApp()
// 检查登录是否已过期
this.checkLogin()
},
onShow: function () {
console.log('App Show')
......@@ -13,27 +11,6 @@ export default {
console.log('App Hide')
},
methods: {
checkLogin() {
if (this.vuex_token) {
uni.checkSession({
success: (result) => {
//session_key 未过期,并且在本生命周期一直有效
console.log("session未过期", result)
},
fail: (error) => {
// session_key 已经失效,需要重新执行登录流程
console.log("error", error)
this.$u.route({
url: 'pages/login/index'
})
}
})
} else {
this.$u.route({
url: 'pages/login/index'
})
}
},
/* 版本更新 */
updateApp() {
if (uni.canIUse('getUpdateManager')) {
......
......@@ -45,7 +45,7 @@ const install = (Vue, vm) => {
vm.$u.toast('验证失败,请重新登录');
setTimeout(() => {
// 此为uView的方法,详见路由相关文档
vm.$u.route('/pages/user/login')
vm.$u.route('/pages/login/index')
}, 1500)
return false;
} else {
......
......@@ -76,7 +76,6 @@
"path": "customer",
"name": "mineCustomer",
"style": {
"navigationStyle": "custom",
"navigationBarTitleText": "联系客服",
"enablePullDownRefresh": false,
"disableScroll": true
......
......@@ -14,7 +14,7 @@
<u-cell-group>
<u-cell-item icon="star" title="个人信息" value="完善度40%"></u-cell-item>
<u-cell-item icon="photo" title="我的题库"></u-cell-item>
<u-cell-item icon="coupon" title="联系客服"></u-cell-item>
<u-cell-item icon="coupon" title="联系客服" @tap="toUrl('pages/mine/customer')"></u-cell-item>
<u-cell-item icon="coupon" title="退出登录" @tap="logout"></u-cell-item>
</u-cell-group>
</view>
......@@ -46,6 +46,11 @@
url: 'pages/login/index'
})
},
toUrl(url) {
this.$u.route({
url: url
})
},
logout() {
this.$u.vuex('vuex_token', '')
this.$u.vuex('userInfo', {})
......
......@@ -18,18 +18,8 @@ export default {
}
},
onShow() {
this.checkLogin()
},
methods: {
checkLogin() {
if (this.vuex_token) {
uni.checkSession({
success: (result) => {
this.$u.route({ type: 'back' })
}
})
}
},
wxLogin() {
// 获取code 小程序专有,用户登录凭证。
uni.getUserProfile({
......
<template>
<view class="content">
<view>
<text class="title">{{title}}</text>
</view>
</view>
</template>
<script>
export default {
data() {
return {
title: '我的客服'
}
},
onLoad() {
},
methods: {
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.logo {
height: 200rpx;
width: 200rpx;
margin: 200rpx auto 50rpx auto;
}
.text-area {
display: flex;
justify-content: center;
}
.title {
font-size: 36rpx;
color: #8f8f94;
}
</style>
import { RouterMount, createRouter } from 'uni-simple-router'
import store from '@/store/index.js'
const router = createRouter({
platform: process.env.VUE_APP_PLATFORM,
routes: [...ROUTES],
})
//全局路由前置守卫
// 检测登录白名单,无需登录也可访问的页面
const whiteList = ['index', 'cert', 'mine', 'login']
// 全局路由前置守卫
router.beforeEach((to, from, next) => {
const token = store.state.vuex_token
// 进入需要授权的页面,未登录跳转
if (!whiteList.includes(to.name) && !token) {
next({ name: 'login', NAVTYPE: 'push' })
}
next()
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment