|
@@ -1,4 +1,5 @@
|
|
|
import config from '@/config';
|
|
import config from '@/config';
|
|
|
|
|
+import { tansParams } from '@/utils/common';
|
|
|
import { useInfoStore } from '@/store';
|
|
import { useInfoStore } from '@/store';
|
|
|
import { getCurrentPage } from '@/utils/public';
|
|
import { getCurrentPage } from '@/utils/public';
|
|
|
import { recursiveDecodeURIComponent } from '@/utils/ruoyi';
|
|
import { recursiveDecodeURIComponent } from '@/utils/ruoyi';
|
|
@@ -7,6 +8,14 @@ const { clientId, appid } = config;
|
|
|
// uniapp封装的请求方法
|
|
// uniapp封装的请求方法
|
|
|
let timeout = 60 * 1000;
|
|
let timeout = 60 * 1000;
|
|
|
|
|
|
|
|
|
|
+interface ClientDownloadConfig {
|
|
|
|
|
+ url: string;
|
|
|
|
|
+ fileName?: string;
|
|
|
|
|
+ params?: Record<string, any>;
|
|
|
|
|
+ timeout?: number;
|
|
|
|
|
+ header?: Record<string, any>;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
// 获取全局请求头方法
|
|
// 获取全局请求头方法
|
|
|
const getHeader = () => {
|
|
const getHeader = () => {
|
|
|
let header = {
|
|
let header = {
|
|
@@ -18,6 +27,103 @@ const getHeader = () => {
|
|
|
return header;
|
|
return header;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+const getFileType = (url: string, result?: { header?: Record<string, any> }): string => {
|
|
|
|
|
+ if (url) {
|
|
|
|
|
+ const filenameFromUrl = url.split('/').pop();
|
|
|
|
|
+ const urlExt = filenameFromUrl?.includes('.') ? filenameFromUrl.split('.').pop()?.split(/[?#]/)[0] : null;
|
|
|
|
|
+ if (urlExt) return urlExt.toLowerCase() || 'pdf';
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (result?.header?.['content-disposition']) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const disposition = result.header['content-disposition'];
|
|
|
|
|
+ const filenameMatch = disposition.match(/filename\*?=(?:UTF-8''|")?([^;"\n]*)/i);
|
|
|
|
|
+
|
|
|
|
|
+ if (filenameMatch && filenameMatch[1]) {
|
|
|
|
|
+ const filename = filenameMatch[1].replace(/['"]/g, '');
|
|
|
|
|
+ if (filename.includes('.')) {
|
|
|
|
|
+ const ext = filename.split('.').pop()?.split(/[?#]/)[0];
|
|
|
|
|
+ return ext?.toLowerCase() || 'pdf';
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ console.warn('解析 Content-Disposition 失败:', e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return 'pdf';
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+const buildDownloadUrl = (url: string, params?: Record<string, any>) => {
|
|
|
|
|
+ const isFullUrl = url.startsWith('http://') || url.startsWith('https://');
|
|
|
|
|
+ let downloadUrl = (isFullUrl ? '' : import.meta.env.VITE_API_BASE_URL || 'http://192.168.1.26:8080') + url;
|
|
|
|
|
+
|
|
|
|
|
+ if (params && Object.keys(params).length) {
|
|
|
|
|
+ const query = tansParams(params).slice(0, -1);
|
|
|
|
|
+ if (query) {
|
|
|
|
|
+ downloadUrl += `${downloadUrl.includes('?') ? '&' : '?'}${query}`;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return downloadUrl;
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+const download = (downloadConfig: ClientDownloadConfig, showTitle?: string): Promise<void> =>
|
|
|
|
|
+ new Promise((resolve, reject) => {
|
|
|
|
|
+ const downloadUrl = buildDownloadUrl(downloadConfig.url, downloadConfig.params);
|
|
|
|
|
+
|
|
|
|
|
+ uni.showLoading({
|
|
|
|
|
+ title: showTitle || '正在导出数据...',
|
|
|
|
|
+ mask: true,
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ uni.downloadFile({
|
|
|
|
|
+ url: downloadUrl,
|
|
|
|
|
+ timeout: downloadConfig.timeout || timeout,
|
|
|
|
|
+ header: {
|
|
|
|
|
+ ...getHeader(),
|
|
|
|
|
+ ...downloadConfig.header,
|
|
|
|
|
+ },
|
|
|
|
|
+ success: (res) => {
|
|
|
|
|
+ console.log(res);
|
|
|
|
|
+
|
|
|
|
|
+ uni.hideLoading();
|
|
|
|
|
+ if (res.statusCode === 200) {
|
|
|
|
|
+ uni.openDocument({
|
|
|
|
|
+ filePath: res.tempFilePath,
|
|
|
|
|
+ showMenu: true,
|
|
|
|
|
+ fileType: getFileType(downloadConfig.fileName || downloadConfig.url, res as any) as any,
|
|
|
|
|
+ success: () => {
|
|
|
|
|
+ resolve();
|
|
|
|
|
+ },
|
|
|
|
|
+ fail: (err) => {
|
|
|
|
|
+ uni.showToast({
|
|
|
|
|
+ title: '打开文件失败',
|
|
|
|
|
+ icon: 'none',
|
|
|
|
|
+ });
|
|
|
|
|
+ reject(err);
|
|
|
|
|
+ },
|
|
|
|
|
+ });
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ uni.showToast({
|
|
|
|
|
+ title: '下载失败',
|
|
|
|
|
+ icon: 'none',
|
|
|
|
|
+ });
|
|
|
|
|
+ reject(res);
|
|
|
|
|
+ },
|
|
|
|
|
+ fail: (error) => {
|
|
|
|
|
+ uni.hideLoading();
|
|
|
|
|
+ uni.showToast({
|
|
|
|
|
+ title: '下载失败',
|
|
|
|
|
+ icon: 'none',
|
|
|
|
|
+ });
|
|
|
|
|
+ reject(error);
|
|
|
|
|
+ },
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
// 获取host地址
|
|
// 获取host地址
|
|
|
export const request = ({ url, method = 'GET', data = {}, isToken = true, header = null }: any) => {
|
|
export const request = ({ url, method = 'GET', data = {}, isToken = true, header = null }: any) => {
|
|
|
const baseUrl = import.meta.env.VITE_API_BASE_URL || 'http://192.168.1.26:8080';
|
|
const baseUrl = import.meta.env.VITE_API_BASE_URL || 'http://192.168.1.26:8080';
|
|
@@ -157,4 +263,7 @@ export const useClientRequest = {
|
|
|
isToken,
|
|
isToken,
|
|
|
}) as Promise<T>;
|
|
}) as Promise<T>;
|
|
|
},
|
|
},
|
|
|
|
|
+ down: (config: ClientDownloadConfig, showTitle?: string): Promise<void> => {
|
|
|
|
|
+ return download(config, showTitle);
|
|
|
|
|
+ },
|
|
|
};
|
|
};
|