跳转至

后端-认证-单账户退出被限制拦截

问题现象

  • 公众号账号页面 /#/public-account-auth,已登录的槽位点击"退出"按钮无反应
  • 槽位状态不变,仍显示账号昵称和 Token,未恢复到"未登录"
  • 浏览器 Network 面板可见 POST /api/wechat/auth/logout?slot=0 返回 HTTP 400

核心问题

  1. 后端拦截modules/wechat/routes.py:177-179 的 logout 路由有 if active <= 1 检查,当仅剩 1 个已登录账户时返回 400 阻止退出
  2. 前端吞错modules/wechat/frontend/Auth.vue:157onLogout catch 块 catch { // cancelled } 吞掉了后端 400 错误,导致界面无任何错误提示

解决方法

  1. 移除后端 active <= 1 检查
    # modules/wechat/routes.py ~168-182
    # 删除 active = count_active_slots() 和 if active <= 1 两行
    # 更新 docstring 去掉"至少保留一个已登录账户"
    
  2. 改进前端错误处理,区分用户取消和 API 错误
    # modules/wechat/frontend/Auth.vue ~157
    catch (e: any) {
      if (e !== 'cancel' && e !== 'close') {
        ElMessage.error(e.response?.data?.detail || e.message || '退出失败')
      }
    }
    
  3. 构建部署(后端变更需重启服务)
    cd web && pnpm build
    git add modules/wechat/frontend/Auth.vue modules/wechat/routes.py web/dist/
    git commit -m "fix: ..."
    git push origin main
    ssh news 'cd /www/news && git pull origin main && systemctl restart news-backend'
    

下次防错措施

  • 修改前后端联动的业务规则时,两端同时排查,不要只改一端
  • catch 块不应裸写 catch {} 吞错,至少区分 cancel 和真实异常
  • 验证方法:curl 测试退出 API,确认返回 {"code": 0, "message": "槽位 X 已退出"}
    curl -X POST "https://news.queding.hk/api/wechat/auth/logout?slot=0"