76 lines
2.8 KiB
JavaScript
76 lines
2.8 KiB
JavaScript
import {config} from "@/common/js/index";
|
||
|
||
export async function requestConfig(ins, options, successHandler = null, failHandler = null, completeHandler = null){
|
||
//原来是:ins.baseUrl = options.baseUrl || ins.baseUrl
|
||
let baseUrl = options.baseUrl || ins.baseUrl;
|
||
// TODO 这里写dev还是生产环境
|
||
if (options.url.split('/').indexOf('newApi') !== -1) {
|
||
baseUrl = "http://127.0.0.1:18001/api/doctor/"
|
||
// baseUrl = "https://api.xiaokang88.com/api/doctor/"
|
||
}
|
||
options.url = options.url.replace('/oldApi/', '');
|
||
options.url = options.url.replace('/newApi/', '');
|
||
// base
|
||
ins.header = options.header || ins.header
|
||
// 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
|
||
|
||
// 如果是需要提交复杂对象/数组的写操作(POST/PUT/PATCH),强制使用 JSON 提交
|
||
const method = (config["method"] || "GET").toUpperCase()
|
||
if (["POST", "PUT", "PATCH"].includes(method)) {
|
||
const data = config["data"]
|
||
if (data && typeof data === 'object') {
|
||
const hasComplexValue = Object.values(data).some((v) => {
|
||
return v && typeof v === 'object'
|
||
})
|
||
if (hasComplexValue) {
|
||
config.header["Content-Type"] = "application/json"
|
||
}
|
||
}
|
||
}
|
||
}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'
|
||
}
|