index.vue
7.52 KB
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<script setup lang="ts">
import type { VbenFormProps } from '@vben/common-ui';
import type { VxeGridProps } from '#/adapter/vxe-table';
import type { PageQuery } from '#/api/common';
import type { OssFile } from '#/api/system/oss/model';
import { onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';
import { Page, useVbenModal } from '@vben/common-ui';
import { $t } from '@vben/locales';
import { getVxePopupContainer } from '@vben/utils';
import {
Image,
message,
Modal,
Popconfirm,
Space,
Spin,
Switch,
Tooltip,
} from 'ant-design-vue';
import {
addSortParams,
useVbenVxeGrid,
vxeCheckboxChecked,
} from '#/adapter/vxe-table';
import { configInfoByKey } from '#/api/system/config';
import { ossDownload, ossList, ossRemove } from '#/api/system/oss';
import { calculateFileSize } from '#/utils/file';
import { downloadByData } from '#/utils/file/download';
import { supportImageList } from './constant';
import { columns, querySchema } from './data';
import fallbackImageBase64 from './fallback-image.txt?raw';
import fileUploadModal from './file-upload-modal.vue';
import imageUploadModal from './image-upload-modal.vue';
const formOptions: VbenFormProps = {
commonConfig: {
labelWidth: 80,
componentProps: {
allowClear: true,
},
},
schema: querySchema(),
wrapperClass: 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4',
// 日期选择格式化
fieldMappingTime: [
[
'creationTime',
['params[beginCreateTime]', 'params[endCreateTime]'],
['YYYY-MM-DD 00:00:00', 'YYYY-MM-DD 23:59:59'],
],
],
};
const gridOptions: VxeGridProps = {
checkboxConfig: {
// 高亮
highlight: true,
// 翻页时保留选中状态
reserve: true,
// 点击行选中
// trigger: 'row',
},
columns,
height: 'auto',
keepSource: true,
pagerConfig: {},
proxyConfig: {
ajax: {
query: async ({ page, sorts }, formValues = {}) => {
const params: PageQuery = {
SkipCount: page.currentPage,
MaxResultCount: page.pageSize,
...formValues,
};
// 添加排序参数
addSortParams(params, sorts);
return await ossList(params);
},
},
},
headerCellConfig: {
height: 44,
},
cellConfig: {
height: 65,
},
rowConfig: {
keyField: 'ossId',
},
sortConfig: {
// 远程排序
remote: true,
// 支持多字段排序 默认关闭
multiple: false,
},
id: 'system-oss-index',
};
const [BasicTable, tableApi] = useVbenVxeGrid({
formOptions,
gridOptions,
gridEvents: {
// 排序 重新请求接口
sortChange: () => tableApi.query(),
},
});
async function handleDownload(row: OssFile) {
const downloadSize = ref($t('pages.common.downloadLoading'));
const hideLoading = message.loading({
content: () => downloadSize.value,
duration: 0,
});
try {
const data = await ossDownload(row.ossId, (e) => {
// 计算下载进度
const percent = Math.floor((e.loaded / e.total!) * 100);
// 已经下载
const current = calculateFileSize(e.loaded);
// 总大小
const total = calculateFileSize(e.total!);
downloadSize.value = `已下载: ${current}/${total} (${percent}%)`;
});
downloadByData(data, row.originalName);
message.success('下载完成');
} finally {
hideLoading();
}
}
async function handleDelete(row: OssFile) {
await ossRemove([row.ossId]);
await tableApi.query();
}
function handleMultiDelete() {
const rows = tableApi.grid.getCheckboxRecords();
const ids = rows.map((row: OssFile) => row.ossId);
Modal.confirm({
title: '提示',
okType: 'danger',
content: `确认删除选中的${ids.length}条记录吗?`,
onOk: async () => {
await ossRemove(ids);
await tableApi.query();
},
});
}
const router = useRouter();
function handleToSettings() {
router.push('/system/oss-config/index');
}
const preview = ref(false);
onMounted(async () => {
const previewStr = await configInfoByKey('sys.oss.previewListResource');
preview.value = previewStr === 'true';
});
/**
* 根据拓展名判断是否是图片
* @param ext 拓展名
*/
function isImageFile(ext: string) {
return supportImageList.some((item) =>
ext.toLocaleLowerCase().includes(item),
);
}
/**
* 判断是否是pdf文件
* @param ext 扩展名
*/
function isPdfFile(ext: string) {
return ext.toLocaleLowerCase().includes('pdf');
}
/**
* pdf预览 使用浏览器接管
* @param url 文件地址
*/
function pdfPreview(url: string) {
window.open(url);
}
const [ImageUploadModal, imageUploadApi] = useVbenModal({
connectedComponent: imageUploadModal,
});
const [FileUploadModal, fileUploadApi] = useVbenModal({
connectedComponent: fileUploadModal,
});
</script>
<template>
<Page :auto-content-height="true">
<BasicTable table-title="文件列表">
<template #toolbar-tools>
<Space>
<Tooltip title="预览图片">
<Switch v-model:checked="preview" />
</Tooltip>
<a-button
v-access:code="['system:ossConfig:list']"
@click="handleToSettings"
>
配置管理
</a-button>
<a-button
:disabled="!vxeCheckboxChecked(tableApi)"
danger
type="primary"
v-access:code="['system:oss:remove']"
@click="handleMultiDelete"
>
{{ $t('pages.common.delete') }}
</a-button>
<a-button
v-access:code="['system:oss:upload']"
@click="fileUploadApi.open"
>
文件上传
</a-button>
<a-button
v-access:code="['system:oss:upload']"
@click="imageUploadApi.open"
>
图片上传
</a-button>
</Space>
</template>
<template #url="{ row }">
<!-- placeholder为图片未加载时显示的占位图 -->
<!-- fallback为图片加载失败时显示 -->
<!-- 需要设置key属性 否则切换翻页会有延迟 -->
<Image
:key="row.ossId"
v-if="preview && isImageFile(row.url)"
:src="row.url"
height="50px"
:fallback="fallbackImageBase64"
>
<template #placeholder>
<div class="flex size-full items-center justify-center">
<Spin />
</div>
</template>
</Image>
<!-- pdf预览 使用浏览器开新窗口 -->
<span
v-else-if="preview && isPdfFile(row.url)"
class="icon-[vscode-icons--file-type-pdf2] size-10 cursor-pointer"
@click.stop="pdfPreview(row.url)"
></span>
<span v-else>{{ row.url }}</span>
</template>
<template #action="{ row }">
<Space>
<ghost-button
v-access:code="['system:oss:download']"
@click="handleDownload(row)"
>
{{ $t('pages.common.download') }}
</ghost-button>
<Popconfirm
:get-popup-container="getVxePopupContainer"
placement="left"
title="确认删除?"
@confirm="handleDelete(row)"
>
<ghost-button
danger
v-access:code="['system:oss:remove']"
@click.stop=""
>
{{ $t('pages.common.delete') }}
</ghost-button>
</Popconfirm>
</Space>
</template>
</BasicTable>
<ImageUploadModal @reload="tableApi.query" />
<FileUploadModal @reload="tableApi.query" />
</Page>
</template>