|
|
@@ -1,6 +1,7 @@
|
|
|
const Parse = getApp().Parse;
|
|
|
const company = getApp().globalData.company;
|
|
|
const login = require("../../../utils/login");
|
|
|
+const clearLogin = require("../../../utils/clearLogin");
|
|
|
|
|
|
Component({
|
|
|
/**
|
|
|
@@ -17,10 +18,13 @@ Component({
|
|
|
|
|
|
lifetimes: {
|
|
|
created() {},
|
|
|
- attached: function () {
|
|
|
+ attached: async function () {
|
|
|
// 页面加载时检查是否首次访问
|
|
|
this.checkFirstVisit();
|
|
|
|
|
|
+ // 检查并清理异常的登录状态
|
|
|
+ await this.checkAndCleanInvalidLoginState();
|
|
|
+
|
|
|
// 在控制台显示当前登录用户信息
|
|
|
this.showCurrentUser();
|
|
|
},
|
|
|
@@ -28,18 +32,34 @@ Component({
|
|
|
|
|
|
pageLifetimes: {
|
|
|
show: function() {
|
|
|
- // 页面显示时,检查并更新登录状态
|
|
|
- const currentUser = Parse.User.current();
|
|
|
- if (currentUser && currentUser.get('mobile')) {
|
|
|
- const userLogin = wx.getStorageSync('userLogin');
|
|
|
- if (!userLogin || userLogin !== currentUser.id) {
|
|
|
- wx.setStorageSync("userLogin", currentUser.id);
|
|
|
- console.log('✅ 页面显示时更新 userLogin:', currentUser.id);
|
|
|
- }
|
|
|
- }
|
|
|
+ console.log('===========================================');
|
|
|
+ console.log('======= 页面显示 (pageLifetimes.show) =======');
|
|
|
|
|
|
- // 更新显示的用户信息
|
|
|
- this.showCurrentUser();
|
|
|
+ // 延迟检查登录状态,等待 Parse 更新
|
|
|
+ setTimeout(() => {
|
|
|
+ const currentUser = Parse.User.current();
|
|
|
+ console.log('当前用户:', currentUser ? currentUser.id : '无');
|
|
|
+ console.log('手机号:', currentUser?.get('mobile') || '无');
|
|
|
+
|
|
|
+ if (currentUser && currentUser.get('mobile')) {
|
|
|
+ const userLogin = wx.getStorageSync('userLogin');
|
|
|
+ console.log('userLogin 存储:', userLogin || '无');
|
|
|
+
|
|
|
+ if (!userLogin || userLogin !== currentUser.id) {
|
|
|
+ wx.setStorageSync("userLogin", currentUser.id);
|
|
|
+ console.log('✅ 页面显示时更新 userLogin:', currentUser.id);
|
|
|
+
|
|
|
+ // 清除游客模式标记
|
|
|
+ wx.removeStorageSync('isGuestMode');
|
|
|
+ console.log('✅ 已清除游客模式标记');
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ console.log('===========================================');
|
|
|
+
|
|
|
+ // 更新显示的用户信息
|
|
|
+ this.showCurrentUser();
|
|
|
+ }, 500); // 延迟 500ms,等待 Parse 更新
|
|
|
}
|
|
|
},
|
|
|
|
|
|
@@ -47,6 +67,44 @@ Component({
|
|
|
* 组件的方法列表
|
|
|
*/
|
|
|
methods: {
|
|
|
+ /**
|
|
|
+ * 检查并清理异常的登录状态
|
|
|
+ * 在页面加载时自动执行,避免点击登录时白屏
|
|
|
+ */
|
|
|
+ async checkAndCleanInvalidLoginState() {
|
|
|
+ try {
|
|
|
+ const currentUser = Parse.User.current();
|
|
|
+ const userLogin = wx.getStorageSync('userLogin');
|
|
|
+
|
|
|
+ // 如果有 Parse 用户但没有 userLogin,说明状态不一致
|
|
|
+ if (currentUser && !userLogin) {
|
|
|
+ console.log('⚠️ 检测到异常登录状态:有 Parse 用户但没有 userLogin');
|
|
|
+ console.log(' 用户 ID:', currentUser.id);
|
|
|
+ console.log(' 手机号:', currentUser.get('mobile') || '无');
|
|
|
+
|
|
|
+ // 如果用户有手机号,补充设置 userLogin
|
|
|
+ if (currentUser.get('mobile')) {
|
|
|
+ wx.setStorageSync('userLogin', currentUser.id);
|
|
|
+ console.log('✅ 已补充设置 userLogin');
|
|
|
+ } else {
|
|
|
+ // 如果用户没有手机号,清除这个无效的用户
|
|
|
+ console.log('🧹 用户没有手机号,清除无效状态');
|
|
|
+ await clearLogin.clearAllLoginState();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果有 userLogin 但没有 Parse 用户,说明状态不一致
|
|
|
+ if (!currentUser && userLogin) {
|
|
|
+ console.log('⚠️ 检测到异常登录状态:有 userLogin 但没有 Parse 用户');
|
|
|
+ console.log(' userLogin:', userLogin);
|
|
|
+ console.log('🧹 清除无效的 userLogin');
|
|
|
+ wx.removeStorageSync('userLogin');
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.error('❌ 检查登录状态失败:', error);
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
/**
|
|
|
* 在控制台显示当前登录用户信息
|
|
|
*/
|
|
|
@@ -493,44 +551,94 @@ Component({
|
|
|
console.log('===========================================');
|
|
|
|
|
|
// 方案定制需要登录
|
|
|
- const currentUser = Parse.User.current();
|
|
|
+ let currentUser = Parse.User.current();
|
|
|
const isGuestMode = wx.getStorageSync('isGuestMode');
|
|
|
- const userLogin = wx.getStorageSync('userLogin');
|
|
|
+ let userLogin = wx.getStorageSync('userLogin');
|
|
|
|
|
|
- console.log('当前用户:', currentUser ? '已登录' : '未登录');
|
|
|
- console.log('用户手机号:', currentUser?.get('mobile'));
|
|
|
+ console.log('当前用户:', currentUser ? currentUser.id : '无');
|
|
|
+ console.log('用户手机号:', currentUser?.get('mobile') || '无');
|
|
|
console.log('游客模式:', isGuestMode);
|
|
|
- console.log('userLogin 存储:', userLogin);
|
|
|
+ console.log('userLogin 存储:', userLogin || '无');
|
|
|
+
|
|
|
+ // 如果有 Parse 用户但没有 userLogin,可能是刚授权完成,等待一下
|
|
|
+ if (currentUser && currentUser.get('mobile') && !userLogin) {
|
|
|
+ console.log('⏳ 检测到刚授权完成,等待状态同步...');
|
|
|
+
|
|
|
+ // 等待 500ms 后重新检查
|
|
|
+ await new Promise(resolve => setTimeout(resolve, 500));
|
|
|
+
|
|
|
+ // 重新获取状态
|
|
|
+ currentUser = Parse.User.current();
|
|
|
+ userLogin = wx.getStorageSync('userLogin');
|
|
|
+
|
|
|
+ // 如果还是没有 userLogin,手动设置
|
|
|
+ if (currentUser && currentUser.get('mobile') && !userLogin) {
|
|
|
+ wx.setStorageSync("userLogin", currentUser.id);
|
|
|
+ userLogin = currentUser.id;
|
|
|
+ console.log('✅ 手动设置 userLogin:', userLogin);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
// 检查用户是否真正登录(有手机号且有 userLogin 存储)
|
|
|
const isReallyLoggedIn = currentUser && currentUser.get('mobile') && userLogin;
|
|
|
|
|
|
- if (!isReallyLoggedIn || isGuestMode) {
|
|
|
- console.log('⚠️ 用户未完成登录或是游客,显示登录提示');
|
|
|
-
|
|
|
- // 如果是游客模式,清除游客标记
|
|
|
- if (isGuestMode) {
|
|
|
- wx.removeStorageSync('isGuestMode');
|
|
|
- wx.removeStorageSync('userLogin');
|
|
|
- }
|
|
|
+ console.log('最终登录状态:', isReallyLoggedIn ? '已登录' : '未登录');
|
|
|
+
|
|
|
+ // 如果用户已经真正登录,清除游客模式标记
|
|
|
+ if (isReallyLoggedIn && isGuestMode) {
|
|
|
+ console.log('✅ 用户已登录,清除游客模式标记');
|
|
|
+ wx.removeStorageSync('isGuestMode');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 只有在真正未登录时才提示
|
|
|
+ if (!isReallyLoggedIn) {
|
|
|
+ console.log('⚠️ 用户未完成登录,显示登录提示');
|
|
|
|
|
|
wx.showModal({
|
|
|
title: '需要登录',
|
|
|
content: '方案定制和咨询功能需要登录后使用,是否立即登录?',
|
|
|
confirmText: '立即登录',
|
|
|
cancelText: '取消',
|
|
|
- success: (res) => {
|
|
|
+ success: async (res) => {
|
|
|
if (res.confirm) {
|
|
|
console.log('用户选择:立即登录');
|
|
|
- console.log('准备调用 login.loginNow()');
|
|
|
|
|
|
- // 直接调用登录方法,不需要延迟
|
|
|
- const loginResult = login.loginNow();
|
|
|
- console.log('login.loginNow() 返回值:', loginResult);
|
|
|
+ // 显示加载提示
|
|
|
+ wx.showLoading({
|
|
|
+ title: '准备登录...',
|
|
|
+ mask: true
|
|
|
+ });
|
|
|
|
|
|
- // 如果返回 false,说明已经跳转到登录页面
|
|
|
- if (!loginResult) {
|
|
|
- console.log('✅ 已跳转到登录页面');
|
|
|
+ try {
|
|
|
+ // 强制清除所有登录相关的缓存
|
|
|
+ console.log('🧹 清除旧的登录状态...');
|
|
|
+ await clearLogin.clearAllLoginState();
|
|
|
+
|
|
|
+ // 等待一下,确保状态清除完成
|
|
|
+ await new Promise(resolve => setTimeout(resolve, 300));
|
|
|
+
|
|
|
+ wx.hideLoading();
|
|
|
+
|
|
|
+ console.log('准备调用 login.loginNow()');
|
|
|
+
|
|
|
+ // 直接调用登录方法
|
|
|
+ const loginResult = login.loginNow();
|
|
|
+ console.log('login.loginNow() 返回值:', loginResult);
|
|
|
+
|
|
|
+ // 如果返回 false,说明已经跳转到登录页面
|
|
|
+ if (!loginResult) {
|
|
|
+ console.log('✅ 已跳转到登录页面');
|
|
|
+ // 设置待处理的跳转,登录成功后自动跳转到咨询页面
|
|
|
+ wx.setStorageSync('pendingNavigation', 'consultation');
|
|
|
+ console.log('📌 已设置待处理跳转: consultation');
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ wx.hideLoading();
|
|
|
+ console.error('❌ 登录准备失败:', error);
|
|
|
+ wx.showToast({
|
|
|
+ title: '登录准备失败,请重试',
|
|
|
+ icon: 'none'
|
|
|
+ });
|
|
|
}
|
|
|
} else {
|
|
|
console.log('用户选择:取消');
|
|
|
@@ -541,6 +649,7 @@ Component({
|
|
|
}
|
|
|
|
|
|
console.log('✅ 用户已登录,跳转到咨询页面');
|
|
|
+ console.log('===========================================');
|
|
|
await this.navigateToH5Page('owner/nav/consultation');
|
|
|
},
|
|
|
|
|
|
@@ -557,8 +666,15 @@ Component({
|
|
|
const currentUser = Parse.User.current();
|
|
|
let userInfo = wx.getStorageSync("userLogin");
|
|
|
|
|
|
+ console.log('📱 当前登录状态检查:');
|
|
|
+ console.log(' Parse.User.current():', currentUser ? currentUser.id : '无');
|
|
|
+ console.log(' 用户手机号:', currentUser?.get('mobile') || '无');
|
|
|
+ console.log(' userLogin 存储:', userInfo || '无');
|
|
|
+ console.log(' Session Token (前20字符):', currentUser?.getSessionToken()?.substring(0, 20) || '无');
|
|
|
+
|
|
|
// 检查是否是游客模式
|
|
|
const isGuestMode = wx.getStorageSync('isGuestMode');
|
|
|
+ console.log(' 游客模式:', isGuestMode);
|
|
|
|
|
|
// 定义允许游客访问的页面
|
|
|
const guestAllowedPages = [
|
|
|
@@ -624,9 +740,10 @@ Component({
|
|
|
}
|
|
|
|
|
|
let token = currentUser.getSessionToken();
|
|
|
- console.log('🔑 当前 Session Token:', token);
|
|
|
- console.log(' - Token 长度:', token ? token.length : 0);
|
|
|
- console.log(' - Token 前20个字符:', token ? token.substring(0, 20) : 'null');
|
|
|
+ console.log('🔑 准备传递的 Token:');
|
|
|
+ console.log(' 完整 Token:', token);
|
|
|
+ console.log(' Token 长度:', token ? token.length : 0);
|
|
|
+ console.log(' Token 前20个字符:', token ? token.substring(0, 20) : 'null');
|
|
|
|
|
|
if (!token) {
|
|
|
console.error('❌ 无法获取 Session Token!');
|
|
|
@@ -688,6 +805,10 @@ Component({
|
|
|
|
|
|
h5Url += `token=${token}`;
|
|
|
|
|
|
+ console.log('🌐 构建的 H5 URL:');
|
|
|
+ console.log(' 完整 URL:', h5Url);
|
|
|
+ console.log(' URL 中的 token (前20字符):', token.substring(0, 20));
|
|
|
+
|
|
|
// 添加额外的参数(如 productId)
|
|
|
if (extraParams && Object.keys(extraParams).length > 0) {
|
|
|
for (const [key, value] of Object.entries(extraParams)) {
|
|
|
@@ -711,9 +832,14 @@ Component({
|
|
|
webViewPath += `&storeName=${encodeURIComponent(storeName)}`;
|
|
|
}
|
|
|
|
|
|
+ console.log('📄 最终跳转路径:', webViewPath.substring(0, 150) + '...');
|
|
|
+ console.log('===========================================');
|
|
|
+
|
|
|
wx.navigateTo({
|
|
|
url: webViewPath,
|
|
|
- success: () => {},
|
|
|
+ success: () => {
|
|
|
+ console.log('✅ 跳转成功');
|
|
|
+ },
|
|
|
fail: (err) => {
|
|
|
console.error('❌ 跳转失败:', err);
|
|
|
wx.showToast({
|