wlzboy
2026-03-24 6676a35122fd9c97d1b1679c211bc8a9b97f08f2
app/pages/login.vue
@@ -21,7 +21,7 @@
          <image :src="codeUrl" @click="getCode" class="login-code-img" mode="aspectFit"></image>
        </view>
      </view>
      <view class="agreement-checkbox">
      <view class="agreement-checkbox" :class="{ 'agreement-highlight': highlightAgreement }">
        <checkbox-group @change="onAgreementChange">
          <label class="checkbox-label">
            <checkbox :checked="agreedToPolicy" value="agreed" color="#007AFF" class="round-checkbox" style="margin-top: 0;" />
@@ -47,8 +47,18 @@
          <text class="cuIcon-wechat" style="margin-right: 10rpx;"></text>
        手机号码快捷登录
        </button>
        <!-- 未同意协议时,显示普通按钮,点击后弹提示 -->
        <button 
          v-else-if="isWechat"
          v-else-if="isWechat && !agreedToPolicy"
          @click="checkAgreementBeforePhone"
          class="wechat-login-btn cu-btn block bg-green lg round"
          style="margin-top: 20rpx;">
          <text class="cuIcon-wechat" style="margin-right: 10rpx;"></text>
          手机号码快捷登录
        </button>
        <!-- 已同意协议时,显示真实授权按钮 -->
        <button
          v-else-if="isWechat && agreedToPolicy"
          open-type="getPhoneNumber" 
          @getphonenumber="onGetPhoneNumber"
          class="wechat-login-btn cu-btn block bg-green lg round"
@@ -64,6 +74,7 @@
<script>
  import { getCodeImg, loginByOpenId, loginByWechatPhone } from '@/api/login'
  import { isWxWorkEnvironment,redirectToQyLogin } from '@/utils/wechat'
  export default {
    data() {
@@ -81,11 +92,26 @@
          code: "",
          uuid: ''
        },
        // 协议区域高亮提示状态
        highlightAgreement: false,
        // 微信一键登录相关
        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 +179,8 @@
        this.$store.dispatch('GetInfo').then(res => {
          // 触发登录成功事件,启动消息轮询
          uni.$emit('user-login')
          this.$tab.reLaunch('/pages/index')
          // 处理自动跳转逻辑
          this.redirectAfterLogin()
        })
      },
      
@@ -170,6 +197,8 @@
        this.isWechat = false
        console.log('当前环境:非微信小程序')
        // #endif
      },
      
      // 尝试自动登录(检查本地是否有保存的OpenID)
@@ -189,6 +218,16 @@
          this.wechatUnionId = savedUnionId // 可能为null
          this.loginByOpenId()
        }
      },
      // 未同意协议时点击手机号快捷登录的处理
      checkAgreementBeforePhone() {
        this.$modal.msgError("请先阅读并同意用户协议和隐私政策")
        // 滚动到协议区域(高亮提示)
        this.highlightAgreement = true
        setTimeout(() => {
          this.highlightAgreement = false
        }, 2000)
      },
      
      // 处理获取手机号的回调
@@ -237,7 +276,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 +317,10 @@
            const { setToken } = require('@/utils/auth')
            setToken(token)
            
            this.loginSuccess()
            // 获取用户信息并处理自动跳转逻辑
            this.$store.dispatch('GetInfo').then(() => {
              this.redirectAfterLogin()
            });
          } else {
            // OpenID未绑定或验证失败,需要获取手机号绑定
            console.log('该OpenID尚未绑定或验证失败,需要获取手机号')
@@ -287,6 +332,57 @@
          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");
          // 如果没有redirect参数,检查是否有保存的目标页面
          if (!redirectUrl) {
            const { getTargetUrl } = require('@/utils/auth')
            redirectUrl = getTargetUrl()
          }
          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');
        }
      }
    }
  }
@@ -427,6 +523,22 @@
        }
      }
      
      .agreement-highlight {
        animation: highlight-shake 0.5s ease-in-out;
        background-color: #fff3cd;
        border-radius: 16rpx;
        border: 2rpx solid #ffc107;
      }
      @keyframes highlight-shake {
        0% { transform: translateX(0); }
        20% { transform: translateX(-8rpx); }
        40% { transform: translateX(8rpx); }
        60% { transform: translateX(-8rpx); }
        80% { transform: translateX(8rpx); }
        100% { transform: translateX(0); }
      }
      .agreement-checkbox {
        margin: 50rpx 0 30rpx 0;
        padding: 20rpx;