zhizhijie
9 天以前 2b39c4eb64b194e23f338873e4467eda2b18ccc3
traffic-audit-web/src/views/Login.vue
@@ -40,16 +40,20 @@
      captchaId: '',
      clickIds: [],
      loggingIn: false,
      captchaRequired: true
      captchaRequired: false
    }
  },
  created() {
    axios.get('/api/auth/captcha-config')
    // 验证码是否启用一律以后端 /api/auth/captcha-config 为准:
    // 默认不显示,只有后端明确返回 required=true 才渲染验证码组件。
    // 修复:此前默认 true 且请求失败时静默忽略,切换网络后该配置读不到,
    // 登录页就会残留一个加载不出来的验证码白框并提示“验证码加载失败,请检查网络”。
    axios.get('/api/auth/captcha-config', { timeout: 10000 })
      .then(res => {
        const d = res.data
        if (d && d.code === 200) this.captchaRequired = d.data.required !== false
        this.captchaRequired = !!(d && d.code === 200 && d.data && d.data.required === true)
      })
      .catch(() => {})
      .catch(() => { this.captchaRequired = false })
  },
  methods: {
    onCaptchaVerified(payload) {
@@ -75,14 +79,14 @@
          password: this.form.password,
          captchaId: this.captchaId,
          captchaClickIds: this.clickIds
        }).then(res => {
        }, { timeout: 15000 }).then(res => {
          this.loggingIn = false
          const d = res.data
          if (d.code === 200) {
            localStorage.setItem('token', d.data.token)
            localStorage.setItem('username', d.data.username)
            localStorage.setItem('realName', d.data.realName || '')
            localStorage.setItem('modules', d.data.modules || '')
            sessionStorage.setItem('token', d.data.token)
            sessionStorage.setItem('username', d.data.username)
            sessionStorage.setItem('realName', d.data.realName || '')
            sessionStorage.setItem('modules', d.data.modules || '')
            this.$message.success('登录成功')
            this.$router.push('/dashboard')
          } else if (d.code === 401) {
@@ -95,8 +99,9 @@
            this.$message.error(d.message || '登录失败,请重试')
            if (this.$refs.captcha) this.$refs.captcha.load()
          }
        }).catch(() => {
        }).catch(e => {
          this.loggingIn = false
          if (e && e.code === 'ECONNABORTED') { this.$message.error('登录请求超时,请重试'); return }
          this.$message.error('网络错误,登录失败')
        })
      })