download.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { saveAs } from 'file-saver'
  2. import axios from 'axios'
  3. import { getToken } from '@/utils/auth'
  4. import { Message } from 'element-ui'
  5. const baseURL = process.env.VUE_APP_BASE_API
  6. export default {
  7. excel(url, params) {
  8. // get请求映射params参数
  9. if (params) {
  10. let urlparams = url + '?';
  11. for (const propName of Object.keys(params)) {
  12. const value = params[propName];
  13. var part = encodeURIComponent(propName) + "=";
  14. if (value !== null && typeof(value) !== "undefined") {
  15. if (typeof value === 'object') {
  16. for (const key of Object.keys(value)) {
  17. if (value[key] !== null && typeof (value[key]) !== 'undefined') {
  18. let params = propName + '[' + key + ']';
  19. let subPart = encodeURIComponent(params) + '=';
  20. urlparams += subPart + encodeURIComponent(value[key]) + '&';
  21. }
  22. }
  23. } else {
  24. urlparams += part + encodeURIComponent(value) + "&";
  25. }
  26. }
  27. }
  28. urlparams = urlparams.slice(0, -1);
  29. url = urlparams;
  30. }
  31. url = baseURL + url
  32. axios({
  33. method: 'get',
  34. url: url,
  35. responseType: 'blob',
  36. headers: { 'Authorization': 'Bearer ' + getToken() }
  37. }).then(async (res) => {
  38. const isLogin = await this.blobValidate(res.data);
  39. if (isLogin) {
  40. const blob = new Blob([res.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' })
  41. this.saveAs(blob, decodeURI(res.headers['download-filename']))
  42. } else {
  43. Message.error('无效的会话,或者会话已过期,请重新登录。');
  44. }
  45. })
  46. },
  47. oss(ossId) {
  48. var url = baseURL + '/system/oss/download/' + ossId
  49. axios({
  50. method: 'get',
  51. url: url,
  52. responseType: 'blob',
  53. headers: { 'Authorization': 'Bearer ' + getToken() }
  54. }).then(async (res) => {
  55. const isLogin = await this.blobValidate(res.data);
  56. if (isLogin) {
  57. const blob = new Blob([res.data], { type: 'application/octet-stream' })
  58. this.saveAs(blob, decodeURI(res.headers['download-filename']))
  59. } else {
  60. Message.error('无效的会话,或者会话已过期,请重新登录。');
  61. }
  62. })
  63. },
  64. zip(url, name) {
  65. var url = baseURL + url
  66. axios({
  67. method: 'get',
  68. url: url,
  69. responseType: 'blob',
  70. headers: { 'Authorization': 'Bearer ' + getToken() }
  71. }).then(async (res) => {
  72. const isLogin = await this.blobValidate(res.data);
  73. if (isLogin) {
  74. const blob = new Blob([res.data], { type: 'application/zip' })
  75. this.saveAs(blob, name)
  76. } else {
  77. Message.error('无效的会话,或者会话已过期,请重新登录。');
  78. }
  79. })
  80. },
  81. saveAs(text, name, opts) {
  82. saveAs(text, name, opts);
  83. },
  84. async blobValidate(data) {
  85. try {
  86. const text = await data.text();
  87. JSON.parse(text);
  88. return false;
  89. } catch (error) {
  90. return true;
  91. }
  92. },
  93. }