提交 539c605a 作者: 宇宙超人

合并

上级 e07d0d6f
[
{
"key": "ctrl+/",
"command": "editor.action.commentLine",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "shift+alt+a",
"command": "editor.action.blockComment",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "ctrl+k ctrl+c",
"command": "editor.action.addCommentLine",
"when": "editorTextFocus && !editorReadonly"
},
{
"key": "ctrl+k ctrl+u",
"command": "editor.action.removeCommentLine",
"when": "editorTextFocus && !editorReadonly"
}
]
\ No newline at end of file
......@@ -15,9 +15,20 @@ export function bizCommonFileList(params = {}) {
})
}
/**
* 资源列表
* @param params
* @returns
*/
export function getResourceList(params = {}) {
return otherHttp.get({
url: '/resource/list',
params,
})
}
/**
* 添加资源
* @param params
* @returns
* @param params
* @returns
*/
export function addResource(params = {}) {
return otherHttp.post({
......@@ -25,3 +36,14 @@ export function addResource(params = {}) {
params,
})
}
/**
* 下载资源
* @param params
* @returns
*/
export function downloadResource(params = {}) {
return otherHttp.get({
url: '/resource/download',
params,
})
}
......@@ -135,6 +135,10 @@
flex-direction: column;
position: relative;
overflow: hidden;
/* #ifdef APP-PLUS */
position: relative;
z-index: 1;
/* #endif */
}
.fui-swiper__dot {
......@@ -146,7 +150,12 @@
flex-direction: row;
align-items: center;
justify-content: center;
/* #ifdef APP-PLUS */
z-index: 998;
/* #endif */
/* #ifndef APP-PLUS */
z-index: 2;
/* #endif */
overflow: hidden;
}
......
......@@ -248,118 +248,21 @@ function onResourceClick(resource: any) {
}
// 资源下载点击事件
function onDownloadClick(e, resource: any) {
async function onDownloadClick(e, resource: any) {
// 阻止事件冒泡
e?.stopPropagation()
// 显示加载提示
uni.showLoading({
title: '下载中...',
mask: true,
})
// 判断是否为H5平台
/* #ifdef H5 */
downloadFileForWeb(resource)
/* #endif */
/* #ifndef H5 */
downloadFileForNative(resource)
/* #endif */
const { downloadResource } = await import('@/utils')
// 使用封装的下载方法
const result = await downloadResource(resource)
if (!result.success) {
console.error('下载失败:', result.error)
}
}
// H5平台下载文件函数
function downloadFileForWeb(resource: any) {
const url = resource.fileSrc
// 创建一个临时的a标签用于下载
const link = document.createElement('a')
link.href = url
link.download = resource.fileName || 'download' // 设置下载文件名
link.style.display = 'none'
// 添加到文档并触发点击
document.body.appendChild(link)
link.click()
// 清理
document.body.removeChild(link)
uni.hideLoading()
uni.showToast({
title: '开始下载',
icon: 'success',
duration: 1500
})
}
// 原生平台下载文件函数
function downloadFileForNative(resource: any) {
uni.downloadFile({
url: resource.fileSrc,
success: (res) => {
if (res.statusCode === 200) {
uni.saveFile({
tempFilePath: res.tempFilePath,
success: (saveRes) => {
uni.hideLoading()
uni.showToast({
title: '下载成功',
icon: 'success',
duration: 1500
})
// 提示用户文件已保存
uni.showModal({
title: '下载完成',
content: `文件已保存至: ${saveRes.savedFilePath}\n是否立即打开?`,
success: (modalRes) => {
if (modalRes.confirm) {
uni.openDocument({
filePath: saveRes.savedFilePath,
fileType: resource.fileType?.toString() || '',
showMenu: true,
fail: () => {
uni.showToast({
title: '无法打开文件',
icon: 'error',
duration: 1500
})
},
})
}
},
})
},
fail: (err) => {
uni.hideLoading()
console.error('保存文件失败:', err)
uni.showToast({
title: '保存文件失败',
icon: 'error',
duration: 1500
})
},
})
} else {
uni.hideLoading()
uni.showToast({
title: `下载失败,状态码: ${res.statusCode}`,
icon: 'error',
duration: 1500
})
}
},
fail: (err) => {
uni.hideLoading()
console.error('下载失败:', err)
uni.showToast({
title: '网络错误,下载失败',
icon: 'error',
duration: 1500
})
},
})
}
// 查看所有资源
function onViewAllResources() {
......
/**
* 下载工具类
* 提供跨平台的文件下载功能,支持H5和原生平台
*/
/**
* 文件下载选项接口
*/
export interface DownloadOptions {
/** 文件下载URL */
url: string
/** 文件名 */
fileName?: string
/** 文件类型 */
fileType?: string
/** 文件大小 */
fileSize?: string
/** 是否显示下载进度 */
showProgress?: boolean
}
/**
* 下载结果接口
*/
export interface DownloadResult {
/** 是否成功 */
success: boolean
/** 错误信息 */
error?: string
/** 文件保存路径(仅原生平台) */
savedFilePath?: string
}
/**
* H5平台下载文件
* @param options 下载选项
* @returns Promise<DownloadResult>
*/
export function downloadFileForWeb(options: DownloadOptions): Promise<DownloadResult> {
return new Promise((resolve) => {
const { url, fileName = 'download' } = options
try {
// 检查是否在浏览器环境
if (typeof document === 'undefined') {
resolve({
success: false,
error: 'H5下载功能仅支持浏览器环境',
})
return
}
// 创建一个临时的a标签用于下载
const link = document.createElement('a')
link.href = url
link.download = fileName
link.style.display = 'none'
// 添加到文档并触发点击
document.body.appendChild(link)
link.click()
// 清理
document.body.removeChild(link)
// 显示下载成功提示
uni.showToast({
title: '开始下载',
icon: 'success',
duration: 1500,
})
resolve({
success: true,
})
} catch (error) {
console.error('H5下载失败:', error)
resolve({
success: false,
error: 'H5下载失败',
})
}
})
}
/**
* 原生平台下载文件
* @param options 下载选项
* @returns Promise<DownloadResult>
*/
export function downloadFileForNative(options: DownloadOptions): Promise<DownloadResult> {
return new Promise((resolve) => {
const { url, fileName = 'download', fileType = '', showProgress = true } = options
if (showProgress) {
// 显示加载提示
uni.showLoading({
title: '下载中...',
mask: true,
})
}
uni.downloadFile({
url,
success: (res) => {
if (res.statusCode === 200) {
uni.saveFile({
tempFilePath: res.tempFilePath,
success: (saveRes) => {
if (showProgress) {
uni.hideLoading()
uni.showToast({
title: '下载成功',
icon: 'success',
duration: 1500,
})
}
// 提示用户文件已保存
uni.showModal({
title: '下载完成',
content: `文件已保存至: ${saveRes.savedFilePath}\n是否立即打开?`,
success: (modalRes) => {
if (modalRes.confirm) {
uni.openDocument({
filePath: saveRes.savedFilePath,
fileType: fileType?.toString() || '',
showMenu: true,
fail: () => {
uni.showToast({
title: '无法打开文件',
icon: 'none',
duration: 1500,
})
},
})
}
},
})
resolve({
success: true,
savedFilePath: saveRes.savedFilePath,
})
},
fail: (err) => {
if (showProgress) {
uni.hideLoading()
console.error('保存文件失败:', err)
uni.showToast({
title: '保存文件失败',
icon: 'none',
duration: 1500,
})
}
resolve({
success: false,
error: '保存文件失败',
})
},
})
} else {
if (showProgress) {
uni.hideLoading()
uni.showToast({
title: `下载失败,状态码: ${res.statusCode}`,
icon: 'none',
duration: 1500,
})
}
resolve({
success: false,
error: `下载失败,状态码: ${res.statusCode}`,
})
}
},
fail: (err) => {
if (showProgress) {
uni.hideLoading()
console.error('下载失败:', err)
uni.showToast({
title: '网络错误,下载失败',
icon: 'none',
duration: 1500,
})
}
resolve({
success: false,
error: '网络错误,下载失败',
})
},
})
})
}
/**
* 通用下载文件方法
* 自动判断平台并调用相应的下载方法
* @param options 下载选项
* @returns Promise<DownloadResult>
*/
export function downloadFile(options: DownloadOptions): Promise<DownloadResult> {
// 判断是否为H5平台
/* #ifdef H5 */
return downloadFileForWeb(options)
/* #endif */
// 其他平台使用原生下载
/* #ifndef H5*/
return downloadFileForNative(options)
/* #endif */
}
/**
* 便捷下载方法,支持资源对象
* @param resource 资源对象,包含 fileSrc、fileName、fileType 等属性
* @returns Promise<DownloadResult>
*/
export function downloadResource(resource: any): Promise<DownloadResult> {
const options: DownloadOptions = {
url: resource.fileSrc,
fileName: resource.fileName || 'download',
fileType: resource.fileType || '',
fileSize: resource.fileSize || '',
showProgress: true,
}
return downloadFile(options)
}
/**
* 检查下载链接是否有效
* @param url 下载链接
* @returns Promise<boolean>
*/
export function checkDownloadUrl(url: string): Promise<boolean> {
return new Promise((resolve) => {
// 在原生平台检查链接有效性
/* #ifndef H5 */
uni.request({
url,
method: 'HEAD',
success: (res) => {
resolve(res.statusCode === 200)
},
fail: () => {
resolve(false)
},
})
/* #endif */
// 在H5平台,直接返回true(浏览器会处理链接有效性)
/* #ifdef H5 */
resolve(true)
/* #endif */
})
}
......@@ -3,6 +3,8 @@ import type { App, Plugin } from 'vue'
import { unref } from 'vue'
import { isObject } from '/@/utils/is'
export * from './download'
export function noop() {}
/**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论