Skip to content

feat(result): 添加账号禁用相关错误处理和国际化支持#593

Merged
kanyxmo merged 3 commits into
masterfrom
patch-1
Apr 24, 2025
Merged

feat(result): 添加账号禁用相关错误处理和国际化支持#593
kanyxmo merged 3 commits into
masterfrom
patch-1

Conversation

@zds-s

@zds-s zds-s commented Apr 24, 2025

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • 新功能
    • 增加了“账号已禁用”状态码及多语言提示,支持英文、简体中文和繁体中文。
    • 前端在遇到账号被禁用时,将显示相应提示并自动登出用户。
    • 登录时新增账号禁用状态校验,禁用用户无法登录。
  • 优化
    • 登录状态过期提示优化为优先显示服务器返回的信息。

- 在 ResultCode 中添加 Disabled 状态码
- 在 HTTP 工具类中增加对 Disabled 状态的处理
- 在多语言文件中添加禁用账号的错误信息
- 优化未授权错误的提示信息
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Apr 24, 2025
@coderabbitai

coderabbitai Bot commented Apr 24, 2025

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

本次变更在后端和前端均新增了对“账号被禁用”状态的支持。后端通过在 ResultCode 枚举和多语言响应文件中增加了相关状态码和消息,并在登录逻辑及权限中间件中新增对禁用用户的校验和异常抛出。前端同步扩展了枚举,并在 HTTP 响应拦截器中新增了对禁用状态的处理逻辑:遇到禁用状态码时,展示提示信息并立即登出用户。未对其他功能做出更改。

Changes

文件/路径 变更摘要
app/Http/Common/ResultCode.php 新增枚举项 DISABLED = 423,并关联消息键 result.disabled
storage/languages/en/result.php
storage/languages/zh_CN/result.php
storage/languages/zh_TW/result.php
各语言响应文件新增 'disabled' 键,分别对应“Account disabled”、“账号已禁用”、“帳號已禁用”。
web/src/utils/ResultCode.ts 前端枚举 ResultCode 新增成员 DISABLED = 423
web/src/utils/http.ts 响应拦截器新增对 ResultCode.DISABLED 的处理:弹出禁用提示并立即登出;同时优化了未授权状态的提示逻辑。
app/Http/Admin/Middleware/PermissionMiddleware.php 修改禁用用户抛出异常的错误码为 ResultCode::DISABLED,保持消息不变。
app/Service/PassportService.php 登录方法新增禁用状态校验,若用户被禁用则抛出 BusinessException,使用 DISABLED 错误码。

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Frontend
    participant Backend

    User->>Frontend: 发起请求
    Frontend->>Backend: 发送 API 请求
    Backend-->>Frontend: 返回结果(含 ResultCode)
    alt ResultCode = DISABLED
        Frontend->>User: 显示“账号已被禁用”消息
        Frontend->>User: 执行登出操作
    else ResultCode = UNAUTHORIZED
        Frontend->>User: 显示“登录已过期”消息
        Frontend->>User: 执行登出操作
    else 其他
        Frontend->>User: 正常处理响应
    end
Loading

Poem

🐰
账号禁用新上线,
前后端同步好愉快。
错误提示多语言,
一键登出不徘徊。
代码更清晰,
兔子欢快跳起来!


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 727b9ee and a3b14c0.

📒 Files selected for processing (5)
  • app/Http/Admin/Middleware/PermissionMiddleware.php (1 hunks)
  • app/Http/Common/ResultCode.php (1 hunks)
  • app/Service/PassportService.php (1 hunks)
  • web/src/utils/ResultCode.ts (1 hunks)
  • web/src/utils/http.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (3)
  • app/Http/Common/ResultCode.php
  • web/src/utils/http.ts
  • web/src/utils/ResultCode.ts
🧰 Additional context used
🧬 Code Graph Analysis (2)
app/Service/PassportService.php (2)
app/Model/Enums/User/Status.php (1)
  • isDisable (35-38)
app/Exception/BusinessException.php (1)
  • BusinessException (18-31)
app/Http/Admin/Middleware/PermissionMiddleware.php (1)
app/Exception/BusinessException.php (1)
  • BusinessException (18-31)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: build
  • GitHub Check: build
🔇 Additional comments (2)
app/Http/Admin/Middleware/PermissionMiddleware.php (1)

40-40: 错误码已更新为更精确的 DISABLED

将已禁用用户的错误码从之前的 FORBIDDEN 修改为新增的 DISABLED,使错误处理更加精确和语义化,有利于前端针对具体情况进行处理。这个修改与 PR 目标保持一致。

app/Service/PassportService.php (1)

51-53: 登录服务中新增用户禁用状态检查

在登录逻辑中新增对用户禁用状态的检查,如果用户被禁用则抛出带有 ResultCode::DISABLED 错误码的异常,可以有效阻止被禁用的用户获取访问令牌。这个检查非常必要且实现正确。

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or PR title anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@dosubot dosubot Bot added the enhancement label Apr 24, 2025

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (4)
storage/languages/zh_TW/result.php (1)

21-21: 统一翻译条目顺序
建议将 'disabled' => '帳號已禁用' 的位置与其他语言文件保持一致(建议放在最后),以提高多语言文件的维护可读性和一致性。

storage/languages/en/result.php (1)

21-21: 统一翻译条目顺序
建议将 'disabled' => 'Account disabled' 的位置与其他语言文件保持一致(建议放在最后),以提高多语言文件的维护可读性和一致性。

storage/languages/zh_CN/result.php (1)

16-16: 统一翻译条目顺序
zh_CN 文件中,'disabled' => '账号已禁用' 被插入到 unauthorizedforbidden 之间,与其他语言文件不一致,建议将其调整到数组末尾以保持一致性。

web/src/utils/http.ts (1)

164-168: 良好实现账号禁用的错误处理

代码正确实现了账号禁用状态的处理逻辑,包括显示错误消息和登出用户。建议为这个新增的状态码添加简短注释,说明其用途。

        case ResultCode.UNAUTHORIZED:
        {
          const logout = async () => {
            if (isLogout === false) {
              isLogout = true
              setTimeout(() => isLogout = false, 5000)
              Message.error(response?.data?.message ?? '登录已过期', { zIndex: 9999 })
              await useUserStore().logout()
            }
          }
          // ... 其他代码
        }
+       // 处理账号被禁用的情况,显示错误消息并登出用户
        case ResultCode.Disabled: {
          Message.error(response?.data?.message ?? '账号已被禁用', {zIndex: 9999})
          await useUserStore().logout()
          break
        }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2aa7003 and 727b9ee.

📒 Files selected for processing (6)
  • app/Http/Common/ResultCode.php (1 hunks)
  • storage/languages/en/result.php (1 hunks)
  • storage/languages/zh_CN/result.php (1 hunks)
  • storage/languages/zh_TW/result.php (1 hunks)
  • web/src/utils/ResultCode.ts (1 hunks)
  • web/src/utils/http.ts (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (21)
  • GitHub Check: Test on PHP8.1 Swoole-master
  • GitHub Check: Test on PHP8.2 Swoole-v5.1.5
  • GitHub Check: Test on PHP8.3 Swoole-master
  • GitHub Check: Test on PHP8.1 Swoole-v5.0.3
  • GitHub Check: Test on PHP8.2 Swoole-v5.0.3
  • GitHub Check: Test on PHP8.1 Swow-develop
  • GitHub Check: Test on PHP8.3 Swow-develop
  • GitHub Check: Test on PHP8.2 Swow-develop
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test on PHP8.2 Swoole-master
  • GitHub Check: Test on PHP8.2 Swoole-v5.0.3
  • GitHub Check: Test on PHP8.1 Swoole-v5.1.5
  • GitHub Check: Test on PHP8.1 Swoole-v5.0.3
  • GitHub Check: Test on PHP8.2 Swoole-v5.1.5
  • GitHub Check: Test on PHP8.1 Swoole-master
  • GitHub Check: Test on PHP8.3 Swoole-master
  • GitHub Check: Test on PHP8.3 Swoole-v5.1.5
  • GitHub Check: Test on PHP8.2 Swow-develop
  • GitHub Check: Test on PHP8.1 Swow-develop
  • GitHub Check: Test on PHP8.3 Swow-develop
  • GitHub Check: build Code coverage report (ubuntu-latest, 8.1, v5.1.3)
🔇 Additional comments (2)
web/src/utils/http.ts (2)

102-103: 优化了代码结构

添加大括号使得 case 语句块更加清晰,避免变量作用域混淆,是一个良好的编码实践。


108-108: 支持了错误消息国际化

使用 response?.data?.message ?? '登录已过期' 替代固定字符串,优先使用服务器返回的消息,这符合国际化要求,同时提供了合理的默认值。

Comment thread app/Http/Common/ResultCode.php Outdated
Comment thread web/src/utils/ResultCode.ts Outdated
zds-s added 2 commits April 24, 2025 12:18
- 在 PassportService 中增加对用户禁用状态的检查
- 更新 ResultCode枚举,将 Disabled 改为大写的 DISABLED
- 在 HTTP 工具类中增加对 DISABLED 错误码的处理
- 将错误代码从 ResultCode::FORBIDDEN 更改为 ResultCode::DISABLED
- 优化了用户状态检查的异常处理
@kanyxmo
kanyxmo merged commit 7f24cb4 into master Apr 24, 2025
@codecov

codecov Bot commented Apr 24, 2025

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 0.00%. Comparing base (2aa7003) to head (a3b14c0).
Report is 3 commits behind head on master.

Additional details and impacted files
@@      Coverage Diff      @@
##   master   #593   +/-   ##
=============================
=============================

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@zds-s
zds-s deleted the patch-1 branch April 26, 2025 07:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants