更新记录
1.0.0(2025-12-22) 下载此版本
1.0.0 (2025-12-22)
- 首次发布
- 支持 27 个配置属性,覆盖基础配置、码点、码眼、Logo、背景等
- 支持 8 种码点形状:方形、圆点、圆角、液化等
- 支持渐变色(线性/径向)和前景图填充
- 支持 Logo 配置:形状、尺寸、边距、阴影
- 支持透明背景
- 提供 5 个公开方法:generate、save、getImage、refresh、clear
- 预览模式与导出模式分离,优化性能
- 兼容 Vue 2 和 Vue 3
平台兼容性
uni-app(3.6.15)
| Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
|---|---|---|---|---|---|---|---|---|
| √ | √ | √ | √ | - | - | - | - | - |
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|
| √ | - | - | - | - | - | - | - | - | - | - |
uni-app x
| Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
|---|---|---|---|---|---|
| √ | √ | - | - | - | √ |
其他
| 多语言 | 暗黑模式 | 宽屏模式 |
|---|---|---|
| × | × | √ |
tri-qrcode 二维码生成组件
📖 组件介绍
tri-qrcode 是一个功能强大、高度可定制的 uni-app 二维码生成组件。基于 EasyQrcode 设计,兼容 qrcode 库标准,支持丰富的样式配置,包括渐变色、Logo、前景图填充、多种码点形状等高级功能。
✨ 特性
- 🎨 丰富的样式配置 - 支持单色、渐变、前景图填充
- 🖼️ Logo支持 - 支持中心Logo,可配置形状、尺寸、边距、阴影
- 🔲 多种形状 - 8种码点形状,4种码眼形状,包含液化风格
- 🌈 渐变效果 - 支持线性和径向渐变,4种渐变方向
- 📐 灵活配置 - 支持容错率、版本号、外边距等基础配置
- 🎯 高性能 - 预览模式和导出模式分离,支持防抖优化
- 🔧 易于使用 - 符合 uni-app 组件规范,支持 Vue 3
🚀 快速开始
基础使用
<template>
<view>
<tri-qrcode
text="https://github.com"
:width="450"
:height="450"
/>
</view>
</template>
<script setup>
import TriQrcode from '@/components/tri-qrcode/tri-qrcode.vue'
</script>
带Logo的二维码
<template>
<tri-qrcode
text="https://github.com"
logo-image="/static/logo.png"
logo-size="medium"
logo-shape="circle"
error-correction-level="H"
/>
</template>
渐变色二维码
<template>
<tri-qrcode
text="https://github.com"
:dots-gradient="{
type: 'linear',
direction: 'diagonal',
colorStops: [
{ offset: 0, color: '#667eea' },
{ offset: 1, color: '#764ba2' }
]
}"
/>
</template>
📋 API 文档
Props
🔹 基础配置
| 属性名 | 说明 | 类型 | 默认值 |
|---|---|---|---|
text |
二维码内容(文本或URL) | String | '' |
width |
二维码宽度(单位:rpx) | Number | 450 |
height |
二维码高度(单位:rpx) | Number | 450 |
margin |
外边距,以色块数量为单位,取值范围:0-4 | Number | 1 |
🔹 二维码选项
| 属性名 | 说明 | 类型 | 默认值 |
|---|---|---|---|
version |
二维码版本号(0为自动,1-40为指定版本)。版本越高,容量越大,码点越密集 | Number | 0 |
error-correction-level |
容错率 • L - 7%容错,适用于清晰环境• M - 15%容错,平衡性能与容错• Q - 25%容错,适用于可能有遮挡的场景• H - 30%容错,适用于Logo较大或环境复杂的场景 |
String | 'Q' |
mode |
编码模式 • Numeric - 数字模式(0-9)• Alphanumeric - 字母数字模式• Byte - 字节模式(支持所有字符)• Kanji - 日文模式 |
String | 'Byte' |
🔹 Logo选项
| 属性名 | 说明 | 类型 | 默认值 |
|---|---|---|---|
logo-image |
Logo图片路径,为空则不显示Logo 支持格式: • 本地路径: /static/logo.png• 网络URL: https://xxx.com/logo.png• base64: data:image/png;base64,... |
String | '' |
logo-size |
Logo尺寸级别 • small - 12%大小,适合简单Logo• medium - 18%大小,平衡显示• large - 25%大小,突出Logo |
String | 'medium' |
logo-shape |
Logo形状 • rectangle - 矩形• circle - 圆形• rounded-rectangle - 圆角矩形 |
String | 'rectangle' |
logo-margin |
Logo边距级别 • none - 无边距• small - 5%边距• medium - 10%边距• large - 15%边距 |
String | 'small' |
logo-shadow |
Logo是否显示阴影效果 | Boolean | false |
hide-background-dots |
是否隐藏Logo下方的背景码点。开启后Logo区域更清晰,但需要更高的容错率 | Boolean | true |
🔹 码点选项
| 属性名 | 说明 | 类型 | 默认值 |
|---|---|---|---|
dots-type |
码点形状 • square - 方形• dots - 圆点• rounded - 圆角方形• extra-rounded - 大圆角方形• classy - 优雅型• tail - 瓷砖型• fluid - 液化型(自定义)• fluid-line - 线性液化型(自定义) |
String | 'square' |
dots-color |
码点颜色(单色模式),与 dots-gradient、dots-image 互斥 |
String | '#000000' |
dots-gradient |
码点渐变色配置,与 dots-color、dots-image 互斥格式: { type, direction, colorStops }详见渐变配置 |
Object | null |
dots-image |
码点前景图填充(图片路径),将图片作为码点的纹理填充 支持本地路径( /static/xxx.png)、网络URL、base64与 dots-color、dots-gradient 互斥 |
String | '' |
🔹 码眼外框选项
| 属性名 | 说明 | 类型 | 默认值 |
|---|---|---|---|
corners-square-type |
码眼外框形状 • square - 方形• dot - 圆形• rounded - 圆角方形• extra-rounded - 大圆角方形 |
String | 'square' |
corners-square-color |
码眼外框颜色(单色模式),与 corners-square-gradient 互斥 |
String | '#000000' |
corners-square-gradient |
码眼外框渐变色配置,与 corners-square-color 互斥格式: { type, direction, colorStops }详见渐变配置 |
Object | null |
🔹 码眼内点选项
| 属性名 | 说明 | 类型 | 默认值 |
|---|---|---|---|
corners-dot-type |
码眼内点形状 • square - 方形• dot - 圆形• rounded - 圆角方形• extra-rounded - 大圆角方形 |
String | 'square' |
corners-dot-color |
码眼内点颜色(单色模式),与 corners-dot-gradient 互斥 |
String | '#000000' |
corners-dot-gradient |
码眼内点渐变色配置,与 corners-dot-color 互斥格式: { type, direction, colorStops }详见渐变配置 |
Object | null |
🔹 背景选项
| 属性名 | 说明 | 类型 | 默认值 |
|---|---|---|---|
background-color |
背景颜色(单色模式),与 background-gradient、background-transparent 互斥 |
String | '#ffffff' |
background-gradient |
背景渐变色配置,与 background-color、background-transparent 互斥格式: { type, direction, colorStops }详见渐变配置 |
Object | null |
background-transparent |
是否使用透明背景。透明背景适用于需要叠加在其他内容上的场景 与 background-color、background-gradient 互斥 |
Boolean | false |
🔹 渲染选项
| 属性名 | 说明 | 类型 | 默认值 |
|---|---|---|---|
preview-mode |
是否为预览模式 • true - 使用image标签显示,性能更好,适合实时预览• false - 使用canvas渲染,适合导出高质量图片 |
Boolean | true |
canvas-id |
Canvas ID(用于多个二维码组件共存)。自动生成,一般不需要手动指定 | String | '' |
Events
| 事件名 | 说明 | 回调参数 |
|---|---|---|
@rendered |
二维码生成完成时触发 | { config: Object, renderTime: Number } - config为配置对象,renderTime为渲染耗时(ms) |
@error |
二维码生成失败时触发 | { error: Error } - error为错误对象 |
@saved |
二维码保存成功时触发 | { filePath: String } - filePath为保存的文件路径 |
@save-error |
二维码保存失败时触发 | { error: Error } - error为错误对象 |
事件使用示例
<template>
<tri-qrcode
text="https://github.com"
@rendered="onRendered"
@error="onError"
/>
</template>
<script setup>
const onRendered = ({ config, renderTime }) => {
console.log('二维码生成完成', `耗时: ${renderTime}ms`)
}
const onError = ({ error }) => {
console.error('生成失败', error.message)
}
</script>
Methods
通过 ref 可以调用组件的方法:
| 方法名 | 说明 | 参数 | 返回值 |
|---|---|---|---|
generate() |
生成/重新生成二维码 | - | Promise<void> |
save() |
保存二维码到相册 | - | Promise<string> - 返回保存的文件路径 |
getImage() |
获取二维码图片数据 | - | Promise<string> - 返回base64或临时文件路径 |
refresh() |
刷新二维码(重新生成) | - | void |
clear() |
清空二维码 | - | void |
方法使用示例
<template>
<view>
<tri-qrcode ref="qrcodeRef" text="https://github.com" />
<button @click="handleSave">保存二维码</button>
<button @click="handleRefresh">刷新</button>
</view>
</template>
<script setup>
import { ref } from 'vue'
const qrcodeRef = ref(null)
const handleSave = async () => {
try {
const filePath = await qrcodeRef.value.save()
uni.showToast({ title: '保存成功', icon: 'success' })
} catch (error) {
uni.showToast({ title: '保存失败', icon: 'error' })
}
}
const handleRefresh = () => {
qrcodeRef.value.refresh()
}
</script>
🎨 渐变配置
渐变配置对象用于 dots-gradient、corners-square-gradient、corners-dot-gradient、background-gradient 等属性。
渐变配置参数
| 参数名 | 说明 | 类型 | 可选值 | 必填 |
|---|---|---|---|---|
type |
渐变类型 | String | 'linear' - 线性渐变'radial' - 径向渐变 |
是 |
direction |
渐变方向 | String | 'horizontal' - 水平(左→右)'vertical' - 垂直(上→下)'diagonal' - 对角(左上→右下)'center' - 中心辐射 |
是 |
colorStops |
颜色停靠点数组 | Array | 至少包含2个颜色停靠点 | 是 |
colorStops 颜色停靠点
| 参数名 | 说明 | 类型 | 取值范围 |
|---|---|---|---|
offset |
颜色位置(0为起点,1为终点) | Number | 0 - 1 |
color |
颜色值(支持十六进制、RGB等) | String | 如 '#667eea'、'rgb(102,126,234)' |
完整格式示例
{
type: 'linear', // 渐变类型:'linear' 或 'radial'
direction: 'diagonal', // 渐变方向
colorStops: [ // 颜色停靠点数组(至少2个)
{ offset: 0, color: '#667eea' },
{ offset: 0.5, color: '#764ba2' }, // 可选的中间色
{ offset: 1, color: '#f093fb' }
]
}
渐变类型说明
- linear(线性渐变):颜色沿直线方向过渡
- radial(径向渐变):颜色从中心向外辐射过渡
渐变方向说明
| 方向 | 说明 | 适用类型 |
|---|---|---|
horizontal |
水平方向(从左到右) | linear |
vertical |
垂直方向(从上到下) | linear |
diagonal |
对角方向(从左上到右下) | linear |
center |
中心辐射 | radial |
渐变示例
线性渐变 - 水平
<tri-qrcode
text="Hello"
:dots-gradient="{
type: 'linear',
direction: 'horizontal',
colorStops: [
{ offset: 0, color: '#667eea' },
{ offset: 1, color: '#764ba2' }
]
}"
/>
径向渐变 - 中心
<tri-qrcode
text="Hello"
:dots-gradient="{
type: 'radial',
direction: 'center',
colorStops: [
{ offset: 0, color: '#f093fb' },
{ offset: 1, color: '#4facfe' }
]
}"
/>
多色渐变
<tri-qrcode
text="Hello"
:dots-gradient="{
type: 'linear',
direction: 'diagonal',
colorStops: [
{ offset: 0, color: '#ff6b6b' },
{ offset: 0.5, color: '#feca57' },
{ offset: 1, color: '#48dbfb' }
]
}"
/>
💡 使用示例
示例1:简约风格
<tri-qrcode
text="https://example.com"
:width="400"
:height="400"
dots-type="rounded"
dots-color="#2c3e50"
background-color="#ecf0f1"
/>
示例2:渐变风格 + Logo
<tri-qrcode
text="https://example.com"
logo-image="/static/logo.png"
logo-size="medium"
logo-shape="circle"
logo-shadow
error-correction-level="H"
:dots-gradient="{
type: 'linear',
direction: 'diagonal',
colorStops: [
{ offset: 0, color: '#667eea' },
{ offset: 1, color: '#764ba2' }
]
}"
/>
示例3:前景图填充
<tri-qrcode
text="https://example.com"
dots-image="/static/pattern.png"
dots-type="rounded"
background-color="#ffffff"
/>
示例4:透明背景
<tri-qrcode
text="https://example.com"
:background-transparent="true"
dots-color="#000000"
/>
示例5:液化风格
<tri-qrcode
text="https://example.com"
dots-type="fluid"
corners-square-type="extra-rounded"
corners-dot-type="dot"
:dots-gradient="{
type: 'radial',
direction: 'center',
colorStops: [
{ offset: 0, color: '#fa709a' },
{ offset: 1, color: '#fee140' }
]
}"
/>
示例6:完整配置
<template>
<view class="container">
<tri-qrcode
ref="qrcodeRef"
text="https://github.com"
:width="450"
:height="450"
:margin="2"
version="0"
error-correction-level="H"
mode="Byte"
logo-image="/static/logo.png"
logo-size="medium"
logo-shape="rounded-rectangle"
logo-margin="small"
:logo-shadow="true"
:hide-background-dots="true"
dots-type="rounded"
:dots-gradient="{
type: 'linear',
direction: 'diagonal',
colorStops: [
{ offset: 0, color: '#667eea' },
{ offset: 1, color: '#764ba2' }
]
}"
corners-square-type="extra-rounded"
corners-square-color="#667eea"
corners-dot-type="dot"
corners-dot-color="#764ba2"
background-color="#ffffff"
:preview-mode="true"
@rendered="onRendered"
@error="onError"
/>
<view class="actions">
<button @click="handleSave">保存到相册</button>
<button @click="handleRefresh">刷新</button>
</view>
</view>
</template>
<script setup>
import { ref } from 'vue'
const qrcodeRef = ref(null)
const onRendered = ({ renderTime }) => {
console.log(`二维码生成完成,耗时: ${renderTime}ms`)
}
const onError = ({ error }) => {
console.error('生成失败:', error.message)
}
const handleSave = async () => {
try {
const filePath = await qrcodeRef.value.save()
uni.showToast({ title: '保存成功', icon: 'success' })
} catch (error) {
uni.showToast({ title: '保存失败', icon: 'error' })
}
}
const handleRefresh = () => {
qrcodeRef.value.refresh()
}
</script>
<style scoped>
.container {
padding: 20rpx;
display: flex;
flex-direction: column;
align-items: center;
}
.actions {
margin-top: 40rpx;
display: flex;
gap: 20rpx;
}
</style>
⚠️ 注意事项
1. 颜色配置互斥
每个部分(码点、码眼、背景)的颜色配置是互斥的:
- 使用单色时,不要同时设置渐变或前景图
- 使用渐变时,不要同时设置单色或前景图
- 使用前景图时,不要同时设置单色或渐变
2. Logo与容错率
- 使用Logo时,建议将容错率设置为
H或Q - Logo尺寸越大,需要的容错率越高
- 开启
hide-background-dots后,需要更高的容错率
3. 性能优化
- 预览时使用
preview-mode="true"(默认),性能更好 - 导出高质量图片时使用
preview-mode="false" - 组件内置防抖机制,频繁更新配置不会影响性能
4. 图片路径
- Logo和前景图支持本地路径、网络URL、base64
- 本地路径示例:
/static/logo.png - 网络URL示例:
https://example.com/logo.png - base64示例:
data:image/png;base64,...
5. 渐变方向
linear类型支持horizontal、vertical、diagonal方向radial类型仅支持center方向
6. 多个二维码组件
- 如果页面中使用多个二维码组件,请确保每个组件有唯一的
canvas-id - 或者不设置
canvas-id,让组件自动生成(推荐)

收藏人数:
下载插件并导入HBuilderX
下载示例项目ZIP
赞赏(0)
下载 8
赞赏 0
下载 12456710
赞赏 1830
赞赏
京公网安备:11010802035340号