This commit is contained in:
2026-01-17 13:15:30 +08:00
parent d517af6469
commit b79e7f2113
4 changed files with 76 additions and 120 deletions

View File

@@ -9,6 +9,7 @@ import { useAppStore } from "@/stores/appStore";
import { useWeixinShare } from "@/composables/useWeixinShare";
import BindPhoneDialog from "@/components/BindPhoneDialog.vue";
import BindPhoneOnlyDialog from "@/components/BindPhoneOnlyDialog.vue";
import WechatOverlay from "@/components/WechatOverlay.vue";
const router = useRouter();
const route = useRoute();
@@ -265,66 +266,67 @@ const refreshTokenIfNeeded = async () => {
* 这个逻辑已经在路由守卫中实现了
* 这里保留这个函数的备份,以防需要其他地方调用
*/
const getWeixinAuthUrl = () => {
const isAuthenticated = localStorage.getItem("token");
// const getWeixinAuthUrl = () => {
// const isAuthenticated = localStorage.getItem("token");
// 检查 token 是否过期
const accessExpire = localStorage.getItem("accessExpire");
const now = Date.now();
let isTokenExpired = false;
if (accessExpire) {
isTokenExpired = now > parseInt(accessExpire) * 1000;
}
console.log("WeChat auth check:", {
isWeChat: isWeChat.value,
isAuthenticated,
isTokenExpired
});
if (isWeChat.value && !isAuthenticated && !isTokenExpired) {
console.log("🔄 Initiating WeChat auth flow");
// 如果正在授权中或已完成授权,则阻止重复授权
console.log("Auth store state:", {
isWeixinAuthing: authStore.isWeixinAuthing,
weixinAuthComplete: authStore.weixinAuthComplete
});
if (authStore.isWeixinAuthing || authStore.weixinAuthComplete) {
return;
}
// 保存目标路由
authStore.startWeixinAuth(route);
console.log("🔖 Saved pendingRoute for WeChat auth:", route.fullPath);
const appId = import.meta.env.VITE_WECHAT_APP_ID;
const url = new URL(window.location.href);
const params = new URLSearchParams(url.search);
params.delete("code");
params.delete("state");
// // 检查 token 是否过期
// const accessExpire = localStorage.getItem("accessExpire");
// const now = Date.now();
// let isTokenExpired = false;
// if (accessExpire) {
// isTokenExpired = now > parseInt(accessExpire) * 1000;
// }
// console.log("WeChat auth check:", {
// isWeChat: isWeChat.value,
// isAuthenticated,
// isTokenExpired
// });
// if (isWeChat.value && !isAuthenticated && !isTokenExpired) {
// console.log("🔄 Initiating WeChat auth flow");
// // 如果正在授权中或已完成授权,则阻止重复授权
// console.log("Auth store state:", {
// isWeixinAuthing: authStore.isWeixinAuthing,
// weixinAuthComplete: authStore.weixinAuthComplete
// });
// if (authStore.isWeixinAuthing || authStore.weixinAuthComplete) {
// return;
// }
// // 保存目标路由
// authStore.startWeixinAuth(route);
// console.log("🔖 Saved pendingRoute for WeChat auth:", route.fullPath);
// const appId = import.meta.env.VITE_WECHAT_APP_ID;
// const url = new URL(window.location.href);
// const params = new URLSearchParams(url.search);
// params.delete("code");
// params.delete("state");
// 使用配置的固定域名,如果没有配置则使用当前域名
const redirectDomain = import.meta.env.VITE_WECHAT_REDIRECT_DOMAIN || url.origin;
const cleanUrl = `${redirectDomain}${url.pathname}${params.toString() ? "?" + params.toString() : ""
}`;
const redirectUri = encodeURIComponent(cleanUrl);
// // 使用配置的固定域名,如果没有配置则使用当前域名
// const redirectDomain = import.meta.env.VITE_WECHAT_REDIRECT_DOMAIN || url.origin;
// const cleanUrl = `${redirectDomain}${url.pathname}${params.toString() ? "?" + params.toString() : ""
// }`;
// const redirectUri = encodeURIComponent(cleanUrl);
console.log("🔗 WeChat redirectUri config:", {
configuredDomain: import.meta.env.VITE_WECHAT_REDIRECT_DOMAIN || "未配置(使用当前域名)",
currentOrigin: url.origin,
finalRedirectDomain: redirectDomain,
redirectUri: cleanUrl
});
const weixinAuthUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_base&state=snsapi_base#wechat_redirect`;
// console.log("🔗 WeChat redirectUri config:", {
// configuredDomain: import.meta.env.VITE_WECHAT_REDIRECT_DOMAIN || "未配置(使用当前域名)",
// currentOrigin: url.origin,
// finalRedirectDomain: redirectDomain,
// redirectUri: cleanUrl
// });
// const weixinAuthUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirectUri}&response_type=code&scope=snsapi_base&state=snsapi_base#wechat_redirect`;
console.log(
"🔄 Triggering WeChat auth from route guard, pendingRoute:",
route.fullPath
);
window.location.href = weixinAuthUrl;
}
};
// console.log(
// "🔄 Triggering WeChat auth from route guard, pendingRoute:",
// route.fullPath
// );
// window.location.href = weixinAuthUrl;
// }
// };
</script>
<template>
<RouterView />
<BindPhoneDialog />
<WechatOverlay />
<BindPhoneOnlyDialog />
</template>

View File

@@ -232,7 +232,7 @@ const isIdCardValid = computed(() => /^\d{17}[\dX]$/i.test(formData.idCard));
const isLoggedIn = computed(() => userStore.isLoggedIn);
const buttonText = computed(() => {
return isLoggedIn.value ? '立即查询' : '前往登录';
return '立即查询'; // 不需要登录,始终显示"立即查询"
});
// 获取产品背景图片
@@ -322,33 +322,14 @@ function handleBindSuccess() {
// 处理输入框点击事件
const handleInputClick = async () => {
if (!isLoggedIn.value) {
if (!isWeChat.value && props.type !== 'promotion') {
try {
await showConfirmDialog({
title: '提示',
message: '您需要登录后才能进行查询,是否前往登录?',
confirmButtonText: '前往登录',
cancelButtonText: '取消',
});
router.push('/login');
} catch {
// 用户点击取消,什么都不做
}
}
} else {
if (isWeChat.value && !userStore.mobile && props.type !== 'promotion') {
dialogStore.openBindPhone();
}
// 不需要登录检查,允许未登录用户使用
// 如果已登录且在微信环境且未绑定手机号,提示绑定
if (isLoggedIn.value && isWeChat.value && !userStore.mobile && props.type !== 'promotion') {
dialogStore.openBindPhone();
}
};
function handleSubmit() {
if (!isWeChat.value && !isLoggedIn.value && props.type !== 'promotion') {
router.push('/login');
return;
}
// 基本协议验证
if (!formData.agreeToTerms) {
showToast({ message: `请阅读并同意用户协议和隐私政策` });
@@ -379,8 +360,9 @@ function handleSubmit() {
return;
}
// 检查是否需要绑定手机号
if (!userStore.mobile && props.type !== 'promotion') {
// 不需要登录也能查询,直接提交请求
// 如果是已登录用户在微信环境且未绑定手机号,提示绑定(但不强制)
if (isLoggedIn.value && !userStore.mobile && props.type !== 'promotion' && isWeChat.value) {
pendingPayment.value = true;
dialogStore.openBindPhone();
} else {

View File

@@ -18,42 +18,28 @@ const feature = ref(route.params.feature);
// 获取产品信息
const featureData = ref({});
// 检查是否可以查询:已登录且已绑定手机号
// 检查是否可以查询:不需要登录,直接允许查询
const canQuery = computed(() => {
return isLoggedIn.value && mobile.value && mobile.value.trim() !== '';
return true; // 允许未登录用户查询
});
// 检查登录状态和手机号绑定
// 初始化:检查支付回调并加载产品信息
onMounted(async () => {
// 检查支付回调
isFinishPayment();
// 检查是否已登录
// 如果有 token尝试加载用户信息但不强制
const token = localStorage.getItem("token");
if (!token) {
showToast({ message: "请先登录才能使用查询功能" });
router.replace("/login");
return;
if (token) {
try {
await userStore.fetchUserInfo();
} catch (error) {
console.warn("获取用户信息失败(可选):", error);
// 不影响查询功能,继续执行
}
}
// 获取用户信息(包括手机号)
try {
await userStore.fetchUserInfo();
} catch (error) {
console.error("获取用户信息失败:", error);
showToast({ message: "获取用户信息失败,请重新登录" });
router.replace("/login");
return;
}
// 检查是否已绑定手机号
if (!mobile.value || mobile.value.trim() === '') {
showToast({ message: "请先绑定手机号才能使用查询功能" });
router.replace("/me");
return;
}
// 已登录且已绑定手机号,可以查询
// 直接加载产品信息,不需要登录
await getProduct();
});
@@ -88,20 +74,6 @@ async function getProduct() {
</script>
<template>
<!-- 未登录或未绑定手机号提示 -->
<div v-if="!canQuery" class="min-h-screen flex items-center justify-center p-6">
<div class="text-center">
<div class="text-lg font-bold mb-4">无法使用查询功能</div>
<div class="text-gray-600 mb-6">
<span v-if="!isLoggedIn">请先登录</span>
<span v-else>请先绑定手机号</span>
</div>
<button @click="router.push(isLoggedIn ? '/me' : '/login')"
class="px-6 py-3 bg-primary text-white rounded-lg">
{{ isLoggedIn ? '去绑定手机号' : '去登录' }}
</button>
</div>
</div>
<!-- 已登录且已绑定手机号可以使用查询功能 -->
<InquireForm v-else :type="'normal'" :feature="feature" :feature-data="featureData" />
<!-- 直接显示查询表单不需要登录 -->
<InquireForm :type="'normal'" :feature="feature" :feature-data="featureData" />
</template>