2023-03-03 06:56:23 +00:00
|
|
|
import { defineComponent, computed, ref } from 'vue';
|
|
|
|
import type { CSSProperties } from 'vue';
|
2023-03-02 02:46:16 +00:00
|
|
|
import useConfigInject from '../config-provider/hooks/useConfigInject';
|
|
|
|
import useStyle from './style';
|
|
|
|
import { useLocaleReceiver } from '../locale/LocaleReceiver';
|
|
|
|
import { withInstall } from '../_util/type';
|
|
|
|
import Spin from '../spin';
|
|
|
|
import Button from '../button';
|
|
|
|
import { ReloadOutlined } from '@ant-design/icons-vue';
|
|
|
|
import { useToken } from '../theme/internal';
|
2023-03-03 06:56:23 +00:00
|
|
|
import { QRCodeCanvas } from './QRCodeCanvas';
|
|
|
|
import warning from '../_util/warning';
|
|
|
|
import type { QRCodeProps } from './interface';
|
|
|
|
import { qrcodeProps } from './interface';
|
2023-03-02 02:46:16 +00:00
|
|
|
|
|
|
|
const QRCode = defineComponent({
|
|
|
|
name: 'AQrcode',
|
2023-03-03 06:56:23 +00:00
|
|
|
inheritAttrs: false,
|
|
|
|
props: qrcodeProps(),
|
2023-03-02 02:46:16 +00:00
|
|
|
emits: ['refresh'],
|
2023-03-03 06:56:23 +00:00
|
|
|
setup(props, { emit, attrs, expose }) {
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
warning(
|
|
|
|
!(props.icon && props.errorLevel === 'L'),
|
|
|
|
'QRCode',
|
|
|
|
'ErrorLevel `L` is not recommended to be used with `icon`, for scanning result would be affected by low level.',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const [locale] = useLocaleReceiver('QRCode');
|
2023-03-02 02:46:16 +00:00
|
|
|
const { prefixCls } = useConfigInject('qrcode', props);
|
|
|
|
const [wrapSSR, hashId] = useStyle(prefixCls);
|
|
|
|
const [, token] = useToken();
|
2023-03-03 06:56:23 +00:00
|
|
|
const qrCodeCanvas = ref();
|
|
|
|
expose({
|
|
|
|
toDataURL: (type?: string, quality?: any) => {
|
|
|
|
return qrCodeCanvas.value?.toDataURL(type, quality);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const qrCodeProps = computed(() => {
|
|
|
|
const {
|
|
|
|
value,
|
|
|
|
icon = '',
|
|
|
|
size = 160,
|
|
|
|
iconSize = 40,
|
|
|
|
color = '#000',
|
|
|
|
errorLevel = 'M',
|
|
|
|
} = props;
|
|
|
|
const imageSettings: QRCodeProps['imageSettings'] = {
|
|
|
|
src: icon,
|
|
|
|
x: undefined,
|
|
|
|
y: undefined,
|
|
|
|
height: iconSize,
|
|
|
|
width: iconSize,
|
|
|
|
excavate: true,
|
|
|
|
};
|
|
|
|
return {
|
|
|
|
value,
|
|
|
|
size: size - (token.value.paddingSM + token.value.lineWidth) * 2,
|
|
|
|
level: errorLevel,
|
|
|
|
bgColor: 'transparent',
|
|
|
|
fgColor: color,
|
|
|
|
imageSettings: icon ? imageSettings : undefined,
|
|
|
|
};
|
|
|
|
});
|
2023-03-02 02:46:16 +00:00
|
|
|
return () => {
|
2023-03-03 06:56:23 +00:00
|
|
|
const pre = prefixCls.value;
|
2023-03-02 02:46:16 +00:00
|
|
|
return wrapSSR(
|
|
|
|
<div
|
2023-03-03 06:56:23 +00:00
|
|
|
{...attrs}
|
|
|
|
style={[
|
|
|
|
attrs.style as CSSProperties,
|
|
|
|
{ width: props.size + 'px', height: props.size + 'px' },
|
|
|
|
]}
|
|
|
|
class={[
|
|
|
|
hashId.value,
|
|
|
|
pre,
|
|
|
|
{
|
|
|
|
[`${prefixCls}-borderless`]: !props.bordered,
|
|
|
|
},
|
|
|
|
]}
|
2023-03-02 02:46:16 +00:00
|
|
|
>
|
|
|
|
{props.status !== 'active' && (
|
2023-03-03 06:56:23 +00:00
|
|
|
<div class={`${pre}-mask`}>
|
2023-03-02 02:46:16 +00:00
|
|
|
{props.status === 'loading' && <Spin />}
|
|
|
|
{props.status === 'expired' && (
|
|
|
|
<>
|
2023-03-03 06:56:23 +00:00
|
|
|
<p class={`${pre}-expired`}>{locale.value.expired}</p>
|
|
|
|
<Button
|
|
|
|
type="link"
|
|
|
|
onClick={e => emit('refresh', e)}
|
|
|
|
v-slots={{ icon: () => <ReloadOutlined /> }}
|
|
|
|
>
|
2023-03-02 02:46:16 +00:00
|
|
|
{locale.value.refresh}
|
|
|
|
</Button>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
2023-03-03 06:56:23 +00:00
|
|
|
<QRCodeCanvas ref={qrCodeCanvas} {...qrCodeProps.value} />
|
2023-03-02 02:46:16 +00:00
|
|
|
</div>,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
export default withInstall(QRCode);
|