Browse Source

http

devlop
mo-bai 4 years ago
parent
commit
d23d26ddba
1 changed files with 13 additions and 6 deletions
  1. 19
      utils/http/http.js

19
utils/http/http.js

@ -2,11 +2,14 @@ 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 +59,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 +75,8 @@ export default class Http {
})
}).catch((err) => {
// 吃掉请求产生的异常
// 后期可以记录
console.error('native response error', err)
return new ErrorPromise()
})
}
// config:{}

Loading…
Cancel
Save