# 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