util.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import apiConfig from './apiConfig'
  2. import template from 'url-template'
  3. import qs from 'qs'
  4. import axiosConfig from '_config/axios-config'
  5. import { isObject } from '@cip/utils/util'
  6. // 判断返回是否成功
  7. export const isSuccess = (res, options, defaultSuccessCode = axiosConfig?.DEFAULT_SUCCESS_CODE) => {
  8. const code = res.data.code === undefined ? res.data.result : res.data.code
  9. const successCode = options.successCode !== undefined ? options.successCode : defaultSuccessCode
  10. return (code === successCode || options.noSuccessCode)
  11. }
  12. // application/octet-stream文件下载获取文件名
  13. export const getFileNameFormHeader = (disposition) => {
  14. let result = null
  15. const reg = /filename=.*/ig
  16. if (disposition && reg.test(disposition)) {
  17. result = disposition.match(reg)
  18. return decodeURI(result[0].split('=')[1])
  19. } else {
  20. return null
  21. }
  22. }
  23. // 下载流
  24. export const downloadByStream = (stream, filename) => {
  25. const blob = new Blob([stream])
  26. const eLink = document.createElement('a')
  27. eLink.download = filename
  28. eLink.style.display = 'none'
  29. eLink.href = URL.createObjectURL(blob)
  30. document.body.appendChild(eLink)
  31. eLink.click()
  32. URL.revokeObjectURL(eLink.href)
  33. document.body.removeChild(eLink)
  34. }
  35. export const notifyErrorMessage = async (res) => {
  36. // 如果请求为blob类型
  37. let data = res.response?.data
  38. if (Object.prototype.toString.call(data) === '[object Blob]') {
  39. data = await blobToJson(data)
  40. if (data && typeof data === 'string') {
  41. return data
  42. } else { // blob中为对象时
  43. return data.message
  44. }
  45. }
  46. if (res?.data?.message) return res.data.message
  47. if (res?.response?.data?.message) return res.response.data.message
  48. if (res.message) return res.message
  49. return '发生了未知的错误'
  50. }
  51. const blobToJson = (blob) => {
  52. return new Promise((resolve, reject) => {
  53. const reader = new FileReader()
  54. reader.readAsText(blob, 'utf-8')
  55. reader.onload = function () {
  56. resolve(JSON.parse(reader.result))
  57. }
  58. })
  59. }
  60. export const getArgs = (args) => {
  61. if (isObject(args[0])) {
  62. return args[0]
  63. } else {
  64. let [method, apiName, url, params, options = {}] = args
  65. let data = {}
  66. method = method.toLocaleLowerCase()
  67. if (['put', 'post'].includes(method)) {
  68. data = params
  69. params = options.params
  70. Reflect.deleteProperty(options, 'params')
  71. }
  72. const pathParams = options.pathParams
  73. Reflect.deleteProperty(options, 'pathParams')
  74. return {
  75. method,
  76. apiName,
  77. url,
  78. data, // post | put 请求
  79. params,
  80. pathParams,
  81. options
  82. }
  83. }
  84. }
  85. export const getRequestPath = (apiName, path, params) => {
  86. const apiContextPath = apiConfig[apiName]
  87. let requestPath = apiContextPath ? apiContextPath + path : path
  88. if (requestPath.indexOf?.('{') > -1) {
  89. requestPath = template.parse(requestPath).expand(params)
  90. }
  91. return requestPath
  92. }
  93. export const getRequestData = (data, options) => {
  94. if (options.form) {
  95. data = qs.stringify(data)
  96. }
  97. return data
  98. }
  99. export const getRequestConfig = (params = {}, options = {}, appendTimestamp = true) => {
  100. const config = options
  101. // 是否需要添加时间戳
  102. if (appendTimestamp) params._t = Date.now()
  103. // post/put使用formData
  104. if (options.form) {
  105. config.headers = { ...config.headers, 'Content-Type': 'application/x-www-form-urlencoded' }
  106. Reflect.deleteProperty(options, 'form') // options.form
  107. }
  108. config.params = params
  109. return config
  110. }
  111. export const fileUploadProgress = (info, writeObj) => {
  112. // eslint-disable-next-line
  113. for (const item in writeObj) {
  114. info[item] = writeObj[item]
  115. }
  116. }