解决登录显示、首页显示bug

This commit is contained in:
2026-05-19 11:06:46 +08:00
parent 2f7e097e6c
commit 00a859c551
181 changed files with 55329 additions and 998 deletions

View File

@@ -1,10 +1,16 @@
<script lang="uts">
import { setIsLoggedIn, setUserProfile, getCurrentUser } from '@/utils/store.uts'
import supa from '@/components/supadb/aksupainstance.uts'
import { HOME_REDIRECT } from '@/ak/config.uts'
import { handleDeliveryUnauthorized, isCurrentDeliveryPage } from '@/utils/deliveryAuth.uts'
import { CURRENT_PAGES_MODE } from '@/utils/pagesMode.uts'
export default {
onLaunch: function () {
console.log('App Launch')
uni.$on('AUTH_SESSION_EXPIRED', () => {
this.handleAuthSessionExpired()
})
// 检查是否已有有效会话,有则恢复登录状态
this.checkExistingSession()
@@ -16,6 +22,23 @@
console.log('App Hide')
},
methods: {
handleAuthSessionExpired: function(): void {
if (isCurrentDeliveryPage()) {
handleDeliveryUnauthorized('', '')
}
},
shouldGuestBrowseHome: function(): boolean {
return CURRENT_PAGES_MODE == 'consumer' || CURRENT_PAGES_MODE == 'full'
},
getLoginEntryUrl: function(): string {
if (CURRENT_PAGES_MODE == 'delivery') {
return '/pages/user/login?mode=delivery'
}
if (CURRENT_PAGES_MODE == 'merchant') {
return '/pages/user/login?mode=merchant'
}
return '/pages/user/login'
},
safeReLaunch: function(targetUrl: string, fallbackUrl: string): void {
try {
uni.reLaunch({
@@ -37,19 +60,23 @@
}
},
checkExistingSession: function(): void {
if (CURRENT_PAGES_MODE == 'delivery') {
this.safeReLaunch(this.getLoginEntryUrl(), '/pages/user/login')
return
}
// 检查是否已有有效会话
const session = supa.getSession()
if (session.user != null) {
console.log('已有有效会话,恢复登录状态')
setIsLoggedIn(true)
// #ifdef H5
this.safeReLaunch('/pages/main/index', '/pages/user/login')
this.safeReLaunch(HOME_REDIRECT, '/pages/user/login')
// #endif
// #ifdef MP-WEIXIN
this.safeReLaunch('/pages/main/index', '/pages/user/login')
this.safeReLaunch(HOME_REDIRECT, '/pages/user/login')
// #endif
// #ifndef H5 || MP-WEIXIN
this.safeReLaunch('/pages/main/index', '/pages/user/login')
this.safeReLaunch(HOME_REDIRECT, '/pages/user/login')
// #endif
return
}
@@ -58,30 +85,43 @@
const savedUserId = uni.getStorageSync('user_id')
if (savedUserId != null && savedUserId != '') {
console.log('本地存储中有用户ID尝试恢复会话')
getCurrentUser().then((profile) => {
getCurrentUser().then((profile) => {
if (profile != null) {
console.log('会话恢复成功')
setIsLoggedIn(true)
// #ifdef H5
this.safeReLaunch('/pages/main/index', '/pages/user/login')
this.safeReLaunch(HOME_REDIRECT, '/pages/user/login')
// #endif
// #ifdef MP-WEIXIN
this.safeReLaunch('/pages/main/index', '/pages/user/login')
this.safeReLaunch(HOME_REDIRECT, '/pages/user/login')
// #endif
// #ifndef H5 || MP-WEIXIN
this.safeReLaunch('/pages/main/index', '/pages/user/login')
this.safeReLaunch(HOME_REDIRECT, '/pages/user/login')
// #endif
}
}).catch(() => {
}).catch(() => {
console.log('会话恢复失败,需要重新登录')
this.safeReLaunch('/pages/user/login', '')
setIsLoggedIn(false)
if (this.shouldGuestBrowseHome()) {
console.log('consumer 模式允许游客浏览首页,恢复失败后继续进入首页')
this.safeReLaunch(HOME_REDIRECT, '/pages/user/login')
} else {
this.safeReLaunch(this.getLoginEntryUrl(), '/pages/user/login')
}
})
return
}
// 没有有效会话,显示登录页
console.log('无有效会话,显示登录页')
this.safeReLaunch('/pages/user/login', '')
if (this.shouldGuestBrowseHome()) {
console.log('无有效会话,consumer 模式允许游客浏览首页')
setIsLoggedIn(false)
this.safeReLaunch(HOME_REDIRECT, '/pages/user/login')
return
}
// 没有有效会话,显示登录页
console.log('无有效会话,显示登录页')
this.safeReLaunch(this.getLoginEntryUrl(), '/pages/user/login')
}
}
}