Foreword:
The login judgment of the uni-app data, you can continue to view it when you log in, and put it on the login interface if you are not logged in.
Specific steps:
1. Encapsulate the cache method: util/auth.js
const TokenKey = 'uni_token';
// 认证令牌
export function getToken() {
return uni.getStorageSync(TokenKey)
}
export function setToken(token) {
return uni.setStorageSync(TokenKey, token)
}
export function removeToken() {
return uni.removeStorageSync(TokenKey)
}
2. Login page, introduce cache method, login successfully and add token
import {setToken} from '../../util/auth';
登录成功以后
setToken('111111')
3. App.vue page plus control method
import {getToken} from './util/auth';
onLaunch: function() {
//判断是否登录
let token = getToken()
if(!token){
//不存在则跳转至登录页
uni.reLaunch({
url: "/pages/login/login"
})
}
}