200 lines
5.1 KiB
Vue
200 lines
5.1 KiB
Vue
<script>
|
||
import initApp from '@/common/appInit.js';
|
||
import openApp from '@/common/openApp.js';
|
||
// #ifdef H5
|
||
openApp() //创建在h5端全局悬浮引导用户下载app的功能
|
||
// #endif
|
||
import checkIsAgree from '@/pages/uni-agree/utils/uni-agree.js';
|
||
import uniIdPageInit from '@/uni_modules/uni-id-pages/init.js';
|
||
import { store } from '@/uni_modules/uni-id-pages/common/store.js';
|
||
|
||
export default {
|
||
globalData: {
|
||
searchText: '',
|
||
appVersion: {},
|
||
config: {},
|
||
$i18n: {},
|
||
$t: {}
|
||
},
|
||
onLaunch: function() {
|
||
console.log('App Launch')
|
||
this.globalData.$i18n = this.$i18n
|
||
this.globalData.$t = str => this.$t(str)
|
||
initApp();
|
||
uniIdPageInit()
|
||
|
||
// 设置全局路由拦截,检查登录状态
|
||
this.setupRouteInterceptor()
|
||
|
||
// 延迟检查登录状态,确保 store 已初始化
|
||
setTimeout(() => {
|
||
this.checkCurrentPageLogin()
|
||
}, 100)
|
||
|
||
// #ifdef APP
|
||
//checkIsAgree(); APP端暂时先用原生默认生成的。目前,自定义方式启动vue界面时,原生层已经请求了部分权限这并不符合国家的法规
|
||
// #endif
|
||
|
||
// #ifdef H5
|
||
// checkIsAgree(); // 默认不开启。目前全球,仅欧盟国家有网页端同意隐私权限的需要。如果需要可以自己去掉注视后生效
|
||
// #endif
|
||
|
||
// #ifdef APP-PLUS
|
||
//idfa有需要的用户在应用首次启动时自己获取存储到storage中
|
||
/*var idfa = '';
|
||
var manager = plus.ios.invoke('ASIdentifierManager', 'sharedManager');
|
||
if(plus.ios.invoke(manager, 'isAdvertisingTrackingEnabled')){
|
||
var identifier = plus.ios.invoke(manager, 'advertisingIdentifier');
|
||
idfa = plus.ios.invoke(identifier, 'UUIDString');
|
||
plus.ios.deleteObject(identifier);
|
||
}
|
||
plus.ios.deleteObject(manager);
|
||
console.log('idfa = '+idfa);*/
|
||
// #endif
|
||
},
|
||
onShow: function() {
|
||
console.log('App Show')
|
||
// 每次应用显示时也检查当前页面是否需要登录
|
||
this.checkCurrentPageLogin()
|
||
},
|
||
onHide: function() {
|
||
console.log('App Hide')
|
||
},
|
||
methods: {
|
||
/**
|
||
* 设置全局路由拦截
|
||
*/
|
||
setupRouteInterceptor() {
|
||
const app = this
|
||
|
||
// 拦截路由跳转(适用于所有平台)
|
||
uni.addInterceptor('navigateTo', {
|
||
invoke: (options) => {
|
||
if (app.shouldRedirectToLogin(options.url)) {
|
||
app.redirectToLogin()
|
||
return false // 阻止跳转
|
||
}
|
||
}
|
||
})
|
||
|
||
uni.addInterceptor('redirectTo', {
|
||
invoke: (options) => {
|
||
if (app.shouldRedirectToLogin(options.url)) {
|
||
app.redirectToLogin()
|
||
return false // 阻止跳转
|
||
}
|
||
}
|
||
})
|
||
|
||
uni.addInterceptor('switchTab', {
|
||
invoke: (options) => {
|
||
if (app.shouldRedirectToLogin(options.url)) {
|
||
app.redirectToLogin()
|
||
return false // 阻止跳转
|
||
}
|
||
}
|
||
})
|
||
|
||
uni.addInterceptor('reLaunch', {
|
||
invoke: (options) => {
|
||
if (app.shouldRedirectToLogin(options.url)) {
|
||
app.redirectToLogin()
|
||
return false // 阻止跳转
|
||
}
|
||
}
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 判断是否需要跳转到登录页
|
||
*/
|
||
shouldRedirectToLogin(url) {
|
||
if (!url) return false
|
||
|
||
// 排除登录相关页面
|
||
const excludePaths = [
|
||
'/uni_modules/uni-id-pages/pages/login',
|
||
'/uni_modules/uni-id-pages/pages/register',
|
||
'/uni_modules/uni-id-pages/pages/retrieve'
|
||
]
|
||
|
||
// 检查是否在排除列表中
|
||
for (let excludePath of excludePaths) {
|
||
if (url.includes(excludePath)) {
|
||
return false
|
||
}
|
||
}
|
||
|
||
// 检查登录状态
|
||
return !this.isLoggedIn()
|
||
},
|
||
|
||
/**
|
||
* 检查是否已登录
|
||
*/
|
||
isLoggedIn() {
|
||
// 优先检查后端登录态
|
||
const backendToken = uni.getStorageSync('backend-token')
|
||
if (backendToken) {
|
||
return true
|
||
}
|
||
|
||
// 其次检查 uni-id 的登录态
|
||
return store.hasLogin
|
||
},
|
||
|
||
/**
|
||
* 跳转到登录页
|
||
*/
|
||
redirectToLogin() {
|
||
const loginPage = '/uni_modules/uni-id-pages/pages/login/login-withpwd'
|
||
const pages = getCurrentPages()
|
||
|
||
// 如果当前已经在登录页,不重复跳转
|
||
if (pages.length > 0) {
|
||
const currentPage = pages[pages.length - 1]
|
||
if (currentPage.route && currentPage.route.includes('login')) {
|
||
return
|
||
}
|
||
}
|
||
|
||
uni.reLaunch({
|
||
url: loginPage,
|
||
fail: (err) => {
|
||
console.error('跳转登录页失败:', err)
|
||
}
|
||
})
|
||
},
|
||
|
||
/**
|
||
* 检查当前页面是否需要登录
|
||
*/
|
||
checkCurrentPageLogin() {
|
||
const pages = getCurrentPages()
|
||
if (pages.length === 0) return
|
||
|
||
const currentPage = pages[pages.length - 1]
|
||
if (!currentPage || !currentPage.route) return
|
||
|
||
const currentRoute = '/' + currentPage.route
|
||
|
||
// 排除登录相关页面
|
||
if (currentRoute.includes('/uni_modules/uni-id-pages/pages/login') ||
|
||
currentRoute.includes('/uni_modules/uni-id-pages/pages/register') ||
|
||
currentRoute.includes('/uni_modules/uni-id-pages/pages/retrieve')) {
|
||
return
|
||
}
|
||
|
||
// 如果未登录,跳转到登录页
|
||
if (!this.isLoggedIn()) {
|
||
this.redirectToLogin()
|
||
}
|
||
}
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<style>
|
||
/*每个页面公共css */
|
||
</style>
|