downLoad.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import Notify from '../miniprogram_npm/@vant/weapp/notify/notify';
  2. const config = {
  3. baseURL: 'http://192.168.2.111:8802', // 基础URL
  4. // baseURL: 'https://www.xsip.cn', // 基础URL
  5. timeout: 30000, // 超时时间(单位ms)
  6. header: {
  7. 'content-type': 'application/xls', // 默认请求头
  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: {
  31. ...config.header
  32. },
  33. // timeout: 0,
  34. success: (result) => {
  35. wx.hideLoading()
  36. var obj = getPages()
  37. if(obj.message){
  38. Notify(obj)
  39. }
  40. console.log(result)
  41. if (result.statusCode === 200) {
  42. const tempFilePath = result.tempFilePath; // 临时文件路径
  43. wx.openDocument({
  44. filePath: tempFilePath,
  45. showMenu:true,
  46. success: function (res) {
  47. console.log('打开文档成功')
  48. }
  49. })
  50. // wx.saveFile({
  51. // tempFilePath: tempFilePath, // 传入临时文件路径
  52. // success(saveRes) {
  53. // const savedFilePath = saveRes.savedFilePath;
  54. // console.log('文件已保存到本地', savedFilePath);
  55. // resolve(saveRes)
  56. // // 可以在这里做后续操作,如打开文件或通知用户
  57. // },
  58. // fail(error) {
  59. // console.error('保存文件失败', error);
  60. // reject('保存文件失败')
  61. // }
  62. // })
  63. }
  64. },
  65. fail: (res) => {
  66. wx.hideLoading()
  67. var obj = getPages()
  68. if(obj.message){
  69. Notify(obj)
  70. }
  71. reject('导出失败')
  72. },
  73. complete: (res) => {},
  74. })
  75. })
  76. }
  77. // 导出请求方法
  78. module.exports = {
  79. downLoadFile: (url) => downLoad(url),
  80. };