download.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import axios from 'axios'
  2. import {Message} from 'element-ui'
  3. import {saveAs} from 'file-saver'
  4. import {getToken} from '@/utils/auth'
  5. import errorCode from '@/utils/errorCode'
  6. import {blobValidate} from "@/utils/elf";
  7. import Cookies from "js-cookie";
  8. const baseURL = process.env.VUE_APP_BASE_API
  9. export default {
  10. windTowerStatus(urls, startTime, endTime, equipmentId) {
  11. var url = baseURL + urls + "?startTime=" + encodeURI(startTime) + "&endTime=" + encodeURI(endTime) + "&equipmentId=" + encodeURI(equipmentId);
  12. axios({
  13. method: 'get',
  14. url: url,
  15. responseType: 'blob',
  16. headers: {'Authorization': 'Bearer ' + getToken()}
  17. }).then(async (res) => {
  18. const isLogin = await blobValidate(res.data);
  19. if (isLogin) {
  20. const blob = new Blob([res.data])
  21. this.saveAs(blob, decodeURI(res.headers['content-disposition'].split('=')[1]))
  22. } else {
  23. this.printErrMsg(res.data);
  24. }
  25. })
  26. Cookies.set("exportDataLoading", true, {expires: 60});
  27. },
  28. exportAllData(urls, eqId) {
  29. var url = baseURL + urls + "?equipmentId=" + encodeURI(eqId);
  30. axios({
  31. method: 'get',
  32. url: url,
  33. responseType: 'blob',
  34. headers: {'Authorization': 'Bearer ' + getToken()}
  35. }).then(async (res) => {
  36. const isLogin = await blobValidate(res.data);
  37. if (isLogin) {
  38. const blob = new Blob([res.data])
  39. this.saveAs(blob, decodeURI(res.headers['content-disposition'].split('=')[1]))
  40. } else {
  41. this.printErrMsg(res.data);
  42. }
  43. Cookies.set("exportLoading", true, {expires: 60});
  44. })
  45. },
  46. name(urls, path, name, type) {
  47. var url = baseURL + urls + "?path=" + encodeURI(path) + "&name=" + encodeURI(name) + "&type=" + encodeURI(type);
  48. axios({
  49. method: 'get',
  50. url: url,
  51. responseType: 'blob',
  52. headers: {'Authorization': 'Bearer ' + getToken()}
  53. }).then(async (res) => {
  54. const isLogin = await blobValidate(res.data);
  55. if (isLogin) {
  56. const blob = new Blob([res.data])
  57. this.saveAs(blob, decodeURI(res.headers['content-disposition'].split('=')[1]))
  58. } else {
  59. this.printErrMsg(res.data);
  60. }
  61. })
  62. },
  63. resource(resource) {
  64. var url = baseURL + "/common/download/resource?resource=" + encodeURI(resource);
  65. axios({
  66. method: 'get',
  67. url: url,
  68. responseType: 'blob',
  69. headers: {'Authorization': 'Bearer ' + getToken()}
  70. }).then(async (res) => {
  71. const isLogin = await blobValidate(res.data);
  72. if (isLogin) {
  73. const blob = new Blob([res.data])
  74. this.saveAs(blob, decodeURI(res.headers['download-filename']))
  75. } else {
  76. this.printErrMsg(res.data);
  77. }
  78. })
  79. },
  80. zip(url, name) {
  81. var url = baseURL + url
  82. axios({
  83. method: 'get',
  84. url: url,
  85. responseType: 'blob',
  86. headers: {'Authorization': 'Bearer ' + getToken()}
  87. }).then(async (res) => {
  88. const isLogin = await blobValidate(res.data);
  89. if (isLogin) {
  90. const blob = new Blob([res.data], {type: 'application/zip'})
  91. this.saveAs(blob, name)
  92. } else {
  93. this.printErrMsg(res.data);
  94. }
  95. })
  96. },
  97. saveAs(text, name, opts) {
  98. saveAs(text, name, opts);
  99. },
  100. async printErrMsg(data) {
  101. const resText = await data.text();
  102. const rspObj = JSON.parse(resText);
  103. const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default']
  104. Message.error(errMsg);
  105. }
  106. }