import { ElMessage } from 'element-plus'
import { ObjectInterface } from './util'
import axios from 'axios'
import { i18n } from '../i18n'
const configDefault = {
params: undefined,
successText: i18n.global.t('operationSuccess'),
errorText: '',
success () {
},
error () {
},
interface () {
},
loading: {}, // 默认值是true,可以在请求返回结果时候将对象内的值修改为false
tips: false,
errTips: true,
isField: false
}
// 根据返回的错误信息弹出
function getErrorMessage (response: ObjectInterface, config: ObjectInterface) {
config.loading.isLoading = false // 请求失败去掉加载遮罩
if (!config.errTips) return
let err
if (Object.prototype.toString.call(response.data) === '[object Object]') {
err = response.data
}
if (Object.prototype.toString.call(response.data) === '[object ArrayBuffer]') {
const enc = new TextDecoder('utf-8')
const res = JSON.parse(enc.decode(new Uint8Array(response.data)))
err = res
}
let errorMessage = err?.message
const isHtml = errorMessage?.indexOf('
') > -1
if (isHtml) {
errorMessage = `
${errorMessage}
`// 最宽50%,自动换行
}
ElMessage.error({
dangerouslyUseHTMLString: isHtml, // 如果错误信息含有换行就使用html解析
message: errorMessage
})
}
// 根据返回的成功信息弹出
function getSuccessMessage (config: any) {
if (config.tips) {
ElMessage.success({
message: config.successText
})
}
}
// eslint-disable-next-line complexity
async function serverApi (options: any) {
const config = { ...configDefault, ...options }// 传入配置覆盖当前默认值
if (!config.interface) {
throw new Error('必须输入接口方法') // 接口没有直接报错
}
config.loading.isLoading = true // 加载遮罩默认值
try {
let res = await config.interface(config.params)
if (!res) {
config.loading.isLoading = false
return null
}
const response = (res.response || res)
// 如果没有返回数据并且 res 为 AxiosError,提示网络请求错误,或网络请求超时
if (axios.isAxiosError(res)) {
// ERR_FR_TOO_MANY_REDIRECTS: 表示请求遇到了过多的重定向。
// ERR_BAD_OPTION_VALUE: 表示提供了无效的选项值。
// ERR_BAD_OPTION: 表示提供了无效的选项。
// ERR_DEPRECATED: 表示某些用法已被废弃。
// ERR_BAD_RESPONSE: 表示响应的内容不合法。 40x 50x
// ERR_BAD_REQUEST: 表示请求不合法。
// ERR_CANCELED: 表示请求被取消。
// ERR_NETWORK: 表示网络错误。
// ECONNABORTED: 表示连接被中止。 message: 'timeout of ...'
// ETIMEDOUT: 表示连接超时。
// 超时或网络问题
if (res.code === 'ERR_NETWORK' || res.code === 'ETIMEDOUT' || res.message.indexOf('timeout') > -1) {
const errorCodeMsg = { 'ERR_NETWORK': window.$t('networkRequestError'), 'ETIMEDOUT': window.$t('networkRequestTimeout') }
const error = {
data: {
message: errorCodeMsg[res.code || 'ERR_NETWORK'] || window.$t('networkRequestError')
}
}
if (res.message.indexOf('timeout') > -1) {
error.data.message = window.$t('networkRequestTimeout')
}
getErrorMessage(error, config)
config.error(res)
return null
}
}
const statusCode = [200, 201, 202, 204]
// 旧版代码逻辑,response直接返回数据内容
if (response.data?.status !== 0 && !response.data.status) {
if (statusCode.includes(response?.status)) {
config.loading.isLoading = false // 请求成功去掉加载遮罩
getSuccessMessage(config)
config.success(config.returnResp ? response : response.data)
} else { // 401 403 404 500 503...
getErrorMessage(response, config)
config.error(response)
}
} else {
// 新版代码逻辑
if (statusCode.includes(response?.status)) {
config.loading.isLoading = false // 请求成功去掉加载遮罩
const dataStatusCode = [0]
// 返回数据中状态没有0就要报错
if (dataStatusCode.includes(response.data.status)) {
getSuccessMessage(config)
config.success(config.returnResp ? response : response.data.content)
} else {
getErrorMessage(response, config)
config.error(response)
}
} else { // 401 403 404 500 503...
getErrorMessage(response, config)
config.error(response)
}
}
} catch (error) { // 程序错误
// @ts-ignore
getErrorMessage(error, config)
throw error
}
}
export { serverApi }