forked from vueComponent/ant-design-vue
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.tsx
162 lines (161 loc) · 5.27 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { defineComponent, onMounted, ref, toRefs, watch } from 'vue';
import type { ExtractPropTypes } from 'vue';
import classNames from '../_util/classNames';
import useConfigInject from '../config-provider/hooks/useConfigInject';
import { initDefaultProps } from '../_util/props-util';
import useStyle from './style';
import { useLocaleReceiver } from '../locale/LocaleReceiver';
import defaultLocale from '../locale/en_US';
import { toCanvas, toDataURL } from 'qrcode';
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';
interface QRCodeCanvasColor {
dark?: string; // 默认#000000ff
light?: string; // 默认#ffffffff
}
interface QRCodeCanvasOptions {
version?: number;
errorCorrectionLevel?: string; // 默认"M"
maskPattern?: number; // 遮罩符号的掩码图案
toSJISFunc?: Function; // 将汉字转换为其 Shift JIS 值的帮助程序函数
margin?: number;
scale?: number;
small?: boolean;
width: number;
color?: QRCodeCanvasColor;
}
const qrcodeProps = () => {
return {
value: { type: String, required: true },
errorLevel: String,
size: { type: Number, default: 160 },
icon: String,
iconSize: { type: Number, default: 40 },
color: String,
status: { type: String, default: 'active' },
bordered: { type: Boolean, default: true },
};
};
export type QRCodeProps = Partial<ExtractPropTypes<ReturnType<typeof qrcodeProps>>>;
const canvasProps = () => {
return {
value: String,
errorLevel: { type: String, default: 'M' },
size: Number,
icon: String,
iconSize: { type: Number, default: 40 },
color: { type: String, default: '#000000ff' },
};
};
const QRCodeCanvas = defineComponent({
name: 'QRCodeCanvas',
props: initDefaultProps(canvasProps(), {}),
setup(props) {
const qrcodeCanvasRef = ref();
const { size, value, errorLevel, icon, iconSize, color } = toRefs(props);
watch(size, newSize => {
createQRCode(newSize);
});
watch(errorLevel, newLevel => {
createQRCode(size.value, newLevel);
});
const createQRCode = (width = size.value, level = errorLevel.value) => {
const options: QRCodeCanvasOptions = {
errorCorrectionLevel: level || getErrorCorrectionLevel(value.value),
margin: 0,
width,
color: { dark: color.value },
};
toCanvas(qrcodeCanvasRef.value, value.value, options);
if (icon.value) {
const ctx = qrcodeCanvasRef.value.getContext('2d');
const image = new Image(iconSize.value, iconSize.value);
image.src = icon.value;
image.style.backgroundColor = '#000';
image.onload = () => {
/*
drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh)
sx,sy 在画布指定位置绘制
sw,sh 被剪切的部分
dx,dy 在目标画布的起点位置
dw,dh 在目标画布绘制的宽高
*/
ctx.drawImage(qrcodeCanvasRef.value, 0, 0, width, width);
const center = (width - iconSize.value) / 2;
ctx.drawImage(image, center, center, iconSize.value, iconSize.value);
};
}
};
function getErrorCorrectionLevel(content) {
if (content.length > 36) {
return 'M';
} else if (content.length > 16) {
return 'Q';
} else {
return 'H';
}
}
onMounted(() => {
createQRCode();
});
return () => (
<>
<canvas ref={qrcodeCanvasRef} />
</>
);
},
});
const QRCode = defineComponent({
name: 'AQrcode',
props: initDefaultProps(qrcodeProps(), {}),
emits: ['refresh'],
setup(props, { emit, expose }) {
const [locale] = useLocaleReceiver('QRCode', defaultLocale.QRCode);
const { prefixCls } = useConfigInject('qrcode', props);
const [wrapSSR, hashId] = useStyle(prefixCls);
const [, token] = useToken();
const { value, size, status, bordered } = toRefs(props);
const pre = prefixCls.value;
const toDataUrl = async () => {
return await toDataURL(value.value);
};
expose({ toDataUrl });
return () => {
return wrapSSR(
<div
style={{ width: size.value + 'px', height: size.value + 'px' }}
class={classNames(hashId.value, pre, {
[`${prefixCls}-borderless`]: !bordered.value,
})}
>
{status.value !== 'active' && (
<div class={classNames(`${pre}-mask`)}>
{status.value === 'loading' && <Spin />}
{status.value === 'expired' && (
<>
<p class={classNames(`${pre}-expired`)}>{locale.value.expired}</p>
<Button type="link" onClick={() => emit('refresh')}>
<ReloadOutlined />
{locale.value.refresh}
</Button>
</>
)}
</div>
)}
<QRCodeCanvas
value={props.value}
errorLevel={props.errorLevel}
size={props.size - (token.value.paddingSM + token.value.lineWidth) * 2}
icon={props.icon}
iconSize={props.iconSize}
color={props.color}
/>
</div>,
);
};
},
});
export default withInstall(QRCode);