138 lines
3.9 KiB
TypeScript
138 lines
3.9 KiB
TypeScript
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) => {
|
||
if (!options.scene) return;
|
||
|
||
try {
|
||
const decoded = decodeURIComponent(options.scene || "");
|
||
const params: Record<string, string> = {};
|
||
|
||
decoded.split("&").forEach((pair) => {
|
||
const [key, value] = pair.split("=");
|
||
if (key) params[key] = value ? decodeURIComponent(value) : "";
|
||
});
|
||
|
||
const query = Object.entries(params)
|
||
.map(([k, v]) => `${k}=${encodeURIComponent(v)}`)
|
||
.join("&");
|
||
|
||
Taro.redirectTo({
|
||
url: query ? `/${defaultPage}?${query}` : `/${defaultPage}`,
|
||
});
|
||
} catch (e) {
|
||
console.warn(e);
|
||
}
|
||
};
|
||
|
||
// 获取当前页面路径
|
||
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,
|
||
});
|
||
}
|
||
|
||
export function toast(message) {
|
||
Taro.showToast({ title: message, icon: "none" });
|
||
}
|
||
|
||
/** 从接口/请求错误中取出后端返回的文案,用于保存失败等场景的 toast */
|
||
export function getBackendErrorMsg(error: any, fallback = "操作失败"): string {
|
||
if (error == null) return fallback;
|
||
const msg =
|
||
error?.message ||
|
||
(typeof error?.error === "string" ? error.error : error?.error?.message) ||
|
||
error?.data?.message ||
|
||
error?.data?.msg ||
|
||
"";
|
||
const s = String(msg).trim();
|
||
return s || fallback;
|
||
}
|
||
|
||
// 将·作为连接符插入到标签文本之间
|
||
export function insertDotInTags(tags: string[]) {
|
||
if (!tags) return [];
|
||
return tags.join("-·-").split("-");
|
||
}
|
||
|
||
// 格式化NTRP水平显示,统一保留一位小数
|
||
export function formatNtrpDisplay(val) {
|
||
if (!val || val === "0" || val === 0) return val;
|
||
const num = Number(val);
|
||
if (isNaN(num)) return val;
|
||
return num.toFixed(1);
|
||
}
|
||
|
||
export function genNTRPRequirementText(min, max) {
|
||
if (min && max && min !== max) {
|
||
return `${formatNtrpDisplay(min)} - ${formatNtrpDisplay(max)} 之间`;
|
||
} else if (max === "1") {
|
||
return "无要求";
|
||
} else if (max) {
|
||
return `${formatNtrpDisplay(max)} 以上`;
|
||
}
|
||
return "-";
|
||
}
|
||
|
||
export function isPhoneNumber(str) {
|
||
return /^1[3-9]\d{9}$/.test(str);
|
||
}
|