From 840aa8d33190a42d1fa20e31fad06a00e5491c8a Mon Sep 17 00:00:00 2001 From: mo-bai <1873032855@qq.com> Date: Tue, 19 Apr 2022 16:38:28 +0800 Subject: [PATCH] http --- utils/http/http.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/utils/http/http.js b/utils/http/http.js index 3d8600c..a392ffd 100644 --- a/utils/http/http.js +++ b/utils/http/http.js @@ -2,11 +2,16 @@ import { isFunction } from '../is.js' import env from '@/env/index.js' const urlEnv = env === 'production' ? '' : `-${env}` const uploadUrl = `https://api-ops-yyt${urlEnv}.qniao.cn/cloud-print-user-center/utils/uploadImage` + +/** + * 定义的一个thenable对象 + */ +class ErrorPromise { + then() {} +} + export default class Http { - constructor( - config = {}, - { reqInterceptor, resInterceptor, tranformConfig } = {} - ) { + constructor(config = {}, { reqInterceptor, resInterceptor, tranformConfig } = {}) { this.baseUrl = config.baseUrl this.header = config.header || { 'content-type': 'application/json;charset=UTF-8' @@ -56,7 +61,11 @@ export default class Http { method: conf.method, success: (res) => { let data = resInterceptor(res, opt) - // 服务器错误也会用then抛出,需要自己判断data==null + if (!data) { + console.log('报错的请求参数:', conf) + reject(res) + return + } resolve(data) }, fail: (err) => { @@ -68,8 +77,8 @@ export default class Http { }) }).catch((err) => { // 吃掉请求产生的异常 - // 后期可以记录 console.error('native response error', err) + return new ErrorPromise() }) } // config:{}