# Qrcode 二维码
自研纯 JS 二维码编码(@lh-ui/share/core/qrcode,ISO/IEC 18004 全链路:
UTF-8 字节模式 → Reed-Solomon 纠错 → 掩码惩罚择优 → 格式 / 版本信息 BCH),
矩阵渲染为 SVG <path>,viewBox 含静默区;根类 lh-qrcode,
样式来自 share styles/qrcode。
二维码(纠错级别 / 尺寸 / 色彩 / 空态降级)
<template>
<div class="demo-col" style="gap: 18px; max-width: 480px">
<div class="demo-row" style="gap: 12px; flex-wrap: wrap; align-items: center">
<LhInput v-model="text" placeholder="编码文本" style="width: 220px" />
<LhButton size="small" @click="cycleLevel">纠错级:{{ level }}</LhButton>
</div>
<div class="demo-row" style="gap: 24px; flex-wrap: wrap; align-items: flex-start">
<div class="demo-col" style="align-items: center; gap: 8px">
<LhQrcode :value="text" :level="level" label="默认二维码" />
<span class="demo-note">默认(M 级 / size=120)</span>
</div>
<div class="demo-col" style="align-items: center; gap: 8px">
<LhQrcode :value="text" :level="level" :size="160" :margin="2" color="#A63D40" />
<span class="demo-note">size=160 margin=2 朱砂模块</span>
</div>
<div class="demo-col" style="align-items: center; gap: 8px">
<LhQrcode :value="text" :level="level" :version="5" bg-color="#F6F1EA" />
<span class="demo-note">version=5 固定 + 背景 rect</span>
</div>
</div>
<div class="demo-row" style="gap: 24px; flex-wrap: wrap; align-items: flex-start">
<div class="demo-col" style="align-items: center; gap: 8px">
<LhQrcode value="" />
<span class="demo-note">空 value 空态</span>
</div>
<div class="demo-col" style="align-items: center; gap: 8px">
<LhQrcode :value="tooLong" />
<span class="demo-note">超容量文本降级空态(console.warn)</span>
</div>
</div>
</div>
</template>
<script>
export default {
data: () => ({
text: 'lh-ui 家族 · 水墨丹青',
level: 'M',
tooLong: 'lh-ui'.repeat(600),
}),
methods: {
cycleLevel() {
const order = ['L', 'M', 'Q', 'H']
this.level = order[(order.indexOf(this.level) + 1) % order.length]
},
},
}
</script>
# 纠错级别与版本
level 为 L / M / Q / H 四档(默认 M),级别越高冗余越大、可容纳字节越少;
version 为 1-40(默认 0 = 自动选最小可容纳版本)。固定 version 时文本超出该版本
容量会抛错,组件层 try/catch 守卫降级为空态并 console.warn:
<LhQrcode value="https://lhgb.top" level="H" :version="10" />
# 尺寸与静默区
size 控制 svg 渲染宽高(写入根 CSS 变量 --lh-qrcode-size,默认 120);
margin 为静默区模块数(默认 4,spec 最小值),已计入 viewBox:
<LhQrcode value="水墨丹青" :size="160" :margin="2" />
# 色彩
color 为模块色、bgColor 为背景色(渲染为铺满 viewBox 的 <rect>);
未传 color 走 --lh-text-primary 令牌兜底,未传 bgColor 透明(根容器
背景透出):
<LhQrcode value="朱砂" color="#A63D40" bg-color="#F6F1EA" />
# Props
| 属性 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| value | 编码文本(UTF-8,支持中文) | string | '' |
| level | 纠错级别 | 'L' \| 'M' \| 'Q' \| 'H' | 'M' |
| version | 版本 1-40(0 = 自动选最小可容纳版本) | number | 0 |
| margin | 静默区模块数 | number | 4 |
| color | 前景色(模块色,未传走 lh-qrcode 令牌) | string | — |
| bgColor | 背景色(未传走 lh-qrcode 令牌) | string | — |
| size | 尺寸 px(渲染画布大小) | number | 120 |
| label | 无障碍可达名称(未传走默认「二维码」文案) | string | — |
# 无障碍
根元素 role="img" + aria-label(label 可覆盖默认「二维码」)。
空 value 或超容量降级时不输出 svg,根节点保留 lh-qrcode--empty 修饰。
Copyright © 2024-2026 风默文学 | 宁ICP备2024006574号 |
苏公网安备32040002010537