hookehuyr

fix: 修复未加入家庭用户重复跳转Welcome页面的问题

修改Dashboard页面和请求拦截器中的跳转逻辑,增加当前路由检查
更新silentAuth函数支持skipRedirect参数,避免在Welcome页面重复跳转
......@@ -363,12 +363,22 @@ useLoad(async () => {
// 检查用户是否已加入家庭
const hasFamily = await checkUserHasFamily()
// 如果用户没有加入家庭,跳转到欢迎页面
// 如果用户没有加入家庭,检查当前页面是否已经是Welcome页面
if (!hasFamily) {
console.warn('用户未加入家庭,跳转到欢迎页面');
await Taro.reLaunch({
url: '/pages/Welcome/index'
})
// 获取当前页面路由
const pages = Taro.getCurrentPages();
const currentPage = pages[pages.length - 1];
const currentRoute = currentPage?.route || '';
// 只有当前页面不是Welcome页面时才跳转,避免重复跳转
if (currentRoute !== 'pages/Welcome/index') {
console.warn('用户未加入家庭,跳转到欢迎页面');
await Taro.reLaunch({
url: '/pages/Welcome/index'
})
} else {
console.log('当前已在Welcome页面,跳过跳转');
}
return
}
},
......
/*
* @Date: 2025-01-25 10:00:00
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-09-12 21:32:12
* @LastEditTime: 2025-09-12 21:49:10
* @FilePath: /lls_program/src/utils/authRedirect.js
* @Description: 授权重定向处理工具函数
*/
......@@ -180,19 +180,19 @@ export const handleSharePageAuth = async (options, callback) => {
try {
// 使用静默授权
await silentAuth(
() => {
// 静默授权成功
if (callback && typeof callback === 'function') {
callback()
}
},
() => {
// 静默授权失败时,仍然跳转到授权页面作为备选方案
Taro.navigateTo({
url: '/pages/auth/index'
})
}
)
() => {
// 静默授权成功
if (callback && typeof callback === 'function') {
callback()
}
},
() => {
// 静默授权失败时,仍然跳转到授权页面作为备选方案
Taro.navigateTo({
url: '/pages/auth/index'
})
}
)
return true
} catch (error) {
console.error('静默授权异常:', error)
......@@ -226,9 +226,10 @@ export const addShareFlag = (path) => {
* 在后台处理授权,不跳转页面,避免用户感知
* @param {Function} onSuccess - 授权成功回调
* @param {Function} onError - 授权失败回调
* @param {boolean} skipRedirect - 是否跳过重定向,默认false
* @returns {Promise} 授权结果
*/
export const silentAuth = async (onSuccess, onError) => {
export const silentAuth = async (onSuccess, onError, skipRedirect = false) => {
try {
// 检查是否已经授权
if (!needAuth()) {
......@@ -264,12 +265,12 @@ export const silentAuth = async (onSuccess, onError) => {
// 测试环境下传递openid,正式环境不传递
if (process.env.NODE_ENV === 'development') {
requestData.openid = 'h-008';
// requestData.openid = 'h-008';
// requestData.openid = 'h-009';
// requestData.openid = 'h-010';
// requestData.openid = 'h-011';
// requestData.openid = 'h-012';
// requestData.openid = 'h-013';
requestData.openid = 'h-013';
// requestData.openid = 'oWbdFvkD5VtloC50wSNR9IWiU2q8';
// requestData.openid = 'oex8h5QZnZJto3ttvO6swSvylAQo';
}
......@@ -307,13 +308,15 @@ export const silentAuth = async (onSuccess, onError) => {
try {
const hasFamily = await checkUserHasFamily()
// 如果用户没有加入家庭,跳转到欢迎页面
// 如果用户没有加入家庭,根据skipRedirect参数决定是否跳转
if (!hasFamily) {
await Taro.reLaunch({
url: '/pages/Welcome/index'
})
if (!skipRedirect) {
await Taro.reLaunch({
url: '/pages/Welcome/index'
})
}
const result = { ...response.data, redirected: true }
const result = { ...response.data, redirected: !skipRedirect }
if (onSuccess) {
onSuccess(result)
}
......
/*
* @Date: 2022-09-19 14:11:06
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-09-12 10:44:34
* @LastEditTime: 2025-09-12 22:05:53
* @FilePath: /lls_program/src/utils/request.js
* @Description: 简单axios封装,后续按实际处理
*/
......@@ -153,18 +153,31 @@ service.interceptors.response.use(
// 清除无效的sessionid
clearSessionId();
// 使用静默授权处理,避免页面跳转
// 检查当前页面,避免在Welcome页面重复跳转
let currentRoute = '';
try {
const pages = getCurrentPages();
if (pages && pages.length > 0) {
const currentPage = pages[pages.length - 1];
currentRoute = currentPage.route || '';
}
} catch (error) {
console.error('获取当前页面路由失败:', error);
}
// 使用静默授权处理,但避免重复跳转
return silentAuth(
() => {
// 授权成功后重新发起原始请求
const originalRequest = response.config;
return service.request(originalRequest);
},
(error) => {
() => {
// 静默授权失败,直接返回错误,不跳转页面
console.error('静默授权失败:', error);
return Promise.reject(new Error('授权失败,请稍后重试'));
}
},
// 传入当前路由,避免重复跳转到Welcome页面
currentRoute === 'pages/Welcome/index'
).catch(() => {
// 如果静默授权完全失败,直接返回错误,不跳转页面
return Promise.reject(new Error('授权失败,请稍后重试'));
......