修改用户登陆认证流程

This commit is contained in:
张成
2025-11-16 23:55:49 +08:00
parent 5f8fb9b1b3
commit abbfc27479
12 changed files with 229 additions and 99 deletions

View File

@@ -1,4 +1,6 @@
import Taro from "@tarojs/taro";
import { check_login_status, get_user_info } from "@/services/loginService";
import { useUser } from "@/store/userStore";
// 普通函数,不调用 useLoad
export const sceneRedirectLogic = (options, defaultPage: string) => {
@@ -25,6 +27,64 @@ export const sceneRedirectLogic = (options, defaultPage: string) => {
}
};
// 获取当前页面路径
const getCurrentPagePath = (): string => {
const pages = Taro.getCurrentPages();
const currentPage = pages[pages.length - 1];
return currentPage ? `/${currentPage.route}${currentPage.options ? '?' + Object.entries(currentPage.options).map(([k, v]) => `${k}=${encodeURIComponent(v as string)}`).join('&') : ''}` : '';
};
// 跳转到登录页
const navigateToLogin = (currentPath?: string) => {
const path = currentPath || getCurrentPagePath();
(Taro as any).navigateTo({
url: `/login_pages/index/index${path ? `?redirect=${encodeURIComponent(path)}` : ''}`,
});
};
// 检查登录状态,如果未登录则跳转到登录页
export const requireLogin = (): boolean => {
const isLoggedIn = check_login_status();
if (!isLoggedIn) {
navigateToLogin();
return false;
}
return true;
};
// 检查登录状态和手机号,如果未登录或没有手机号则跳转到登录页绑定手机号
export const requireLoginWithPhone = (): boolean => {
// 先检查登录状态
const isLoggedIn = check_login_status();
if (!isLoggedIn) {
navigateToLogin();
return false;
}
// 检查是否有手机号
// 优先从 store 中获取用户信息
const userInfo = useUser.getState().user;
let phone = (userInfo as any)?.phone;
// 如果 store 中没有,尝试从本地存储获取
if (!phone || phone.trim() === '') {
const localUserInfo = get_user_info();
phone = localUserInfo?.phone;
}
// 如果用户信息中没有手机号,或者手机号为空
if (!phone || phone.trim() === '') {
console.log('用户未绑定手机号,跳转到登录页绑定');
navigateToLogin();
return false;
}
return true;
};
export function navto(url) {
Taro.navigateTo({
url: url,

View File

@@ -11,3 +11,4 @@ export * from './share'
export * from './genPoster'
export * from './wx_helper'
export * from './navigation';
export * from './helper';