wlzboy
3 天以前 40a8157440e3b906da8f52e07d939d78c3f4c313
app/pages/qylogin.vue
@@ -34,13 +34,68 @@
  },
  onLoad(options) {
    // 保存页面参数
    this.pageOptions = options || {};
    // 页面加载时执行免登流程
    this.qyWechatAutoLogin();
    // 添加容错处理
    try {
      // 保存页面参数
      this.pageOptions = options || {};
      // 打印环境信息
      this.logEnvironmentInfo();
      // 页面加载时执行免登流程
      this.qyWechatAutoLogin();
    } catch (e) {
      console.error('页面加载出错:', e);
      this.loading = false;
      this.error = true;
      this.errorMessage = "页面初始化失败,请重试";
    }
  },
  methods: {
    /**
     * 打印环境信息(调试用)
     */
    logEnvironmentInfo() {
      // #ifdef MP-WEIXIN
      console.log('========== 环境信息 ==========');
      console.log('wx 是否存在:', typeof wx !== 'undefined');
      console.log('wx.qy 是否存在:', typeof wx !== 'undefined' && typeof wx.qy !== 'undefined');
      if (typeof wx !== 'undefined') {
        console.log('wx.login 是否存在:', typeof wx.login === 'function');
        if (typeof wx.qy !== 'undefined') {
          console.log('wx.qy.login 是否存在:', typeof wx.qy.login === 'function');
          console.log('wx.qy 对象:', wx.qy);
        } else {
          console.warn('⚠️ wx.qy 不存在!');
          console.warn('可能原因:');
          console.warn('1. 当前在开发者工具中,wx.qy API 可能不可用');
          console.warn('2. 需要在真机上调试');
          console.warn('3. appid 配置可能不正确');
        }
        // 获取系统信息
        try {
          const systemInfo = wx.getSystemInfoSync();
          console.log('系统信息:', systemInfo);
        } catch (e) {
          console.error('获取系统信息失败:', e);
        }
        // 获取账号信息
        try {
          const accountInfo = wx.getAccountInfoSync();
          console.log('账号信息:', accountInfo);
        } catch (e) {
          console.error('获取账号信息失败:', e);
        }
      }
      console.log('==============================');
      // #endif
    },
    /**
     * 企业微信免登流程
     */
@@ -127,26 +182,80 @@
    },
    /**
     * 等待 wx.qy 对象初始化
     */
    waitForWxQy(timeout = 5000) {
      return new Promise((resolve, reject) => {
        const startTime = Date.now();
        let checkCount = 0;
        const checkWxQy = () => {
          checkCount++;
          const elapsed = Date.now() - startTime;
          if (typeof wx !== 'undefined' && typeof wx.qy !== 'undefined' && typeof wx.qy.login === 'function') {
            console.log(`✅ wx.qy 已初始化 (耗时: ${elapsed}ms, 检查次数: ${checkCount})`);
            resolve(true);
          } else if (elapsed > timeout) {
            console.error(`❌ 等待 wx.qy 初始化超时 (${timeout}ms)`);
            console.error('当前状态:', {
              wx存在: typeof wx !== 'undefined',
              'wx.qy存在': typeof wx !== 'undefined' && typeof wx.qy !== 'undefined',
              检查次数: checkCount
            });
            // 提供更详细的错误信息
            const errorMsg = '企业微信环境初始化超时。\n\n' +
                           '可能的原因:\n' +
                           '1. 当前在开发者工具中,wx.qy API 不可用\n' +
                           '2. 请使用真机预览或真机调试\n' +
                           '3. 检查小程序 appid 是否配置正确';
            reject(new Error(errorMsg));
          } else {
            if (checkCount % 10 === 1) {
              console.log(`⏳ 等待 wx.qy 初始化... (${elapsed}ms, 第${checkCount}次检查)`);
            }
            setTimeout(checkWxQy, 100);
          }
        };
        checkWxQy();
      });
    },
    /**
     * 获取企业微信登录code
     */
    getLoginCode() {
      return new Promise((resolve, reject) => {
      return new Promise(async (resolve, reject) => {
        // #ifdef MP-WEIXIN
        // 使用企业微信小程序API获取code
        wx.qy.login({
          success: (res) => {
            if (res.code) {
              console.log("企业微信小程序 ---> code:", res.code);
              resolve(res.code);
            } else {
              reject(new Error("获取code失败:" + res.errMsg));
            }
          },
          fail: (err) => {
            console.error("wx.qy.login调用失败:", err);
            reject(new Error("企业微信登录接口调用失败:" + err.errMsg));
          },
        });
        try {
          // 检查是否在企业微信小程序环境
          console.log('检查企业微信环境,wx.qy 是否存在:', typeof wx !== 'undefined' && typeof wx.qy !== 'undefined');
          // 等待 wx.qy 初始化完成
          await this.waitForWxQy();
          // 使用企业微信小程序API获取code
          console.log('使用 wx.qy.login 获取code');
          wx.qy.login({
            success: (res) => {
              if (res.code) {
                console.log("企业微信小程序 ---> code:", res.code);
                resolve(res.code);
              } else {
                reject(new Error("获取code失败:" + (res.errMsg || '未知错误')));
              }
            },
            fail: (err) => {
              console.error("wx.qy.login调用失败:", err);
              reject(new Error("企业微信登录接口调用失败:" + (err.errMsg || '未知错误')));
            },
          });
        } catch (error) {
          console.error('获取登录code异常:', error);
          reject(error);
        }
        // #endif
        
        // #ifndef MP-WEIXIN
@@ -215,6 +324,12 @@
        // 检查是否有redirect参数指定跳转页面
        let redirectUrl = this.getUrlParam("redirect");
        
        // 如果没有redirect参数,检查是否有保存的目标页面
        if (!redirectUrl) {
          const { getTargetUrl } = require('@/utils/auth')
          redirectUrl = getTargetUrl()
        }
        if (redirectUrl) {
          // 解码redirect参数
          redirectUrl = decodeURIComponent(redirectUrl);