123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- import apiConfig from './apiConfig'
- import template from 'url-template'
- import qs from 'qs'
- import axiosConfig from '_config/axios-config'
- import { isObject } from '@cip/utils/util'
- // 判断返回是否成功
- export const isSuccess = (res, options, defaultSuccessCode = axiosConfig?.DEFAULT_SUCCESS_CODE) => {
- const code = res.data.code === undefined ? res.data.result : res.data.code
- const successCode = options.successCode !== undefined ? options.successCode : defaultSuccessCode
- return (code === successCode || options.noSuccessCode)
- }
- // application/octet-stream文件下载获取文件名
- export const getFileNameFormHeader = (disposition) => {
- let result = null
- const reg = /filename=.*/ig
- if (disposition && reg.test(disposition)) {
- result = disposition.match(reg)
- return decodeURI(result[0].split('=')[1])
- } else {
- return null
- }
- }
- // 下载流
- export const downloadByStream = (stream, filename) => {
- const blob = new Blob([stream])
- const eLink = document.createElement('a')
- eLink.download = filename
- eLink.style.display = 'none'
- eLink.href = URL.createObjectURL(blob)
- document.body.appendChild(eLink)
- eLink.click()
- URL.revokeObjectURL(eLink.href)
- document.body.removeChild(eLink)
- }
- export const notifyErrorMessage = async (res) => {
- // 如果请求为blob类型
- let data = res.response?.data
- if (Object.prototype.toString.call(data) === '[object Blob]') {
- data = await blobToJson(data)
- if (data && typeof data === 'string') {
- return data
- } else { // blob中为对象时
- return data.message
- }
- }
- if (res?.data?.message) return res.data.message
- if (res?.response?.data?.message) return res.response.data.message
- if (res.message) return res.message
- return '发生了未知的错误'
- }
- const blobToJson = (blob) => {
- return new Promise((resolve, reject) => {
- const reader = new FileReader()
- reader.readAsText(blob, 'utf-8')
- reader.onload = function () {
- resolve(JSON.parse(reader.result))
- }
- })
- }
- export const getArgs = (args) => {
- if (isObject(args[0])) {
- return args[0]
- } else {
- let [method, apiName, url, params, options = {}] = args
- let data = {}
- method = method.toLocaleLowerCase()
- if (['put', 'post'].includes(method)) {
- data = params
- params = options.params
- Reflect.deleteProperty(options, 'params')
- }
- const pathParams = options.pathParams
- Reflect.deleteProperty(options, 'pathParams')
- return {
- method,
- apiName,
- url,
- data, // post | put 请求
- params,
- pathParams,
- options
- }
- }
- }
- export const getRequestPath = (apiName, path, params) => {
- const apiContextPath = apiConfig[apiName]
- let requestPath = apiContextPath ? apiContextPath + path : path
- if (requestPath.indexOf?.('{') > -1) {
- requestPath = template.parse(requestPath).expand(params)
- }
- return requestPath
- }
- export const getRequestData = (data, options) => {
- if (options.form) {
- data = qs.stringify(data)
- }
- return data
- }
- export const getRequestConfig = (params = {}, options = {}, appendTimestamp = true) => {
- const config = options
- // 是否需要添加时间戳
- if (appendTimestamp) params._t = Date.now()
- // post/put使用formData
- if (options.form) {
- config.headers = { ...config.headers, 'Content-Type': 'application/x-www-form-urlencoded' }
- Reflect.deleteProperty(options, 'form') // options.form
- }
- config.params = params
- return config
- }
- export const fileUploadProgress = (info, writeObj) => {
- // eslint-disable-next-line
- for (const item in writeObj) {
- info[item] = writeObj[item]
- }
- }
|