Skip to content

feat: 新增拼图切片平移的验证码 #6471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 6, 2025

Conversation

cwr0401
Copy link
Contributor

@cwr0401 cwr0401 commented Jul 3, 2025

Description

随机选取图片中的一部分制作成拼图切片,复用 SliderCaptcha 组件平移该切片,使之与图片缺失匹配,获得验证通过。

验证码

拼图验证码1

验证成功

拼图验证码2

验证失败

拼图验证码3

Summary by CodeRabbit

  • New Features

    • Introduced a slider-based puzzle captcha component for enhanced user verification.
    • Added a new example page demonstrating the slider captcha, accessible via the examples section.
    • Provided localization for the new captcha in both English and Chinese.
  • Documentation

    • Updated example routes and descriptions to include the new slider captcha feature.

Copy link
Contributor

coderabbitai bot commented Jul 3, 2025

Walkthrough

A slider-based puzzle captcha feature was introduced. This includes a new Vue component for the slider captcha, updates to component exports, type definitions for its props, localization entries in English and Chinese, a new example route, and a demonstration view integrating the new captcha.

Changes

File(s) Change Summary
packages/effects/common-ui/src/components/captcha/index.ts Exported the new SliderTranslateCaptcha component.
packages/effects/common-ui/src/components/captcha/slider-translate-captcha/index.vue Added a Vue component implementing a slider puzzle captcha with props, verification, and events.
packages/effects/common-ui/src/components/captcha/types.ts Introduced SliderTranslateCaptchaProps interface for captcha configuration.
playground/src/locales/langs/en-US/examples.json
playground/src/locales/langs/zh-CN/examples.json
Added localization for "sliderTranslateCaptcha" in English and Chinese.
playground/src/router/routes/modules/examples.ts Added a new route for the slider translate captcha example.
playground/src/views/examples/captcha/slider-translate-captcha.vue Added a view demonstrating the new slider translate captcha component.
packages/locales/src/langs/en-US/ui.json
packages/locales/src/langs/zh-CN/ui.json
Added new UI localization keys for slider translate captcha default, fail, and success tips.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant SliderTranslateCaptcha
    participant ParentComponent

    User->>SliderTranslateCaptcha: Drag slider to move puzzle piece
    SliderTranslateCaptcha->>SliderTranslateCaptcha: Verify piece position
    alt Success
        SliderTranslateCaptcha->>ParentComponent: Emit success event
    else Failure
        SliderTranslateCaptcha->>SliderTranslateCaptcha: Reset piece position
    end
Loading

Poem

🧩
A puzzle piece slides left and right,
With a gentle tug, it finds its light.
New routes and tips in every tongue,
Captcha fun for old and young.
Hop along, success in sight—
The rabbit cheers the code tonight!
🐇


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8ccd01a and 1aafb43.

📒 Files selected for processing (1)
  • packages/effects/common-ui/src/components/captcha/slider-translate-captcha/index.vue (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/effects/common-ui/src/components/captcha/slider-translate-captcha/index.vue
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: post-update (windows-latest)
  • GitHub Check: post-update (ubuntu-latest)

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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

@cwr0401 cwr0401 changed the title 新增拼图切片平移的验证码 feat: 新增拼图切片平移的验证码 Jul 3, 2025
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 4

🧹 Nitpick comments (4)
packages/effects/common-ui/src/components/captcha/slider-translate-captcha/index.vue (4)

38-43: Remove unnecessary eslint-disable comments.

TypeScript enums don't trigger unused variable warnings for their members, so these eslint-disable comments are not needed.

 enum CanvasOpr {
-  // eslint-disable-next-line no-unused-vars
   Clip = 'clip',
-  // eslint-disable-next-line no-unused-vars
   Fill = 'fill',
 }

154-157: Translate Chinese comment to English.

For consistency and international collaboration, comments should be in English.

   const img = new Image();
-  // 解决跨域
+  // Solve cross-origin issues
   img.crossOrigin = 'Anonymous';
   img.src = src;

126-135: Apply willReadFrequently optimization consistently.

Both canvas contexts should use willReadFrequently: true since both perform readback operations, not just the pieceCanvas.

   pieceCanvas.width = canvasWidth;
-  const puzzleCanvasCtx = puzzleCanvas.getContext('2d');
+  const puzzleCanvasCtx = puzzleCanvas.getContext('2d', {
+    willReadFrequently: true,
+  });
   // Canvas2D: Multiple readback operations using getImageData
   // are faster with the willReadFrequently attribute set to true.
   // See: https://html.spec.whatwg.org/multipage/canvas.html#concept-canvas-will-read-frequently (anonymous)
   const pieceCanvasCtx = pieceCanvas.getContext('2d', {
     willReadFrequently: true,
   });

The same change should be applied in the initCanvas function as well.


222-230: Document the purpose of the magic number.

The addition of 0.4 to the radius lacks explanation, making the code less maintainable.

   ctx.arc(
     x + circleRadius - 2,
     y + squareLength / 2,
-    circleRadius + 0.4,
+    circleRadius + 0.4, // Slightly larger radius for the left notch to ensure proper fitting
     2.76 * PI,
     1.24 * PI,
     true,
   );
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fee811d and bbd8a53.

📒 Files selected for processing (7)
  • packages/effects/common-ui/src/components/captcha/index.ts (1 hunks)
  • packages/effects/common-ui/src/components/captcha/slider-translate-captcha/index.vue (1 hunks)
  • packages/effects/common-ui/src/components/captcha/types.ts (1 hunks)
  • playground/src/locales/langs/en-US/examples.json (1 hunks)
  • playground/src/locales/langs/zh-CN/examples.json (1 hunks)
  • playground/src/router/routes/modules/examples.ts (1 hunks)
  • playground/src/views/examples/captcha/slider-translate-captcha.vue (1 hunks)
🧰 Additional context used
🧠 Learnings (6)
📓 Common learnings
Learnt from: mynetfan
PR: vbenjs/vue-vben-admin#5587
File: playground/src/views/examples/loading/index.vue:15-18
Timestamp: 2025-02-23T04:21:24.691Z
Learning: Chinese text in the description of the loading component example (`playground/src/views/examples/loading/index.vue`) is intentionally kept without i18n support.
packages/effects/common-ui/src/components/captcha/index.ts (1)
Learnt from: mynetfan
PR: vbenjs/vue-vben-admin#5587
File: playground/src/views/examples/loading/index.vue:15-18
Timestamp: 2025-02-23T04:21:24.691Z
Learning: Chinese text in the description of the loading component example (`playground/src/views/examples/loading/index.vue`) is intentionally kept without i18n support.
playground/src/locales/langs/zh-CN/examples.json (2)
Learnt from: mynetfan
PR: vbenjs/vue-vben-admin#5587
File: playground/src/views/examples/loading/index.vue:15-18
Timestamp: 2025-02-23T04:21:24.691Z
Learning: Chinese text in the description of the loading component example (`playground/src/views/examples/loading/index.vue`) is intentionally kept without i18n support.
Learnt from: mynetfan
PR: vbenjs/vue-vben-admin#5013
File: docs/src/components/layout-ui/page.md:23-24
Timestamp: 2024-12-04T04:43:22.179Z
Learning: 在 `docs/src/components/layout-ui/page.md` 的 `Page` 组件中,`title` 和 `description` 属性有意使用 `string|slot` 类型表示法,表示它们可以接受字符串或插槽。
playground/src/router/routes/modules/examples.ts (1)
Learnt from: mynetfan
PR: vbenjs/vue-vben-admin#5587
File: playground/src/views/examples/loading/index.vue:15-18
Timestamp: 2025-02-23T04:21:24.691Z
Learning: Chinese text in the description of the loading component example (`playground/src/views/examples/loading/index.vue`) is intentionally kept without i18n support.
playground/src/views/examples/captcha/slider-translate-captcha.vue (3)
Learnt from: mynetfan
PR: vbenjs/vue-vben-admin#5587
File: playground/src/views/examples/loading/index.vue:15-18
Timestamp: 2025-02-23T04:21:24.691Z
Learning: Chinese text in the description of the loading component example (`playground/src/views/examples/loading/index.vue`) is intentionally kept without i18n support.
Learnt from: mynetfan
PR: vbenjs/vue-vben-admin#5013
File: docs/src/components/layout-ui/page.md:23-24
Timestamp: 2024-12-04T04:43:22.179Z
Learning: 在 `docs/src/components/layout-ui/page.md` 的 `Page` 组件中,`title` 和 `description` 属性有意使用 `string|slot` 类型表示法,表示它们可以接受字符串或插槽。
Learnt from: mynetfan
PR: vbenjs/vue-vben-admin#5013
File: docs/src/components/layout-ui/page.md:31-31
Timestamp: 2024-12-04T04:41:30.161Z
Learning: 在 Vben Admin 项目的 `Page` 组件中,`extra` 内容仅通过 slots 提供,没有对应的 prop 属性。
packages/effects/common-ui/src/components/captcha/slider-translate-captcha/index.vue (1)
Learnt from: mynetfan
PR: vbenjs/vue-vben-admin#5587
File: playground/src/views/examples/loading/index.vue:15-18
Timestamp: 2025-02-23T04:21:24.691Z
Learning: Chinese text in the description of the loading component example (`playground/src/views/examples/loading/index.vue`) is intentionally kept without i18n support.
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: post-update (ubuntu-latest)
  • GitHub Check: post-update (windows-latest)
🔇 Additional comments (6)
packages/effects/common-ui/src/components/captcha/types.ts (1)

162-196: Well-structured interface for puzzle captcha component.

The SliderTranslateCaptchaProps interface is properly defined with appropriate types, comprehensive JSDoc documentation, and reasonable default values. The naming convention is consistent with other interfaces in the file.

playground/src/locales/langs/en-US/examples.json (1)

44-44: Appropriate localization key added.

The new localization entry follows the established naming pattern and provides a clear, descriptive translation for the slider puzzle captcha feature.

playground/src/locales/langs/zh-CN/examples.json (1)

47-47: Accurate Chinese localization added.

The Chinese translation "拼图滑块验证" accurately describes the puzzle slider verification functionality and maintains consistency with the English localization key.

packages/effects/common-ui/src/components/captcha/index.ts (1)

6-6: Component export follows established pattern.

The export statement for SliderTranslateCaptcha is correctly structured and maintains consistency with the existing captcha component exports.

playground/src/router/routes/modules/examples.ts (1)

199-207: Route configuration follows established conventions.

The new route entry for the slider translate captcha example is properly structured with consistent naming, appropriate path structure, and correct localization key usage.

playground/src/views/examples/captcha/slider-translate-captcha.vue (1)

1-27: LGTM! Well-structured example component.

The implementation follows the established patterns for example components in this codebase. The hardcoded Chinese text is intentional (as per project conventions for example files), and the fixed values are appropriate for a demonstration.

Comment on lines 92 to 101
function handleDragEnd() {
const { pieceX } = state;
const { diffDistance } = props;

if (Math.abs(pieceX - state.moveDistance) >= (diffDistance || 10)) {
setLeft('0');
state.moveDistance = 0;
} else {
checkPass();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix inconsistent default value for diffDistance.

The fallback value (10) doesn't match the prop default (3), which could lead to unexpected behavior.

 function handleDragEnd() {
   const { pieceX } = state;
   const { diffDistance } = props;
 
-  if (Math.abs(pieceX - state.moveDistance) >= (diffDistance || 10)) {
+  if (Math.abs(pieceX - state.moveDistance) >= diffDistance) {
     setLeft('0');
     state.moveDistance = 0;
   } else {
     checkPass();
   }

Since diffDistance has a default value in props, the fallback is unnecessary.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
function handleDragEnd() {
const { pieceX } = state;
const { diffDistance } = props;
if (Math.abs(pieceX - state.moveDistance) >= (diffDistance || 10)) {
setLeft('0');
state.moveDistance = 0;
} else {
checkPass();
}
function handleDragEnd() {
const { pieceX } = state;
const { diffDistance } = props;
if (Math.abs(pieceX - state.moveDistance) >= diffDistance) {
setLeft('0');
state.moveDistance = 0;
} else {
checkPass();
}
}
🤖 Prompt for AI Agents
In
packages/effects/common-ui/src/components/captcha/slider-translate-captcha/index.vue
around lines 92 to 101, remove the fallback value (10) used with diffDistance in
the handleDragEnd function. Instead, rely directly on the diffDistance prop
since it already has a default value of 3 defined. This will ensure consistent
behavior and avoid unexpected discrepancies.

@jinmao88
Copy link
Collaborator

jinmao88 commented Jul 3, 2025

根据AI提示 有不少可以优化的地方

Copy link

changeset-bot bot commented Jul 4, 2025

⚠️ No Changeset found

Latest commit: 2f7de24

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@jinmao88 jinmao88 merged commit 2f7de24 into vbenjs:main Jul 6, 2025
11 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants