调整下载资源文件

This commit is contained in:
ywyonui 2025-07-09 18:06:08 +08:00
parent 0b487b6d68
commit a2c8b4dd11
3 changed files with 143 additions and 60 deletions

View File

@ -122,7 +122,11 @@ export const typesFindTreeApi = async () => {
// 获取资源分页 // 获取资源分页
export const resourcesFindPageApi = async (params: any) => { export const resourcesFindPageApi = async (params: any) => {
return await get("/api/resources/findPage", params); return await get("/api/resources/findPage", params);
};
// 获取资源分页
export const resourcesAddNumByTypeApi = async (params: any) => {
return await post("/api/resources/addNumByType", params);
}; };

View File

@ -1,64 +1,64 @@
import {isFunction} from "lodash"; import { isFunction } from "lodash";
import {Ref} from "vue"; import { Ref } from "vue";
import {hideLoading, showLoading} from "@/utils/uniapp"; import { hideLoading, showLoading } from "@/utils/uniapp";
import type { import type {
LayoutCallback, LayoutCallback,
LayoutOptions, LayoutOptions,
PagingRefInterface, PagingRefInterface,
UseLayoutInterfaceReturn UseLayoutInterfaceReturn
} from "@/components/BasicListLayout/type/useLayout"; } from "@/components/BasicListLayout/type/useLayout";
export function useLayout(options: LayoutOptions): UseLayoutInterfaceReturn { export function useLayout(options: LayoutOptions): UseLayoutInterfaceReturn {
let methods: Ref<PagingRefInterface> let methods: Ref<PagingRefInterface>
async function requestApi(pageNo: number, pageSize: number) { async function requestApi(pageNo: number, pageSize: number) {
if (isFunction(options.api)) { if (isFunction(options.api)) {
try { try {
let params = Object.assign({}, { let params = Object.assign({}, {
rows: pageSize, rows: pageSize,
page: pageNo page: pageNo
}, options.param) }, options.param)
const result = await options.api(params) const result = await options.api(params)
await nextTick() await nextTick()
if (methods.value) { if (methods.value) {
// @ts-ignore // @ts-ignore
await methods.value.complete(result[options.resultKey || 'rows']) await methods.value.complete(result[options.resultKey || 'rows'])
}
} catch (err) {
console.log('err', err)
if (methods.value) {
await methods.value.complete(false);
}
}
} }
} } catch (err) {
console.log('err', err)
if (!options.query) { if (methods.value) {
options.query = requestApi await methods.value.complete(false);
}
const register = (callback: LayoutCallback, pagingRef: Ref<PagingRefInterface>) => {
methods = pagingRef
callback(options)
}
return [register,
{
reload: async (isLoading: boolean = false) => {
if (isLoading) {
showLoading({title: '加载中...'})
}
await nextTick()
if (methods.value) {
await methods.value.reload(false)
}
hideLoading()
},
setParam: (param: any) => {
options.param = Object.assign({}, options.param, param)
}
} }
] }
}
}
if (!options.query) {
options.query = requestApi
}
const register = (callback: LayoutCallback, pagingRef: Ref<PagingRefInterface>) => {
methods = pagingRef
callback(options)
}
return [register,
{
reload: async (isLoading: boolean = false) => {
if (isLoading) {
showLoading({ title: '加载中...' })
}
await nextTick()
if (methods.value) {
await methods.value.reload(false)
}
hideLoading()
},
setParam: (param: any) => {
options.param = Object.assign({}, options.param, param)
}
}
]
} }

View File

@ -17,7 +17,7 @@
<template #default="{ data }"> <template #default="{ data }">
<view class="list-container"> <view class="list-container">
<!-- Add @click handler to navigate --> <!-- Add @click handler to navigate -->
<view class="resource-item" @click="goToDetail(data.id)"> <view class="resource-item" @click="downloadResouce(data)">
<view class="item-icon-container"> <view class="item-icon-container">
<view class="item-icon">{{ data.resSuf }}</view> <view class="item-icon">{{ data.resSuf }}</view>
<!-- <text class="item-pages">-{{ item.pages }}-</text> --> <!-- <text class="item-pages">-{{ item.pages }}-</text> -->
@ -44,8 +44,9 @@
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { imagUrl } from "@/utils";
import { useLayout } from "@/components/BasicListLayout/hooks/useLayout"; import { useLayout } from "@/components/BasicListLayout/hooks/useLayout";
import { resourcesFindPageApi } from "@/api/base/server"; import { resourcesFindPageApi, resourcesAddNumByTypeApi } from "@/api/base/server";
import { useDataStore } from "@/store/modules/data"; import { useDataStore } from "@/store/modules/data";
const { getData } = useDataStore(); const { getData } = useDataStore();
@ -75,10 +76,88 @@ const clearSearch = () => {
buildParams(); buildParams();
}; };
// --- Navigation --- const downloadResouce = (item: any) => {
const goToDetail = (id: number) => { const finalUrl = imagUrl(item.resourUrl);
uni.navigateTo({ const fileName = item.resourName + '.' + item.resSuf;
url: `./detail?id=${id}` // Navigate to detail page in the same directory downloadForWeb(finalUrl, fileName);
resourcesAddNumByTypeApi({
id: item.id,
type: 'down'
});
item.downNum = item.downNum + 1;
};
//
const download = (finalUrl: string, fileName: string) => {
console.log("Final Download URL:", finalUrl); //
const { platform } = uni.getSystemInfoSync();
// H5
if (platform === 'web') {
downloadForWeb(finalUrl, fileName);
return;
}
uni.showLoading({ title: "下载中..." });
// 使
uni.downloadFile({
url: finalUrl,
success: async (res) => {
if (res.statusCode === 200) {
const saved = await saveFile(res.tempFilePath, fileName);
if (saved) {
uni.openDocument({ filePath: saved });
}
} else {
uni.showToast({ title: "下载失败", icon: "none" });
}
},
fail: (err) => {
console.error("下载失败:", err);
uni.showToast({ title: "下载失败", icon: "none" });
},
complete: () => uni.hideLoading()
});
};
// H5
const downloadForWeb = (url: string, filename: string) => {
fetch(url)
.then((response) => response.blob())
.then((blob) => {
const downloadUrl = window.URL.createObjectURL(new Blob([blob]));
const link = document.createElement('a');
link.href = downloadUrl;
link.setAttribute('download', filename); //
document.body.appendChild(link);
link.click();
link.remove();
window.URL.revokeObjectURL(downloadUrl);
uni.hideLoading();
uni.showToast({ title: "开始下载", icon: "success" });
})
.catch((err) => {
console.error("H5下载失败:", err);
uni.hideLoading();
uni.showToast({ title: "下载失败", icon: "none" });
});
};
const saveFile = (tempPath: string, filename: string): Promise<string | null> => {
return new Promise((resolve) => {
const fs = uni.getFileSystemManager();
const appPath = `${filename}`;
fs.rename({
oldPath: tempPath,
newPath: appPath,
success: () => resolve(appPath),
fail: () => {
uni.showToast({ title: "保存失败", icon: "none" });
resolve(null);
}
});
}); });
}; };