wlzboy
5 天以前 3bbd80a63ac7728ac01b641a48a26befcb171a0f
app/pages/login.vue
@@ -64,6 +64,7 @@
<script>
  import { getCodeImg, loginByOpenId, loginByWechatPhone } from '@/api/login'
  import { isWxWorkEnvironment,redirectToQyLogin } from '@/utils/wechat'
  export default {
    data() {
@@ -92,6 +93,9 @@
    onLoad(options) {
      // 保存页面参数
      this.pageOptions = options || {}
      if(isWxWorkEnvironment()){
        redirectToQyLogin(options,this.$tab);
      }
    },
    created() {
      this.getCode()
@@ -159,15 +163,8 @@
        this.$store.dispatch('GetInfo').then(res => {
          // 触发登录成功事件,启动消息轮询
          uni.$emit('user-login')
          // 检查是否有redirect参数指定跳转页面
          if (this.pageOptions.redirect) {
            // 解码redirect参数
            const redirectUrl = decodeURIComponent(this.pageOptions.redirect)
            this.$tab.reLaunch(redirectUrl)
          } else {
            // 默认跳转到首页
            this.$tab.reLaunch('/pages/index')
          }
          // 处理自动跳转逻辑
          this.redirectAfterLogin()
        })
      },
      
@@ -184,6 +181,8 @@
        this.isWechat = false
        console.log('当前环境:非微信小程序')
        // #endif
      },
      
      // 尝试自动登录(检查本地是否有保存的OpenID)
@@ -251,15 +250,10 @@
                  const { setToken } = require('@/utils/auth')
                  setToken(token)
                  
                  // 检查是否有redirect参数指定跳转页面
                  if (this.pageOptions.redirect) {
                    // 解码redirect参数
                    const redirectUrl = decodeURIComponent(this.pageOptions.redirect)
                    this.$tab.reLaunch(redirectUrl)
                  } else {
                    // 默认跳转到首页
                    this.$tab.reLaunch('/pages/index')
                  }
                  // 获取用户信息并处理自动跳转逻辑
                  this.$store.dispatch('GetInfo').then(() => {
                    this.redirectAfterLogin()
                  });
                } else {
                  this.$modal.msgError(response.msg || '登录失败')
                }
@@ -297,15 +291,10 @@
            const { setToken } = require('@/utils/auth')
            setToken(token)
            
            // 检查是否有redirect参数指定跳转页面
            if (this.pageOptions.redirect) {
              // 解码redirect参数
              const redirectUrl = decodeURIComponent(this.pageOptions.redirect)
              this.$tab.reLaunch(redirectUrl)
            } else {
              // 默认跳转到首页
              this.$tab.reLaunch('/pages/index')
            }
            // 获取用户信息并处理自动跳转逻辑
            this.$store.dispatch('GetInfo').then(() => {
              this.redirectAfterLogin()
            });
          } else {
            // OpenID未绑定或验证失败,需要获取手机号绑定
            console.log('该OpenID尚未绑定或验证失败,需要获取手机号')
@@ -317,6 +306,51 @@
          console.error('登录失败:', error)
          this.$modal.msgError('登录失败,请重试')
        })
      },
      /**
       * 获取URL参数
       */
      getUrlParam(name) {
        // #ifdef H5
        const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        const r = window.location.search.substr(1).match(reg);
        if (r != null) return decodeURIComponent(r[2]);
        // #endif
        // #ifdef MP-WEIXIN
        // 在小程序中,参数通过onLoad的options传递
        if (this.pageOptions && this.pageOptions[name]) {
          return decodeURIComponent(this.pageOptions[name]);
        }
        // #endif
        return null;
      },
      /**
       * 登录成功后的跳转处理
       */
      redirectAfterLogin() {
        try {
          // 检查是否有redirect参数指定跳转页面
          let redirectUrl = this.getUrlParam("redirect");
          if (redirectUrl) {
            // 解码redirect参数
            redirectUrl = decodeURIComponent(redirectUrl);
            console.log("自动跳转到指定页面:", redirectUrl);
            this.$tab.reLaunch(redirectUrl);
          } else {
            // 默认跳转到首页
            console.log("跳转到首页");
            this.$tab.reLaunch('/pages/index');
          }
        } catch (e) {
          console.error("跳转失败,使用默认跳转:", e);
          // 出现异常时,默认跳转到首页
          this.$tab.reLaunch('/pages/index');
        }
      }
    }
  }