This commit is contained in:
dap 2024-11-11 14:09:47 +08:00
commit 12d53db740
48 changed files with 314 additions and 55 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@vben/web-ele",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://vben.pro",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben/web-naive",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://vben.pro",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben/docs",
"version": "5.4.5",
"version": "5.4.6",
"private": true,
"scripts": {
"build": "vitepress build",

View File

@ -1,6 +1,6 @@
{
"name": "@vben/commitlint-config",
"version": "5.4.5",
"version": "5.4.6",
"private": true,
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",

View File

@ -1,6 +1,6 @@
{
"name": "@vben/stylelint-config",
"version": "5.4.5",
"version": "5.4.6",
"private": true,
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",

View File

@ -1,6 +1,6 @@
{
"name": "@vben/node-utils",
"version": "5.4.5",
"version": "5.4.6",
"private": true,
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",

View File

@ -1,6 +1,6 @@
{
"name": "@vben/tailwind-config",
"version": "5.4.5",
"version": "5.4.6",
"private": true,
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",

View File

@ -1,6 +1,6 @@
{
"name": "@vben/tsconfig",
"version": "5.4.5",
"version": "5.4.6",
"private": true,
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",

View File

@ -1,6 +1,6 @@
{
"name": "@vben/vite-config",
"version": "5.4.5",
"version": "5.4.6",
"private": true,
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",

View File

@ -1,6 +1,6 @@
{
"name": "vben-admin-monorepo",
"version": "5.4.5",
"version": "5.4.6",
"private": true,
"keywords": [
"monorepo",

View File

@ -1,6 +1,6 @@
{
"name": "@vben-core/design",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben-core/icons",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben-core/shared",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -0,0 +1,160 @@
import { openWindow } from './window';
interface DownloadOptions<T = string> {
fileName?: string;
source: T;
target?: string;
}
const DEFAULT_FILENAME = 'downloaded_file';
/**
* URL
* @throws {Error} -
*/
export async function downloadFileFromUrl({
fileName,
source,
target = '_blank',
}: DownloadOptions): Promise<void> {
if (!source || typeof source !== 'string') {
throw new Error('Invalid URL.');
}
const isChrome = window.navigator.userAgent.toLowerCase().includes('chrome');
const isSafari = window.navigator.userAgent.toLowerCase().includes('safari');
if (/iP/.test(window.navigator.userAgent)) {
console.error('Your browser does not support download!');
return;
}
if (isChrome || isSafari) {
triggerDownload(source, resolveFileName(source, fileName));
}
if (!source.includes('?')) {
source += '?download';
}
openWindow(source, { target });
}
/**
* Base64
*/
export function downloadFileFromBase64({ fileName, source }: DownloadOptions) {
if (!source || typeof source !== 'string') {
throw new Error('Invalid Base64 data.');
}
const resolvedFileName = fileName || DEFAULT_FILENAME;
triggerDownload(source, resolvedFileName);
}
/**
* URL
*/
export async function downloadFileFromImageUrl({
fileName,
source,
}: DownloadOptions) {
const base64 = await urlToBase64(source);
downloadFileFromBase64({ fileName, source: base64 });
}
/**
* Blob
* @param blob - Blob
* @param fileName -
*/
export function downloadFileFromBlob({
fileName = DEFAULT_FILENAME,
source,
}: DownloadOptions<Blob>): void {
if (!(source instanceof Blob)) {
throw new TypeError('Invalid Blob data.');
}
const url = URL.createObjectURL(source);
triggerDownload(url, fileName);
}
/**
* Blob BlobPart
* @param data - BlobPart
* @param fileName -
*/
export function downloadFileFromBlobPart({
fileName = DEFAULT_FILENAME,
source,
}: DownloadOptions<BlobPart>): void {
// 如果 data 不是 Blob则转换为 Blob
const blob =
source instanceof Blob
? source
: new Blob([source], { type: 'application/octet-stream' });
// 创建对象 URL 并触发下载
const url = URL.createObjectURL(blob);
triggerDownload(url, fileName);
}
/**
* img url to base64
* @param url
*/
export function urlToBase64(url: string, mineType?: string): Promise<string> {
return new Promise((resolve, reject) => {
let canvas = document.createElement('CANVAS') as HTMLCanvasElement | null;
const ctx = canvas?.getContext('2d');
const img = new Image();
img.crossOrigin = '';
img.addEventListener('load', () => {
if (!canvas || !ctx) {
return reject(new Error('Failed to create canvas.'));
}
canvas.height = img.height;
canvas.width = img.width;
ctx.drawImage(img, 0, 0);
const dataURL = canvas.toDataURL(mineType || 'image/png');
canvas = null;
resolve(dataURL);
});
img.src = url;
});
}
/**
*
* @param href - URL
* @param fileName -
* @param revokeDelay - URL ()
*/
export function triggerDownload(
href: string,
fileName: string | undefined,
revokeDelay: number = 100,
): void {
const defaultFileName = 'downloaded_file';
const finalFileName = fileName || defaultFileName;
const link = document.createElement('a');
link.href = href;
link.download = finalFileName;
link.style.display = 'none';
if (link.download === undefined) {
link.setAttribute('target', '_blank');
}
document.body.append(link);
link.click();
link.remove();
// 清理临时 URL 以释放内存
setTimeout(() => URL.revokeObjectURL(href), revokeDelay);
}
function resolveFileName(url: string, fileName?: string): string {
return fileName || url.slice(url.lastIndexOf('/') + 1) || DEFAULT_FILENAME;
}

View File

@ -2,6 +2,7 @@ export * from './cn';
export * from './date';
export * from './diff';
export * from './dom';
export * from './download';
export * from './inference';
export * from './letter';
export * from './merge';

View File

@ -1,6 +1,6 @@
{
"name": "@vben-core/typings",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben-core/composables",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben-core/preferences",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben-core/layout-ui",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben-core/menu-ui",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben-core/shadcn-ui",
"version": "5.4.5",
"version": "5.4.6",
"#main": "./dist/index.mjs",
"#module": "./dist/index.mjs",
"homepage": "https://github.com/vbenjs/vue-vben-admin",

View File

@ -10,15 +10,18 @@ defineOptions({
inheritAttrs: false,
});
const props = withDefaults(defineProps<PinInputProps>(), {
btnLoading: false,
codeLength: 6,
handleSendCode: async () => {},
maxTime: 60,
});
const {
codeLength = 6,
createText = async () => {},
disabled = false,
handleSendCode = async () => {},
loading = false,
maxTime = 60,
} = defineProps<PinInputProps>();
const emit = defineEmits<{
complete: [];
sendError: [error: any];
}>();
const timer = ref<ReturnType<typeof setTimeout>>();
@ -30,11 +33,11 @@ const countdown = ref(0);
const btnText = computed(() => {
const countdownValue = countdown.value;
return props.createText?.(countdownValue);
return createText?.(countdownValue);
});
const btnLoading = computed(() => {
return props.loading || countdown.value > 0;
return loading || countdown.value > 0;
});
watch(
@ -50,10 +53,16 @@ function handleComplete(e: string[]) {
}
async function handleSend(e: Event) {
e?.preventDefault();
await props.handleSendCode();
countdown.value = props.maxTime;
startCountdown();
try {
e?.preventDefault();
await handleSendCode();
countdown.value = maxTime;
startCountdown();
} catch (error) {
console.error('Failed to send code:', error);
// Consider emitting an error event or showing a notification
emit('sendError', error);
}
}
function startCountdown() {
@ -77,6 +86,7 @@ const id = useId();
<PinInput
:id="id"
v-model="inputValue"
:disabled="disabled"
class="flex w-full justify-between"
otp
placeholder="○"
@ -92,6 +102,7 @@ const id = useId();
/>
</PinInputGroup>
<VbenButton
:disabled="disabled"
:loading="btnLoading"
class="flex-grow"
size="lg"

View File

@ -8,6 +8,10 @@ interface PinInputProps {
*
*/
createText?: (countdown: number) => string;
/**
*
*/
disabled?: boolean;
/**
*
* @returns

View File

@ -1,6 +1,6 @@
{
"name": "@vben-core/tabs-ui",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben/constants",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben/access",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben/common-ui",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben/hooks",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben/layouts",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {
@ -37,7 +37,6 @@
"@vben/types": "workspace:*",
"@vben/utils": "workspace:*",
"@vueuse/core": "catalog:",
"default-passive-events": "catalog:",
"vue": "catalog:",
"vue-router": "catalog:"
}

View File

@ -29,8 +29,6 @@ import {
} from './menu';
import { LayoutTabbar } from './tabbar';
import 'default-passive-events';
defineOptions({ name: 'BasicLayout' });
const emit = defineEmits<{ clearPreferencesAndLogout: [] }>();

View File

@ -1,6 +1,6 @@
{
"name": "@vben/plugins",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben/request",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben/icons",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben/locales",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben/preferences",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben/stores",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben/styles",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben/types",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben/utils",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://github.com/vbenjs/vue-vben-admin",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -1,6 +1,6 @@
{
"name": "@vben/playground",
"version": "5.4.5",
"version": "5.4.6",
"homepage": "https://vben.pro",
"bugs": "https://github.com/vbenjs/vue-vben-admin/issues",
"repository": {

View File

@ -49,7 +49,8 @@
"fullScreen": "FullScreen",
"clipboard": "Clipboard",
"menuWithQuery": "Menu With Query",
"openInNewWindow": "Open in New Window"
"openInNewWindow": "Open in New Window",
"fileDownload": "File Download"
},
"breadcrumb": {
"navigation": "Breadcrumb Navigation",

View File

@ -49,7 +49,8 @@
"fullScreen": "全屏",
"clipboard": "剪贴板",
"menuWithQuery": "带参菜单",
"openInNewWindow": "新窗口打开"
"openInNewWindow": "新窗口打开",
"fileDownload": "文件下载"
},
"breadcrumb": {
"navigation": "面包屑导航",

View File

@ -177,6 +177,16 @@ const routes: RouteRecordRaw[] = [
title: $t('demos.features.fullScreen'),
},
},
{
name: 'FileDownloadDemo',
path: '/demos/features/file-download',
component: () =>
import('#/views/demos/features/file-download/index.vue'),
meta: {
icon: 'lucide:hard-drive-download',
title: $t('demos.features.fileDownload'),
},
},
{
name: 'ClipboardDemo',
path: '/demos/features/clipboard',

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,74 @@
<script setup lang="ts">
import { Page } from '@vben/common-ui';
import {
downloadFileFromBase64,
downloadFileFromBlobPart,
downloadFileFromImageUrl,
downloadFileFromUrl,
} from '@vben/utils';
import { Button, Card } from 'ant-design-vue';
import imageBase64 from './base64';
</script>
<template>
<Page title="文件下载示例">
<Card title="根据文件地址下载文件">
<Button
type="primary"
@click="
downloadFileFromUrl({
source:
'https://codeload.github.com/vbenjs/vue-vben-admin-doc/zip/main',
target: '_self',
})
"
>
Download File
</Button>
</Card>
<Card class="my-5" title="根据地址下载图片">
<Button
type="primary"
@click="
downloadFileFromImageUrl({
source:
'https://unpkg.com/@vbenjs/static-source@0.1.7/source/logo-v1.webp',
fileName: 'vben-logo.png',
})
"
>
Download File
</Button>
</Card>
<Card class="my-5" title="base64流下载">
<Button
type="primary"
@click="
downloadFileFromBase64({
source: imageBase64,
fileName: 'image.png',
})
"
>
Download Image
</Button>
</Card>
<Card class="my-5" title="文本下载">
<Button
type="primary"
@click="
downloadFileFromBlobPart({
source: 'text content',
fileName: 'test.txt',
})
"
>
Download TxT
</Button>
</Card>
</Page>
</template>

View File

@ -79,7 +79,6 @@ catalog:
cz-git: ^1.11.0
czg: ^1.11.0
dayjs: ^1.11.13
default-passive-events: ^2.0.0
defu: ^6.1.4
depcheck: ^1.4.7
dotenv: ^16.4.5

View File

@ -1,6 +1,6 @@
{
"name": "@vben/turbo-run",
"version": "5.4.5",
"version": "5.4.6",
"private": true,
"license": "MIT",
"type": "module",

View File

@ -1,6 +1,6 @@
{
"name": "@vben/vsh",
"version": "5.4.5",
"version": "5.4.6",
"private": true,
"license": "MIT",
"type": "module",