|
|
@@ -17,15 +17,25 @@ Page({
|
|
|
/**
|
|
|
* 生命周期函数--监听页面加载
|
|
|
*/
|
|
|
- onLoad: function (options) {
|
|
|
- // 解码 URL
|
|
|
- let path = decodeURIComponent(options.path || '');
|
|
|
-
|
|
|
+ onLoad: async function (options) {
|
|
|
console.log('===========================================');
|
|
|
console.log('======= web-view 页面加载 =======');
|
|
|
+
|
|
|
+ // 1. 先检查用户登录状态
|
|
|
+ const loginCheck = await this.checkUserLogin(options);
|
|
|
+ if (!loginCheck) {
|
|
|
+ console.log('⚠️ 用户未登录或没有手机号,已跳转到授权页面');
|
|
|
+ console.log('===========================================');
|
|
|
+ return; // 停止后续加载
|
|
|
+ }
|
|
|
+
|
|
|
+ console.log('✅ 用户已登录且有手机号,继续加载 web-view');
|
|
|
+
|
|
|
+ // 2. 解码 URL
|
|
|
+ let path = decodeURIComponent(options.path || '');
|
|
|
+
|
|
|
console.log('原始 options.path:', options.path);
|
|
|
console.log('解码后的 path:', path);
|
|
|
- console.log('===========================================');
|
|
|
|
|
|
// 拼接额外参数(避免重复添加已存在的参数)
|
|
|
let hasQuery = path.indexOf('?') !== -1;
|
|
|
@@ -78,6 +88,115 @@ Page({
|
|
|
// 启动标题轮询监听
|
|
|
this.startTitlePolling();
|
|
|
},
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查用户登录状态
|
|
|
+ * @returns {Promise<boolean>} true: 已登录且有手机号,false: 需要登录
|
|
|
+ */
|
|
|
+ checkUserLogin: async function(options) {
|
|
|
+ try {
|
|
|
+ console.log('===========================================');
|
|
|
+ console.log('🔍 开始检查用户登录状态...');
|
|
|
+
|
|
|
+ // 检查是否有 skipAuth 参数(用于跳过登录检查)
|
|
|
+ if (options.skipAuth === 'true') {
|
|
|
+ console.log('ℹ️ 检测到 skipAuth 参数,跳过登录检查');
|
|
|
+ console.log('===========================================');
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 先调用 checkAuth 初始化用户(不强制授权)
|
|
|
+ console.log('📱 调用 checkAuth 初始化用户...');
|
|
|
+ try {
|
|
|
+ await getApp().checkAuth(false); // false = 不强制授权,只是初始化
|
|
|
+ console.log('✅ checkAuth 调用成功');
|
|
|
+ } catch (err) {
|
|
|
+ console.warn('⚠️ checkAuth 调用失败(可能是游客模式):', err);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 检查用户状态
|
|
|
+ const currentUser = Parse.User.current();
|
|
|
+ const hasMobile = currentUser?.get('mobile');
|
|
|
+ const userLogin = wx.getStorageSync('userLogin');
|
|
|
+
|
|
|
+ console.log('📊 用户状态:');
|
|
|
+ console.log(' - 当前用户:', currentUser ? currentUser.id : '无');
|
|
|
+ console.log(' - 手机号:', hasMobile || '无');
|
|
|
+ console.log(' - userLogin 存储:', userLogin || '无');
|
|
|
+
|
|
|
+ // 只有同时满足以下条件才认为已完整登录:
|
|
|
+ // 1. Parse.User.current() 存在
|
|
|
+ // 2. 用户有手机号
|
|
|
+ // 3. userLogin 存储存在
|
|
|
+ if (currentUser && hasMobile && userLogin) {
|
|
|
+ console.log('✅ 用户已完整登录');
|
|
|
+ console.log('===========================================');
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 用户未登录或没有手机号,跳转到授权页面
|
|
|
+ console.log('⚠️ 用户未完整登录,准备跳转到授权页面');
|
|
|
+
|
|
|
+ // 构建返回 URL(登录成功后返回当前页面)
|
|
|
+ const currentPath = options.path || '';
|
|
|
+ const returnUrl = encodeURIComponent(currentPath);
|
|
|
+
|
|
|
+ console.log('🔗 returnUrl:', returnUrl);
|
|
|
+ console.log('===========================================');
|
|
|
+
|
|
|
+ // 跳转到授权页面,并传递 returnUrl
|
|
|
+ wx.redirectTo({
|
|
|
+ url: `/components/app-auth/index?returnUrl=${returnUrl}`,
|
|
|
+ success: () => {
|
|
|
+ console.log('✅ redirectTo 到授权页面成功');
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ console.error('❌ redirectTo 失败:', err);
|
|
|
+
|
|
|
+ // 降级:使用 navigateTo
|
|
|
+ wx.navigateTo({
|
|
|
+ url: `/components/app-auth/index?returnUrl=${returnUrl}`,
|
|
|
+ success: () => {
|
|
|
+ console.log('✅ navigateTo 到授权页面成功');
|
|
|
+ },
|
|
|
+ fail: (err2) => {
|
|
|
+ console.error('❌ navigateTo 也失败:', err2);
|
|
|
+
|
|
|
+ // 最后降级:使用 reLaunch
|
|
|
+ wx.reLaunch({
|
|
|
+ url: `/components/app-auth/index?returnUrl=${returnUrl}`,
|
|
|
+ success: () => {
|
|
|
+ console.log('✅ reLaunch 到授权页面成功');
|
|
|
+ },
|
|
|
+ fail: (err3) => {
|
|
|
+ console.error('❌ 所有跳转方式都失败:', err3);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return false;
|
|
|
+
|
|
|
+ } catch (err) {
|
|
|
+ console.error('❌ 检查登录状态失败:', err);
|
|
|
+ console.log('===========================================');
|
|
|
+
|
|
|
+ // 出错时也跳转到授权页面
|
|
|
+ const returnUrl = options.path ? encodeURIComponent(options.path) : '';
|
|
|
+ wx.redirectTo({
|
|
|
+ url: `/components/app-auth/index?returnUrl=${returnUrl}`,
|
|
|
+ fail: () => {
|
|
|
+ wx.navigateTo({
|
|
|
+ url: `/components/app-auth/index?returnUrl=${returnUrl}`
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ },
|
|
|
|
|
|
onReady: function () {
|
|
|
},
|
|
|
@@ -244,4 +363,4 @@ Page({
|
|
|
console.error('设置 web-view 标题失败:', e);
|
|
|
}
|
|
|
}
|
|
|
-})
|
|
|
+})
|