调整下载资源文件
This commit is contained in:
parent
0b487b6d68
commit
a2c8b4dd11
@ -124,6 +124,10 @@ 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);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
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,
|
||||||
@ -48,7 +48,7 @@ export function useLayout(options: LayoutOptions): UseLayoutInterfaceReturn {
|
|||||||
{
|
{
|
||||||
reload: async (isLoading: boolean = false) => {
|
reload: async (isLoading: boolean = false) => {
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
showLoading({title: '加载中...'})
|
showLoading({ title: '加载中...' })
|
||||||
}
|
}
|
||||||
await nextTick()
|
await nextTick()
|
||||||
if (methods.value) {
|
if (methods.value) {
|
||||||
|
|||||||
@ -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);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user