| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- import $config from "@/config/global.config.js"
- /***
- * 文件下载
- * @param {string} url 下载地址
- *
- * */
- export const fileDownload = (url) => {
- uni.showLoading({
- title: '加载中',
- mask: true
- });
- uni.downloadFile({
- url: `${$config.baseUrl}${url}`,
- timeout: 1000000,
- header: {
- 'apiToken': uni.getStorageSync('apiToken') //自定义请求头信息
- },
- success: (res) => {
- // console.log(res);
- uni.hideLoading();
- if (res.tempFilePath && res.statusCode === 200) {
- uni.openDocument({
- filePath: res.tempFilePath,
- success: () => {
- console.log('文档打开成功');
- uni.showToast({ title: '文件已打开,请点击右上角保存到手机', icon: 'none', duration: 3000 });
- },
- fail: (openErr) => {
- console.warn('openDocument 失败(可能不支持 .xlsx 预览)', openErr);
- }
- });
- } else {
- uni.showToast({ title: '文件下载失败', icon: 'none' });
- }
- },
- fail: (err) => {
- uni.hideLoading();
- console.error('请求失败', err);
- uni.showToast({ title: '请求失败', icon: 'none' });
- }
- });
- };
- /***
- * 打开文档预览
- * @param {string} url 文件网络地址
- *
- * */
- export const openDocument = (url) => {
- if (!url) return uni.showToast({ title: '文件地址不存在', icon: 'none' });
- uni.downloadFile({
- url: url,
- success: function (res) {
- let filePath = res.tempFilePath;
- uni.openDocument({
- filePath: filePath,
- // showMenu: true,
- success: function (res) {
- console.log('打开文档成功');
- uni.showToast({ title: '文件已打开,请点击右上角保存到手机', icon: 'none', duration: 3000 });
- }
- });
- }
- });
- };
|