-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from mao2006/qrCode
feat: 新增问卷二维码分享
- Loading branch information
Showing
3 changed files
with
76 additions
and
3 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
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,51 @@ | ||
import { ref, onMounted } from 'vue' | ||
import QRCode from 'qrcode'; | ||
import { ElMessage } from 'element-plus'; | ||
import { showModal } from '@/components'; | ||
|
||
export const useQrCode = (url: string) => { | ||
const qrCodeURL = ref(''); // 用于存储二维码的 base64 URL | ||
//复制二维码到剪贴板 | ||
const generateQRCode = async () => { | ||
try { | ||
qrCodeURL.value = await QRCode.toDataURL(url, { | ||
width: 300, | ||
margin: 2, | ||
color: { | ||
dark: '#000000', | ||
light: '#FFFFFF', | ||
}, | ||
}); | ||
} catch (error) { | ||
console.error('二维码生成失败:', error); | ||
} | ||
}; | ||
|
||
onMounted(() => { | ||
generateQRCode(); | ||
}); | ||
|
||
//复制二维码到剪贴板 | ||
const copyQrCode = async () => { | ||
try { | ||
const response = await fetch(qrCodeURL.value) | ||
const blob = await response.blob() | ||
const tempItem = new ClipboardItem({ [blob.type]: blob }); | ||
await navigator.clipboard.write([tempItem]) | ||
showModal('QRcode', true); | ||
ElMessage({ | ||
message: '二维码复制成功', | ||
type: 'success', | ||
}) | ||
} catch { | ||
ElMessage({ | ||
message: '二维码复制失败', | ||
type: 'error', | ||
}) | ||
} | ||
} | ||
return { | ||
qrCodeURL, | ||
copyQrCode | ||
} | ||
} |