华西海圻ELN前端工程
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.4 KiB

  1. import axios from 'axios'
  2. import {Loading, 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/ruoyi"
  7. const baseURL = process.env.VUE_APP_BASE_API
  8. let downloadLoadingInstance
  9. export default {
  10. zip(url, name) {
  11. var url = baseURL + url
  12. downloadLoadingInstance = Loading.service({ text: "正在下载数据,请稍候", spinner: "el-icon-loading", background: "rgba(0, 0, 0, 0.7)", })
  13. axios({
  14. method: 'get',
  15. url: url,
  16. responseType: 'blob',
  17. headers: { 'Authorization': 'Bearer ' + getToken() }
  18. }).then((res) => {
  19. const isBlob = blobValidate(res.data)
  20. if (isBlob) {
  21. const blob = new Blob([res.data], { type: 'application/zip' })
  22. this.saveAs(blob, name)
  23. } else {
  24. this.printErrMsg(res.data)
  25. }
  26. downloadLoadingInstance.close()
  27. }).catch((r) => {
  28. console.error(r)
  29. Message.error('下载文件出现错误,请联系管理员!')
  30. downloadLoadingInstance.close()
  31. })
  32. },
  33. saveAs(text, name, opts) {
  34. saveAs(text, name, opts)
  35. },
  36. async printErrMsg(data) {
  37. const resText = await data.text()
  38. const rspObj = JSON.parse(resText)
  39. const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default']
  40. Message.error(errMsg)
  41. }
  42. }