import{_ as n}from"./chunks/AdminExample.vue_vue_type_script_setup_true_lang.wPcM-fsm.js";import{o as h,c as k,j as i,a,E as l,a3 as t}from"./chunks/framework.D8G7yXPy.js";const g=JSON.parse('{"title":"二维码","description":"","frontmatter":{},"headers":[],"relativePath":"reference/components/qr-code.md","filePath":"reference/components/qr-code.md"}'),p={name:"reference/components/qr-code.md"},y=Object.assign(p,{setup(e){return(E,s)=>(h(),k("div",null,[s[0]||(s[0]=i("h1",{id:"二维码",tabindex:"-1"},[a("二维码 "),i("a",{class:"header-anchor",href:"#二维码","aria-label":'Permalink to "二维码"'},"")],-1)),s[1]||(s[1]=i("p",null,[a("Ray Template 直接使用 "),i("code",null,"vue3-next-qrcode"),a(" 的 "),i("code",null,"Vue3NextQrcode"),a(",公开实例类型为 "),i("code",null,"QRCodeInst"),a("。组件演示和登录扫码占位页分别使用它。")],-1)),s[2]||(s[2]=i("h2",{id:"交互示例",tabindex:"-1"},[a("交互示例 "),i("a",{class:"header-anchor",href:"#交互示例","aria-label":'Permalink to "交互示例"'},"")],-1)),l(n,{path:"/qrcode",title:"二维码参数和预览"}),s[3]||(s[3]=t(`查看示例源码
import LOGO from '@/assets/images/ray.svg'
import { useI18n } from '@/hooks'
import {
NButton,
NCard,
NFlex,
NFormItem,
NInput,
NRadioButton,
NRadioGroup,
NSwitch,
} from 'naive-ui'
import { Vue3NextQrcode } from 'vue3-next-qrcode'
import type { QRCodeInst, QRCodeStatus } from 'vue3-next-qrcode'
import DemoPage from '../components/DemoPage'
import DemoResult from '../components/DemoResult'
const initial = 'https://xiaodaigua-ray.github.io/ray-template/'
export default defineComponent({
name: 'RQRCodeDemo',
setup() {
const { t } = useI18n('demoDetails')
const text = ref(initial)
const brand = ref(true)
const animated = ref(false)
const status = ref<'ready' | QRCodeStatus>('ready')
const rendered = ref(false)
const failed = ref(false)
const instance = useTemplateRef<QRCodeInst>('qrcode')
const id = useId()
watch([text, brand, animated], () => {
rendered.value = false
failed.value = false
})
return () => (
<DemoPage title={t('qr.title')} description={t('qr.intro')}>
<div class="r-demo-grid">
<NCard title={t('qr.controls')}>
<div class="r-demo-card-stack">
<NFormItem
label={t('qr.content')}
labelProps={{ for: id }}
showFeedback={false}
>
<NInput
v-model:value={text.value}
maxlength={160}
showCount
inputProps={{ id }}
/>
</NFormItem>
<NFlex align="center">
<NSwitch
v-model:value={brand.value}
aria-label={t('qr.brand')}
/>
<span>{t('qr.brand')}</span>
<NSwitch
v-model:value={animated.value}
aria-label={t('qr.animated')}
/>
<span>{t('qr.animated')}</span>
</NFlex>
<NRadioGroup
v-model:value={status.value}
aria-label={t('qr.status')}
>
{(['ready', 'loading', 'error'] as const).map((state) => (
<NRadioButton key={state} value={state}>
{t(\`qr.\${state}\`)}
</NRadioButton>
))}
</NRadioGroup>
<NButton
type="primary"
disabled={
!rendered.value || failed.value || status.value !== 'ready'
}
onClick={() => instance.value?.downloadQRCode()}
>
{t('qr.download')}
</NButton>
</div>
</NCard>
<NCard title={t('qr.preview')}>
<div class="r-demo-card-stack">
<div class="r-demo-artifact">
{text.value.trim() && !failed.value ? (
<Vue3NextQrcode
ref="qrcode"
text={text.value}
size={180}
logoImage={brand.value ? LOGO : void 0}
logoScale={0.22}
gifBackgroundURL={
animated.value
? \`\${import.meta.env.BASE_URL}examples/qr-background.gif\`
: void 0
}
dotScale={animated.value ? 0.9 : void 0}
status={status.value === 'ready' ? void 0 : status.value}
errorDescription={t('qr.expired')}
errorActionDescription={t('qr.reload')}
onReload={() => {
status.value = 'ready'
}}
onSuccess={() => {
rendered.value = true
}}
onError={() => {
failed.value = true
}}
/>
) : (
<span>{failed.value ? t('qr.failed') : t('qr.noText')}</span>
)}
</div>
<DemoResult label={t('qr.content')}>
<code>{text.value || t('empty')}</code>
</DemoResult>
</div>
</NCard>
</div>
</DemoPage>
)
},
})
import { NButton } from 'naive-ui'
import { defineComponent, ref } from 'vue'
import { Vue3NextQrcode, type QRCodeInst } from 'vue3-next-qrcode'
export default defineComponent({
setup() {
const qr = ref<QRCodeInst>()
return () => (
<>
<Vue3NextQrcode ref={qr} text="https://example.com/report" />
<NButton onClick={() => qr.value?.downloadQRCode()}>下载</NButton>
</>
)
},
})text 设置内容,默认可随内容变化重绘。logoImage、backgroundImage、gifBackgroundURL、dotScale、colorDark 可调整样式。带背景的二维码要实际扫码检查清晰度,远程素材还受跨域和浏览器加载限制。
status 可显示 loading、error 等状态,onReload 在重试时通知调用方。downloadQRCode() 等到实例挂载后调用。二维码只表达数据,生成成功与扫码认证成功是两回事;登录需要业务后端签发和验证对应凭据。