downLoad.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import Notify from '../miniprogram_npm/@vant/weapp/notify/notify';
  2. const config = {
  3. // baseURL: 'http://192.168.1.2:8801', // 基础URL
  4. baseURL: 'https://www.xsip.cn', // 基础URL
  5. timeout: 30000, // 超时时间(单位ms)
  6. header: {
  7. // 'content-type': 'application/json', // 默认请求头
  8. // 其他全局请求头...
  9. },
  10. // 其他全局配置...
  11. tabbar:["pages/index/index","pages/hotProduct/hotProduct","pages/mine/mine"]
  12. };
  13. function getPages(){
  14. var obj = {
  15. type: 'danger'
  16. }
  17. var pages = getCurrentPages()
  18. var route = pages[pages.length - 1]?.route
  19. console.log(config.tabbar,route)
  20. if(route && config.tabbar.indexOf(route)==-1){
  21. obj.top = wx.getStorageSync('height')
  22. }
  23. return obj
  24. }
  25. function downLoad(url){
  26. return new Promise((resolve, reject)=>{
  27. wx.downloadFile({
  28. url:config.baseURL + '/fileManager/downloadFile?fileId=' + url,
  29. // filePath: 'filePath',
  30. // header: header,
  31. // timeout: 0,
  32. success: (result) => {
  33. wx.hideLoading()
  34. var obj = getPages()
  35. if(obj.message){
  36. Notify(obj)
  37. }
  38. console.log(result)
  39. if (result.statusCode === 200) {
  40. const tempFilePath = result.tempFilePath; // 临时文件路径
  41. wx.saveFile({
  42. tempFilePath: tempFilePath, // 传入临时文件路径
  43. success(saveRes) {
  44. const savedFilePath = saveRes.savedFilePath;
  45. console.log('文件已保存到本地', savedFilePath);
  46. resolve(saveRes)
  47. // 可以在这里做后续操作,如打开文件或通知用户
  48. },
  49. fail(error) {
  50. console.error('保存文件失败', error);
  51. reject('保存文件失败')
  52. }
  53. })
  54. }
  55. },
  56. fail: (res) => {
  57. wx.hideLoading()
  58. var obj = getPages()
  59. if(obj.message){
  60. Notify(obj)
  61. }
  62. reject('导出失败')
  63. },
  64. complete: (res) => {},
  65. })
  66. })
  67. }
  68. // 导出请求方法
  69. module.exports = {
  70. downLoadFile: (url) => downLoad(url),
  71. };