wlzboy
4 天以前 06a17c236d4cb9b8da75fce43af938cb7ea510bf
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() {
@@ -85,7 +86,20 @@
        isWechat: false, // 是否为微信小程序环境
        wechatOpenId: '', // 微信OpenID
        wechatUnionId: '', // 微信UnionID
        // 页面参数
        pageOptions: {}
      }
    },
    onLoad(options) {
      // 保存页面参数
      this.pageOptions = options || {}
      isWxWorkEnvironment().then(res=>{
        if(res){
          // console.log("企业微信环境 login.vue....")
          redirectToQyLogin(options,this.$tab);
        }
      });
    },
    created() {
      this.getCode()
@@ -153,7 +167,8 @@
        this.$store.dispatch('GetInfo').then(res => {
          // 触发登录成功事件,启动消息轮询
          uni.$emit('user-login')
          this.$tab.reLaunch('/pages/index')
          // 处理自动跳转逻辑
          this.redirectAfterLogin()
        })
      },
      
@@ -170,6 +185,8 @@
        this.isWechat = false
        console.log('当前环境:非微信小程序')
        // #endif
      },
      
      // 尝试自动登录(检查本地是否有保存的OpenID)
@@ -181,7 +198,7 @@
        // 从本地存储中获取OpenID和UnionID
        const savedOpenId = uni.getStorageSync('wechat_openid')
        const savedUnionId = uni.getStorageSync('wechat_unionid')
        const autoLogin=false;
        const autoLogin=true;
        
        if (savedOpenId && autoLogin) {
          console.log('检测到已保存的OpenID,尝试自动登录')
@@ -237,7 +254,10 @@
                  const { setToken } = require('@/utils/auth')
                  setToken(token)
                  
                  this.loginSuccess()
                  // 获取用户信息并处理自动跳转逻辑
                  this.$store.dispatch('GetInfo').then(() => {
                    this.redirectAfterLogin()
                  });
                } else {
                  this.$modal.msgError(response.msg || '登录失败')
                }
@@ -275,7 +295,10 @@
            const { setToken } = require('@/utils/auth')
            setToken(token)
            
            this.loginSuccess()
            // 获取用户信息并处理自动跳转逻辑
            this.$store.dispatch('GetInfo').then(() => {
              this.redirectAfterLogin()
            });
          } else {
            // OpenID未绑定或验证失败,需要获取手机号绑定
            console.log('该OpenID尚未绑定或验证失败,需要获取手机号')
@@ -287,6 +310,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');
        }
      }
    }
  }