|
|
@@ -58,45 +58,46 @@ const httpService = new (class {
|
|
|
console.error(err)
|
|
|
}
|
|
|
// 异常提示待优化
|
|
|
- return Promise.reject(msg || message || '服务器异常,请稍后再试')
|
|
|
+ return Promise.reject(msg || message)
|
|
|
}
|
|
|
)
|
|
|
}
|
|
|
return Promise.resolve(this.axiosInstance)
|
|
|
}
|
|
|
|
|
|
- private request = async (url: string, method: Method, params?: unknown) => {
|
|
|
+ private request = async (url: string, method: Method, payload?: unknown) => {
|
|
|
const requestConfig: AxiosRequestConfig = {
|
|
|
url,
|
|
|
method,
|
|
|
}
|
|
|
- if (params instanceof Object) {
|
|
|
+ if (payload instanceof Object) {
|
|
|
if (['post', 'POST', 'put', 'PUT', 'patch', 'PATCH'].includes(method)) {
|
|
|
- requestConfig.data = params
|
|
|
+ requestConfig.data = payload
|
|
|
} else {
|
|
|
- requestConfig.params = params
|
|
|
+ requestConfig.params = payload
|
|
|
}
|
|
|
} else {
|
|
|
- requestConfig.url = url + (params ?? '')
|
|
|
+ requestConfig.url = url + (payload ?? '')
|
|
|
}
|
|
|
const instance = await this.init()
|
|
|
return instance(requestConfig)
|
|
|
}
|
|
|
|
|
|
- commonRequest = async <T extends Payload>(url: string, method: Method, params: CommonParams<T>) => {
|
|
|
+ commonRequest = async <T extends Payload>(url: string, method: Method, params: CommonParams<T>, errMsg?: string) => {
|
|
|
const { data, success, fail, complete } = params
|
|
|
await this.request(url, method, data).then((res) => {
|
|
|
const data = res.data as T['rsp']
|
|
|
success && success(data)
|
|
|
}).catch((err) => {
|
|
|
- fail && fail(err)
|
|
|
- return Promise.reject(err)
|
|
|
+ const msg = err ?? (errMsg ? '请求失败: ' + errMsg : '服务异常,请稍后重试')
|
|
|
+ fail && fail(msg)
|
|
|
+ return Promise.reject(msg)
|
|
|
}).finally(() => {
|
|
|
complete && complete()
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- httpRequest = async <T extends Payload>(url: string, method: Method, params: HttpParams<T>) => {
|
|
|
+ httpRequest = async <T extends Payload>(url: string, method: Method, params: HttpParams<T>, errMsg?: string) => {
|
|
|
const { data, success, fail, complete } = params
|
|
|
await this.request(url, method, data).then((res) => {
|
|
|
const data = res.data as HttpResponse<T['rsp']>
|
|
|
@@ -115,8 +116,9 @@ const httpService = new (class {
|
|
|
return Promise.reject(data.msg)
|
|
|
}
|
|
|
}).catch((err) => {
|
|
|
- fail && fail(err)
|
|
|
- return Promise.reject(err)
|
|
|
+ const msg = err ?? (errMsg ? '请求失败: ' + errMsg : '服务异常,请稍后重试')
|
|
|
+ fail && fail(msg)
|
|
|
+ return Promise.reject(msg)
|
|
|
}).finally(() => {
|
|
|
complete && complete()
|
|
|
})
|