修改用户登陆认证流程
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -11,3 +11,4 @@ export * from './share'
|
||||
export * from './genPoster'
|
||||
export * from './wx_helper'
|
||||
export * from './navigation';
|
||||
export * from './helper';
|
||||
|
||||
Reference in New Issue
Block a user