forked from vbenjs/vue-vben-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
361 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Thank you for contributing to vue-vben-admin! | ||
|
||
Please follow this steps: | ||
|
||
1. Fork it ( https://github.com/anncwb/vue-vben-admin/fork ) | ||
2. Create your feature branch (`git checkout -b my-new-feature`) | ||
3. Commit your changes (`git commit -am 'feat(feat-name): Add some feature'`) | ||
4. Push to the branch (`git push origin my-new-feature`) | ||
5. Create a new Pull Request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
## Description | ||
|
||
## Fix or Feature? | ||
|
||
### Environment | ||
|
||
- OS: Write here | ||
- NPM Version: Write here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// import zxcvbn from 'zxcvbn'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import { getAsyncComponent } from '@/common/factory/getAsyncComponent'; | ||
|
||
export const QrCode = getAsyncComponent(() => import('./src/index.vue')); | ||
|
||
export * from './src/types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { toCanvas, QRCodeRenderersOptions } from 'qrcode'; | ||
import { RenderQrCodeParams, ContentType } from './types'; | ||
export const renderQrCode = ({ canvas, content, width = 0, options = {} }: RenderQrCodeParams) => { | ||
// 容错率,默认对内容少的二维码采用高容错率,内容多的二维码采用低容错率 | ||
options.errorCorrectionLevel = options.errorCorrectionLevel || getErrorCorrectionLevel(content); | ||
|
||
return getOriginWidth(content, options).then((_width: number) => { | ||
options.scale = width === 0 ? undefined : (width / _width) * 4; | ||
return toCanvas(canvas, content, options); | ||
}); | ||
}; | ||
|
||
// 得到原QrCode的大小,以便缩放得到正确的QrCode大小 | ||
function getOriginWidth(content: ContentType, options: QRCodeRenderersOptions) { | ||
const _canvas = document.createElement('canvas'); | ||
return toCanvas(_canvas, content, options).then(() => _canvas.width); | ||
} | ||
|
||
// 对于内容少的QrCode,增大容错率 | ||
function getErrorCorrectionLevel(content: ContentType) { | ||
if (content.length > 36) { | ||
return 'M'; | ||
} else if (content.length > 16) { | ||
return 'Q'; | ||
} else { | ||
return 'H'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { isString } from '@/utils/is'; | ||
import { RenderQrCodeParams, LogoType } from './types'; | ||
export const drawLogo = ({ canvas, logo }: RenderQrCodeParams) => { | ||
if (!logo) { | ||
return new Promise((resolve) => { | ||
resolve((canvas as HTMLCanvasElement).toDataURL()); | ||
}); | ||
} | ||
|
||
const canvasWidth = (canvas as HTMLCanvasElement).width; | ||
const { | ||
logoSize = 0.15, | ||
bgColor = '#ffffff', | ||
borderSize = 0.05, | ||
crossOrigin, | ||
borderRadius = 8, | ||
logoRadius = 0, | ||
} = logo as LogoType; | ||
|
||
const logoSrc: string = isString(logo) ? logo : logo.src; | ||
const logoWidth = canvasWidth * logoSize; | ||
const logoXY = (canvasWidth * (1 - logoSize)) / 2; | ||
const logoBgWidth = canvasWidth * (logoSize + borderSize); | ||
const logoBgXY = (canvasWidth * (1 - logoSize - borderSize)) / 2; | ||
|
||
const ctx = canvas.getContext('2d'); | ||
if (!ctx) return; | ||
|
||
// logo 底色 | ||
canvasRoundRect(ctx)(logoBgXY, logoBgXY, logoBgWidth, logoBgWidth, borderRadius); | ||
ctx.fillStyle = bgColor; | ||
ctx.fill(); | ||
|
||
// logo | ||
const image = new Image(); | ||
if (crossOrigin || logoRadius) { | ||
image.setAttribute('crossOrigin', crossOrigin || 'anonymous'); | ||
} | ||
image.src = logoSrc; | ||
|
||
// 使用image绘制可以避免某些跨域情况 | ||
const drawLogoWithImage = (image: CanvasImageSource) => { | ||
ctx.drawImage(image, logoXY, logoXY, logoWidth, logoWidth); | ||
}; | ||
|
||
// 使用canvas绘制以获得更多的功能 | ||
const drawLogoWithCanvas = (image: HTMLImageElement) => { | ||
const canvasImage = document.createElement('canvas'); | ||
canvasImage.width = logoXY + logoWidth; | ||
canvasImage.height = logoXY + logoWidth; | ||
const imageCanvas = canvasImage.getContext('2d'); | ||
if (!imageCanvas || !ctx) return; | ||
imageCanvas.drawImage(image, logoXY, logoXY, logoWidth, logoWidth); | ||
|
||
canvasRoundRect(ctx)(logoXY, logoXY, logoWidth, logoWidth, logoRadius); | ||
if (!ctx) return; | ||
const fillStyle = ctx.createPattern(canvasImage, 'no-repeat'); | ||
if (fillStyle) { | ||
ctx.fillStyle = fillStyle; | ||
ctx.fill(); | ||
} | ||
}; | ||
|
||
// 将 logo绘制到 canvas上 | ||
return new Promise((resolve) => { | ||
image.onload = () => { | ||
logoRadius ? drawLogoWithCanvas(image) : drawLogoWithImage(image); | ||
resolve((canvas as HTMLCanvasElement).toDataURL()); | ||
}; | ||
}); | ||
}; | ||
|
||
// copy来的方法,用于绘制圆角 | ||
function canvasRoundRect(ctx: CanvasRenderingContext2D) { | ||
return (x: number, y: number, w: number, h: number, r: number) => { | ||
const minSize = Math.min(w, h); | ||
if (r > minSize / 2) { | ||
r = minSize / 2; | ||
} | ||
ctx.beginPath(); | ||
ctx.moveTo(x + r, y); | ||
ctx.arcTo(x + w, y, x + w, y + h, r); | ||
ctx.arcTo(x + w, y + h, x, y + h, r); | ||
ctx.arcTo(x, y + h, x, y, r); | ||
ctx.arcTo(x, y, x + w, y, r); | ||
ctx.closePath(); | ||
return ctx; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// 参考 qr-code-with-logo 进行ts版本修改 | ||
import { toCanvas } from './toCanvas'; | ||
export * from './types'; | ||
|
||
export { toCanvas }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { renderQrCode } from './drawCanvas'; | ||
import { drawLogo } from './drawLogo'; | ||
import { RenderQrCodeParams } from './types'; | ||
export const toCanvas = (options: RenderQrCodeParams) => { | ||
return renderQrCode(options) | ||
.then(() => { | ||
return options; | ||
}) | ||
.then(drawLogo) as Promise<string>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { QRCodeSegment, QRCodeRenderersOptions } from 'qrcode'; | ||
|
||
export type ContentType = string | QRCodeSegment[]; | ||
|
||
export { QRCodeRenderersOptions }; | ||
export interface RenderQrCodeParams { | ||
canvas: any; | ||
content: ContentType; | ||
width?: number; | ||
options?: QRCodeRenderersOptions; | ||
logo?: LogoType | string; | ||
image?: HTMLImageElement; | ||
downloadName?: string; | ||
download?: boolean | ((...arg) => void); | ||
} | ||
|
||
export type LogoType = { | ||
src: string; | ||
logoSize: number; | ||
borderColor: string; | ||
bgColor: string; | ||
borderSize: number; | ||
crossOrigin: string; | ||
borderRadius: number; | ||
logoRadius: number; | ||
}; | ||
|
||
export type ToCanvasFn = (options: RenderQrCodeParams) => Promise<unknown>; | ||
|
||
export interface QrCodeActionType { | ||
download: (fileName?: string) => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.