From 7fabf7b65cc2af960827eccf6c3c824f75aab8ed Mon Sep 17 00:00:00 2001 From: Squall2017 <252766926@qq.com> Date: Sat, 14 Sep 2024 09:53:06 +0800 Subject: [PATCH] refactor: refactor code structure and improve demo page (#4389) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: captcha example * fix: fix lint errors * chore: event handling and methods * chore: add accessibility features ARIA labels and roles * refactor: refactor code structure and improve captcha demo page * feat: add captcha internationalization * chore: 适配时间戳国际化展示 --------- Co-authored-by: vince --- .../src/components/captcha/captcha-card.vue | 77 +++++++ .../common-ui/src/components/captcha/index.ts | 1 + .../captcha/point-selection-captcha.vue | 216 +++++++----------- .../common-ui/src/components/captcha/types.ts | 87 ++++++- .../common-ui/src/components/captcha/utils.ts | 7 + packages/locales/src/langs/en-US.json | 9 + packages/locales/src/langs/zh-CN.json | 9 + playground/src/locales/langs/en-US.json | 22 +- playground/src/locales/langs/zh-CN.json | 22 +- .../src/views/examples/captcha/index.vue | 166 ++++++++++++-- 10 files changed, 459 insertions(+), 157 deletions(-) create mode 100644 packages/effects/common-ui/src/components/captcha/captcha-card.vue create mode 100644 packages/effects/common-ui/src/components/captcha/utils.ts diff --git a/packages/effects/common-ui/src/components/captcha/captcha-card.vue b/packages/effects/common-ui/src/components/captcha/captcha-card.vue new file mode 100644 index 00000000000..c4eb8cfd63a --- /dev/null +++ b/packages/effects/common-ui/src/components/captcha/captcha-card.vue @@ -0,0 +1,77 @@ + + diff --git a/packages/effects/common-ui/src/components/captcha/index.ts b/packages/effects/common-ui/src/components/captcha/index.ts index 59cbc827686..6aab0d16e64 100644 --- a/packages/effects/common-ui/src/components/captcha/index.ts +++ b/packages/effects/common-ui/src/components/captcha/index.ts @@ -1,2 +1,3 @@ +export { default as CaptchaCard } from './captcha-card.vue'; export { default as PointSelectionCaptcha } from './point-selection-captcha.vue'; export type * from './types'; diff --git a/packages/effects/common-ui/src/components/captcha/point-selection-captcha.vue b/packages/effects/common-ui/src/components/captcha/point-selection-captcha.vue index 7bc40847acc..3b1ac90729a 100644 --- a/packages/effects/common-ui/src/components/captcha/point-selection-captcha.vue +++ b/packages/effects/common-ui/src/components/captcha/point-selection-captcha.vue @@ -1,107 +1,37 @@ diff --git a/packages/effects/common-ui/src/components/captcha/types.ts b/packages/effects/common-ui/src/components/captcha/types.ts index 4e3901ba056..b9d35537278 100644 --- a/packages/effects/common-ui/src/components/captcha/types.ts +++ b/packages/effects/common-ui/src/components/captcha/types.ts @@ -1,6 +1,89 @@ -export interface CaptchaPoint { - i: number; +export interface CaptchaData { + /** + * x + */ x: number; + /** + * y + */ y: number; + /** + * 时间戳 + */ t: number; } +export interface CaptchaPoint extends CaptchaData { + /** + * 数据索引 + */ + i: number; +} +export interface CaptchaCardProps { + /** + * 验证码图片 + */ + captchaImage: string; + /** + * 验证码图片高度 + * @default '220px' + */ + height?: number | string; + /** + * 水平内边距 + * @default '12px' + */ + paddingX?: number | string; + /** + * 垂直内边距 + * @default '16px' + */ + paddingY?: number | string; + /** + * 标题 + * @default '请按图依次点击' + */ + title?: string; + /** + * 验证码图片宽度 + * @default '300px' + */ + width?: number | string; +} + +export interface PointSelectionCaptchaProps extends CaptchaCardProps { + /** + * 是否展示确定按钮 + * @default false + */ + showConfirm?: boolean; + /** + * 提示图片 + * @default '' + */ + hintImage?: string; + /** + * 提示文本 + * @default '' + */ + hintText?: string; +} + +/** + * TODO: 滑动验证码 + */ +// export interface SlideCaptchaProps extends CaptchaCardProps { +// /** +// * 瓦片图片高度 +// * @default '40px' +// */ +// tileHeight?: number | string; +// /** +// * 瓦片图片宽度 +// * @default '150px' +// */ +// tileWidth?: number | string; +// /** +// * 瓦片图片 +// */ +// tileImage: string; +// } diff --git a/packages/effects/common-ui/src/components/captcha/utils.ts b/packages/effects/common-ui/src/components/captcha/utils.ts new file mode 100644 index 00000000000..4deb272ce25 --- /dev/null +++ b/packages/effects/common-ui/src/components/captcha/utils.ts @@ -0,0 +1,7 @@ +export const parseValue = (value: number | string) => { + if (typeof value === 'number') { + return value; + } + const parsed = Number.parseFloat(value); + return Number.isNaN(parsed) ? 0 : parsed; +}; diff --git a/packages/locales/src/langs/en-US.json b/packages/locales/src/langs/en-US.json index 789fc6f20ba..4f2d3c48c7e 100644 --- a/packages/locales/src/langs/en-US.json +++ b/packages/locales/src/langs/en-US.json @@ -311,5 +311,14 @@ "sidebarToggle": "Enable Sidebar Toggle", "lockScreen": "Enable Lock Screen" } + }, + "captcha": { + "alt": "Supports img tag src attribute value", + "title": "Please complete the security verification", + "refreshAriaLabel": "Refresh captcha", + "confirmAriaLabel": "Confirm selection", + "confirm": "Confirm", + "pointAriaLabel": "Click point", + "clickInOrder": "Please click in order" } } diff --git a/packages/locales/src/langs/zh-CN.json b/packages/locales/src/langs/zh-CN.json index a245b8625f2..88c5cac1e25 100644 --- a/packages/locales/src/langs/zh-CN.json +++ b/packages/locales/src/langs/zh-CN.json @@ -311,5 +311,14 @@ "sidebarToggle": "启用侧边栏切换", "lockScreen": "启用锁屏" } + }, + "captcha": { + "alt": "支持img标签src属性值", + "title": "请完成安全验证", + "refreshAriaLabel": "刷新验证码", + "confirmAriaLabel": "确认选择", + "confirm": "确认", + "pointAriaLabel": "点击点", + "clickInOrder": "请依次点击" } } diff --git a/playground/src/locales/langs/en-US.json b/playground/src/locales/langs/en-US.json index 1577eb37ca7..95f3c8831b6 100644 --- a/playground/src/locales/langs/en-US.json +++ b/playground/src/locales/langs/en-US.json @@ -81,7 +81,27 @@ "custom": "Custom Component" }, "captcha": { - "title": "Captcha" + "title": "Captcha", + "captchaCardTitle": "Please complete the security verification", + "pageDescription": "Verify user identity by clicking on specific locations in the image.", + "pageTitle": "Captcha Component Example", + "basic": "Basic Usage", + "titlePlaceholder": "Captcha Title Text", + "captchaImageUrlPlaceholder": "Captcha Image (supports img tag src attribute value)", + "hintImage": "Hint Image", + "hintText": "Hint Text", + "hintImagePlaceholder": "Hint Image (supports img tag src attribute value)", + "hintTextPlaceholder": "Hint Text", + "showConfirm": "Show Confirm", + "hideConfirm": "Hide Confirm", + "widthPlaceholder": "Captcha Image Width Default 300px", + "heightPlaceholder": "Captcha Image Height Default 220px", + "paddingXPlaceholder": "Horizontal Padding Default 12px", + "paddingYPlaceholder": "Vertical Padding Default 16px", + "index": "Index:", + "timestamp": "Timestamp:", + "x": "x:", + "y": "y:" } } } diff --git a/playground/src/locales/langs/zh-CN.json b/playground/src/locales/langs/zh-CN.json index 8bcac9632b1..91d4962c2b0 100644 --- a/playground/src/locales/langs/zh-CN.json +++ b/playground/src/locales/langs/zh-CN.json @@ -81,7 +81,27 @@ "custom": "自定义组件" }, "captcha": { - "title": "验证码" + "title": "验证码", + "captchaCardTitle": "请完成安全验证", + "pageDescription": "通过点击图片中的特定位置来验证用户身份。", + "pageTitle": "验证码组件示例", + "basic": "基本使用", + "titlePlaceholder": "验证码标题文案", + "captchaImageUrlPlaceholder": "验证码图片(支持img标签src属性值)", + "hintImage": "提示图片", + "hintText": "提示文本", + "hintImagePlaceholder": "提示图片(支持img标签src属性值)", + "hintTextPlaceholder": "提示文本", + "showConfirm": "展示确认", + "hideConfirm": "隐藏确认", + "widthPlaceholder": "验证码图片宽度 默认300px", + "heightPlaceholder": "验证码图片高度 默认220px", + "paddingXPlaceholder": "水平内边距 默认12px", + "paddingYPlaceholder": "垂直内边距 默认16px", + "index": "索引:", + "timestamp": "时间戳:", + "x": "x:", + "y": "y:" } } } diff --git a/playground/src/views/examples/captcha/index.vue b/playground/src/views/examples/captcha/index.vue index f5fae2f146f..7ceecb31c24 100644 --- a/playground/src/views/examples/captcha/index.vue +++ b/playground/src/views/examples/captcha/index.vue @@ -1,45 +1,177 @@