54 lines
1.9 KiB
JavaScript
54 lines
1.9 KiB
JavaScript
|
||
export async function requestConfig(ins, options, successHandler = null, failHandler = null, completeHandler = null){
|
||
// base
|
||
ins.header = options.header || ins.header
|
||
//原来是:ins.baseUrl = options.baseUrl || ins.baseUrl
|
||
let baseUrl = options.baseUrl || ins.baseUrl;
|
||
// config base
|
||
let config = {
|
||
url:`${baseUrl}${options.url?options.url:''}`,
|
||
header:ins.header
|
||
}
|
||
if(ins.requestInterceptor){
|
||
let _cg = null
|
||
// 合并请求拦截器配置
|
||
try {
|
||
_cg = await ins.requestInterceptor(Object.assign({}, options, config))
|
||
} catch (e){
|
||
return false
|
||
}
|
||
// config为false或null return
|
||
if(!_cg || typeof _cg !== 'object'){
|
||
return false
|
||
}
|
||
// 更新options
|
||
Object.assign(options, _cg)
|
||
config.url = options.url
|
||
config.header = options.header
|
||
}
|
||
const type = options.type || "request"
|
||
// config 详情 如果options没有直接使用,删除props
|
||
if(type === "request"){
|
||
config["data"] = options.data || options.params || {}
|
||
config["method"] = options.method || "GET"
|
||
config["dataType"] = options.dataType || "json"
|
||
config["responseType"] = options.responseType || "text"
|
||
config['sslVerify'] = false
|
||
}else if(type === "upload"){
|
||
config["filePath"] = options.filePath
|
||
config["name"] = options.name
|
||
config["method"] = options.method || "POST"
|
||
config["formData"] = options.formData
|
||
// fileType for alipay
|
||
config["fileType"] = options.fileType || "image"
|
||
config['sslVerify'] = false
|
||
// 强制删除 Content-Type
|
||
delete config.header["Content-Type"]
|
||
}
|
||
return config
|
||
}
|
||
|
||
function _isPromise(obj) {
|
||
return obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'
|
||
}
|