edu/uniapp/src/utils/request/http.ts

141 lines
5.7 KiB
TypeScript
Raw Normal View History

2022-08-26 09:52:43 +00:00
import { merge } from 'lodash-es'
import { isFunction } from '@vue/shared'
import { HttpRequestOptions, RequestConfig, RequestOptions, UploadFileOption } from './type'
import { RequestErrMsgEnum, RequestMethodsEnum } from '@/enums/requestEnums'
2022-08-26 09:52:43 +00:00
import requestCancel from './cancel'
export default class HttpRequest {
private readonly options: HttpRequestOptions
constructor(options: HttpRequestOptions) {
this.options = options
}
/**
* @description
*/
retryRequest(options: RequestOptions, config: RequestConfig) {
const { retryCount, retryTimeout } = config
if (!retryCount || options.method?.toUpperCase() == RequestMethodsEnum.POST) {
return Promise.reject()
}
uni.showLoading({ title: '加载中...' })
config.hasRetryCount = config.hasRetryCount ?? 0
if (config.hasRetryCount >= retryCount) {
return Promise.reject()
}
config.hasRetryCount++
config.requestHooks.requestInterceptorsHook = (options) => options
return new Promise((resolve) => setTimeout(resolve, retryTimeout))
.then(() => this.request(options, config))
.finally(() => uni.hideLoading())
}
2022-08-26 09:52:43 +00:00
/**
* @description get请求
*/
get<T = any>(options: RequestOptions, config?: Partial<RequestConfig>): Promise<T> {
return this.request({ ...options, method: RequestMethodsEnum.GET }, config)
}
/**
* @description post请求
*/
post<T = any>(options: RequestOptions, config?: Partial<RequestConfig>): Promise<T> {
return this.request({ ...options, method: RequestMethodsEnum.POST }, config)
}
/**
* @description
*/
uploadFile(options: UploadFileOption, config?: Partial<RequestConfig>) {
let mergeOptions: RequestOptions = merge({}, this.options.requestOptions, options)
const mergeConfig: RequestConfig = merge({}, this.options, config)
const { requestInterceptorsHook, responseInterceptorsHook, responseInterceptorsCatchHook } =
mergeConfig.requestHooks || {}
if (requestInterceptorsHook && isFunction(requestInterceptorsHook)) {
mergeOptions = requestInterceptorsHook(mergeOptions, mergeConfig)
}
return new Promise((resolve, reject) => {
uni.uploadFile({
...mergeOptions,
success: async (response) => {
if (response.statusCode == 200) {
response.data = JSON.parse(response.data)
if (responseInterceptorsHook && isFunction(responseInterceptorsHook)) {
try {
response = await responseInterceptorsHook(response, mergeConfig)
resolve(response)
} catch (error) {
reject(error)
}
return
}
resolve(response)
}
},
fail: async (err) => {
if (
responseInterceptorsCatchHook &&
isFunction(responseInterceptorsCatchHook)
) {
reject(await responseInterceptorsCatchHook(mergeOptions, err))
return
}
reject(err)
}
})
})
}
2022-08-26 09:52:43 +00:00
/**
* @description
*/
async request(options: RequestOptions, config?: Partial<RequestConfig>): Promise<any> {
let mergeOptions: RequestOptions = merge({}, this.options.requestOptions, options)
const mergeConfig: RequestConfig = merge({}, this.options, config)
const { requestInterceptorsHook, responseInterceptorsHook, responseInterceptorsCatchHook } =
mergeConfig.requestHooks || {}
if (requestInterceptorsHook && isFunction(requestInterceptorsHook)) {
mergeOptions = requestInterceptorsHook(mergeOptions, mergeConfig)
2022-08-26 09:52:43 +00:00
}
return new Promise((resolve, reject) => {
const requestTask = uni.request({
...mergeOptions,
async success(response) {
2022-08-26 09:52:43 +00:00
if (responseInterceptorsHook && isFunction(responseInterceptorsHook)) {
try {
response = await responseInterceptorsHook(response, mergeConfig)
2022-08-26 09:52:43 +00:00
resolve(response)
} catch (error) {
reject(error)
}
return
}
resolve(response)
},
fail: async (err) => {
if (err.errMsg == RequestErrMsgEnum.TIMEOUT) {
this.retryRequest(mergeOptions, mergeConfig)
.then((res) => resolve(res))
.catch((err) => reject(err))
return
}
2022-08-26 09:52:43 +00:00
if (
responseInterceptorsCatchHook &&
isFunction(responseInterceptorsCatchHook)
) {
reject(await responseInterceptorsCatchHook(mergeOptions, err))
2022-08-26 09:52:43 +00:00
return
}
reject(err)
},
2022-09-08 07:28:51 +00:00
complete(err) {
if (err.errMsg !== RequestErrMsgEnum.ABORT) {
2022-09-08 07:28:51 +00:00
requestCancel.remove(options.url)
}
2022-08-26 09:52:43 +00:00
}
})
const { ignoreCancel } = mergeConfig
!ignoreCancel && requestCancel.add(options.url, requestTask)
})
}
}