优化:请求接口全局处理loading和错误提示
This commit is contained in:
@@ -8,6 +8,7 @@ import { t } from '@/utils/i18n'
|
||||
export function createRequestClient(cfg: ICreateClientConfig) {
|
||||
const baseURL = cfg.baseURL;
|
||||
const timeout = cfg.timeout ?? REQUEST_TIMEOUT;
|
||||
let reqCount= 0
|
||||
|
||||
async function request<T = any>(options: IRequestOptions): Promise<T> {
|
||||
// 组装 final options
|
||||
@@ -23,14 +24,18 @@ export function createRequestClient(cfg: ICreateClientConfig) {
|
||||
|
||||
// 全局处理请求 loading
|
||||
const loading = !cfg.loading ? true : cfg.loading // 接口请求参数不传loading,默认显示loading
|
||||
loading && uni.showLoading()
|
||||
if (loading) {
|
||||
uni.showLoading({ mask: true })
|
||||
reqCount++
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
...intercepted,
|
||||
complete() {
|
||||
// 请求完成关闭 loading
|
||||
loading && uni.hideLoading()
|
||||
loading && reqCount--
|
||||
reqCount <= 0 && uni.hideLoading()
|
||||
},
|
||||
success(res: any) {
|
||||
// 委托给响应拦截器处理
|
||||
|
||||
Reference in New Issue
Block a user